view VMS.h @ 177:3bd35fc83c61

move loopgraph recording to plugin (\! changed scheduler fn prototype \!)
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Wed, 04 Jan 2012 16:40:10 +0100
parents 981acd1db6af
children 7523ee70d66c
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 #ifdef DETECT_LOOP_GRAPH
284 #endif
285 }
286 MasterEnv;
288 //========================= Extra Stuff Data Strucs =======================
289 typedef struct
290 {
292 }
293 VMSExcp;
295 struct _GateStruc
296 {
297 int32 gateClosed;
298 int32 preGateProgress;
299 int32 waitProgress;
300 int32 exitProgress;
301 };
302 //GateStruc
304 //======================= OS Thread related ===============================
306 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
307 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
308 void masterLoop( void *initData, VirtProcr *masterPr );
311 typedef struct
312 {
313 void *endThdPt;
314 unsigned int coreNum;
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;
325 //===================== Global Vars ===================
327 volatile MasterEnv *_VMSMasterEnv;
332 //=========================== Function Prototypes =========================
335 //========== Setup and shutdown ==========
336 void
337 VMS__init();
339 void
340 VMS__init_Seq();
342 void
343 VMS__start_the_work_then_wait_until_done();
345 void
346 VMS__start_the_work_then_wait_until_done_Seq();
348 inline VirtProcr *
349 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
351 void
352 VMS__dissipate_procr( VirtProcr *procrToDissipate );
354 //Use this to create processor inside entry point & other places outside
355 // the VMS system boundary (IE, not run in slave nor Master)
356 VirtProcr *
357 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
359 void
360 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
362 void
363 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
365 void
366 VMS__shutdown();
368 void
369 VMS__cleanup_at_end_of_shutdown();
371 void *
372 VMS__give_sem_env_for( VirtProcr *animPr );
375 //============== Request Related ===============
377 void
378 VMS__suspend_procr( VirtProcr *callingPr );
380 inline void
381 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
383 /*inline*/ __attribute__ ((noinline)) void
384 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
386 void
387 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
389 void /*inline**/ __attribute__ ((noinline))
390 VMS__send_dissipate_req( VirtProcr *prToDissipate );
392 /*inline**/ __attribute__ ((noinline)) void
393 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
395 VMSReqst *
396 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
398 inline void *
399 VMS__take_sem_reqst_from( VMSReqst *req );
401 void inline
402 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
403 ResumePrFnPtr resumePrFnPtr );
405 //======================== STATS ======================
407 //===== RDTSC wrapper ===== //Also runs with x86_64 code
409 #define saveTimeStampCountInto(low, high) \
410 asm volatile("RDTSC; \
411 movl %%eax, %0; \
412 movl %%edx, %1;" \
413 /* outputs */ : "=m" (low), "=m" (high)\
414 /* inputs */ : \
415 /* clobber */ : "%eax", "%edx" \
416 );
418 #define saveLowTimeStampCountInto(low) \
419 asm volatile("RDTSC; \
420 movl %%eax, %0;" \
421 /* outputs */ : "=m" (low) \
422 /* inputs */ : \
423 /* clobber */ : "%eax", "%edx" \
424 );
426 //====================
427 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
428 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
429 _VMSMasterEnv->measHists[idx] = \
430 makeFixedBinHist( numBins, startVal, binWidth, name );
432 #define saveCyclesAndInstrs(core,cycles,instrs) do{ \
433 int cycles_fd = _VMSMasterEnv->cycles_counter_fd[core]; \
434 int instrs_fd = _VMSMasterEnv->instrs_counter_fd[core]; \
435 int nread; \
436 \
437 nread = read(cycles_fd,&(cycles),sizeof(cycles)); \
438 if(nread<0){ \
439 perror("Error reading cycles counter"); \
440 cycles = 0; \
441 } \
442 \
443 nread = read(instrs_fd,&(instrs),sizeof(instrs)); \
444 if(nread<0){ \
445 perror("Error reading cycles counter"); \
446 instrs = 0; \
447 } \
448 } while (0)
450 #define getReturnAddressBeforeLibraryCall(vp_ptr, res_ptr) do{ \
451 void* frame_ptr0 = vp_ptr->framePtr; \
452 void* frame_ptr1 = *((void**)frame_ptr0); \
453 void* frame_ptr2 = *((void**)frame_ptr1); \
454 void* frame_ptr3 = *((void**)frame_ptr2); \
455 void* ret_addr = *((void**)frame_ptr3 + 1); \
456 *res_ptr = ret_addr; \
457 } while (0)
459 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
461 #ifdef VPTHREAD
463 //VPThread
464 #define createHistIdx 1
465 #define mutexLockHistIdx 2
466 #define mutexUnlockHistIdx 3
467 #define condWaitHistIdx 4
468 #define condSignalHistIdx 5
470 #define MakeTheMeasHists() \
471 _VMSMasterEnv->measHistsInfo = \
472 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
473 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
474 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
475 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
476 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
477 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
479 #endif
482 #ifdef VCILK
484 //VCilk
485 #define spawnHistIdx 1
486 #define syncHistIdx 2
488 #define MakeTheMeasHists() \
489 _VMSMasterEnv->measHistsInfo = \
490 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
491 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
492 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
495 #endif
497 #ifdef SSR
499 //SSR
500 #define SendFromToHistIdx 1
501 #define SendOfTypeHistIdx 2
502 #define ReceiveFromToHistIdx 3
503 #define ReceiveOfTypeHistIdx 4
505 #define MakeTheMeasHists() \
506 _VMSMasterEnv->measHistsInfo = \
507 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
508 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
509 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
510 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
511 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
513 #endif
515 //===========================================================================
516 //VPThread
519 #define Meas_startCreate \
520 int32 startStamp, endStamp; \
521 saveLowTimeStampCountInto( startStamp ); \
523 #define Meas_endCreate \
524 saveLowTimeStampCountInto( endStamp ); \
525 addIntervalToHist( startStamp, endStamp, \
526 _VMSMasterEnv->measHists[ createHistIdx ] );
528 #define Meas_startMutexLock \
529 int32 startStamp, endStamp; \
530 saveLowTimeStampCountInto( startStamp ); \
532 #define Meas_endMutexLock \
533 saveLowTimeStampCountInto( endStamp ); \
534 addIntervalToHist( startStamp, endStamp, \
535 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
537 #define Meas_startMutexUnlock \
538 int32 startStamp, endStamp; \
539 saveLowTimeStampCountInto( startStamp ); \
541 #define Meas_endMutexUnlock \
542 saveLowTimeStampCountInto( endStamp ); \
543 addIntervalToHist( startStamp, endStamp, \
544 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
546 #define Meas_startCondWait \
547 int32 startStamp, endStamp; \
548 saveLowTimeStampCountInto( startStamp ); \
550 #define Meas_endCondWait \
551 saveLowTimeStampCountInto( endStamp ); \
552 addIntervalToHist( startStamp, endStamp, \
553 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
555 #define Meas_startCondSignal \
556 int32 startStamp, endStamp; \
557 saveLowTimeStampCountInto( startStamp ); \
559 #define Meas_endCondSignal \
560 saveLowTimeStampCountInto( endStamp ); \
561 addIntervalToHist( startStamp, endStamp, \
562 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
564 //===========================================================================
565 // VCilk
566 #define Meas_startSpawn \
567 int32 startStamp, endStamp; \
568 saveLowTimeStampCountInto( startStamp ); \
570 #define Meas_endSpawn \
571 saveLowTimeStampCountInto( endStamp ); \
572 addIntervalToHist( startStamp, endStamp, \
573 _VMSMasterEnv->measHists[ spawnHistIdx ] );
575 #define Meas_startSync \
576 int32 startStamp, endStamp; \
577 saveLowTimeStampCountInto( startStamp ); \
579 #define Meas_endSync \
580 saveLowTimeStampCountInto( endStamp ); \
581 addIntervalToHist( startStamp, endStamp, \
582 _VMSMasterEnv->measHists[ syncHistIdx ] );
584 //===========================================================================
585 // SSR
586 #define Meas_startSendFromTo \
587 int32 startStamp, endStamp; \
588 saveLowTimeStampCountInto( startStamp ); \
590 #define Meas_endSendFromTo \
591 saveLowTimeStampCountInto( endStamp ); \
592 addIntervalToHist( startStamp, endStamp, \
593 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
595 #define Meas_startSendOfType \
596 int32 startStamp, endStamp; \
597 saveLowTimeStampCountInto( startStamp ); \
599 #define Meas_endSendOfType \
600 saveLowTimeStampCountInto( endStamp ); \
601 addIntervalToHist( startStamp, endStamp, \
602 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
604 #define Meas_startReceiveFromTo \
605 int32 startStamp, endStamp; \
606 saveLowTimeStampCountInto( startStamp ); \
608 #define Meas_endReceiveFromTo \
609 saveLowTimeStampCountInto( endStamp ); \
610 addIntervalToHist( startStamp, endStamp, \
611 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
613 #define Meas_startReceiveOfType \
614 int32 startStamp, endStamp; \
615 saveLowTimeStampCountInto( startStamp ); \
617 #define Meas_endReceiveOfType \
618 saveLowTimeStampCountInto( endStamp ); \
619 addIntervalToHist( startStamp, endStamp, \
620 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
622 //=====
624 #include "ProcrContext.h"
625 #include "probes.h"
626 #include "vutilities.h"
628 #endif /* _VMS_H */