Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.h @ 114:73fc5aafbe45
merge things changed in default branch
| author | Nina Engelhardt |
|---|---|
| date | Wed, 24 Aug 2011 16:07:38 +0200 |
| parents | fac1d896f6e9 efb55f1b5fb9 |
| children | ce02441b77cf |
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"
21 #include <pthread.h>
22 #include <sys/time.h>
25 //=============================== Debug ===================================
26 //
27 //When SEQUENTIAL is defined, VMS does sequential exe in the main thread
28 // It still does co-routines and all the mechanisms are the same, it just
29 // has only a single thread and animates VPs one at a time
30 //#define SEQUENTIAL
32 //#define USE_WORK_STEALING
34 //turns on the probe-instrumentation in the application -- when not
35 // defined, the calls to the probe functions turn into comments
36 //#define STATS__ENABLE_PROBES
37 //#define TURN_ON_DEBUG_PROBES
39 //These defines turn types of bug messages on and off
40 // be sure debug messages are un-commented (next block of defines)
41 #define dbgAppFlow TRUE /* Top level flow of application code -- general*/
42 #define dbgProbes FALSE /* for issues inside probes themselves*/
43 #define dbgB2BMaster FALSE /* in coreloop, back to back master VPs*/
44 #define dbgRqstHdlr TRUE /* in request handler code*/
46 //Comment or un- the substitute half to turn on/off types of debug message
47 #define DEBUG( bool, msg) \
48 if( bool){ printf(msg); fflush(stdin);}
49 #define DEBUG1( bool, msg, param) \
50 if(bool){printf(msg, param); fflush(stdin);}
51 #define DEBUG2( bool, msg, p1, p2) \
52 if(bool) {printf(msg, p1, p2); fflush(stdin);}
54 #define ERROR(msg) printf(msg);
55 #define ERROR1(msg, param) printf(msg, param);
56 #define ERROR2(msg, p1, p2) printf(msg, p1, p2);
58 //=========================== STATS =======================
60 //when MEAS__TIME_STAMP_SUSP is defined, causes code to be inserted and
61 // compiled-in that saves the low part of the time stamp count just before
62 // suspending a processor and just after resuming that processor. It is
63 // saved into a field added to VirtProcr. Have to sanity-check for
64 // rollover of low portion into high portion.
65 //#define MEAS__TIME_STAMP_SUSP
66 //#define MEAS__TIME_MASTER
67 //#define MEAS__TIME_PLUGIN
68 //#define MEAS__TIME_MALLOC
69 //#define MEAS__TIME_MASTER_LOCK
70 //#define MEAS__NUM_TIMES_TO_RUN 100000
72 //For code that calculates normalization-offset between TSC counts of
73 // different cores.
74 //#define NUM_TSC_ROUND_TRIPS 10
76 #define MEAS__PERF_COUNTERS
78 //========================= Hardware related Constants =====================
79 //This value is the number of hardware threads in the shared memory
80 // machine
81 #define NUM_CORES 2
83 // tradeoff amortizing master fixed overhead vs imbalance potential
84 // when work-stealing, can make bigger, at risk of losing cache affinity
85 #define NUM_SCHED_SLOTS 5
87 #define MIN_WORK_UNIT_CYCLES 20000
89 #define MASTERLOCK_RETRIES 10000
91 // stack size in virtual processors created
92 #define VIRT_PROCR_STACK_SIZE 0x8000 /* 32K */
94 // memory for VMS__malloc
95 #define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
97 #define CACHE_LINE 64
98 #define PAGE_SIZE 4096
101 //==============================
103 #define SUCCESS 0
105 #define writeVMSQ writePrivQ
106 #define readVMSQ readPrivQ
107 #define makeVMSQ makeVMSPrivQ
108 #define numInVMSQ numInPrivQ
109 #define VMSQueueStruc PrivQueueStruc
113 //===========================================================================
114 typedef unsigned long long TSCount;
116 typedef struct _SchedSlot SchedSlot;
117 typedef struct _VMSReqst VMSReqst;
118 typedef struct _VirtProcr VirtProcr;
119 typedef struct _IntervalProbe IntervalProbe;
120 typedef struct _GateStruc GateStruc;
123 typedef VirtProcr * (*SlaveScheduler) ( void *, int ); //semEnv, coreIdx
124 typedef void (*RequestHandler) ( VirtProcr *, void * ); //prWReqst, semEnv
125 typedef void (*VirtProcrFnPtr) ( void *, VirtProcr * ); //initData, animPr
126 typedef void VirtProcrFn ( void *, VirtProcr * ); //initData, animPr
127 typedef void (*ResumePrFnPtr) ( VirtProcr *, void * );
130 //============= Requests ===========
131 //
133 enum VMSReqstType //avoid starting enums at 0, for debug reasons
134 {
135 semantic = 1,
136 createReq,
137 dissipate,
138 VMSSemantic //goes with VMSSemReqst below
139 };
141 struct _VMSReqst
142 {
143 enum VMSReqstType reqType;//used for dissipate and in future for IO requests
144 void *semReqData;
146 VMSReqst *nextReqst;
147 };
148 //VMSReqst
150 enum VMSSemReqstType //These are equivalent to semantic requests, but for
151 { // VMS's services available directly to app, like OS
152 createProbe = 1, // and probe services -- like a VMS-wide built-in lang
153 openFile,
154 otherIO
155 };
157 typedef struct
158 { enum VMSSemReqstType reqType;
159 VirtProcr *requestingPr;
160 char *nameStr; //for create probe
161 }
162 VMSSemReq;
165 //==================== Core data structures ===================
167 struct _SchedSlot
168 {
169 int workIsDone;
170 int needsProcrAssigned;
171 VirtProcr *procrAssignedToSlot;
172 };
173 //SchedSlot
175 /*WARNING: re-arranging this data structure could cause VP switching
176 * assembly code to fail -- hard-codes offsets of fields
177 */
178 struct _VirtProcr
179 { int procrID; //for debugging -- count up each time create
180 int coreAnimatedBy;
181 void *startOfStack;
182 void *stackPtr;
183 void *framePtr;
184 void *nextInstrPt;
186 void *coreLoopStartPt; //allows proto-runtime to be linked later
187 void *coreLoopFramePtr; //restore before jmp back to core loop
188 void *coreLoopStackPtr; //restore before jmp back to core loop
190 void *initialData;
192 SchedSlot *schedSlot;
193 VMSReqst *requests;
195 void *semanticData; //this livesUSE_GNU here for the life of VP
196 void *dataRetFromReq;//values returned from plugin to VP go here
198 //=========== MEASUREMENT STUFF ==========
199 #ifdef MEAS__TIME_STAMP_SUSP
200 unsigned int preSuspTSCLow;
201 unsigned int postSuspTSCLow;
202 #endif
203 #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
204 unsigned int startMasterTSCLow;USE_GNU
205 unsigned int endMasterTSCLow;
206 #endif
207 #ifdef MEAS__PERF_COUNTERS //
208 CounterRecord** counter_history;
209 PrivDynArrayInfo* counter_history_array_info;
210 #endif
211 //========================================
213 float64 createPtInSecs; //have space but don't use on some configs
214 };
215 //VirtProcr
218 /*WARNING: re-arranging this data structure could cause VP-switching
219 * assembly code to fail -- hard-codes offsets of fields
220 * (because -O3 messes with things otherwise)
221 */
222 typedef struct
223 {
224 SlaveScheduler slaveScheduler;
225 RequestHandler requestHandler;
227 SchedSlot ***allSchedSlots;
228 VMSQueueStruc **readyToAnimateQs;
229 VirtProcr **masterVPs;
231 void *semanticEnv;
232 void *OSEventStruc; //for future, when add I/O to BLIS
233 MallocProlog *freeListHead;
234 int32 amtOfOutstandingMem; //total currently allocated
236 void *coreLoopReturnPt;//addr to jump to to re-enter coreLoop
238 int32 setupComplete;
239 volatile int32 masterLock;
241 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
242 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
243 int32 workStealingLock;
245 int32 numProcrsCreated; //gives ordering to processor creation
247 //=========== MEASUREMENT STUFF =============
248 IntervalProbe **intervalProbes;
249 PrivDynArrayInfo *dynIntervalProbesInfo;
250 HashTable *probeNameHashTbl;
251 int32 masterCreateProbeID;
252 float64 createPtInSecs;
253 Histogram **measHists;
254 PrivDynArrayInfo *measHistsInfo;
255 #ifdef MEAS__TIME_PLUGIN
256 Histogram *reqHdlrLowTimeHist;
257 Histogram *reqHdlrHighTimeHist;
258 #endif
259 #ifdef MEAS__TIME_MALLOC
260 Histogram *mallocTimeHist;
261 Histogram *freeTimeHist;
262 #endif
263 #ifdef MEAS__TIME_MASTER_LOCK
264 Histogram *masterLockLowTimeHist;
265 Histogram *masterLockHighTimeHist;
266 #endif
267 #ifdef MEAS__PERF_COUNTERS
268 int cycles_counter_fd[NUM_CORES];
269 int instrs_counter_fd[NUM_CORES];
270 FILE* counteroutput;
271 #endif
272 }
273 MasterEnv;
275 //========================= Extra Stuff Data Strucs =======================
276 typedef struct
277 {
279 }
280 VMSExcp;
282 struct _GateStruc
283 {
284 int32 gateClosed;
285 int32 preGateProgress;
286 int32 waitProgress;
287 int32 exitProgress;
288 };
289 //GateStruc
291 //======================= OS Thread related ===============================
293 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
294 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
295 void masterLoop( void *initData, VirtProcr *masterPr );
298 typedef struct
299 {
300 void *endThdPt;
301 unsigned int coreNum;
302 }
303 ThdParams;
305 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
306 ThdParams *coreLoopThdParams [ NUM_CORES ];
307 pthread_mutex_t suspendLock;
308 pthread_cond_t suspend_cond;
312 //===================== Global Vars ===================
314 volatile MasterEnv *_VMSMasterEnv;
319 //=========================== Function Prototypes =========================
322 //========== Setup and shutdown ==========
323 void
324 VMS__init();
326 void
327 VMS__init_Seq();
329 void
330 VMS__start_the_work_then_wait_until_done();
332 void
333 VMS__start_the_work_then_wait_until_done_Seq();
335 inline VirtProcr *
336 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
338 void
339 VMS__dissipate_procr( VirtProcr *procrToDissipate );
341 //Use this to create processor inside entry point & other places outside
342 // the VMS system boundary (IE, not run in slave nor Master)
343 VirtProcr *
344 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
346 void
347 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
349 void
350 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
352 void
353 VMS__shutdown();
355 void
356 VMS__cleanup_at_end_of_shutdown();
358 void *
359 VMS__give_sem_env_for( VirtProcr *animPr );
362 //============== Request Related ===============
364 void
365 VMS__suspend_procr( VirtProcr *callingPr );
367 inline void
368 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
370 /*inline*/ __attribute__ ((noinline)) void
371 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
373 void
374 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
376 void /*inline**/ __attribute__ ((noinline))
377 VMS__send_dissipate_req( VirtProcr *prToDissipate );
379 /*inline**/ __attribute__ ((noinline)) void
380 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
382 VMSReqst *
383 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
385 inline void *
386 VMS__take_sem_reqst_from( VMSReqst *req );
388 void inline
389 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
390 ResumePrFnPtr resumePrFnPtr );
392 //======================== STATS ======================
394 //===== RDTSC wrapper ===== //Also runs with x86_64 code
396 #define saveTimeStampCountInto(low, high) \
397 asm volatile("RDTSC; \
398 movl %%eax, %0; \
399 movl %%edx, %1;" \
400 /* outputs */ : "=m" (low), "=m" (high)\
401 /* inputs */ : \
402 /* clobber */ : "%eax", "%edx" \
403 );
405 #define saveLowTimeStampCountInto(low) \
406 asm volatile("RDTSC; \
407 movl %%eax, %0;" \
408 /* outputs */ : "=m" (low) \
409 /* inputs */ : \
410 /* clobber */ : "%eax", "%edx" \
411 );
413 //====================
414 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
415 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
416 _VMSMasterEnv->measHists[idx] = \
417 makeFixedBinHist( numBins, startVal, binWidth, name );
419 #define saveCyclesAndInstrs(core,cycles,instrs) do{ \
420 int cycles_fd = _VMSMasterEnv->cycles_counter_fd[core]; \
421 int instrs_fd = _VMSMasterEnv->instrs_counter_fd[core]; \
422 int nread; \
423 \
424 nread = read(cycles_fd,&(cycles),sizeof(cycles)); \
425 if(nread<0){ \
426 perror("Error reading cycles counter"); \
427 cycles = 0; \
428 } \
429 \
430 nread = read(instrs_fd,&(instrs),sizeof(instrs)); \
431 if(nread<0){ \
432 perror("Error reading cycles counter"); \
433 instrs = 0; \
434 } \
435 } while (0)
437 #define getReturnAddressBeforeLibraryCall(vp_ptr, res_ptr) do{ \
438 void* frame_ptr0 = vp_ptr->framePtr; \
439 void* frame_ptr1 = *((void**)frame_ptr0); \
440 void* frame_ptr2 = *((void**)frame_ptr1); \
441 void* frame_ptr3 = *((void**)frame_ptr2); \
442 void* ret_addr = *((void**)frame_ptr3 + 1); \
443 *res_ptr = ret_addr; \
444 } while (0)
446 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
448 #ifdef VPTHREAD
450 //VPThread
451 #define createHistIdx 1
452 #define mutexLockHistIdx 2
453 #define mutexUnlockHistIdx 3
454 #define condWaitHistIdx 4
455 #define condSignalHistIdx 5
457 #define MakeTheMeasHists() \
458 _VMSMasterEnv->measHistsInfo = \
459 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
460 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
461 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
462 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
463 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
464 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
466 #endif
469 #ifdef VCILK
471 //VCilk
472 #define spawnHistIdx 1
473 #define syncHistIdx 2
475 #define MakeTheMeasHists() \
476 _VMSMasterEnv->measHistsInfo = \
477 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
478 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
479 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
482 #endif
484 #ifdef SSR
486 //SSR
487 #define SendFromToHistIdx 1
488 #define SendOfTypeHistIdx 2
489 #define ReceiveFromToHistIdx 3
490 #define ReceiveOfTypeHistIdx 4
492 #define MakeTheMeasHists() \
493 _VMSMasterEnv->measHistsInfo = \
494 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
495 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
496 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
497 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
498 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
500 #endif
502 //===========================================================================
503 //VPThread
506 #define Meas_startCreate \
507 int32 startStamp, endStamp; \
508 saveLowTimeStampCountInto( startStamp ); \
510 #define Meas_endCreate \
511 saveLowTimeStampCountInto( endStamp ); \
512 addIntervalToHist( startStamp, endStamp, \
513 _VMSMasterEnv->measHists[ createHistIdx ] );
515 #define Meas_startMutexLock \
516 int32 startStamp, endStamp; \
517 saveLowTimeStampCountInto( startStamp ); \
519 #define Meas_endMutexLock \
520 saveLowTimeStampCountInto( endStamp ); \
521 addIntervalToHist( startStamp, endStamp, \
522 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
524 #define Meas_startMutexUnlock \
525 int32 startStamp, endStamp; \
526 saveLowTimeStampCountInto( startStamp ); \
528 #define Meas_endMutexUnlock \
529 saveLowTimeStampCountInto( endStamp ); \
530 addIntervalToHist( startStamp, endStamp, \
531 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
533 #define Meas_startCondWait \
534 int32 startStamp, endStamp; \
535 saveLowTimeStampCountInto( startStamp ); \
537 #define Meas_endCondWait \
538 saveLowTimeStampCountInto( endStamp ); \
539 addIntervalToHist( startStamp, endStamp, \
540 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
542 #define Meas_startCondSignal \
543 int32 startStamp, endStamp; \
544 saveLowTimeStampCountInto( startStamp ); \
546 #define Meas_endCondSignal \
547 saveLowTimeStampCountInto( endStamp ); \
548 addIntervalToHist( startStamp, endStamp, \
549 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
551 //===========================================================================
552 // VCilk
553 #define Meas_startSpawn \
554 int32 startStamp, endStamp; \
555 saveLowTimeStampCountInto( startStamp ); \
557 #define Meas_endSpawn \
558 saveLowTimeStampCountInto( endStamp ); \
559 addIntervalToHist( startStamp, endStamp, \
560 _VMSMasterEnv->measHists[ spawnHistIdx ] );
562 #define Meas_startSync \
563 int32 startStamp, endStamp; \
564 saveLowTimeStampCountInto( startStamp ); \
566 #define Meas_endSync \
567 saveLowTimeStampCountInto( endStamp ); \
568 addIntervalToHist( startStamp, endStamp, \
569 _VMSMasterEnv->measHists[ syncHistIdx ] );
571 //===========================================================================
572 // SSR
573 #define Meas_startSendFromTo \
574 int32 startStamp, endStamp; \
575 saveLowTimeStampCountInto( startStamp ); \
577 #define Meas_endSendFromTo \
578 saveLowTimeStampCountInto( endStamp ); \
579 addIntervalToHist( startStamp, endStamp, \
580 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
582 #define Meas_startSendOfType \
583 int32 startStamp, endStamp; \
584 saveLowTimeStampCountInto( startStamp ); \
586 #define Meas_endSendOfType \
587 saveLowTimeStampCountInto( endStamp ); \
588 addIntervalToHist( startStamp, endStamp, \
589 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
591 #define Meas_startReceiveFromTo \
592 int32 startStamp, endStamp; \
593 saveLowTimeStampCountInto( startStamp ); \
595 #define Meas_endReceiveFromTo \
596 saveLowTimeStampCountInto( endStamp ); \
597 addIntervalToHist( startStamp, endStamp, \
598 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
600 #define Meas_startReceiveOfType \
601 int32 startStamp, endStamp; \
602 saveLowTimeStampCountInto( startStamp ); \
604 #define Meas_endReceiveOfType \
605 saveLowTimeStampCountInto( endStamp ); \
606 addIntervalToHist( startStamp, endStamp, \
607 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
609 //=====
611 #include "ProcrContext.h"
612 #include "probes.h"
613 #include "vutilities.h"
615 #endif /* _VMS_H */
