Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.h @ 166:aefd87f9d12f
loop graph
| author | Nina Engelhardt |
|---|---|
| date | Tue, 18 Oct 2011 15:53:04 +0200 |
| parents | 395f58384a5c |
| children | 981acd1db6af |
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 "dependency.h"
21 #include "loop.h"
23 #include <pthread.h>
24 #include <sys/time.h>
27 //=============================== Debug ===================================
28 //
29 //When SEQUENTIAL is defined, VMS does sequential exe in the main thread
30 // It still does co-routines and all the mechanisms are the same, it just
31 // has only a single thread and animates VPs one at a time
32 //#define SEQUENTIAL
34 //#define USE_WORK_STEALING
36 //turns on the probe-instrumentation in the application -- when not
37 // defined, the calls to the probe functions turn into comments
38 //#define STATS__ENABLE_PROBES
39 //#define TURN_ON_DEBUG_PROBES
41 //These defines turn types of bug messages on and off
42 // be sure debug messages are un-commented (next block of defines)
43 #define dbgAppFlow FALSE /* Top level flow of application code -- general*/
44 #define dbgProbes FALSE /* for issues inside probes themselves*/
45 #define dbgB2BMaster FALSE /* in coreloop, back to back master VPs*/
46 #define dbgRqstHdlr FALSE /* in request handler code*/
47 #define dbgDependency TRUE /* in request handler code, print dependencies */
49 //Comment or un- the substitute half to turn on/off types of debug message
50 #define DEBUG( bool, msg) \
51 if( bool){ printf(msg); fflush(stdin);}
52 #define DEBUG1( bool, msg, param) \
53 if(bool){printf(msg, param); fflush(stdin);}
54 #define DEBUG2( bool, msg, p1, p2) \
55 if(bool) {printf(msg, p1, p2); fflush(stdin);}
57 #define ERROR(msg) printf(msg);
58 #define ERROR1(msg, param) printf(msg, param);
59 #define ERROR2(msg, p1, p2) printf(msg, p1, p2);
61 //=========================== STATS =======================
63 //when MEAS__TIME_STAMP_SUSP is defined, causes code to be inserted and
64 // compiled-in that saves the low part of the time stamp count just before
65 // suspending a processor and just after resuming that processor. It is
66 // saved into a field added to VirtProcr. Have to sanity-check for
67 // rollover of low portion into high portion.
68 //#define MEAS__TIME_STAMP_SUSP
69 //#define MEAS__TIME_MASTER
70 //#define MEAS__TIME_PLUGIN
71 //#define MEAS__TIME_MALLOC
72 //#define MEAS__TIME_MASTER_LOCK
73 //#define MEAS__NUM_TIMES_TO_RUN 100000
75 //For code that calculates normalization-offset between TSC counts of
76 // different cores.
77 //#define NUM_TSC_ROUND_TRIPS 10
79 #define MEAS__PERF_COUNTERS
80 #define DETECT_DEPENDENCIES
81 #define DETECT_LOOP_GRAPH
83 //========================= Hardware related Constants =====================
84 //This value is the number of hardware threads in the shared memory
85 // machine
86 #define NUM_CORES 2
88 // tradeoff amortizing master fixed overhead vs imbalance potential
89 // when work-stealing, can make bigger, at risk of losing cache affinity
90 #define NUM_SCHED_SLOTS 5
92 #define MIN_WORK_UNIT_CYCLES 20000
94 #define MASTERLOCK_RETRIES 10000
96 // stack size in virtual processors created
97 #define VIRT_PROCR_STACK_SIZE 0x8000 /* 32K */
99 // memory for VMS__malloc
100 #define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
102 #define CACHE_LINE 64
103 #define PAGE_SIZE 4096
106 //==============================
108 #define SUCCESS 0
110 #define writeVMSQ writePrivQ
111 #define readVMSQ readPrivQ
112 #define makeVMSQ makeVMSPrivQ
113 #define numInVMSQ numInPrivQ
114 #define VMSQueueStruc PrivQueueStruc
118 //===========================================================================
119 typedef unsigned long long TSCount;
121 typedef struct _SchedSlot SchedSlot;
122 typedef struct _VMSReqst VMSReqst;
123 typedef struct _VirtProcr VirtProcr;
124 typedef struct _IntervalProbe IntervalProbe;
125 typedef struct _GateStruc GateStruc;
128 typedef VirtProcr * (*SlaveScheduler) ( void *, int ); //semEnv, coreIdx
129 typedef void (*RequestHandler) ( VirtProcr *, void * ); //prWReqst, semEnv
130 typedef void (*VirtProcrFnPtr) ( void *, VirtProcr * ); //initData, animPr
131 typedef void VirtProcrFn ( void *, VirtProcr * ); //initData, animPr
132 typedef void (*ResumePrFnPtr) ( VirtProcr *, void * );
135 //============= Requests ===========
136 //
138 enum VMSReqstType //avoid starting enums at 0, for debug reasons
139 {
140 semantic = 1,
141 createReq,
142 dissipate,
143 VMSSemantic //goes with VMSSemReqst below
144 };
146 struct _VMSReqst
147 {
148 enum VMSReqstType reqType;//used for dissipate and in future for IO requests
149 void *semReqData;
151 VMSReqst *nextReqst;
152 };
153 //VMSReqst
155 enum VMSSemReqstType //These are equivalent to semantic requests, but for
156 { // VMS's services available directly to app, like OS
157 createProbe = 1, // and probe services -- like a VMS-wide built-in lang
158 openFile,
159 otherIO
160 };
162 typedef struct
163 { enum VMSSemReqstType reqType;
164 VirtProcr *requestingPr;
165 char *nameStr; //for create probe
166 }
167 VMSSemReq;
170 //==================== Core data structures ===================
172 struct _SchedSlot
173 {
174 int workIsDone;
175 int needsProcrAssigned;
176 VirtProcr *procrAssignedToSlot;
177 };
178 //SchedSlot
180 /*WARNING: re-arranging this data structure could cause VP switching
181 * assembly code to fail -- hard-codes offsets of fields
182 */
183 struct _VirtProcr
184 { int procrID; //for debugging -- count up each time create
185 int coreAnimatedBy;
186 void *startOfStack;
187 void *stackPtr;
188 void *framePtr;
189 void *nextInstrPt;
191 void *coreLoopStartPt; //allows proto-runtime to be linked later
192 void *coreLoopFramePtr; //restore before jmp back to core loop
193 void *coreLoopStackPtr; //restore before jmp back to core loop
195 void *initialData;
197 SchedSlot *schedSlot;
198 VMSReqst *requests;
200 void *semanticData; //this livesUSE_GNU here for the life of VP
201 void *dataRetFromReq;//values returned from plugin to VP go here
203 //=========== MEASUREMENT STUFF ==========
204 #ifdef MEAS__TIME_STAMP_SUSP
205 unsigned int preSuspTSCLow;
206 unsigned int postSuspTSCLow;
207 #endif
208 #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
209 unsigned int startMasterTSCLow;USE_GNU
210 unsigned int endMasterTSCLow;
211 #endif
212 #ifdef MEAS__PERF_COUNTERS //
213 CounterRecord** counter_history;
214 PrivDynArrayInfo* counter_history_array_info;
215 #endif
216 //========================================
218 float64 createPtInSecs; //have space but don't use on some configs
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 DETECT_DEPENDENCIES
278 Dependency** dependencies;
279 PrivDynArrayInfo* dependenciesInfo;
280 #endif
281 #ifdef MEAS__PERF_COUNTERS //
282 CounterRecord** counter_history;
283 PrivDynArrayInfo* counter_history_array_info;
284 #endif
285 #ifdef DETECT_LOOP_GRAPH
286 bulb* loop_graph;
287 PrivDynArrayInfo* loop_graph_array_info;
288 int loop_counter[NUM_CORES];
289 #endif
290 }
291 MasterEnv;
293 //========================= Extra Stuff Data Strucs =======================
294 typedef struct
295 {
297 }
298 VMSExcp;
300 struct _GateStruc
301 {
302 int32 gateClosed;
303 int32 preGateProgress;
304 int32 waitProgress;
305 int32 exitProgress;
306 };
307 //GateStruc
309 //======================= OS Thread related ===============================
311 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
312 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
313 void masterLoop( void *initData, VirtProcr *masterPr );
316 typedef struct
317 {
318 void *endThdPt;
319 unsigned int coreNum;
320 }
321 ThdParams;
323 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
324 ThdParams *coreLoopThdParams [ NUM_CORES ];
325 pthread_mutex_t suspendLock;
326 pthread_cond_t suspend_cond;
330 //===================== Global Vars ===================
332 volatile MasterEnv *_VMSMasterEnv;
337 //=========================== Function Prototypes =========================
340 //========== Setup and shutdown ==========
341 void
342 VMS__init();
344 void
345 VMS__init_Seq();
347 void
348 VMS__start_the_work_then_wait_until_done();
350 void
351 VMS__start_the_work_then_wait_until_done_Seq();
353 inline VirtProcr *
354 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
356 void
357 VMS__dissipate_procr( VirtProcr *procrToDissipate );
359 //Use this to create processor inside entry point & other places outside
360 // the VMS system boundary (IE, not run in slave nor Master)
361 VirtProcr *
362 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
364 void
365 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
367 void
368 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
370 void
371 VMS__shutdown();
373 void
374 VMS__cleanup_at_end_of_shutdown();
376 void *
377 VMS__give_sem_env_for( VirtProcr *animPr );
380 //============== Request Related ===============
382 void
383 VMS__suspend_procr( VirtProcr *callingPr );
385 inline void
386 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
388 /*inline*/ __attribute__ ((noinline)) void
389 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
391 void
392 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
394 void /*inline**/ __attribute__ ((noinline))
395 VMS__send_dissipate_req( VirtProcr *prToDissipate );
397 /*inline**/ __attribute__ ((noinline)) void
398 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
400 VMSReqst *
401 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
403 inline void *
404 VMS__take_sem_reqst_from( VMSReqst *req );
406 void inline
407 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
408 ResumePrFnPtr resumePrFnPtr );
410 //======================== STATS ======================
412 //===== RDTSC wrapper ===== //Also runs with x86_64 code
414 #define saveTimeStampCountInto(low, high) \
415 asm volatile("RDTSC; \
416 movl %%eax, %0; \
417 movl %%edx, %1;" \
418 /* outputs */ : "=m" (low), "=m" (high)\
419 /* inputs */ : \
420 /* clobber */ : "%eax", "%edx" \
421 );
423 #define saveLowTimeStampCountInto(low) \
424 asm volatile("RDTSC; \
425 movl %%eax, %0;" \
426 /* outputs */ : "=m" (low) \
427 /* inputs */ : \
428 /* clobber */ : "%eax", "%edx" \
429 );
431 //====================
432 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
433 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
434 _VMSMasterEnv->measHists[idx] = \
435 makeFixedBinHist( numBins, startVal, binWidth, name );
437 #define saveCyclesAndInstrs(core,cycles,instrs) do{ \
438 int cycles_fd = _VMSMasterEnv->cycles_counter_fd[core]; \
439 int instrs_fd = _VMSMasterEnv->instrs_counter_fd[core]; \
440 int nread; \
441 \
442 nread = read(cycles_fd,&(cycles),sizeof(cycles)); \
443 if(nread<0){ \
444 perror("Error reading cycles counter"); \
445 cycles = 0; \
446 } \
447 \
448 nread = read(instrs_fd,&(instrs),sizeof(instrs)); \
449 if(nread<0){ \
450 perror("Error reading cycles counter"); \
451 instrs = 0; \
452 } \
453 } while (0)
455 #define getReturnAddressBeforeLibraryCall(vp_ptr, res_ptr) do{ \
456 void* frame_ptr0 = vp_ptr->framePtr; \
457 void* frame_ptr1 = *((void**)frame_ptr0); \
458 void* frame_ptr2 = *((void**)frame_ptr1); \
459 void* frame_ptr3 = *((void**)frame_ptr2); \
460 void* ret_addr = *((void**)frame_ptr3 + 1); \
461 *res_ptr = ret_addr; \
462 } while (0)
464 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
466 #ifdef VPTHREAD
468 //VPThread
469 #define createHistIdx 1
470 #define mutexLockHistIdx 2
471 #define mutexUnlockHistIdx 3
472 #define condWaitHistIdx 4
473 #define condSignalHistIdx 5
475 #define MakeTheMeasHists() \
476 _VMSMasterEnv->measHistsInfo = \
477 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
478 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
479 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
480 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
481 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
482 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
484 #endif
487 #ifdef VCILK
489 //VCilk
490 #define spawnHistIdx 1
491 #define syncHistIdx 2
493 #define MakeTheMeasHists() \
494 _VMSMasterEnv->measHistsInfo = \
495 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
496 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
497 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
500 #endif
502 #ifdef SSR
504 //SSR
505 #define SendFromToHistIdx 1
506 #define SendOfTypeHistIdx 2
507 #define ReceiveFromToHistIdx 3
508 #define ReceiveOfTypeHistIdx 4
510 #define MakeTheMeasHists() \
511 _VMSMasterEnv->measHistsInfo = \
512 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
513 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
514 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
515 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
516 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
518 #endif
520 //===========================================================================
521 //VPThread
524 #define Meas_startCreate \
525 int32 startStamp, endStamp; \
526 saveLowTimeStampCountInto( startStamp ); \
528 #define Meas_endCreate \
529 saveLowTimeStampCountInto( endStamp ); \
530 addIntervalToHist( startStamp, endStamp, \
531 _VMSMasterEnv->measHists[ createHistIdx ] );
533 #define Meas_startMutexLock \
534 int32 startStamp, endStamp; \
535 saveLowTimeStampCountInto( startStamp ); \
537 #define Meas_endMutexLock \
538 saveLowTimeStampCountInto( endStamp ); \
539 addIntervalToHist( startStamp, endStamp, \
540 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
542 #define Meas_startMutexUnlock \
543 int32 startStamp, endStamp; \
544 saveLowTimeStampCountInto( startStamp ); \
546 #define Meas_endMutexUnlock \
547 saveLowTimeStampCountInto( endStamp ); \
548 addIntervalToHist( startStamp, endStamp, \
549 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
551 #define Meas_startCondWait \
552 int32 startStamp, endStamp; \
553 saveLowTimeStampCountInto( startStamp ); \
555 #define Meas_endCondWait \
556 saveLowTimeStampCountInto( endStamp ); \
557 addIntervalToHist( startStamp, endStamp, \
558 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
560 #define Meas_startCondSignal \
561 int32 startStamp, endStamp; \
562 saveLowTimeStampCountInto( startStamp ); \
564 #define Meas_endCondSignal \
565 saveLowTimeStampCountInto( endStamp ); \
566 addIntervalToHist( startStamp, endStamp, \
567 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
569 //===========================================================================
570 // VCilk
571 #define Meas_startSpawn \
572 int32 startStamp, endStamp; \
573 saveLowTimeStampCountInto( startStamp ); \
575 #define Meas_endSpawn \
576 saveLowTimeStampCountInto( endStamp ); \
577 addIntervalToHist( startStamp, endStamp, \
578 _VMSMasterEnv->measHists[ spawnHistIdx ] );
580 #define Meas_startSync \
581 int32 startStamp, endStamp; \
582 saveLowTimeStampCountInto( startStamp ); \
584 #define Meas_endSync \
585 saveLowTimeStampCountInto( endStamp ); \
586 addIntervalToHist( startStamp, endStamp, \
587 _VMSMasterEnv->measHists[ syncHistIdx ] );
589 //===========================================================================
590 // SSR
591 #define Meas_startSendFromTo \
592 int32 startStamp, endStamp; \
593 saveLowTimeStampCountInto( startStamp ); \
595 #define Meas_endSendFromTo \
596 saveLowTimeStampCountInto( endStamp ); \
597 addIntervalToHist( startStamp, endStamp, \
598 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
600 #define Meas_startSendOfType \
601 int32 startStamp, endStamp; \
602 saveLowTimeStampCountInto( startStamp ); \
604 #define Meas_endSendOfType \
605 saveLowTimeStampCountInto( endStamp ); \
606 addIntervalToHist( startStamp, endStamp, \
607 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
609 #define Meas_startReceiveFromTo \
610 int32 startStamp, endStamp; \
611 saveLowTimeStampCountInto( startStamp ); \
613 #define Meas_endReceiveFromTo \
614 saveLowTimeStampCountInto( endStamp ); \
615 addIntervalToHist( startStamp, endStamp, \
616 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
618 #define Meas_startReceiveOfType \
619 int32 startStamp, endStamp; \
620 saveLowTimeStampCountInto( startStamp ); \
622 #define Meas_endReceiveOfType \
623 saveLowTimeStampCountInto( endStamp ); \
624 addIntervalToHist( startStamp, endStamp, \
625 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
627 //=====
629 #include "ProcrContext.h"
630 #include "probes.h"
631 #include "vutilities.h"
633 #endif /* _VMS_H */
