view VMS.h @ 193:20358f56e498

fix coreloop recording bug and eliminate race condition on measurement-collecting list
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Thu, 09 Feb 2012 15:40:11 +0100
parents fe5ad5726e36
children d83f59e6e2db
line source
1 /*
2 * Copyright 2009 OpenSourceStewardshipFoundation.org
3 * Licensed under GNU General Public License version 2
4 *
5 * Author: seanhalle@yahoo.com
6 *
7 */
9 #ifndef _VMS_H
10 #define _VMS_H
11 #define _GNU_SOURCE
13 #include "VMS_primitive_data_types.h"
14 #include "Queue_impl/PrivateQueue.h"
15 #include "Histogram/Histogram.h"
16 #include "DynArray/DynArray.h"
17 #include "Hash_impl/PrivateHash.h"
18 #include "vmalloc.h"
19 #include "loop.h"
20 #include "ListOfArrays/ListOfArrays.h"
22 #include <pthread.h>
23 #include <sys/time.h>
26 //=============================== Debug ===================================
27 //
28 //When SEQUENTIAL is defined, VMS does sequential exe in the main thread
29 // It still does co-routines and all the mechanisms are the same, it just
30 // has only a single thread and animates VPs one at a time
31 //#define SEQUENTIAL
33 //#define USE_WORK_STEALING
35 //turns on the probe-instrumentation in the application -- when not
36 // defined, the calls to the probe functions turn into comments
37 //#define STATS__ENABLE_PROBES
38 //#define TURN_ON_DEBUG_PROBES
40 //These defines turn types of bug messages on and off
41 // be sure debug messages are un-commented (next block of defines)
42 #define dbgAppFlow FALSE /* Top level flow of application code -- general*/
43 #define dbgProbes FALSE /* for issues inside probes themselves*/
44 #define dbgB2BMaster FALSE /* in coreloop, back to back master VPs*/
45 #define dbgRqstHdlr FALSE /* in request handler code*/
46 #define dbgDependency TRUE /* in request handler code, print dependencies */
48 //Comment or un- the substitute half to turn on/off types of debug message
49 #define DEBUG( bool, msg) \
50 if( bool){ printf(msg); fflush(stdin);}
51 #define DEBUG1( bool, msg, param) \
52 if(bool){printf(msg, param); fflush(stdin);}
53 #define DEBUG2( bool, msg, p1, p2) \
54 if(bool) {printf(msg, p1, p2); fflush(stdin);}
56 #define ERROR(msg) printf(msg);
57 #define ERROR1(msg, param) printf(msg, param);
58 #define ERROR2(msg, p1, p2) printf(msg, p1, p2);
60 //=========================== STATS =======================
62 //when MEAS__TIME_STAMP_SUSP is defined, causes code to be inserted and
63 // compiled-in that saves the low part of the time stamp count just before
64 // suspending a processor and just after resuming that processor. It is
65 // saved into a field added to VirtProcr. Have to sanity-check for
66 // rollover of low portion into high portion.
67 //#define MEAS__TIME_STAMP_SUSP
68 //#define MEAS__TIME_MASTER
69 //#define MEAS__TIME_PLUGIN
70 //#define MEAS__TIME_MALLOC
71 //#define MEAS__TIME_MASTER_LOCK
72 //#define MEAS__NUM_TIMES_TO_RUN 100000
74 //For code that calculates normalization-offset between TSC counts of
75 // different cores.
76 //#define NUM_TSC_ROUND_TRIPS 10
78 #define MEAS__PERF_COUNTERS
79 #define OBSERVE_UCC
80 #define DETECT_LOOP_GRAPH
82 //========================= Hardware related Constants =====================
83 //This value is the number of hardware threads in the shared memory
84 // machine
85 #define NUM_CORES 4
87 // tradeoff amortizing master fixed overhead vs imbalance potential
88 // when work-stealing, can make bigger, at risk of losing cache affinity
89 #define NUM_SCHED_SLOTS 5
91 #define MIN_WORK_UNIT_CYCLES 20000
93 #define MASTERLOCK_RETRIES 10000
95 // stack size in virtual processors created
96 #define VIRT_PROCR_STACK_SIZE 0x8000 /* 32K */
98 // memory for VMS__malloc
99 #define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
101 #define CACHE_LINE 64
102 #define PAGE_SIZE 4096
105 //==============================
107 #define SUCCESS 0
109 #define writeVMSQ writePrivQ
110 #define readVMSQ readPrivQ
111 #define makeVMSQ makeVMSPrivQ
112 #define numInVMSQ numInPrivQ
113 #define VMSQueueStruc PrivQueueStruc
117 //===========================================================================
118 typedef unsigned long long TSCount;
120 typedef struct _SchedSlot SchedSlot;
121 typedef struct _VMSReqst VMSReqst;
122 typedef struct _VirtProcr VirtProcr;
123 typedef struct _IntervalProbe IntervalProbe;
124 typedef struct _GateStruc GateStruc;
127 typedef VirtProcr * (*SlaveScheduler) ( void *, int, int ); //semEnv, coreIdx
128 typedef void (*RequestHandler) ( VirtProcr *, void * ); //prWReqst, semEnv
129 typedef void (*VirtProcrFnPtr) ( void *, VirtProcr * ); //initData, animPr
130 typedef void VirtProcrFn ( void *, VirtProcr * ); //initData, animPr
131 typedef void (*ResumePrFnPtr) ( VirtProcr *, void * );
132 typedef void (*CounterHandler) (int,int,int,VirtProcr*,uint64,uint64);
134 //============= Requests ===========
135 //
137 enum VMSReqstType //avoid starting enums at 0, for debug reasons
138 {
139 semantic = 1,
140 createReq,
141 dissipate,
142 VMSSemantic //goes with VMSSemReqst below
143 };
145 struct _VMSReqst
146 {
147 enum VMSReqstType reqType;//used for dissipate and in future for IO requests
148 void *semReqData;
150 VMSReqst *nextReqst;
151 };
152 //VMSReqst
154 enum VMSSemReqstType //These are equivalent to semantic requests, but for
155 { // VMS's services available directly to app, like OS
156 createProbe = 1, // and probe services -- like a VMS-wide built-in lang
157 openFile,
158 otherIO
159 };
161 typedef struct
162 { enum VMSSemReqstType reqType;
163 VirtProcr *requestingPr;
164 char *nameStr; //for create probe
165 }
166 VMSSemReq;
169 //==================== Core data structures ===================
171 struct _SchedSlot
172 {
173 int workIsDone;
174 int needsProcrAssigned;
175 VirtProcr *procrAssignedToSlot;
176 };
177 //SchedSlot
179 /*WARNING: re-arranging this data structure could cause VP switching
180 * assembly code to fail -- hard-codes offsets of fields
181 */
182 struct _VirtProcr
183 { int procrID; //for debugging -- count up each time create
184 int coreAnimatedBy;
185 void *startOfStack;
186 void *stackPtr;
187 void *framePtr;
188 void *nextInstrPt;
190 void *coreLoopStartPt; //allows proto-runtime to be linked later
191 void *coreLoopFramePtr; //restore before jmp back to core loop
192 void *coreLoopStackPtr; //restore before jmp back to core loop
194 void *initialData;
196 SchedSlot *schedSlot;
197 VMSReqst *requests;
199 void *semanticData; //this livesUSE_GNU here for the life of VP
200 void *dataRetFromReq;//values returned from plugin to VP go here
202 //=========== MEASUREMENT STUFF ==========
203 #ifdef MEAS__TIME_STAMP_SUSP
204 unsigned int preSuspTSCLow;
205 unsigned int postSuspTSCLow;
206 #endif
207 #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
208 unsigned int startMasterTSCLow;USE_GNU
209 unsigned int endMasterTSCLow;
210 #endif
211 #ifdef MEAS__PERF_COUNTERS //
212 //CounterRecord** counter_history;
213 //PrivDynArrayInfo* counter_history_array_info;
214 #endif
215 int isMasterVP;
216 int isShutdownVP;
217 //========================================
219 float64 createPtInSecs; //have space but don't use on some configs
220 int numTimesScheduled; //defines units together w/ procrID
221 };
222 //VirtProcr
225 /*WARNING: re-arranging this data structure could cause VP-switching
226 * assembly code to fail -- hard-codes offsets of fields
227 * (because -O3 messes with things otherwise)
228 */
229 typedef struct
230 {
231 SlaveScheduler slaveScheduler;
232 RequestHandler requestHandler;
234 SchedSlot ***allSchedSlots;
235 VMSQueueStruc **readyToAnimateQs;
236 VirtProcr **masterVPs;
238 void *semanticEnv;
239 void *OSEventStruc; //for future, when add I/O to BLIS
240 MallocProlog *freeListHead;
241 int32 amtOfOutstandingMem; //total currently allocated
243 void *coreLoopReturnPt;//addr to jump to to re-enter coreLoop
245 int32 setupComplete;
246 volatile int32 masterLock;
248 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
249 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
250 int32 workStealingLock;
252 int32 numProcrsCreated; //gives ordering to processor creation
254 //=========== MEASUREMENT STUFF =============
255 IntervalProbe **intervalProbes;
256 PrivDynArrayInfo *dynIntervalProbesInfo;
257 HashTable *probeNameHashTbl;
258 int32 masterCreateProbeID;
259 float64 createPtInSecs;
260 Histogram **measHists;
261 PrivDynArrayInfo *measHistsInfo;
262 #ifdef MEAS__TIME_PLUGIN
263 Histogram *reqHdlrLowTimeHist;
264 Histogram *reqHdlrHighTimeHist;
265 #endif
266 #ifdef MEAS__TIME_MALLOC
267 Histogram *mallocTimeHist;
268 Histogram *freeTimeHist;
269 #endif
270 #ifdef MEAS__TIME_MASTER_LOCK
271 Histogram *masterLockLowTimeHist;
272 Histogram *masterLockHighTimeHist;
273 #endif
274 #ifdef MEAS__PERF_COUNTERS
275 int cycles_counter_fd[NUM_CORES];
276 int instrs_counter_fd[NUM_CORES];
277 //FILE* counteroutput;
278 //CounterRecord** counter_history;
279 //PrivDynArrayInfo* counter_history_array_info;
280 CounterHandler counterHandler;
281 #endif
282 }
283 MasterEnv;
285 //========================= Extra Stuff Data Strucs =======================
286 typedef struct
287 {
289 }
290 VMSExcp;
292 struct _GateStruc
293 {
294 int32 gateClosed;
295 int32 preGateProgress;
296 int32 waitProgress;
297 int32 exitProgress;
298 };
299 //GateStruc
301 //======================= OS Thread related ===============================
303 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
304 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
305 void masterLoop( void *initData, VirtProcr *masterPr );
308 typedef struct
309 {
310 void *endThdPt;
311 unsigned int coreNum;
312 }
313 ThdParams;
315 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
316 ThdParams *coreLoopThdParams [ NUM_CORES ];
317 pthread_mutex_t suspendLock;
318 pthread_cond_t suspend_cond;
322 //===================== Global Vars ===================
324 volatile MasterEnv *_VMSMasterEnv;
329 //=========================== Function Prototypes =========================
332 //========== Setup and shutdown ==========
333 void
334 VMS__init();
336 void
337 VMS__init_Seq();
339 void
340 VMS__start_the_work_then_wait_until_done();
342 void
343 VMS__start_the_work_then_wait_until_done_Seq();
345 inline VirtProcr *
346 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
348 void
349 VMS__dissipate_procr( VirtProcr *procrToDissipate );
351 //Use this to create processor inside entry point & other places outside
352 // the VMS system boundary (IE, not run in slave nor Master)
353 VirtProcr *
354 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
356 void
357 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
359 void
360 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
362 void
363 VMS__shutdown();
365 void
366 VMS__cleanup_at_end_of_shutdown();
368 void *
369 VMS__give_sem_env_for( VirtProcr *animPr );
372 //============== Request Related ===============
374 void
375 VMS__suspend_procr( VirtProcr *callingPr );
377 inline void
378 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
380 inline void
381 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
383 void
384 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
386 void inline
387 VMS__send_dissipate_req( VirtProcr *prToDissipate );
389 inline void
390 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
392 VMSReqst *
393 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
395 inline void *
396 VMS__take_sem_reqst_from( VMSReqst *req );
398 void inline
399 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
400 ResumePrFnPtr resumePrFnPtr );
402 //======================== STATS ======================
404 //===== RDTSC wrapper ===== //Also runs with x86_64 code
406 #define saveTimeStampCountInto(low, high) \
407 asm volatile("RDTSC; \
408 movl %%eax, %0; \
409 movl %%edx, %1;" \
410 /* outputs */ : "=m" (low), "=m" (high)\
411 /* inputs */ : \
412 /* clobber */ : "%eax", "%edx" \
413 );
415 #define saveLowTimeStampCountInto(low) \
416 asm volatile("RDTSC; \
417 movl %%eax, %0;" \
418 /* outputs */ : "=m" (low) \
419 /* inputs */ : \
420 /* clobber */ : "%eax", "%edx" \
421 );
423 //====================
424 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
425 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
426 _VMSMasterEnv->measHists[idx] = \
427 makeFixedBinHist( numBins, startVal, binWidth, name );
429 #define saveCyclesAndInstrs(core,cycles,instrs) do{ \
430 int cycles_fd = _VMSMasterEnv->cycles_counter_fd[core]; \
431 int instrs_fd = _VMSMasterEnv->instrs_counter_fd[core]; \
432 int nread; \
433 \
434 nread = read(cycles_fd,&(cycles),sizeof(cycles)); \
435 if(nread<0){ \
436 perror("Error reading cycles counter"); \
437 cycles = 0; \
438 } \
439 \
440 nread = read(instrs_fd,&(instrs),sizeof(instrs)); \
441 if(nread<0){ \
442 perror("Error reading cycles counter"); \
443 instrs = 0; \
444 } \
445 } while (0)
447 enum eventType {
448 DebugEvt = 0,
449 AppResponderInvocation_start,
450 AppResponder_start,
451 AppResponder_end,
452 Assigner_start,
453 Assigner_end,
454 Work_start,
455 Work_end,
456 HwResponderInvocation_start
457 };
459 #define getReturnAddressBeforeLibraryCall(vp_ptr, res_ptr) do{ \
460 void* frame_ptr0 = vp_ptr->framePtr; \
461 void* frame_ptr1 = *((void**)frame_ptr0); \
462 void* frame_ptr2 = *((void**)frame_ptr1); \
463 void* frame_ptr3 = *((void**)frame_ptr2); \
464 void* ret_addr = *((void**)frame_ptr3 + 1); \
465 *res_ptr = ret_addr; \
466 } while (0)
468 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
470 #ifdef VPTHREAD
472 //VPThread
473 #define createHistIdx 0
474 #define mutexLockHistIdx 1
475 #define mutexUnlockHistIdx 2
476 #define condWaitHistIdx 3
477 #define condSignalHistIdx 4
479 #define MakeTheMeasHists() \
480 _VMSMasterEnv->measHistsInfo = \
481 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
482 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
483 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
484 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
485 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
486 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
488 #endif
491 #ifdef VCILK
493 //VCilk
494 #define spawnHistIdx 0
495 #define syncHistIdx 1
497 #define MakeTheMeasHists() \
498 _VMSMasterEnv->measHistsInfo = \
499 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
500 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
501 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
504 #endif
506 #ifdef SSR
508 //SSR
509 #define SendFromToHistIdx 0
510 #define SendOfTypeHistIdx 1
511 #define ReceiveFromToHistIdx 2
512 #define ReceiveOfTypeHistIdx 3
514 #define MakeTheMeasHists() \
515 _VMSMasterEnv->measHistsInfo = \
516 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
517 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
518 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
519 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
520 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
522 #endif
524 //===========================================================================
525 //VPThread
528 #define Meas_startCreate \
529 int32 startStamp, endStamp; \
530 saveLowTimeStampCountInto( startStamp ); \
532 #define Meas_endCreate \
533 saveLowTimeStampCountInto( endStamp ); \
534 addIntervalToHist( startStamp, endStamp, \
535 _VMSMasterEnv->measHists[ createHistIdx ] );
537 #define Meas_startMutexLock \
538 int32 startStamp, endStamp; \
539 saveLowTimeStampCountInto( startStamp ); \
541 #define Meas_endMutexLock \
542 saveLowTimeStampCountInto( endStamp ); \
543 addIntervalToHist( startStamp, endStamp, \
544 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
546 #define Meas_startMutexUnlock \
547 int32 startStamp, endStamp; \
548 saveLowTimeStampCountInto( startStamp ); \
550 #define Meas_endMutexUnlock \
551 saveLowTimeStampCountInto( endStamp ); \
552 addIntervalToHist( startStamp, endStamp, \
553 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
555 #define Meas_startCondWait \
556 int32 startStamp, endStamp; \
557 saveLowTimeStampCountInto( startStamp ); \
559 #define Meas_endCondWait \
560 saveLowTimeStampCountInto( endStamp ); \
561 addIntervalToHist( startStamp, endStamp, \
562 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
564 #define Meas_startCondSignal \
565 int32 startStamp, endStamp; \
566 saveLowTimeStampCountInto( startStamp ); \
568 #define Meas_endCondSignal \
569 saveLowTimeStampCountInto( endStamp ); \
570 addIntervalToHist( startStamp, endStamp, \
571 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
573 //===========================================================================
574 // VCilk
575 #define Meas_startSpawn \
576 int32 startStamp, endStamp; \
577 saveLowTimeStampCountInto( startStamp ); \
579 #define Meas_endSpawn \
580 saveLowTimeStampCountInto( endStamp ); \
581 addIntervalToHist( startStamp, endStamp, \
582 _VMSMasterEnv->measHists[ spawnHistIdx ] );
584 #define Meas_startSync \
585 int32 startStamp, endStamp; \
586 saveLowTimeStampCountInto( startStamp ); \
588 #define Meas_endSync \
589 saveLowTimeStampCountInto( endStamp ); \
590 addIntervalToHist( startStamp, endStamp, \
591 _VMSMasterEnv->measHists[ syncHistIdx ] );
593 //===========================================================================
594 // SSR
595 #define Meas_startSendFromTo \
596 int32 startStamp, endStamp; \
597 saveLowTimeStampCountInto( startStamp ); \
599 #define Meas_endSendFromTo \
600 saveLowTimeStampCountInto( endStamp ); \
601 addIntervalToHist( startStamp, endStamp, \
602 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
604 #define Meas_startSendOfType \
605 int32 startStamp, endStamp; \
606 saveLowTimeStampCountInto( startStamp ); \
608 #define Meas_endSendOfType \
609 saveLowTimeStampCountInto( endStamp ); \
610 addIntervalToHist( startStamp, endStamp, \
611 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
613 #define Meas_startReceiveFromTo \
614 int32 startStamp, endStamp; \
615 saveLowTimeStampCountInto( startStamp ); \
617 #define Meas_endReceiveFromTo \
618 saveLowTimeStampCountInto( endStamp ); \
619 addIntervalToHist( startStamp, endStamp, \
620 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
622 #define Meas_startReceiveOfType \
623 int32 startStamp, endStamp; \
624 saveLowTimeStampCountInto( startStamp ); \
626 #define Meas_endReceiveOfType \
627 saveLowTimeStampCountInto( endStamp ); \
628 addIntervalToHist( startStamp, endStamp, \
629 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
631 //=====
633 #include "ProcrContext.h"
634 #include "probes.h"
635 #include "vutilities.h"
637 #endif /* _VMS_H */