Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.h @ 185:28cc465f7eb7
new counter interface works now
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Fri, 13 Jan 2012 18:34:31 +0100 |
| parents | 50b29548d4f0 |
| children | 69eb54ce9c4b |
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 "loop.h"
20 #include "ListOfArrays/ListOfArrays.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, 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 * );
132 typedef void (*CounterHandler) (int,VirtProcr*,uint64,uint64);
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 //CounterRecord** counter_history;
277 //PrivDynArrayInfo* counter_history_array_info;
278 CounterHandler counterHandler;
279 #endif
280 }
281 MasterEnv;
283 //========================= Extra Stuff Data Strucs =======================
284 typedef struct
285 {
287 }
288 VMSExcp;
290 struct _GateStruc
291 {
292 int32 gateClosed;
293 int32 preGateProgress;
294 int32 waitProgress;
295 int32 exitProgress;
296 };
297 //GateStruc
299 //======================= OS Thread related ===============================
301 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
302 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
303 void masterLoop( void *initData, VirtProcr *masterPr );
306 typedef struct
307 {
308 void *endThdPt;
309 unsigned int coreNum;
310 }
311 ThdParams;
313 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
314 ThdParams *coreLoopThdParams [ NUM_CORES ];
315 pthread_mutex_t suspendLock;
316 pthread_cond_t suspend_cond;
320 //===================== Global Vars ===================
322 volatile MasterEnv *_VMSMasterEnv;
327 //=========================== Function Prototypes =========================
330 //========== Setup and shutdown ==========
331 void
332 VMS__init();
334 void
335 VMS__init_Seq();
337 void
338 VMS__start_the_work_then_wait_until_done();
340 void
341 VMS__start_the_work_then_wait_until_done_Seq();
343 inline VirtProcr *
344 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
346 void
347 VMS__dissipate_procr( VirtProcr *procrToDissipate );
349 //Use this to create processor inside entry point & other places outside
350 // the VMS system boundary (IE, not run in slave nor Master)
351 VirtProcr *
352 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
354 void
355 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
357 void
358 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
360 void
361 VMS__shutdown();
363 void
364 VMS__cleanup_at_end_of_shutdown();
366 void *
367 VMS__give_sem_env_for( VirtProcr *animPr );
370 //============== Request Related ===============
372 void
373 VMS__suspend_procr( VirtProcr *callingPr );
375 inline void
376 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
378 inline void
379 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
381 void
382 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
384 void inline
385 VMS__send_dissipate_req( VirtProcr *prToDissipate );
387 inline void
388 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
390 VMSReqst *
391 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
393 inline void *
394 VMS__take_sem_reqst_from( VMSReqst *req );
396 void inline
397 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
398 ResumePrFnPtr resumePrFnPtr );
400 //======================== STATS ======================
402 //===== RDTSC wrapper ===== //Also runs with x86_64 code
404 #define saveTimeStampCountInto(low, high) \
405 asm volatile("RDTSC; \
406 movl %%eax, %0; \
407 movl %%edx, %1;" \
408 /* outputs */ : "=m" (low), "=m" (high)\
409 /* inputs */ : \
410 /* clobber */ : "%eax", "%edx" \
411 );
413 #define saveLowTimeStampCountInto(low) \
414 asm volatile("RDTSC; \
415 movl %%eax, %0;" \
416 /* outputs */ : "=m" (low) \
417 /* inputs */ : \
418 /* clobber */ : "%eax", "%edx" \
419 );
421 //====================
422 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
423 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
424 _VMSMasterEnv->measHists[idx] = \
425 makeFixedBinHist( numBins, startVal, binWidth, name );
427 #define saveCyclesAndInstrs(core,cycles,instrs) do{ \
428 int cycles_fd = _VMSMasterEnv->cycles_counter_fd[core]; \
429 int instrs_fd = _VMSMasterEnv->instrs_counter_fd[core]; \
430 int nread; \
431 \
432 nread = read(cycles_fd,&(cycles),sizeof(cycles)); \
433 if(nread<0){ \
434 perror("Error reading cycles counter"); \
435 cycles = 0; \
436 } \
437 \
438 nread = read(instrs_fd,&(instrs),sizeof(instrs)); \
439 if(nread<0){ \
440 perror("Error reading cycles counter"); \
441 instrs = 0; \
442 } \
443 } while (0)
445 enum eventType {
446 MasterLoop_beforeReqHdlr = 1,
447 MasterLoop_afterReqHdlr,
448 MasterLoop_beforeAssign,
449 MasterLoop_afterAssign,
450 CoreLoop_afterWork,
451 CoreLoop_beforeWork
452 };
454 #define getReturnAddressBeforeLibraryCall(vp_ptr, res_ptr) do{ \
455 void* frame_ptr0 = vp_ptr->framePtr; \
456 void* frame_ptr1 = *((void**)frame_ptr0); \
457 void* frame_ptr2 = *((void**)frame_ptr1); \
458 void* frame_ptr3 = *((void**)frame_ptr2); \
459 void* ret_addr = *((void**)frame_ptr3 + 1); \
460 *res_ptr = ret_addr; \
461 } while (0)
463 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
465 #ifdef VPTHREAD
467 //VPThread
468 #define createHistIdx 0
469 #define mutexLockHistIdx 1
470 #define mutexUnlockHistIdx 2
471 #define condWaitHistIdx 3
472 #define condSignalHistIdx 4
474 #define MakeTheMeasHists() \
475 _VMSMasterEnv->measHistsInfo = \
476 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
477 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
478 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
479 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
480 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
481 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
483 #endif
486 #ifdef VCILK
488 //VCilk
489 #define spawnHistIdx 0
490 #define syncHistIdx 1
492 #define MakeTheMeasHists() \
493 _VMSMasterEnv->measHistsInfo = \
494 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
495 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
496 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
499 #endif
501 #ifdef SSR
503 //SSR
504 #define SendFromToHistIdx 0
505 #define SendOfTypeHistIdx 1
506 #define ReceiveFromToHistIdx 2
507 #define ReceiveOfTypeHistIdx 3
509 #define MakeTheMeasHists() \
510 _VMSMasterEnv->measHistsInfo = \
511 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
512 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
513 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
514 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
515 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
517 #endif
519 //===========================================================================
520 //VPThread
523 #define Meas_startCreate \
524 int32 startStamp, endStamp; \
525 saveLowTimeStampCountInto( startStamp ); \
527 #define Meas_endCreate \
528 saveLowTimeStampCountInto( endStamp ); \
529 addIntervalToHist( startStamp, endStamp, \
530 _VMSMasterEnv->measHists[ createHistIdx ] );
532 #define Meas_startMutexLock \
533 int32 startStamp, endStamp; \
534 saveLowTimeStampCountInto( startStamp ); \
536 #define Meas_endMutexLock \
537 saveLowTimeStampCountInto( endStamp ); \
538 addIntervalToHist( startStamp, endStamp, \
539 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
541 #define Meas_startMutexUnlock \
542 int32 startStamp, endStamp; \
543 saveLowTimeStampCountInto( startStamp ); \
545 #define Meas_endMutexUnlock \
546 saveLowTimeStampCountInto( endStamp ); \
547 addIntervalToHist( startStamp, endStamp, \
548 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
550 #define Meas_startCondWait \
551 int32 startStamp, endStamp; \
552 saveLowTimeStampCountInto( startStamp ); \
554 #define Meas_endCondWait \
555 saveLowTimeStampCountInto( endStamp ); \
556 addIntervalToHist( startStamp, endStamp, \
557 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
559 #define Meas_startCondSignal \
560 int32 startStamp, endStamp; \
561 saveLowTimeStampCountInto( startStamp ); \
563 #define Meas_endCondSignal \
564 saveLowTimeStampCountInto( endStamp ); \
565 addIntervalToHist( startStamp, endStamp, \
566 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
568 //===========================================================================
569 // VCilk
570 #define Meas_startSpawn \
571 int32 startStamp, endStamp; \
572 saveLowTimeStampCountInto( startStamp ); \
574 #define Meas_endSpawn \
575 saveLowTimeStampCountInto( endStamp ); \
576 addIntervalToHist( startStamp, endStamp, \
577 _VMSMasterEnv->measHists[ spawnHistIdx ] );
579 #define Meas_startSync \
580 int32 startStamp, endStamp; \
581 saveLowTimeStampCountInto( startStamp ); \
583 #define Meas_endSync \
584 saveLowTimeStampCountInto( endStamp ); \
585 addIntervalToHist( startStamp, endStamp, \
586 _VMSMasterEnv->measHists[ syncHistIdx ] );
588 //===========================================================================
589 // SSR
590 #define Meas_startSendFromTo \
591 int32 startStamp, endStamp; \
592 saveLowTimeStampCountInto( startStamp ); \
594 #define Meas_endSendFromTo \
595 saveLowTimeStampCountInto( endStamp ); \
596 addIntervalToHist( startStamp, endStamp, \
597 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
599 #define Meas_startSendOfType \
600 int32 startStamp, endStamp; \
601 saveLowTimeStampCountInto( startStamp ); \
603 #define Meas_endSendOfType \
604 saveLowTimeStampCountInto( endStamp ); \
605 addIntervalToHist( startStamp, endStamp, \
606 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
608 #define Meas_startReceiveFromTo \
609 int32 startStamp, endStamp; \
610 saveLowTimeStampCountInto( startStamp ); \
612 #define Meas_endReceiveFromTo \
613 saveLowTimeStampCountInto( endStamp ); \
614 addIntervalToHist( startStamp, endStamp, \
615 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
617 #define Meas_startReceiveOfType \
618 int32 startStamp, endStamp; \
619 saveLowTimeStampCountInto( startStamp ); \
621 #define Meas_endReceiveOfType \
622 saveLowTimeStampCountInto( endStamp ); \
623 addIntervalToHist( startStamp, endStamp, \
624 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
626 //=====
628 #include "ProcrContext.h"
629 #include "probes.h"
630 #include "vutilities.h"
632 #endif /* _VMS_H */
