view VMS.h @ 187:fe5ad5726e36

counters working ...sort of
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Fri, 03 Feb 2012 17:32:48 +0100
parents 69eb54ce9c4b
children 20358f56e498
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 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 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 Procr_suspend,
453 MasterLoop_beforeNextAssign
454 };
456 #define getReturnAddressBeforeLibraryCall(vp_ptr, res_ptr) do{ \
457 void* frame_ptr0 = vp_ptr->framePtr; \
458 void* frame_ptr1 = *((void**)frame_ptr0); \
459 void* frame_ptr2 = *((void**)frame_ptr1); \
460 void* frame_ptr3 = *((void**)frame_ptr2); \
461 void* ret_addr = *((void**)frame_ptr3 + 1); \
462 *res_ptr = ret_addr; \
463 } while (0)
465 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
467 #ifdef VPTHREAD
469 //VPThread
470 #define createHistIdx 0
471 #define mutexLockHistIdx 1
472 #define mutexUnlockHistIdx 2
473 #define condWaitHistIdx 3
474 #define condSignalHistIdx 4
476 #define MakeTheMeasHists() \
477 _VMSMasterEnv->measHistsInfo = \
478 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
479 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
480 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
481 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
482 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
483 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
485 #endif
488 #ifdef VCILK
490 //VCilk
491 #define spawnHistIdx 0
492 #define syncHistIdx 1
494 #define MakeTheMeasHists() \
495 _VMSMasterEnv->measHistsInfo = \
496 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
497 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
498 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
501 #endif
503 #ifdef SSR
505 //SSR
506 #define SendFromToHistIdx 0
507 #define SendOfTypeHistIdx 1
508 #define ReceiveFromToHistIdx 2
509 #define ReceiveOfTypeHistIdx 3
511 #define MakeTheMeasHists() \
512 _VMSMasterEnv->measHistsInfo = \
513 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
514 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
515 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
516 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
517 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
519 #endif
521 //===========================================================================
522 //VPThread
525 #define Meas_startCreate \
526 int32 startStamp, endStamp; \
527 saveLowTimeStampCountInto( startStamp ); \
529 #define Meas_endCreate \
530 saveLowTimeStampCountInto( endStamp ); \
531 addIntervalToHist( startStamp, endStamp, \
532 _VMSMasterEnv->measHists[ createHistIdx ] );
534 #define Meas_startMutexLock \
535 int32 startStamp, endStamp; \
536 saveLowTimeStampCountInto( startStamp ); \
538 #define Meas_endMutexLock \
539 saveLowTimeStampCountInto( endStamp ); \
540 addIntervalToHist( startStamp, endStamp, \
541 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
543 #define Meas_startMutexUnlock \
544 int32 startStamp, endStamp; \
545 saveLowTimeStampCountInto( startStamp ); \
547 #define Meas_endMutexUnlock \
548 saveLowTimeStampCountInto( endStamp ); \
549 addIntervalToHist( startStamp, endStamp, \
550 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
552 #define Meas_startCondWait \
553 int32 startStamp, endStamp; \
554 saveLowTimeStampCountInto( startStamp ); \
556 #define Meas_endCondWait \
557 saveLowTimeStampCountInto( endStamp ); \
558 addIntervalToHist( startStamp, endStamp, \
559 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
561 #define Meas_startCondSignal \
562 int32 startStamp, endStamp; \
563 saveLowTimeStampCountInto( startStamp ); \
565 #define Meas_endCondSignal \
566 saveLowTimeStampCountInto( endStamp ); \
567 addIntervalToHist( startStamp, endStamp, \
568 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
570 //===========================================================================
571 // VCilk
572 #define Meas_startSpawn \
573 int32 startStamp, endStamp; \
574 saveLowTimeStampCountInto( startStamp ); \
576 #define Meas_endSpawn \
577 saveLowTimeStampCountInto( endStamp ); \
578 addIntervalToHist( startStamp, endStamp, \
579 _VMSMasterEnv->measHists[ spawnHistIdx ] );
581 #define Meas_startSync \
582 int32 startStamp, endStamp; \
583 saveLowTimeStampCountInto( startStamp ); \
585 #define Meas_endSync \
586 saveLowTimeStampCountInto( endStamp ); \
587 addIntervalToHist( startStamp, endStamp, \
588 _VMSMasterEnv->measHists[ syncHistIdx ] );
590 //===========================================================================
591 // SSR
592 #define Meas_startSendFromTo \
593 int32 startStamp, endStamp; \
594 saveLowTimeStampCountInto( startStamp ); \
596 #define Meas_endSendFromTo \
597 saveLowTimeStampCountInto( endStamp ); \
598 addIntervalToHist( startStamp, endStamp, \
599 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
601 #define Meas_startSendOfType \
602 int32 startStamp, endStamp; \
603 saveLowTimeStampCountInto( startStamp ); \
605 #define Meas_endSendOfType \
606 saveLowTimeStampCountInto( endStamp ); \
607 addIntervalToHist( startStamp, endStamp, \
608 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
610 #define Meas_startReceiveFromTo \
611 int32 startStamp, endStamp; \
612 saveLowTimeStampCountInto( startStamp ); \
614 #define Meas_endReceiveFromTo \
615 saveLowTimeStampCountInto( endStamp ); \
616 addIntervalToHist( startStamp, endStamp, \
617 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
619 #define Meas_startReceiveOfType \
620 int32 startStamp, endStamp; \
621 saveLowTimeStampCountInto( startStamp ); \
623 #define Meas_endReceiveOfType \
624 saveLowTimeStampCountInto( endStamp ); \
625 addIntervalToHist( startStamp, endStamp, \
626 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
628 //=====
630 #include "ProcrContext.h"
631 #include "probes.h"
632 #include "vutilities.h"
634 #endif /* _VMS_H */