Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.h @ 167:981acd1db6af
Separate UCC recording from VMS core and put it into SSR plugin
| author | Nina Engelhardt |
|---|---|
| date | Mon, 05 Dec 2011 18:59:48 +0100 |
| parents | aefd87f9d12f |
| children | 3bd35fc83c61 |
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 "Counters/Counters.h"
20 #include "loop.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 2
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 ); //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 * );
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 //========================================
217 float64 createPtInSecs; //have space but don't use on some configs
218 int numTimesScheduled; //defines units together w/ procrID
219 };
220 //VirtProcr
223 /*WARNING: re-arranging this data structure could cause VP-switching
224 * assembly code to fail -- hard-codes offsets of fields
225 * (because -O3 messes with things otherwise)
226 */
227 typedef struct
228 {
229 SlaveScheduler slaveScheduler;
230 RequestHandler requestHandler;
232 SchedSlot ***allSchedSlots;
233 VMSQueueStruc **readyToAnimateQs;
234 VirtProcr **masterVPs;
236 void *semanticEnv;
237 void *OSEventStruc; //for future, when add I/O to BLIS
238 MallocProlog *freeListHead;
239 int32 amtOfOutstandingMem; //total currently allocated
241 void *coreLoopReturnPt;//addr to jump to to re-enter coreLoop
243 int32 setupComplete;
244 volatile int32 masterLock;
246 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
247 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
248 int32 workStealingLock;
250 int32 numProcrsCreated; //gives ordering to processor creation
252 //=========== MEASUREMENT STUFF =============
253 IntervalProbe **intervalProbes;
254 PrivDynArrayInfo *dynIntervalProbesInfo;
255 HashTable *probeNameHashTbl;
256 int32 masterCreateProbeID;
257 float64 createPtInSecs;
258 Histogram **measHists;
259 PrivDynArrayInfo *measHistsInfo;
260 #ifdef MEAS__TIME_PLUGIN
261 Histogram *reqHdlrLowTimeHist;
262 Histogram *reqHdlrHighTimeHist;
263 #endif
264 #ifdef MEAS__TIME_MALLOC
265 Histogram *mallocTimeHist;
266 Histogram *freeTimeHist;
267 #endif
268 #ifdef MEAS__TIME_MASTER_LOCK
269 Histogram *masterLockLowTimeHist;
270 Histogram *masterLockHighTimeHist;
271 #endif
272 #ifdef MEAS__PERF_COUNTERS
273 int cycles_counter_fd[NUM_CORES];
274 int instrs_counter_fd[NUM_CORES];
275 FILE* counteroutput;
276 #endif
277 #ifdef MEAS__PERF_COUNTERS //
278 CounterRecord** counter_history;
279 PrivDynArrayInfo* counter_history_array_info;
280 #endif
281 #ifdef DETECT_LOOP_GRAPH
282 bulb* loop_graph;
283 PrivDynArrayInfo* loop_graph_array_info;
284 int loop_counter[NUM_CORES];
285 #endif
286 }
287 MasterEnv;
289 //========================= Extra Stuff Data Strucs =======================
290 typedef struct
291 {
293 }
294 VMSExcp;
296 struct _GateStruc
297 {
298 int32 gateClosed;
299 int32 preGateProgress;
300 int32 waitProgress;
301 int32 exitProgress;
302 };
303 //GateStruc
305 //======================= OS Thread related ===============================
307 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
308 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
309 void masterLoop( void *initData, VirtProcr *masterPr );
312 typedef struct
313 {
314 void *endThdPt;
315 unsigned int coreNum;
316 }
317 ThdParams;
319 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
320 ThdParams *coreLoopThdParams [ NUM_CORES ];
321 pthread_mutex_t suspendLock;
322 pthread_cond_t suspend_cond;
326 //===================== Global Vars ===================
328 volatile MasterEnv *_VMSMasterEnv;
333 //=========================== Function Prototypes =========================
336 //========== Setup and shutdown ==========
337 void
338 VMS__init();
340 void
341 VMS__init_Seq();
343 void
344 VMS__start_the_work_then_wait_until_done();
346 void
347 VMS__start_the_work_then_wait_until_done_Seq();
349 inline VirtProcr *
350 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
352 void
353 VMS__dissipate_procr( VirtProcr *procrToDissipate );
355 //Use this to create processor inside entry point & other places outside
356 // the VMS system boundary (IE, not run in slave nor Master)
357 VirtProcr *
358 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
360 void
361 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
363 void
364 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
366 void
367 VMS__shutdown();
369 void
370 VMS__cleanup_at_end_of_shutdown();
372 void *
373 VMS__give_sem_env_for( VirtProcr *animPr );
376 //============== Request Related ===============
378 void
379 VMS__suspend_procr( VirtProcr *callingPr );
381 inline void
382 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
384 /*inline*/ __attribute__ ((noinline)) void
385 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
387 void
388 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
390 void /*inline**/ __attribute__ ((noinline))
391 VMS__send_dissipate_req( VirtProcr *prToDissipate );
393 /*inline**/ __attribute__ ((noinline)) void
394 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
396 VMSReqst *
397 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
399 inline void *
400 VMS__take_sem_reqst_from( VMSReqst *req );
402 void inline
403 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
404 ResumePrFnPtr resumePrFnPtr );
406 //======================== STATS ======================
408 //===== RDTSC wrapper ===== //Also runs with x86_64 code
410 #define saveTimeStampCountInto(low, high) \
411 asm volatile("RDTSC; \
412 movl %%eax, %0; \
413 movl %%edx, %1;" \
414 /* outputs */ : "=m" (low), "=m" (high)\
415 /* inputs */ : \
416 /* clobber */ : "%eax", "%edx" \
417 );
419 #define saveLowTimeStampCountInto(low) \
420 asm volatile("RDTSC; \
421 movl %%eax, %0;" \
422 /* outputs */ : "=m" (low) \
423 /* inputs */ : \
424 /* clobber */ : "%eax", "%edx" \
425 );
427 //====================
428 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
429 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
430 _VMSMasterEnv->measHists[idx] = \
431 makeFixedBinHist( numBins, startVal, binWidth, name );
433 #define saveCyclesAndInstrs(core,cycles,instrs) do{ \
434 int cycles_fd = _VMSMasterEnv->cycles_counter_fd[core]; \
435 int instrs_fd = _VMSMasterEnv->instrs_counter_fd[core]; \
436 int nread; \
437 \
438 nread = read(cycles_fd,&(cycles),sizeof(cycles)); \
439 if(nread<0){ \
440 perror("Error reading cycles counter"); \
441 cycles = 0; \
442 } \
443 \
444 nread = read(instrs_fd,&(instrs),sizeof(instrs)); \
445 if(nread<0){ \
446 perror("Error reading cycles counter"); \
447 instrs = 0; \
448 } \
449 } while (0)
451 #define getReturnAddressBeforeLibraryCall(vp_ptr, res_ptr) do{ \
452 void* frame_ptr0 = vp_ptr->framePtr; \
453 void* frame_ptr1 = *((void**)frame_ptr0); \
454 void* frame_ptr2 = *((void**)frame_ptr1); \
455 void* frame_ptr3 = *((void**)frame_ptr2); \
456 void* ret_addr = *((void**)frame_ptr3 + 1); \
457 *res_ptr = ret_addr; \
458 } while (0)
460 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
462 #ifdef VPTHREAD
464 //VPThread
465 #define createHistIdx 1
466 #define mutexLockHistIdx 2
467 #define mutexUnlockHistIdx 3
468 #define condWaitHistIdx 4
469 #define condSignalHistIdx 5
471 #define MakeTheMeasHists() \
472 _VMSMasterEnv->measHistsInfo = \
473 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
474 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
475 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
476 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
477 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
478 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
480 #endif
483 #ifdef VCILK
485 //VCilk
486 #define spawnHistIdx 1
487 #define syncHistIdx 2
489 #define MakeTheMeasHists() \
490 _VMSMasterEnv->measHistsInfo = \
491 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
492 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
493 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
496 #endif
498 #ifdef SSR
500 //SSR
501 #define SendFromToHistIdx 1
502 #define SendOfTypeHistIdx 2
503 #define ReceiveFromToHistIdx 3
504 #define ReceiveOfTypeHistIdx 4
506 #define MakeTheMeasHists() \
507 _VMSMasterEnv->measHistsInfo = \
508 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
509 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
510 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
511 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
512 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
514 #endif
516 //===========================================================================
517 //VPThread
520 #define Meas_startCreate \
521 int32 startStamp, endStamp; \
522 saveLowTimeStampCountInto( startStamp ); \
524 #define Meas_endCreate \
525 saveLowTimeStampCountInto( endStamp ); \
526 addIntervalToHist( startStamp, endStamp, \
527 _VMSMasterEnv->measHists[ createHistIdx ] );
529 #define Meas_startMutexLock \
530 int32 startStamp, endStamp; \
531 saveLowTimeStampCountInto( startStamp ); \
533 #define Meas_endMutexLock \
534 saveLowTimeStampCountInto( endStamp ); \
535 addIntervalToHist( startStamp, endStamp, \
536 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
538 #define Meas_startMutexUnlock \
539 int32 startStamp, endStamp; \
540 saveLowTimeStampCountInto( startStamp ); \
542 #define Meas_endMutexUnlock \
543 saveLowTimeStampCountInto( endStamp ); \
544 addIntervalToHist( startStamp, endStamp, \
545 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
547 #define Meas_startCondWait \
548 int32 startStamp, endStamp; \
549 saveLowTimeStampCountInto( startStamp ); \
551 #define Meas_endCondWait \
552 saveLowTimeStampCountInto( endStamp ); \
553 addIntervalToHist( startStamp, endStamp, \
554 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
556 #define Meas_startCondSignal \
557 int32 startStamp, endStamp; \
558 saveLowTimeStampCountInto( startStamp ); \
560 #define Meas_endCondSignal \
561 saveLowTimeStampCountInto( endStamp ); \
562 addIntervalToHist( startStamp, endStamp, \
563 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
565 //===========================================================================
566 // VCilk
567 #define Meas_startSpawn \
568 int32 startStamp, endStamp; \
569 saveLowTimeStampCountInto( startStamp ); \
571 #define Meas_endSpawn \
572 saveLowTimeStampCountInto( endStamp ); \
573 addIntervalToHist( startStamp, endStamp, \
574 _VMSMasterEnv->measHists[ spawnHistIdx ] );
576 #define Meas_startSync \
577 int32 startStamp, endStamp; \
578 saveLowTimeStampCountInto( startStamp ); \
580 #define Meas_endSync \
581 saveLowTimeStampCountInto( endStamp ); \
582 addIntervalToHist( startStamp, endStamp, \
583 _VMSMasterEnv->measHists[ syncHistIdx ] );
585 //===========================================================================
586 // SSR
587 #define Meas_startSendFromTo \
588 int32 startStamp, endStamp; \
589 saveLowTimeStampCountInto( startStamp ); \
591 #define Meas_endSendFromTo \
592 saveLowTimeStampCountInto( endStamp ); \
593 addIntervalToHist( startStamp, endStamp, \
594 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
596 #define Meas_startSendOfType \
597 int32 startStamp, endStamp; \
598 saveLowTimeStampCountInto( startStamp ); \
600 #define Meas_endSendOfType \
601 saveLowTimeStampCountInto( endStamp ); \
602 addIntervalToHist( startStamp, endStamp, \
603 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
605 #define Meas_startReceiveFromTo \
606 int32 startStamp, endStamp; \
607 saveLowTimeStampCountInto( startStamp ); \
609 #define Meas_endReceiveFromTo \
610 saveLowTimeStampCountInto( endStamp ); \
611 addIntervalToHist( startStamp, endStamp, \
612 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
614 #define Meas_startReceiveOfType \
615 int32 startStamp, endStamp; \
616 saveLowTimeStampCountInto( startStamp ); \
618 #define Meas_endReceiveOfType \
619 saveLowTimeStampCountInto( endStamp ); \
620 addIntervalToHist( startStamp, endStamp, \
621 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
623 //=====
625 #include "ProcrContext.h"
626 #include "probes.h"
627 #include "vutilities.h"
629 #endif /* _VMS_H */
