view VMS.h @ 68:9c3107044f86

Added measurement hists macros
author Me
date Sat, 20 Nov 2010 08:19:05 +0100
parents 2377967a2732
children 11bfe9d136ed a49f02980151
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 __USE_GNU
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"
20 #include <pthread.h>
21 #include <sys/time.h>
24 //=============================== Debug ===================================
25 //
26 //When SEQUENTIAL is defined, VMS does sequential exe in the main thread
27 // It still does co-routines and all the mechanisms are the same, it just
28 // has only a single thread and animates VPs one at a time
29 //#define SEQUENTIAL
31 //#define USE_WORK_STEALING
33 //turns on the probe-instrumentation in the application -- when not
34 // defined, the calls to the probe functions turn into comments
35 #define STATS__ENABLE_PROBES
36 //#define TURN_ON_DEBUG_PROBES
38 //These defines turn types of bug messages on and off
39 // be sure debug messages are un-commented (next block of defines)
40 #define dbgAppFlow TRUE /* Top level flow of application code -- general*/
41 #define dbgProbes FALSE /* for issues inside probes themselves*/
42 #define dbgB2BMaster FALSE /* in coreloop, back to back master VPs*/
43 #define dbgRqstHdlr FALSE /* in request handler code*/
45 //Comment or un- the substitute half to turn on/off types of debug message
46 #define DEBUG( bool, msg) \
47 // if( bool){ printf(msg); fflush(stdin);}
48 #define DEBUG1( bool, msg, param) \
49 // if(bool){printf(msg, param); fflush(stdin);}
50 #define DEBUG2( bool, msg, p1, p2) \
51 // if(bool) {printf(msg, p1, p2); fflush(stdin);}
53 #define ERROR(msg) printf(msg); fflush(stdin);
54 #define ERROR1(msg, param) printf(msg, param); fflush(stdin);
55 #define ERROR2(msg, p1, p2) printf(msg, p1, p2); fflush(stdin);
57 //=========================== STATS =======================
59 //when MEAS__TIME_STAMP_SUSP is defined, causes code to be inserted and
60 // compiled-in that saves the low part of the time stamp count just before
61 // suspending a processor and just after resuming that processor. It is
62 // saved into a field added to VirtProcr. Have to sanity-check for
63 // rollover of low portion into high portion.
64 //#define MEAS__TIME_STAMP_SUSP
65 //#define MEAS__TIME_MASTER
66 #define MEAS__TIME_PLUGIN
67 #define MEAS__TIME_MALLOC
68 //#define MEAS__TIME_MASTER_LOCK
69 #define MEAS__NUM_TIMES_TO_RUN 100000
71 //For code that calculates normalization-offset between TSC counts of
72 // different cores.
73 #define NUM_TSC_ROUND_TRIPS 10
76 //========================= Hardware related Constants =====================
77 //This value is the number of hardware threads in the shared memory
78 // machine
79 #define NUM_CORES 4
81 // tradeoff amortizing master fixed overhead vs imbalance potential
82 // when work-stealing, can make bigger, at risk of losing cache affinity
83 #define NUM_SCHED_SLOTS 5
85 #define MIN_WORK_UNIT_CYCLES 20000
87 #define MASTERLOCK_RETRIES 10000
89 // stack size in virtual processors created
90 #define VIRT_PROCR_STACK_SIZE 0x4000 /* 16K */
92 // memory for VMS__malloc
93 #define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
96 //==============================
98 #define SUCCESS 0
100 #define writeVMSQ writePrivQ
101 #define readVMSQ readPrivQ
102 #define makeVMSQ makeVMSPrivQ
103 #define numInVMSQ numInPrivQ
104 #define VMSQueueStruc PrivQueueStruc
108 //===========================================================================
109 typedef unsigned long long TSCount;
111 typedef struct _SchedSlot SchedSlot;
112 typedef struct _VMSReqst VMSReqst;
113 typedef struct _VirtProcr VirtProcr;
114 typedef struct _IntervalProbe IntervalProbe;
115 typedef struct _GateStruc GateStruc;
118 typedef VirtProcr * (*SlaveScheduler) ( void *, int ); //semEnv, coreIdx
119 typedef void (*RequestHandler) ( VirtProcr *, void * ); //prWReqst, semEnv
120 typedef void (*VirtProcrFnPtr) ( void *, VirtProcr * ); //initData, animPr
121 typedef void VirtProcrFn ( void *, VirtProcr * ); //initData, animPr
122 typedef void (*ResumePrFnPtr) ( VirtProcr *, void * );
125 //============= Requests ===========
126 //
128 enum VMSReqstType //avoid starting enums at 0, for debug reasons
129 {
130 semantic = 1,
131 createReq,
132 dissipate,
133 VMSSemantic //goes with VMSSemReqst below
134 };
136 struct _VMSReqst
137 {
138 enum VMSReqstType reqType;//used for dissipate and in future for IO requests
139 void *semReqData;
141 VMSReqst *nextReqst;
142 };
143 //VMSReqst
145 enum VMSSemReqstType //These are equivalent to semantic requests, but for
146 { // VMS's services available directly to app, like OS
147 createProbe = 1, // and probe services -- like a VMS-wide built-in lang
148 openFile,
149 otherIO
150 };
152 typedef struct
153 { enum VMSSemReqstType reqType;
154 VirtProcr *requestingPr;
155 char *nameStr; //for create probe
156 }
157 VMSSemReq;
160 //==================== Core data structures ===================
162 struct _SchedSlot
163 {
164 int workIsDone;
165 int needsProcrAssigned;
166 VirtProcr *procrAssignedToSlot;
167 };
168 //SchedSlot
170 /*WARNING: re-arranging this data structure could cause VP switching
171 * assembly code to fail -- hard-codes offsets of fields
172 */
173 struct _VirtProcr
174 { int procrID; //for debugging -- count up each time create
175 int coreAnimatedBy;
176 void *startOfStack;
177 void *stackPtr;
178 void *framePtr;
179 void *nextInstrPt;
181 void *coreLoopStartPt; //allows proto-runtime to be linked later
182 void *coreLoopFramePtr; //restore before jmp back to core loop
183 void *coreLoopStackPtr; //restore before jmp back to core loop
185 void *initialData;
187 SchedSlot *schedSlot;
188 VMSReqst *requests;
190 void *semanticData; //this lives here for the life of VP
191 void *dataRetFromReq;//values returned from plugin to VP go here
193 //=========== MEASUREMENT STUFF ==========
194 #ifdef MEAS__TIME_STAMP_SUSP
195 unsigned int preSuspTSCLow;
196 unsigned int postSuspTSCLow;
197 #endif
198 #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
199 unsigned int startMasterTSCLow;
200 unsigned int endMasterTSCLow;
201 #endif
202 //========================================
204 float64 createPtInSecs; //have space but don't use on some configs
205 };
206 //VirtProcr
209 /*WARNING: re-arranging this data structure could cause VP-switching
210 * assembly code to fail -- hard-codes offsets of fields
211 * (because -O3 messes with things otherwise)
212 */
213 typedef struct
214 {
215 SlaveScheduler slaveScheduler;
216 RequestHandler requestHandler;
218 SchedSlot ***allSchedSlots;
219 VMSQueueStruc **readyToAnimateQs;
220 VirtProcr **masterVPs;
222 void *semanticEnv;
223 void *OSEventStruc; //for future, when add I/O to BLIS
224 MallocProlog *freeListHead;
225 int32 amtOfOutstandingMem; //total currently allocated
227 void *coreLoopStartPt;//addr to jump to to re-enter coreLoop
228 void *coreLoopEndPt; //addr to jump to to shut down a coreLoop
230 int32 setupComplete;
231 int32 masterLock;
233 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
234 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
235 int32 workStealingLock;
237 int32 numProcrsCreated; //gives ordering to processor creation
239 //=========== MEASUREMENT STUFF =============
240 IntervalProbe **intervalProbes;
241 PrivDynArrayInfo *dynIntervalProbesInfo;
242 HashTable *probeNameHashTbl;
243 int32 masterCreateProbeID;
244 float64 createPtInSecs;
245 Histogram **measHists;
246 PrivDynArrayInfo *measHistsInfo;
247 #ifdef MEAS__TIME_PLUGIN
248 Histogram *reqHdlrLowTimeHist;
249 Histogram *reqHdlrHighTimeHist;
250 #endif
251 #ifdef MEAS__TIME_MALLOC
252 Histogram *mallocTimeHist;
253 Histogram *freeTimeHist;
254 #endif
255 #ifdef MEAS__TIME_MASTER_LOCK
256 Histogram *masterLockLowTimeHist;
257 Histogram *masterLockHighTimeHist;
258 #endif
259 }
260 MasterEnv;
262 //========================= Extra Stuff Data Strucs =======================
263 typedef struct
264 {
266 }
267 VMSExcp;
269 struct _GateStruc
270 {
271 int32 gateClosed;
272 int32 preGateProgress;
273 int32 waitProgress;
274 int32 exitProgress;
275 };
276 //GateStruc
278 //======================= OS Thread related ===============================
280 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
281 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
282 void masterLoop( void *initData, VirtProcr *masterPr );
285 typedef struct
286 {
287 void *endThdPt;
288 unsigned int coreNum;
289 }
290 ThdParams;
292 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
293 ThdParams *coreLoopThdParams [ NUM_CORES ];
294 pthread_mutex_t suspendLock;
295 pthread_cond_t suspend_cond;
299 //===================== Global Vars ===================
301 volatile MasterEnv *_VMSMasterEnv;
306 //=========================== Function Prototypes =========================
309 //========== Setup and shutdown ==========
310 void
311 VMS__init();
313 void
314 VMS__init_Seq();
316 void
317 VMS__start_the_work_then_wait_until_done();
319 void
320 VMS__start_the_work_then_wait_until_done_Seq();
322 VirtProcr *
323 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
325 void
326 VMS__dissipate_procr( VirtProcr *procrToDissipate );
328 //Use this to create processor inside entry point & other places outside
329 // the VMS system boundary (IE, not run in slave nor Master)
330 VirtProcr *
331 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
333 void
334 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
336 void
337 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
339 void
340 VMS__shutdown();
342 void
343 VMS__cleanup_at_end_of_shutdown();
345 void *
346 VMS__give_sem_env_for( VirtProcr *animPr );
349 //============== Request Related ===============
351 void
352 VMS__suspend_procr( VirtProcr *callingPr );
354 inline void
355 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
357 inline void
358 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
360 void
361 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
363 void inline
364 VMS__send_dissipate_req( VirtProcr *prToDissipate );
366 inline void
367 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
369 VMSReqst *
370 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
372 inline void *
373 VMS__take_sem_reqst_from( VMSReqst *req );
375 //======================== STATS ======================
377 //===== RDTSC wrapper =====
379 #define saveTimeStampCountInto(low, high) \
380 asm volatile("RDTSC; \
381 movl %%eax, %0; \
382 movl %%edx, %1;" \
383 /* outputs */ : "=m" (low), "=m" (high)\
384 /* inputs */ : \
385 /* clobber */ : "%eax", "%edx" \
386 );
388 #define saveLowTimeStampCountInto(low) \
389 asm volatile("RDTSC; \
390 movl %%eax, %0;" \
391 /* outputs */ : "=m" (low) \
392 /* inputs */ : \
393 /* clobber */ : "%eax", "%edx" \
394 );
396 //====================
397 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
398 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
399 _VMSMasterEnv->measHists[idx] = \
400 makeFixedBinHist( numBins, startVal, binWidth, name );
403 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
404 #define createHistIdx 1
405 #define mutexLockHistIdx 2
406 #define mutexUnlockHistIdx 3
407 #define condWaitHistIdx 4
408 #define condSignalHistIdx 5
411 #define MakeTheMeasHists \
412 _VMSMasterEnv->measHistsInfo = \
413 makePrivDynArrayOfSize( &(_VMSMasterEnv->measHists), 200);\
414 makeAMeasHist( createHistIdx, "Create", 50, 0, 200 ) \
415 makeAMeasHist( mutexLockHistIdx, "mutex lock", 50, 0, 100 ) \
416 makeAMeasHist( mutexUnlockHistIdx, "mutex unlock", 50, 0, 100 ) \
417 makeAMeasHist( condWaitHistIdx, "cond wait", 50, 0, 100 ) \
418 makeAMeasHist( condSignalHistIdx, "cond signal", 50, 0, 100 )
420 #define Meas_startCreate \
421 int32 startStamp, endStamp; \
422 saveLowTimeStampCountInto( startStamp ); \
424 #define Meas_endCreate \
425 saveLowTimeStampCountInto( endStamp ); \
426 addIntervalToHist( startStamp, endStamp, \
427 _VMSMasterEnv->measHists[ createHistIdx ] );
429 #define Meas_startMutexLock \
430 int32 startStamp, endStamp; \
431 saveLowTimeStampCountInto( startStamp ); \
433 #define Meas_endMutexLock \
434 saveLowTimeStampCountInto( endStamp ); \
435 addIntervalToHist( startStamp, endStamp, \
436 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
438 #define Meas_startMutexUnlock \
439 int32 startStamp, endStamp; \
440 saveLowTimeStampCountInto( startStamp ); \
442 #define Meas_endMutexUnlock \
443 saveLowTimeStampCountInto( endStamp ); \
444 addIntervalToHist( startStamp, endStamp, \
445 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
447 #define Meas_startCondWait \
448 int32 startStamp, endStamp; \
449 saveLowTimeStampCountInto( startStamp ); \
451 #define Meas_endCondWait \
452 saveLowTimeStampCountInto( endStamp ); \
453 addIntervalToHist( startStamp, endStamp, \
454 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
456 #define Meas_startCondSignal \
457 int32 startStamp, endStamp; \
458 saveLowTimeStampCountInto( startStamp ); \
460 #define Meas_endCondSignal \
461 saveLowTimeStampCountInto( endStamp ); \
462 addIntervalToHist( startStamp, endStamp, \
463 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
466 //=====
468 #include "SwitchAnimators.h"
469 #include "probes.h"
470 #include "vutilities.h"
472 #endif /* _VMS_H */