-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScoreSegment.cc
699 lines (592 loc) · 18.6 KB
/
ScoreSegment.cc
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//////////////////////////////////////////////////////////////////////////////
//
// 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.52 $
//
//////////////////////////////////////////////////////////////////////////////
#include <signal.h>
#include <execinfo.h>
#include "ScoreSegment.h"
#include "ScoreConfig.h"
#include <iostream> // added by Nachiket for newer file IO constructs
#include <iomanip> // added by Nachiket for newer file IO constructs
#include <ios> // added by Nachiket for newer file IO constructs
#include <sstream>
#include <stdlib.h>
using std::ios;
using std::fstream;
using std::setfill;
using std::setw;
using std::stringstream;
int ScoreSegment::currentID = 0;
int ScoreSegment::tempID = 0;
ScoreSegment *ScoreSegment::shmptr=0;
void *ScoreSegment::dataPtrTable[NUMOFSHAREDSEG] = {0};
void *ScoreSegment::dataRangeTable[NUMOFSHAREDSEG] = {0};
ScoreSegment *ScoreSegment::segPtrTable[NUMOFSHAREDSEG] = {0};
// want to initiate signal catching code
int ScoreSegment::initSig=initSigCatch();
void *ScoreSegment::operator new(size_t size) {
// create a new shared segment and malloc
/* COMMENT_REMOVE on April 15th 2010 (Anyone reading this, DO YOUR FREAKING TAXES!!)
// need to get new segmentID
// since we don't have one for Segment ID, we will re-use /proc/streamid
int file = (int) open("/proc/streamid",O_RDONLY);
if (file < 0) {
perror("ERROR: Could not open /proc/streamid ");
exit(errno);
}
if (read(file, &tempID, 4) != 4) {
perror("ERROR: Did not get 4 bytes of data back ");
exit(errno);
}
close(file);
*/ // COMMENT_REMOVE
std::stringstream filename;
filename << "/tmp/streamid." << getenv("USER");
// cout << filename << endl;
fstream infile(filename.str().c_str(),ios::in);
if (infile.fail() || infile.bad()) {
cout << "Initialize " << filename << endl;
infile.close();
// initialize the file..
fstream outfile(filename.str().c_str(),ios::out);
outfile << setfill('0');
outfile << setw(4);
outfile << 1;
tempID=0;
outfile.close();
} else {
char buffer[4]; //=(char*)malloc(4);
infile.read(buffer,4);
tempID=(buffer[3]-48)+
10*(buffer[2]-48)+
100*(buffer[1]-48)+
1000*(buffer[0]-48);
// cout << "Found existing /tmp/streamid: " << tempID << endl;
infile.close();
// Nachiket added update routine...
// need to get new streamID
fstream outfile(filename.str().c_str(),ios::out);
outfile << setfill('0');
outfile << setw(4);
outfile << tempID+1;
outfile.close();
}
// Nachiket added update routine...
// need to get new streamID
// fstream outfile("/tmp/streamid",ios::out);
// outfile << setfill('0');
// outfile << setw(4);
// outfile << tempID+1;
// outfile.close();
if(errno) {
perror("Something went wrong..");
exit(errno);
}
// need to allocate new segment of share memory
if(IMPLEMENT_SEGMENT_WITH_SHMEM)
{
if ((currentID=shmget((int)tempID, size, IPC_EXCL | IPC_CREAT | 0666)) != -1)
{
// need to attach the segment on the page boundary
// this is done automatically already
if ((shmptr=(ScoreSegment *)shmat(currentID, 0, 0))==(ScoreSegment *) -1) {
perror("shmptr -- segment new operator -- attach ");
exit(errno);
}
}
else {
perror("currentID -- segment new operator -- creation ");
exit(errno);
}
} else {
// Added by Nachiket on 13th November 2011
// Avoid using shared memory for segments.
// This mimics ALLOCTAG_PRIVATE behavior with Streams.
// Pardon the use of name shmptr for the non-shared-memory pointer
shmptr=(ScoreSegment*)malloc(size);
if(!shmptr) {
perror("ptr (shmptr) == segment new operator -- unable to allocate mem");
exit(0);
}
}
if (VERBOSEDEBUG || DEBUG) {
cout << " SegmentID is: " << currentID << "\n";
}
// cout << " SegmentID is: " << currentID << " shmptr=" << shmptr << "\n";
return shmptr;
}
void ScoreSegment::operator delete(void *p, size_t size) {
ScoreSegment *currentSegment = (ScoreSegment *) p;
if (currentSegment->getSegmentType() == SCORE_SEGMENT_PLAIN) {
// FIX ME! NEED TO PROPERLY DELETE SHARED SEGMENTS!
cerr << "SCORESEGMENTERR: DELETE OF SCORESEGMENT IS NOT IMPLEMENTED!" <<
endl;
} else if (currentSegment->getSegmentType() == SCORE_SEGMENT_READONLY) {
::delete p;
} else if (currentSegment->getSegmentType() == SCORE_SEGMENT_STITCH) {
::delete p;
}
}
///////////////////////////////////////////////////////////////////////////////
// ScoreSegment::ScoreSegment:
// Constructor for ScoreSegment.
// Initializes all internal structures.
//
// Parameters:
//
// Return value: None.
///////////////////////////////////////////////////////////////////////////////
ScoreSegment::ScoreSegment() {
sched_isStitch = 0;
mode = 0;
maxAddr = 0;
traAddr = 0;
pboAddr = 0;
readAddr = 0;
writeAddr = 0;
sched_mode = 0;
sched_maxAddr = 0;
sched_traAddr = 0;
sched_pboAddr = 0;
sched_readAddr = 0;
sched_writeAddr = 0;
sched_readCount = 0;
sched_writeCount = 0;
sched_cachedSegmentBlock = NULL;
sched_dumpSegmentBlock = NULL;
sched_fifoBuffer = NULL;
sched_isFIFOBufferValid = 0;
sched_isFaulted = 0;
sched_faultedAddr = 0;
sched_residentStart = 0;
sched_residentLength = 0;
sim_isFaulted = 0;
sim_faultedAddr = 0;
sched_dumpOnDone = 0;
readCount = 0;
writeCount = 0;
start_val[0] = 0;
incrementuse.sem_num = SEG_USECOUNT;
incrementuse.sem_op = 1;
incrementuse.sem_flg = 0;
waitfornouse.sem_num = SEG_USECOUNT;
waitfornouse.sem_op = 0;
waitfornouse.sem_flg = 0;
decrementuse.sem_num = SEG_USECOUNT;
decrementuse.sem_op = -1;
decrementuse.sem_flg = 0;
sim_segmentInputMask = 0;
this_segment_is_done = 0;
sched_this_segment_is_done = 0;
sched_old_mode = 0;
dataPtr = (void*)11111;
dataID = 2;
shouldUseUSECOUNT = 0;
_isPage = 0;
_isOperator = 0;
_isSegment = 1;
}
///////////////////////////////////////////////////////////////////////////////
// ScoreSegment::ScoreSegment:
// Constructor for ScoreSegment.
// Initializes all internal structures.
//
// Parameters: nlength, nwidth
//
// Return value: None.
///////////////////////////////////////////////////////////////////////////////
ScoreSegment::ScoreSegment(int nlength, int nwidth, ScoreType type_t) {
segType = type_t;
//segmentType = SCORE_SEGMENT_PLAIN;
segmentType = 0;
sched_isStitch = 0;
mode = 0;
maxAddr = 0;
traAddr = 0;
pboAddr = 0;
readAddr = 0;
writeAddr = 0;
sched_mode = 0;
sched_maxAddr = 0;
sched_traAddr = 0;
sched_pboAddr = 0;
sched_readAddr = 0;
sched_writeAddr = 0;
sched_readCount = 0;
sched_writeCount = 0;
sched_cachedSegmentBlock = NULL;
sched_dumpSegmentBlock = NULL;
sched_fifoBuffer = NULL;
sched_isFIFOBufferValid = 0;
sched_isFaulted = 0;
sched_faultedAddr = 0;
sched_residentStart = 0;
sched_residentLength = 0;
sim_isFaulted = 0;
sim_faultedAddr = 0;
sched_dumpOnDone = 0;
readCount = 0;
writeCount = 0;
recycleID0 = tempID;
segmentID = currentID;
segPtr = shmptr;
segLength = nlength;
segWidth = nwidth;
segSize = (int)segLength * 8; // make everything long long integer sized.
// need to make sure segPtr is page align
// the "data" section will be another shared memory segment
// it will be page aligned when attached to a process
/*
// open /proc/streamid to get a unique number
int file = (int) open("/proc/streamid",O_RDONLY);
if (file < 0) {
perror("ERROR: Could not open /proc/streamid ");
exit(errno);
}
// the unique number will be stored in recycleID1
// so we can recycle this number when we are done
if (read(file, &recycleID1, 4) != 4) {
perror("ERROR: Did not get 4 bytes of data back ");
exit(errno);
}
close(file);
*/
int newID=0;
std::stringstream filename;
filename << "/tmp/streamid." << getenv("USER");
fstream infile(filename.str().c_str(),ios::in);
if (infile.fail() || infile.bad()) {
cout << "Initialize " << filename.str().c_str() << endl;
infile.close();
// initialize the file..
fstream outfile(filename.str().c_str(),ios::out);
outfile << setfill('0');
outfile << setw(4);
outfile << 1;
newID=0;
outfile.close();
} else {
char buffer1[4]; //=(char*)malloc(4);
infile.read(buffer1,4);
newID=(buffer1[3]-48)+
10*(buffer1[2]-48)+
100*(buffer1[1]-48)+
1000*(buffer1[0]-48);
// cout << "Found existing /tmp/streamid: " << recycleID1 << endl;
infile.close();
// Nachiket added update routine...
// need to get new streamID
fstream outfile(filename.str().c_str(),ios::out);
outfile << setfill('0');
outfile << setw(4);
outfile << newID+1;
outfile.close();
}
recycleID1=0;
recycleID1=newID;
// Nachiket added update routine...
// need to get new streamID
// fstream outfile("/tmp/streamid",ios::out);
// outfile << setfill('0');
// outfile << setw(4);
// outfile << recycleID1+1;
// outfile.close();
if(errno) {
perror("Something went wrong..");
exit(errno);
}
if(segSize==0) { cout<<"Warning: segSize=0, assuming you really wanted to create a 0-sized segment" << endl; segSize=1;}
if(IMPLEMENT_SEGMENT_WITH_SHMEM==1) {
// create a share memory segment and stored the segment ID in dataID
if ((dataID=shmget((int)recycleID1, segSize,
IPC_EXCL | IPC_CREAT | 0666)) != -1) {
// attach the shared memory segment to this process
// and store the pointer in dataPtr
if ((dataPtr=(void *)shmat(dataID, 0, 0))==(void *) -1) {
perror("dataPtr -- segment new operator -- attach ");
exit(errno);
}
}
else {
perror("dataID -- segment new operator -- creation ");
exit(errno);
}
} else {
// avoiding shared-memory implementation
dataPtr = (void*)malloc(segSize);
// cout << "Case 1: contents " << this << " dataPtr=" << this->dataPtr << " &dataPtr=" << &dataPtr << endl;
// cout << "Segsize=" << segSize << " dataPtr=" << dataPtr << ", dataId=" << dataID << " recycleId=" << recycleID1 << endl;
}
MORON=-1;
// want to setup the semaphore
// start_val[0] = 0;
// create and initialize the semaphores
if ((semid=semget(segmentID, 1, IPC_EXCL | IPC_CREAT | 0666)) != -1) {
// if ((semid=semget(0xdeadaabe, 1, IPC_CREAT | 0666)) != -1) {
static ushort start_val_tmp = 1;
arg.array = &start_val_tmp;
if (semctl(semid, 0, SETALL, arg) == -1) {
perror("semctl -- segment constructor -- initialization");
exit(errno);
}
}
else {
perror("semget -- segment constructor -- creation ");
exit(errno);
}
// SEG_USECOUNT is set to 0, since we only have one semaphore in this object
incrementuse.sem_num = SEG_USECOUNT;
incrementuse.sem_op = 1;
incrementuse.sem_flg = 0;
waitfornouse.sem_num = SEG_USECOUNT;
waitfornouse.sem_op = 0;
waitfornouse.sem_flg = 0;
decrementuse.sem_num = SEG_USECOUNT;
decrementuse.sem_op = -1;
decrementuse.sem_flg = 0;
// set the table entry so that signal catcher can find it later
// first find an empty space
int index;
for (index = 0; index < NUMOFSHAREDSEG; index++) {
if (dataPtrTable[index] == NULL)
break;
}
// more sanity check
if (index == NUMOFSHAREDSEG) {
perror ("ERROR: No available entry available in dataPtrTable");
perror (" due to implementation limitation");
exit(-1);
}
// Initialization
for(int k=0; k<segPtr->segLength; k++) {
((long long int*)dataPtr)[k]=k*k;
}
if (VERBOSEDEBUG || DEBUG) {
cout << " SEG: segment creation" << endl;
cout << " SEG: dataPtr - " << dataPtr << endl;
cout << " SEG: segPtr - " << segPtr << endl;
cout << " SEG: stored in table index: " << index << endl;
}
dataPtrTable[index] = dataPtr;
dataRangeTable[index] = (int *)dataPtr+segSize;
segPtrTable[index] = segPtr;
sim_segmentInputMask = 0;
this_segment_is_done = 0;
sched_this_segment_is_done = 0;
sched_old_mode = 0;
shouldUseUSECOUNT = 0;
_isPage = 0;
_isOperator = 0;
_isSegment = 1;
// cout << "Again Segsize=" << segSize << " dataPtr=" << dataPtr << ", dataId=" << dataID << " recycleId=" << recycleID1 << endl;
}
///////////////////////////////////////////////////////////////////////////////
// ScoreSegment::~ScoreSegment:
// Destructor for ScoreSegment.
// Cleans up all internal structures.
//
// Parameters: None.
//
// Return value: None.
///////////////////////////////////////////////////////////////////////////////
ScoreSegment::~ScoreSegment() {
}
void ScoreSegment::noAccess() {
// return ;
//}
// will use mprotect() call to protect a page from read/write
// the trick is to catch the SIGSEGV signal
// instead of exit, we want the thread/process to wait
// increment SEG_USECOUNT
while (semop(semid, &incrementuse, 1) == -1){
perror("semop -- noAccess -- incrementuse ");
if (errno != EINTR)
exit(errno);
}
if (mprotect(dataPtr, segSize, PROT_NONE)) {
perror("ERROR: Couldn't mprotect");
exit(errno);
}
if (VERBOSEDEBUG || DEBUG) {
cout << " SEG: calling noaccess on dataPtr - " << dataPtr << endl;
}
shouldUseUSECOUNT = 1;
}
void ScoreSegment::returnAccess() {
return ;
}
/*
// need to release/unprotect the memory segment and wakeup any thread/process
// blocking on this memory segment
if (VERBOSEDEBUG || DEBUG) {
cout << " SEG: calling allow returnAccess()" << endl;
}
if (shouldUseUSECOUNT) {
// decrement SEG_USECOUNT
while (semop(semid, &decrementuse, 1) == -1){
perror("semop -- returnAccess -- decrementuse");
if (errno != EINTR)
exit(errno);
}
}
}
*/
int initSigCatch() {
// set up signal handling
struct sigaction sact;
sigemptyset(&sact.sa_mask); // don't block any other signals
sact.sa_flags = SA_SIGINFO;
// sact.sa_handler = catchSig;
sact.sa_sigaction = catchSig;
sigaction(SIGSEGV, &sact, (struct sigaction *) 0);
if (VERBOSEDEBUG || DEBUG) {
cout << "dataPtrTable initial value: " << ScoreSegment::dataPtrTable[0]
<< " - " << ScoreSegment::dataPtrTable[1] << endl;
}
return(1);
}
// Added by Nachiket on 21st April 2010
// http://www.soopertutorials.com/technology/programming/3538-generate-system-stack-trace.html
// print a backtrace
void print_btrace(void)
{
void *trace[50];
int tsize, counter;
char **buffer;
// get backtrace of process
tsize = backtrace(trace, 50);
//get back trace symbolsl
buffer = backtrace_symbols(trace, tsize);
if (buffer == NULL)
{
perror("backtrace_symbols");
return;
}
for (counter = 0; counter < tsize; counter++)
printf("%s\n", buffer[counter]);
//release memeory
free(buffer);
}
void catchSig(int sig, siginfo_t *si, void *ctx) {
print_btrace();
int index;
// sanity check
if (sig == SIGSEGV) {
// more sanity check
if (si) {
if (si->si_addr) {
if (VERBOSEDEBUG || DEBUG) {
cout << " CATCHSIG: caught SEGV for " << si->si_addr << endl;
}
// need to page align this address
// this is also the source of the limitation of the data segment
// if the data segment is more than one page, we possibly will not
// find the address in dataPtrTable
for (index = 0; index < NUMOFSHAREDSEG; index++) {
if ((si->si_addr >= ScoreSegment::dataPtrTable[index]) &&
(si->si_addr < ScoreSegment::dataRangeTable[index])) {
if (VERBOSEDEBUG || DEBUG) {
cout << " CATCHSIG: found segfault address in index: "
<< index << endl;
}
break;
}
}
// if we cannot find this address in the dataPtrTable,
// it means it is a real segfault, and we exit
if (index == NUMOFSHAREDSEG) {
cerr << " CATCHSIG: This is a real segfault: " << si->si_addr <<
endl;
exit(-1);
}
// else we usr the index to find the ScoreSegment object pointer
ScoreSegment *obj = ScoreSegment::segPtrTable[index];
if (VERBOSEDEBUG) {
cout << " CATCHSIG: semaphoreID is - " << obj->semid << endl;
}
// try to test the semaphore
if (VERBOSEDEBUG) {
cout << " SEG: the semop for waitfornouse is: " <<
obj->waitfornouse.sem_op << endl;
}
while (semop(obj->semid, &(obj->waitfornouse), 1) == -1){
perror("semop -- catchSig -- waitfornouse ");
if (errno != EINTR)
exit(errno);
}
if (VERBOSEDEBUG || DEBUG) {
cout << " CATCHSIG: waking up" << endl;
}
mprotect(ScoreSegment::dataPtrTable[index],
obj->segSize, PROT_READ|PROT_WRITE);
return;
} else {
printf("seg fault address is NULL\n");
exit(-1);
}
}else
cout << "caught SEGV, no siginfo!" << endl;
} else
printf("caught unexpected sig \n");
exit(1);
}
char ScoreSegment::checkIfAddrFault(unsigned int newAddr) {
return(!((newAddr >= traAddr) &&
(newAddr < (traAddr+maxAddr))));
}
SCORE_SEGMENT_ID segmentOBJ_to_ID(ScoreSegment *seg) {
// take access away from this process.
seg->noAccess();
return(seg->id());
}
ScoreSegment *segmentID_to_OBJ(int nid) {
if (VERBOSEDEBUG || DEBUG) {
cout << " SEG: segmentID received - " << nid << endl;
}
ScoreSegment *segPtr;
while ((segPtr=(ScoreSegment *)shmat(nid, 0, 0))==(ScoreSegment *) -1) {
perror("segPtr -- SegmentID_to_OBJ -- attach ");
if (errno != EINTR)
exit(errno);
}
return(segPtr);
}
// required by LEDA for use with lists/etc.
// provides comparison operation between ScoreStreams.
// NOTE: Right now, we only say 2 streams are equal if their pointers
// are equal. Otherwise, less than/greater than is determined
// simply by their pointer values.
int compare(ScoreSegment * const & left, ScoreSegment * const & right) {
if (left == right) {
return(0);
} else if (left < right) {
return(-1);
} else {
return(1);
}
}