-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScoreSegment.h
341 lines (279 loc) · 11 KB
/
ScoreSegment.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 1999 The Regents of the University of California
// Permission to use, copy, modify, and distribute this software and
// its documentation for any purpose, without fee, and without a
// written agreement is hereby granted, provided that the above copyright
// notice and this paragraph and the following two paragraphs appear in
// all copies.
//
// IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
// LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
// EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
// THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON
// AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
// PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
//
//////////////////////////////////////////////////////////////////////////////
//
// BRASS source file
//
// SCORE runtime support
// $Revision: 1.56 $
//
//////////////////////////////////////////////////////////////////////////////
#ifndef _ScoreSegment_H
#define _ScoreSegment_H
#include <signal.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/sem.h>
//#include <sys/sem_buf.h>
#include <sys/shm.h>
#include <sys/mman.h> /* for mprotect call */
#include <limits.h> /* for the page size limit */
#include "ScoreGraphNode.h"
#include "ScoreSegmentTable.h"
#include <unistd.h>
#include "ScoreType.h"
#define NOCHECK 0
#define SCORE_SEGMENT_ID int
#define SEG_USECOUNT 0
#define OBJECT_SIZE 400
#define NUMOFSHAREDSEG 126 // this is a current system limit
#ifndef PAGESIZE
#define PAGESIZE 4096
#endif
#define IMPLEMENT_SEGMENT_WITH_SHMEM 0 // added by Nachiket 13/11/2011 for non-shared-memory implementation of SCORE segments
// the various modes a segment can be in.
#define SCORE_CMB_SEQSRC 0
#define SCORE_CMB_SEQSINK 1
#define SCORE_CMB_SEQSRCSINK 2
#define SCORE_CMB_RAMSRC 3
#define SCORE_CMB_RAMSINK 4
#define SCORE_CMB_RAMSRCSINK 5
// for each of the various modes, this tells which input and output numbers
// the logical streams correspond to.
#define SCORE_CMB_SEQSRC_DATA_OUTNUM 0
#define SCORE_CMB_SEQSINK_DATA_INNUM 0
#define SCORE_CMB_SEQSRCSINK_DATAW_INNUM 0
#define SCORE_CMB_SEQSRCSINK_DATAR_OUTNUM 0
#define SCORE_CMB_RAMSRC_ADDR_INNUM 0
#define SCORE_CMB_RAMSRC_DATA_OUTNUM 0
#define SCORE_CMB_RAMSINK_ADDR_INNUM 0
#define SCORE_CMB_RAMSINK_DATA_INNUM 1
#define SCORE_CMB_RAMSRCSINK_ADDR_INNUM 0
#define SCORE_CMB_RAMSRCSINK_DATAW_INNUM 1
#define SCORE_CMB_RAMSRCSINK_WRITE_INNUM 2
#define SCORE_CMB_RAMSRCSINK_DATAR_OUTNUM 0
#define SCORE_CMB_STITCH_DATAW_INNUM 0
#define SCORE_CMB_STITCH_DATAR_OUTNUM 0
// the various types of ScoreSegment derivations so that the delete
// operator knows what to do.
#define SCORE_SEGMENT_PLAIN 1
#define SCORE_SEGMENT_READONLY 2
#define SCORE_SEGMENT_STITCH 3
// ScoreSegment: a memory segment.
class ScoreSegment : public ScoreGraphNode {
public:
// used to store mode/bounds information.
int mode;
unsigned int maxAddr; // this is the size of the loaded
// segment block.
// (address based on token width).
unsigned int traAddr; // this is the lower bound virtual
// address for the loaded segment block.
// (address based on token width).
unsigned int pboAddr; // this is the lower bound physical
// address for the loaded segment block.
// (address based on bytes).
unsigned int readAddr;
unsigned int writeAddr;
unsigned int readCount;
unsigned int writeCount;
char this_segment_is_done;
struct sembuf incrementuse, waitfornouse, decrementuse;
int semid; // made static by Nachiket 1/16/2010
union semun arg;
static void *dataPtrTable[NUMOFSHAREDSEG];
static void *dataRangeTable[NUMOFSHAREDSEG];
static ScoreSegment *segPtrTable[NUMOFSHAREDSEG];
unsigned int segSize;
int dataID;
size_t segLength;
unsigned int segWidth;
SCORE_SEGMENT_ID segmentID;
char sim_isFaulted;
unsigned int sim_faultedAddr;
unsigned int segmentType;
unsigned int getSegmentType() {return(segmentType);}
ScoreType segType;
int copyIn, copyOut;
//////////////////////////////////////////////////////
// BEGIN SCHEDULER VARIABLES
//////////////////////////////////////////////////////
// used to store the scheduler version of the mode/bounds information.
int sched_mode;
unsigned int sched_maxAddr;
unsigned int sched_traAddr;
unsigned int sched_pboAddr;
unsigned int sched_readAddr;
unsigned int sched_writeAddr;
unsigned int sched_readCount;
unsigned int sched_writeCount;
char sched_this_segment_is_done;
int sched_old_mode;
// indicates whether this segment is a stitch buffer or not.
char sched_isStitch;
// stores (for scheduler) whether or not the segment has faulted (and if
// so, which address caused the fault).
char sched_isFaulted;
unsigned int sched_faultedAddr;
// stores the pointers to the memory segment block for the data and
// state.
// FIX ME! REALLY SHOULD ALLOW MULTIPLE CACHING OF DATA AND STATE!
// FOR NOW, WE ASSUME THAT EACH SEGMENT IS STORED IN A CMB ONLY WHEN IT
// IS RESIDENT (ONLY IN ONE PLACE), AND IS UNCACHED WHEN SEGMENT IS UNLOADED!
// dumpSegmentBlock is there so that a segment still knows where to dump its
// resident fifo even if it is getting evicted.
ScoreSegmentBlock *sched_cachedSegmentBlock;
ScoreSegmentBlock *sched_dumpSegmentBlock;
// provides a place to store the dumped FIFO information.
void *sched_fifoBuffer;
char sched_isFIFOBufferValid;
// indicates if this needs to be dumped when this segment is marked done.
char sched_dumpOnDone;
// indicates the starting address and length of the resident section of the
// segment.
unsigned int sched_residentStart;
unsigned int sched_residentLength;
//////////////////////////////////////////////////////
// END SCHEDULER VARIABLES
//////////////////////////////////////////////////////
int recycleID1;
ScoreIOMaskType sim_segmentInputMask;
char shouldUseUSECOUNT;
int recycleID0; // same as tempID, need to recycle this on exit
char checkIfAddrFault(unsigned int newAddr);
static int tempID;
static SCORE_SEGMENT_ID currentID;
static int initSig; // variable used to initialize initSigCatch only once
static ScoreSegment *shmptr;
ScoreSegment *segPtr;
ushort start_val[1];
void *dataPtr;
void *operator new(size_t size);
void operator delete(void *p, size_t size);
ScoreSegment();
ScoreSegment(int, int, ScoreType); //first arg is lenth, second arg is width
// should return a pointer to data in segment
void* data() {
// printf("Case size of this %d\n",sizeof(*this));
// printf("returning hiy: %d %lu, %lu\n",hiy,&hiy,dataPtr);
return(dataPtr);
}
// set data
void setData(int address, long long int data) {
((long long int*)dataPtr)[address]=data;
}
// length of segment
// to humor Eylon, this means number of elements
size_t length() {
return(segLength);
}
// data width
int width() {
return(segWidth);
}
// size in bytes.
int size() {
return(segSize);
}
long MORON;
SCORE_SEGMENT_ID id() {return segmentID;} // return id (like ScoreStream)
void noAccess() ; // remove access from parent process
// maybe others, maybe should take args
void returnAccess(); // put access back
~ScoreSegment();
virtual int step() {return(0);}// should be overridden
virtual NodeTags getTag() { return ScoreSegmentTag; }
};
//public
template <ScoreType ScoreType_t>
class TypedScoreSegment : public ScoreSegment
{
public:
TypedScoreSegment(int nlength_, int nwidth_) :
ScoreSegment(nlength_, nwidth_, ScoreType_t)
{}
};
typedef TypedScoreSegment<SCORE_STREAM_UNTYPED>* SCORE_SEGMENT;
typedef TypedScoreSegment<SCORE_STREAM_BOOLEAN_TYPE>* BOOLEAN_SCORE_SEGMENT;
typedef TypedScoreSegment<SCORE_STREAM_SIGNED_TYPE>* SIGNED_SCORE_SEGMENT;
typedef TypedScoreSegment<SCORE_STREAM_UNSIGNED_TYPE>* UNSIGNED_SCORE_SEGMENT;
typedef TypedScoreSegment<SCORE_STREAM_SIGNED_FIXED_TYPE>* SIGNED_FIXED_SCORE_SEGMENT;
typedef TypedScoreSegment<SCORE_STREAM_UNSIGNED_FIXED_TYPE>* UNSIGNED_FIXED_SCORE_SEGMENT;
typedef TypedScoreSegment<SCORE_STREAM_DOUBLE_TYPE>* DOUBLE_SCORE_SEGMENT;
typedef TypedScoreSegment<SCORE_STREAM_FLOAT_TYPE>* FLOAT_SCORE_SEGMENT;
#define NEW_SCORE_SEGMENT(n,w) \
(new TypedScoreSegment<SCORE_STREAM_UNTYPED>(n,w))
#define NEW_SIGNED_SCORE_SEGMENT(n,w) \
(new TypedScoreSegment<SCORE_STREAM_SIGNED_TYPE>(n,w))
#define NEW_UNSIGNED_SCORE_SEGMENT(n,w) \
(new TypedScoreSegment<SCORE_STREAM_UNSIGNED_TYPE>(n,w))
#define NEW_BOOLEAN_SCORE_SEGMENT(n) \
(new TypedScoreSegment<SCORE_STREAM_BOOLEAN_TYPE>(n,1))
#define NEW_SIGNED_FIXED_SCORE_SEGMENT(n,w) \
(new TypedScoreSegment<SCORE_STREAM_SIGNED_FIXED_TYPE>(n,w))
#define NEW_UNSIGNED_FIXED_SCORE_SEGMENT(n,w) \
(new TypedScoreSegment<SCORE_STREAM_UNSIGNED_FIXED_TYPE>(n,w))
#define NEW_DOUBLE_SCORE_SEGMENT(n) \
(new TypedScoreSegment<SCORE_STREAM_DOUBLE_TYPE>(n,64))
#define NEW_FLOAT_SCORE_SEGMENT(n) \
(new TypedScoreSegment<SCORE_STREAM_FLOAT_TYPE>(n,32))
#define SEGMENT_OBJ_TO_ID(x) (segmentOBJ_to_ID(x))
#define SEGMENT_ID_TO_OBJ(x) (segmentID_to_OBJ(x))
#define GET_SEGMENT_DATA(x) (x->data())
#define SEGMENT_WRITE_DOUBLE(x,y,z) \
{ \
unsigned long long* data = (unsigned long long*)x->data(); \
double y_val = y; \
long long int* y_ptr = (long long int*) &y_val; \
data[z]=*y_ptr; \
}
#define SEGMENT_WRITE(x,y,z) \
{ \
unsigned long long* data = (unsigned long long*)x->data(); \
data[z]=(long long int)y; \
}
#define SEGMENT_WRITE_ARRAY_DOUBLE(x,y,z) \
{ \
int i=0;\
unsigned long long* data = (unsigned long long*)x->data(); \
for(i=0; i<z; i++) { \
double y_val = y[i]; \
long long int* y_ptr=(long long int*) &y_val; \
data[i] = *y_ptr; \
} \
}
#define SEGMENT_WRITE_ARRAY(x,y,z) \
{ \
int j=0;\
unsigned long long* data = (unsigned long long*)x->data(); \
for(j=0; j<z; j++) { \
int y_val = y[j]; \
data[j]=(unsigned long long int)y_val; \
} \
}
int initSigCatch();
void catchSig(int, siginfo_t *, void *);
SCORE_SEGMENT_ID segmentOBJ_to_ID(ScoreSegment *);
ScoreSegment *segmentID_to_OBJ(int nid);
// needed by LEDA for use with lists/etc.
int compare(ScoreSegment * const & left, ScoreSegment * const & right);
#endif