Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.h @ 213:c5c136fc3131
Instrumentation for Holistic Model stats
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Fri, 09 Mar 2012 19:28:50 +0100 |
| parents | df00af7eb307 |
| children |
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 "../../C_Libraries/Queue_impl/PrivateQueue.h"
15 #include "../../C_Libraries/Histogram/Histogram.h"
16 #include "../../C_Libraries/DynArray/DynArray.h"
17 #include "../../C_Libraries/Hash_impl/PrivateHash.h"
18 #include "vmalloc.h"
19 #include "loop.h"
20 #include "../../C_Libraries/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 4
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 1
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 256
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,int,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 int isMasterVP;
216 int isShutdownVP;
217 //========================================
219 float64 createPtInSecs; //have space but don't use on some configs
220 int numTimesScheduled; //defines units together w/ procrID
221 };
222 //VirtProcr
225 /*WARNING: re-arranging this data structure could cause VP-switching
226 * assembly code to fail -- hard-codes offsets of fields
227 * (because -O3 messes with things otherwise)
228 */
229 typedef struct
230 {
231 SlaveScheduler slaveScheduler;
232 RequestHandler requestHandler;
234 SchedSlot ***allSchedSlots;
235 VMSQueueStruc **readyToAnimateQs;
236 VirtProcr **masterVPs;
238 void *semanticEnv;
239 void *OSEventStruc; //for future, when add I/O to BLIS
240 MallocProlog *freeListHead;
241 int32 amtOfOutstandingMem; //total currently allocated
243 void *coreLoopReturnPt;//addr to jump to to re-enter coreLoop
245 int32 setupComplete;
246 volatile int32 masterLock;
248 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
249 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
250 int32 workStealingLock;
252 int32 numProcrsCreated; //gives ordering to processor creation
254 //=========== MEASUREMENT STUFF =============
255 IntervalProbe **intervalProbes;
256 PrivDynArrayInfo *dynIntervalProbesInfo;
257 HashTable *probeNameHashTbl;
258 int32 masterCreateProbeID;
259 float64 createPtInSecs;
260 Histogram **measHists;
261 PrivDynArrayInfo *measHistsInfo;
262 #ifdef MEAS__TIME_PLUGIN
263 Histogram *reqHdlrLowTimeHist;
264 Histogram *reqHdlrHighTimeHist;
265 #endif
266 #ifdef MEAS__TIME_MALLOC
267 Histogram *mallocTimeHist;
268 Histogram *freeTimeHist;
269 #endif
270 #ifdef MEAS__TIME_MASTER_LOCK
271 Histogram *masterLockLowTimeHist;
272 Histogram *masterLockHighTimeHist;
273 #endif
274 #ifdef MEAS__PERF_COUNTERS
275 int cycles_counter_fd[NUM_CORES];
276 int instrs_counter_fd[NUM_CORES];
277 uint64 start_master_lock[NUM_CORES][2];
278 //FILE* counteroutput;
279 //CounterRecord** counter_history;
280 //PrivDynArrayInfo* counter_history_array_info;
281 CounterHandler counterHandler;
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 volatile int sent_ctr;
314 volatile uint64 ret_tsc;
315 }
316 ThdParams;
318 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
319 ThdParams *coreLoopThdParams [ NUM_CORES ];
320 pthread_mutex_t suspendLock;
321 pthread_cond_t suspend_cond;
323 uint64 tsc_offset_send(ThdParams* thisCoresThdParams,uint64 initval);
324 int tsc_offset_resp(ThdParams* sendCoresThdParams,int initctr);
326 //===================== Global Vars ===================
328 volatile MasterEnv *_VMSMasterEnv;
333 //=========================== Function Prototypes =========================
336 //========== Setup and shutdown ==========
337 void
338 VMS__init();
340 void
341 VMS__init_Seq();
343 void
344 VMS__start_the_work_then_wait_until_done();
346 void
347 VMS__start_the_work_then_wait_until_done_Seq();
349 inline VirtProcr *
350 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
352 void
353 VMS__dissipate_procr( VirtProcr *procrToDissipate );
355 //Use this to create processor inside entry point & other places outside
356 // the VMS system boundary (IE, not run in slave nor Master)
357 VirtProcr *
358 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
360 void
361 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
363 void
364 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
366 void
367 VMS__shutdown();
369 void
370 VMS__cleanup_at_end_of_shutdown();
372 void *
373 VMS__give_sem_env_for( VirtProcr *animPr );
376 //============== Request Related ===============
378 void
379 VMS__suspend_procr( VirtProcr *callingPr );
381 inline void
382 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
384 inline void
385 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
387 void
388 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
390 void inline
391 VMS__send_dissipate_req( VirtProcr *prToDissipate );
393 inline void
394 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
396 VMSReqst *
397 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
399 inline void *
400 VMS__take_sem_reqst_from( VMSReqst *req );
402 void inline
403 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
404 ResumePrFnPtr resumePrFnPtr );
406 //======================== STATS ======================
408 //===== RDTSC wrapper ===== //Also runs with x86_64 code
410 #define saveTimeStampCountInto(low, high) \
411 asm volatile("RDTSC; \
412 movl %%eax, %0; \
413 movl %%edx, %1;" \
414 /* outputs */ : "=m" (low), "=m" (high)\
415 /* inputs */ : \
416 /* clobber */ : "%eax", "%edx" \
417 );
419 #define saveLowTimeStampCountInto(low) \
420 asm volatile("RDTSC; \
421 movl %%eax, %0;" \
422 /* outputs */ : "=m" (low) \
423 /* inputs */ : \
424 /* clobber */ : "%eax", "%edx" \
425 );
427 __inline__ uint64_t rdtsc(void);
429 //====================
430 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
431 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
432 _VMSMasterEnv->measHists[idx] = \
433 makeFixedBinHist( numBins, startVal, binWidth, name );
435 #define saveCyclesAndInstrs(core,cycles,instrs) do{ \
436 int cycles_fd = _VMSMasterEnv->cycles_counter_fd[core]; \
437 int instrs_fd = _VMSMasterEnv->instrs_counter_fd[core]; \
438 int nread; \
439 \
440 nread = read(cycles_fd,&(cycles),sizeof(cycles)); \
441 if(nread<0){ \
442 perror("Error reading cycles counter"); \
443 cycles = 0; \
444 } \
445 \
446 nread = read(instrs_fd,&(instrs),sizeof(instrs)); \
447 if(nread<0){ \
448 perror("Error reading cycles counter"); \
449 instrs = 0; \
450 } \
451 } while (0)
453 enum eventType {
454 DebugEvt = 0,
455 AppResponderInvocation_start,
456 AppResponder_start,
457 AppResponder_end,
458 AssignerInvocation_start,
459 NextAssigner_start,
460 Assigner_start,
461 Assigner_end,
462 Work_start,
463 Work_end,
464 HwResponderInvocation_start,
465 Timestamp_start,
466 Timestamp_end
467 };
469 #define getReturnAddressBeforeLibraryCall(vp_ptr, res_ptr) do{ \
470 void* frame_ptr0 = vp_ptr->framePtr; \
471 void* frame_ptr1 = *((void**)frame_ptr0); \
472 void* frame_ptr2 = *((void**)frame_ptr1); \
473 void* frame_ptr3 = *((void**)frame_ptr2); \
474 void* ret_addr = *((void**)frame_ptr3 + 1); \
475 *res_ptr = ret_addr; \
476 } while (0)
478 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
480 #ifdef VPTHREAD
482 //VPThread
483 #define createHistIdx 0
484 #define mutexLockHistIdx 1
485 #define mutexUnlockHistIdx 2
486 #define condWaitHistIdx 3
487 #define condSignalHistIdx 4
489 #define MakeTheMeasHists() \
490 _VMSMasterEnv->measHistsInfo = \
491 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
492 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
493 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
494 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
495 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
496 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
498 #endif
501 #ifdef VCILK
503 //VCilk
504 #define spawnHistIdx 0
505 #define syncHistIdx 1
507 #define MakeTheMeasHists() \
508 _VMSMasterEnv->measHistsInfo = \
509 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
510 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
511 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
514 #endif
516 #ifdef SSR
518 //SSR
519 #define SendFromToHistIdx 0
520 #define SendOfTypeHistIdx 1
521 #define ReceiveFromToHistIdx 2
522 #define ReceiveOfTypeHistIdx 3
524 #define MakeTheMeasHists() \
525 _VMSMasterEnv->measHistsInfo = \
526 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
527 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
528 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
529 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
530 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
532 #endif
534 //===========================================================================
535 //VPThread
538 #define Meas_startCreate \
539 int32 startStamp, endStamp; \
540 saveLowTimeStampCountInto( startStamp ); \
542 #define Meas_endCreate \
543 saveLowTimeStampCountInto( endStamp ); \
544 addIntervalToHist( startStamp, endStamp, \
545 _VMSMasterEnv->measHists[ createHistIdx ] );
547 #define Meas_startMutexLock \
548 int32 startStamp, endStamp; \
549 saveLowTimeStampCountInto( startStamp ); \
551 #define Meas_endMutexLock \
552 saveLowTimeStampCountInto( endStamp ); \
553 addIntervalToHist( startStamp, endStamp, \
554 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
556 #define Meas_startMutexUnlock \
557 int32 startStamp, endStamp; \
558 saveLowTimeStampCountInto( startStamp ); \
560 #define Meas_endMutexUnlock \
561 saveLowTimeStampCountInto( endStamp ); \
562 addIntervalToHist( startStamp, endStamp, \
563 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
565 #define Meas_startCondWait \
566 int32 startStamp, endStamp; \
567 saveLowTimeStampCountInto( startStamp ); \
569 #define Meas_endCondWait \
570 saveLowTimeStampCountInto( endStamp ); \
571 addIntervalToHist( startStamp, endStamp, \
572 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
574 #define Meas_startCondSignal \
575 int32 startStamp, endStamp; \
576 saveLowTimeStampCountInto( startStamp ); \
578 #define Meas_endCondSignal \
579 saveLowTimeStampCountInto( endStamp ); \
580 addIntervalToHist( startStamp, endStamp, \
581 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
583 //===========================================================================
584 // VCilk
585 #define Meas_startSpawn \
586 int32 startStamp, endStamp; \
587 saveLowTimeStampCountInto( startStamp ); \
589 #define Meas_endSpawn \
590 saveLowTimeStampCountInto( endStamp ); \
591 addIntervalToHist( startStamp, endStamp, \
592 _VMSMasterEnv->measHists[ spawnHistIdx ] );
594 #define Meas_startSync \
595 int32 startStamp, endStamp; \
596 saveLowTimeStampCountInto( startStamp ); \
598 #define Meas_endSync \
599 saveLowTimeStampCountInto( endStamp ); \
600 addIntervalToHist( startStamp, endStamp, \
601 _VMSMasterEnv->measHists[ syncHistIdx ] );
603 //===========================================================================
604 // SSR
605 #define Meas_startSendFromTo \
606 int32 startStamp, endStamp; \
607 saveLowTimeStampCountInto( startStamp ); \
609 #define Meas_endSendFromTo \
610 saveLowTimeStampCountInto( endStamp ); \
611 addIntervalToHist( startStamp, endStamp, \
612 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
614 #define Meas_startSendOfType \
615 int32 startStamp, endStamp; \
616 saveLowTimeStampCountInto( startStamp ); \
618 #define Meas_endSendOfType \
619 saveLowTimeStampCountInto( endStamp ); \
620 addIntervalToHist( startStamp, endStamp, \
621 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
623 #define Meas_startReceiveFromTo \
624 int32 startStamp, endStamp; \
625 saveLowTimeStampCountInto( startStamp ); \
627 #define Meas_endReceiveFromTo \
628 saveLowTimeStampCountInto( endStamp ); \
629 addIntervalToHist( startStamp, endStamp, \
630 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
632 #define Meas_startReceiveOfType \
633 int32 startStamp, endStamp; \
634 saveLowTimeStampCountInto( startStamp ); \
636 #define Meas_endReceiveOfType \
637 saveLowTimeStampCountInto( endStamp ); \
638 addIntervalToHist( startStamp, endStamp, \
639 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
641 //=====
643 #include "ProcrContext.h"
644 #include "probes.h"
645 #include "vutilities.h"
647 #endif /* _VMS_H */
