Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.h @ 168:d7c0c0a8187a
Merged default branch
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Wed, 02 Nov 2011 14:59:29 +0100 |
| parents | 395f58384a5c 9661b8cc8318 |
| 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 "Counters/Counters.h"
20 #include "dependency.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 DETECT_DEPENDENCIES
81 //========================= Hardware related Constants =====================
82 //This value is the number of hardware threads in the shared memory
83 // machine
84 //#define NUM_CORES 8
86 // tradeoff amortizing master fixed overhead vs imbalance potential
87 // when work-stealing, can make bigger, at risk of losing cache affinity
88 #define NUM_SCHED_SLOTS 5
90 #define MIN_WORK_UNIT_CYCLES 20000
92 #define MASTERLOCK_RETRIES 10000
94 // stack size in virtual processors created
95 #define VIRT_PROCR_STACK_SIZE 0x8000 /* 32K */
97 // memory for VMS__malloc
98 #define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
100 #define CACHE_LINE 64
101 #define PAGE_SIZE 4096
104 //==============================
106 #define SUCCESS 0
108 #define writeVMSQ writePrivQ
109 #define readVMSQ readPrivQ
110 #define makeVMSQ makeVMSPrivQ
111 #define numInVMSQ numInPrivQ
112 #define VMSQueueStruc PrivQueueStruc
116 //===========================================================================
117 typedef unsigned long long TSCount;
119 typedef struct _SchedSlot SchedSlot;
120 typedef struct _VMSReqst VMSReqst;
121 typedef struct _VirtProcr VirtProcr;
122 typedef struct _IntervalProbe IntervalProbe;
123 typedef struct _GateStruc GateStruc;
126 typedef VirtProcr * (*SlaveScheduler) ( void *, int ); //semEnv, coreIdx
127 typedef void (*RequestHandler) ( VirtProcr *, void * ); //prWReqst, semEnv
128 typedef void (*VirtProcrFnPtr) ( void *, VirtProcr * ); //initData, animPr
129 typedef void VirtProcrFn ( void *, VirtProcr * ); //initData, animPr
130 typedef void (*ResumePrFnPtr) ( VirtProcr *, void * );
133 //============= Requests ===========
134 //
136 enum VMSReqstType //avoid starting enums at 0, for debug reasons
137 {
138 semantic = 1,
139 createReq,
140 dissipate,
141 VMSSemantic //goes with VMSSemReqst below
142 };
144 struct _VMSReqst
145 {
146 enum VMSReqstType reqType;//used for dissipate and in future for IO requests
147 void *semReqData;
149 VMSReqst *nextReqst;
150 };
151 //VMSReqst
153 enum VMSSemReqstType //These are equivalent to semantic requests, but for
154 { // VMS's services available directly to app, like OS
155 createProbe = 1, // and probe services -- like a VMS-wide built-in lang
156 openFile,
157 otherIO
158 };
160 typedef struct
161 { enum VMSSemReqstType reqType;
162 VirtProcr *requestingPr;
163 char *nameStr; //for create probe
164 }
165 VMSSemReq;
168 //==================== Core data structures ===================
170 struct _SchedSlot
171 {
172 int workIsDone;
173 int needsProcrAssigned;
174 VirtProcr *procrAssignedToSlot;
175 };
176 //SchedSlot
178 /*WARNING: re-arranging this data structure could cause VP switching
179 * assembly code to fail -- hard-codes offsets of fields
180 */
181 struct _VirtProcr
182 { int procrID; //for debugging -- count up each time create
183 int coreAnimatedBy;
184 void *startOfStack;
185 void *stackPtr;
186 void *framePtr;
187 void *nextInstrPt;
189 void *coreLoopStartPt; //allows proto-runtime to be linked later
190 void *coreLoopFramePtr; //restore before jmp back to core loop
191 void *coreLoopStackPtr; //restore before jmp back to core loop
193 void *initialData;
195 SchedSlot *schedSlot;
196 VMSReqst *requests;
198 void *semanticData; //this livesUSE_GNU here for the life of VP
199 void *dataRetFromReq;//values returned from plugin to VP go here
201 //=========== MEASUREMENT STUFF ==========
202 #ifdef MEAS__TIME_STAMP_SUSP
203 unsigned int preSuspTSCLow;
204 unsigned int postSuspTSCLow;
205 #endif
206 #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
207 unsigned int startMasterTSCLow;USE_GNU
208 unsigned int endMasterTSCLow;
209 #endif
210 #ifdef MEAS__PERF_COUNTERS //
211 CounterRecord** counter_history;
212 PrivDynArrayInfo* counter_history_array_info;
213 #endif
214 //========================================
216 float64 createPtInSecs; //have space but don't use on some configs
217 };
218 //VirtProcr
221 /*WARNING: re-arranging this data structure could cause VP-switching
222 * assembly code to fail -- hard-codes offsets of fields
223 * (because -O3 messes with things otherwise)
224 */
225 typedef struct
226 {
227 SlaveScheduler slaveScheduler;
228 RequestHandler requestHandler;
230 SchedSlot ***allSchedSlots;
231 VMSQueueStruc **readyToAnimateQs;
232 VirtProcr **masterVPs;
234 void *semanticEnv;
235 void *OSEventStruc; //for future, when add I/O to BLIS
236 MallocProlog *freeListHead;
237 int32 amtOfOutstandingMem; //total currently allocated
239 void *coreLoopReturnPt;//addr to jump to to re-enter coreLoop
241 int32 setupComplete;
242 volatile int32 masterLock;
244 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
245 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
246 int32 workStealingLock;
248 int32 numProcrsCreated; //gives ordering to processor creation
250 //=========== MEASUREMENT STUFF =============
251 IntervalProbe **intervalProbes;
252 PrivDynArrayInfo *dynIntervalProbesInfo;
253 HashTable *probeNameHashTbl;
254 int32 masterCreateProbeID;
255 float64 createPtInSecs;
256 Histogram **measHists;
257 PrivDynArrayInfo *measHistsInfo;
258 #ifdef MEAS__TIME_PLUGIN
259 Histogram *reqHdlrLowTimeHist;
260 Histogram *reqHdlrHighTimeHist;
261 #endif
262 #ifdef MEAS__TIME_MALLOC
263 Histogram *mallocTimeHist;
264 Histogram *freeTimeHist;
265 #endif
266 #ifdef MEAS__TIME_MASTER_LOCK
267 Histogram *masterLockLowTimeHist;
268 Histogram *masterLockHighTimeHist;
269 #endif
270 #ifdef MEAS__PERF_COUNTERS
271 int cycles_counter_fd[NUM_CORES];
272 int instrs_counter_fd[NUM_CORES];
273 FILE* counteroutput;
274 #endif
275 #ifdef DETECT_DEPENDENCIES
276 Dependency** dependencies;
277 PrivDynArrayInfo* dependenciesInfo;
278 #endif
279 #ifdef MEAS__PERF_COUNTERS //
280 CounterRecord** counter_history;
281 PrivDynArrayInfo* counter_history_array_info;
282 #endif
283 }
284 MasterEnv;
286 //========================= Extra Stuff Data Strucs =======================
287 typedef struct
288 {
290 }
291 VMSExcp;
293 struct _GateStruc
294 {
295 int32 gateClosed;
296 int32 preGateProgress;
297 int32 waitProgress;
298 int32 exitProgress;
299 };
300 //GateStruc
302 //======================= OS Thread related ===============================
304 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
305 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
306 void masterLoop( void *initData, VirtProcr *masterPr );
309 typedef struct
310 {
311 void *endThdPt;
312 unsigned int coreNum;
313 }
314 ThdParams;
316 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
317 ThdParams *coreLoopThdParams [ NUM_CORES ];
318 pthread_mutex_t suspendLock;
319 pthread_cond_t suspend_cond;
323 //===================== Global Vars ===================
325 volatile MasterEnv *_VMSMasterEnv;
330 //=========================== Function Prototypes =========================
333 //========== Setup and shutdown ==========
334 void
335 VMS__init();
337 void
338 VMS__init_Seq();
340 void
341 VMS__start_the_work_then_wait_until_done();
343 void
344 VMS__start_the_work_then_wait_until_done_Seq();
346 inline VirtProcr *
347 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
349 void
350 VMS__dissipate_procr( VirtProcr *procrToDissipate );
352 //Use this to create processor inside entry point & other places outside
353 // the VMS system boundary (IE, not run in slave nor Master)
354 VirtProcr *
355 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
357 void
358 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
360 void
361 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
363 void
364 VMS__shutdown();
366 void
367 VMS__cleanup_at_end_of_shutdown();
369 void *
370 VMS__give_sem_env_for( VirtProcr *animPr );
373 //============== Request Related ===============
375 void
376 VMS__suspend_procr( VirtProcr *callingPr );
378 inline void
379 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
381 /*inline*/ __attribute__ ((noinline)) void
382 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
384 void
385 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
387 void /*inline**/ __attribute__ ((noinline))
388 VMS__send_dissipate_req( VirtProcr *prToDissipate );
390 /*inline**/ __attribute__ ((noinline)) void
391 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
393 VMSReqst *
394 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
396 inline void *
397 VMS__take_sem_reqst_from( VMSReqst *req );
399 void inline
400 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
401 ResumePrFnPtr resumePrFnPtr );
403 //======================== STATS ======================
405 //===== RDTSC wrapper ===== //Also runs with x86_64 code
407 #define saveTimeStampCountInto(low, high) \
408 asm volatile("RDTSC; \
409 movl %%eax, %0; \
410 movl %%edx, %1;" \
411 /* outputs */ : "=m" (low), "=m" (high)\
412 /* inputs */ : \
413 /* clobber */ : "%eax", "%edx" \
414 );
416 #define saveLowTimeStampCountInto(low) \
417 asm volatile("RDTSC; \
418 movl %%eax, %0;" \
419 /* outputs */ : "=m" (low) \
420 /* inputs */ : \
421 /* clobber */ : "%eax", "%edx" \
422 );
424 //====================
425 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
426 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
427 _VMSMasterEnv->measHists[idx] = \
428 makeFixedBinHist( numBins, startVal, binWidth, name );
430 #define saveCyclesAndInstrs(core,cycles,instrs) do{ \
431 int cycles_fd = _VMSMasterEnv->cycles_counter_fd[core]; \
432 int instrs_fd = _VMSMasterEnv->instrs_counter_fd[core]; \
433 int nread; \
434 \
435 nread = read(cycles_fd,&(cycles),sizeof(cycles)); \
436 if(nread<0){ \
437 perror("Error reading cycles counter"); \
438 cycles = 0; \
439 } \
440 \
441 nread = read(instrs_fd,&(instrs),sizeof(instrs)); \
442 if(nread<0){ \
443 perror("Error reading cycles counter"); \
444 instrs = 0; \
445 } \
446 } while (0)
448 #define getReturnAddressBeforeLibraryCall(vp_ptr, res_ptr) do{ \
449 void* frame_ptr0 = vp_ptr->framePtr; \
450 void* frame_ptr1 = *((void**)frame_ptr0); \
451 void* frame_ptr2 = *((void**)frame_ptr1); \
452 void* frame_ptr3 = *((void**)frame_ptr2); \
453 void* ret_addr = *((void**)frame_ptr3 + 1); \
454 *res_ptr = ret_addr; \
455 } while (0)
457 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
459 #ifdef VPTHREAD
461 //VPThread
462 #define createHistIdx 0
463 #define mutexLockHistIdx 1
464 #define mutexUnlockHistIdx 2
465 #define condWaitHistIdx 3
466 #define condSignalHistIdx 4
468 #define MakeTheMeasHists() \
469 _VMSMasterEnv->measHistsInfo = \
470 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
471 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
472 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
473 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
474 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
475 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
477 #endif
480 #ifdef VCILK
482 //VCilk
483 #define spawnHistIdx 0
484 #define syncHistIdx 1
486 #define MakeTheMeasHists() \
487 _VMSMasterEnv->measHistsInfo = \
488 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
489 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
490 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
493 #endif
495 #ifdef SSR
497 //SSR
498 #define SendFromToHistIdx 0
499 #define SendOfTypeHistIdx 1
500 #define ReceiveFromToHistIdx 2
501 #define ReceiveOfTypeHistIdx 3
503 #define MakeTheMeasHists() \
504 _VMSMasterEnv->measHistsInfo = \
505 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
506 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
507 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
508 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
509 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
511 #endif
513 //===========================================================================
514 //VPThread
517 #define Meas_startCreate \
518 int32 startStamp, endStamp; \
519 saveLowTimeStampCountInto( startStamp ); \
521 #define Meas_endCreate \
522 saveLowTimeStampCountInto( endStamp ); \
523 addIntervalToHist( startStamp, endStamp, \
524 _VMSMasterEnv->measHists[ createHistIdx ] );
526 #define Meas_startMutexLock \
527 int32 startStamp, endStamp; \
528 saveLowTimeStampCountInto( startStamp ); \
530 #define Meas_endMutexLock \
531 saveLowTimeStampCountInto( endStamp ); \
532 addIntervalToHist( startStamp, endStamp, \
533 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
535 #define Meas_startMutexUnlock \
536 int32 startStamp, endStamp; \
537 saveLowTimeStampCountInto( startStamp ); \
539 #define Meas_endMutexUnlock \
540 saveLowTimeStampCountInto( endStamp ); \
541 addIntervalToHist( startStamp, endStamp, \
542 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
544 #define Meas_startCondWait \
545 int32 startStamp, endStamp; \
546 saveLowTimeStampCountInto( startStamp ); \
548 #define Meas_endCondWait \
549 saveLowTimeStampCountInto( endStamp ); \
550 addIntervalToHist( startStamp, endStamp, \
551 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
553 #define Meas_startCondSignal \
554 int32 startStamp, endStamp; \
555 saveLowTimeStampCountInto( startStamp ); \
557 #define Meas_endCondSignal \
558 saveLowTimeStampCountInto( endStamp ); \
559 addIntervalToHist( startStamp, endStamp, \
560 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
562 //===========================================================================
563 // VCilk
564 #define Meas_startSpawn \
565 int32 startStamp, endStamp; \
566 saveLowTimeStampCountInto( startStamp ); \
568 #define Meas_endSpawn \
569 saveLowTimeStampCountInto( endStamp ); \
570 addIntervalToHist( startStamp, endStamp, \
571 _VMSMasterEnv->measHists[ spawnHistIdx ] );
573 #define Meas_startSync \
574 int32 startStamp, endStamp; \
575 saveLowTimeStampCountInto( startStamp ); \
577 #define Meas_endSync \
578 saveLowTimeStampCountInto( endStamp ); \
579 addIntervalToHist( startStamp, endStamp, \
580 _VMSMasterEnv->measHists[ syncHistIdx ] );
582 //===========================================================================
583 // SSR
584 #define Meas_startSendFromTo \
585 int32 startStamp, endStamp; \
586 saveLowTimeStampCountInto( startStamp ); \
588 #define Meas_endSendFromTo \
589 saveLowTimeStampCountInto( endStamp ); \
590 addIntervalToHist( startStamp, endStamp, \
591 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
593 #define Meas_startSendOfType \
594 int32 startStamp, endStamp; \
595 saveLowTimeStampCountInto( startStamp ); \
597 #define Meas_endSendOfType \
598 saveLowTimeStampCountInto( endStamp ); \
599 addIntervalToHist( startStamp, endStamp, \
600 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
602 #define Meas_startReceiveFromTo \
603 int32 startStamp, endStamp; \
604 saveLowTimeStampCountInto( startStamp ); \
606 #define Meas_endReceiveFromTo \
607 saveLowTimeStampCountInto( endStamp ); \
608 addIntervalToHist( startStamp, endStamp, \
609 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
611 #define Meas_startReceiveOfType \
612 int32 startStamp, endStamp; \
613 saveLowTimeStampCountInto( startStamp ); \
615 #define Meas_endReceiveOfType \
616 saveLowTimeStampCountInto( endStamp ); \
617 addIntervalToHist( startStamp, endStamp, \
618 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
620 //=====
622 #include "ProcrContext.h"
623 #include "probes.h"
624 #include "vutilities.h"
626 #endif /* _VMS_H */
