Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.h @ 182:7523ee70d66c
merge changes from default
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Fri, 06 Jan 2012 18:55:05 +0100 |
| parents | 3bd35fc83c61 c1784868dcea |
| children | 50b29548d4f0 |
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"
21 #include "ListOfArrays/ListOfArrays.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 OBSERVE_UCC
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, 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 int numTimesScheduled; //defines units together w/ procrID
220 };
221 //VirtProcr
224 /*WARNING: re-arranging this data structure could cause VP-switching
225 * assembly code to fail -- hard-codes offsets of fields
226 * (because -O3 messes with things otherwise)
227 */
228 typedef struct
229 {
230 SlaveScheduler slaveScheduler;
231 RequestHandler requestHandler;
233 SchedSlot ***allSchedSlots;
234 VMSQueueStruc **readyToAnimateQs;
235 VirtProcr **masterVPs;
237 void *semanticEnv;
238 void *OSEventStruc; //for future, when add I/O to BLIS
239 MallocProlog *freeListHead;
240 int32 amtOfOutstandingMem; //total currently allocated
242 void *coreLoopReturnPt;//addr to jump to to re-enter coreLoop
244 int32 setupComplete;
245 volatile int32 masterLock;
247 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
248 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
249 int32 workStealingLock;
251 int32 numProcrsCreated; //gives ordering to processor creation
253 //=========== MEASUREMENT STUFF =============
254 IntervalProbe **intervalProbes;
255 PrivDynArrayInfo *dynIntervalProbesInfo;
256 HashTable *probeNameHashTbl;
257 int32 masterCreateProbeID;
258 float64 createPtInSecs;
259 Histogram **measHists;
260 PrivDynArrayInfo *measHistsInfo;
261 #ifdef MEAS__TIME_PLUGIN
262 Histogram *reqHdlrLowTimeHist;
263 Histogram *reqHdlrHighTimeHist;
264 #endif
265 #ifdef MEAS__TIME_MALLOC
266 Histogram *mallocTimeHist;
267 Histogram *freeTimeHist;
268 #endif
269 #ifdef MEAS__TIME_MASTER_LOCK
270 Histogram *masterLockLowTimeHist;
271 Histogram *masterLockHighTimeHist;
272 #endif
273 #ifdef MEAS__PERF_COUNTERS
274 int cycles_counter_fd[NUM_CORES];
275 int instrs_counter_fd[NUM_CORES];
276 FILE* counteroutput;
277 #endif
278 #ifdef MEAS__PERF_COUNTERS //
279 CounterRecord** counter_history;
280 PrivDynArrayInfo* counter_history_array_info;
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 #define getReturnAddressBeforeLibraryCall(vp_ptr, res_ptr) do{ \
448 void* frame_ptr0 = vp_ptr->framePtr; \
449 void* frame_ptr1 = *((void**)frame_ptr0); \
450 void* frame_ptr2 = *((void**)frame_ptr1); \
451 void* frame_ptr3 = *((void**)frame_ptr2); \
452 void* ret_addr = *((void**)frame_ptr3 + 1); \
453 *res_ptr = ret_addr; \
454 } while (0)
456 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
458 #ifdef VPTHREAD
460 //VPThread
461 #define createHistIdx 0
462 #define mutexLockHistIdx 1
463 #define mutexUnlockHistIdx 2
464 #define condWaitHistIdx 3
465 #define condSignalHistIdx 4
467 #define MakeTheMeasHists() \
468 _VMSMasterEnv->measHistsInfo = \
469 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
470 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
471 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
472 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
473 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
474 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
476 #endif
479 #ifdef VCILK
481 //VCilk
482 #define spawnHistIdx 0
483 #define syncHistIdx 1
485 #define MakeTheMeasHists() \
486 _VMSMasterEnv->measHistsInfo = \
487 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
488 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
489 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
492 #endif
494 #ifdef SSR
496 //SSR
497 #define SendFromToHistIdx 0
498 #define SendOfTypeHistIdx 1
499 #define ReceiveFromToHistIdx 2
500 #define ReceiveOfTypeHistIdx 3
502 #define MakeTheMeasHists() \
503 _VMSMasterEnv->measHistsInfo = \
504 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
505 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
506 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
507 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
508 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
510 #endif
512 //===========================================================================
513 //VPThread
516 #define Meas_startCreate \
517 int32 startStamp, endStamp; \
518 saveLowTimeStampCountInto( startStamp ); \
520 #define Meas_endCreate \
521 saveLowTimeStampCountInto( endStamp ); \
522 addIntervalToHist( startStamp, endStamp, \
523 _VMSMasterEnv->measHists[ createHistIdx ] );
525 #define Meas_startMutexLock \
526 int32 startStamp, endStamp; \
527 saveLowTimeStampCountInto( startStamp ); \
529 #define Meas_endMutexLock \
530 saveLowTimeStampCountInto( endStamp ); \
531 addIntervalToHist( startStamp, endStamp, \
532 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
534 #define Meas_startMutexUnlock \
535 int32 startStamp, endStamp; \
536 saveLowTimeStampCountInto( startStamp ); \
538 #define Meas_endMutexUnlock \
539 saveLowTimeStampCountInto( endStamp ); \
540 addIntervalToHist( startStamp, endStamp, \
541 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
543 #define Meas_startCondWait \
544 int32 startStamp, endStamp; \
545 saveLowTimeStampCountInto( startStamp ); \
547 #define Meas_endCondWait \
548 saveLowTimeStampCountInto( endStamp ); \
549 addIntervalToHist( startStamp, endStamp, \
550 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
552 #define Meas_startCondSignal \
553 int32 startStamp, endStamp; \
554 saveLowTimeStampCountInto( startStamp ); \
556 #define Meas_endCondSignal \
557 saveLowTimeStampCountInto( endStamp ); \
558 addIntervalToHist( startStamp, endStamp, \
559 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
561 //===========================================================================
562 // VCilk
563 #define Meas_startSpawn \
564 int32 startStamp, endStamp; \
565 saveLowTimeStampCountInto( startStamp ); \
567 #define Meas_endSpawn \
568 saveLowTimeStampCountInto( endStamp ); \
569 addIntervalToHist( startStamp, endStamp, \
570 _VMSMasterEnv->measHists[ spawnHistIdx ] );
572 #define Meas_startSync \
573 int32 startStamp, endStamp; \
574 saveLowTimeStampCountInto( startStamp ); \
576 #define Meas_endSync \
577 saveLowTimeStampCountInto( endStamp ); \
578 addIntervalToHist( startStamp, endStamp, \
579 _VMSMasterEnv->measHists[ syncHistIdx ] );
581 //===========================================================================
582 // SSR
583 #define Meas_startSendFromTo \
584 int32 startStamp, endStamp; \
585 saveLowTimeStampCountInto( startStamp ); \
587 #define Meas_endSendFromTo \
588 saveLowTimeStampCountInto( endStamp ); \
589 addIntervalToHist( startStamp, endStamp, \
590 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
592 #define Meas_startSendOfType \
593 int32 startStamp, endStamp; \
594 saveLowTimeStampCountInto( startStamp ); \
596 #define Meas_endSendOfType \
597 saveLowTimeStampCountInto( endStamp ); \
598 addIntervalToHist( startStamp, endStamp, \
599 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
601 #define Meas_startReceiveFromTo \
602 int32 startStamp, endStamp; \
603 saveLowTimeStampCountInto( startStamp ); \
605 #define Meas_endReceiveFromTo \
606 saveLowTimeStampCountInto( endStamp ); \
607 addIntervalToHist( startStamp, endStamp, \
608 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
610 #define Meas_startReceiveOfType \
611 int32 startStamp, endStamp; \
612 saveLowTimeStampCountInto( startStamp ); \
614 #define Meas_endReceiveOfType \
615 saveLowTimeStampCountInto( endStamp ); \
616 addIntervalToHist( startStamp, endStamp, \
617 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
619 //=====
621 #include "ProcrContext.h"
622 #include "probes.h"
623 #include "vutilities.h"
625 #endif /* _VMS_H */
