view VMS.h @ 70:f9b60012fd74

working ucontext version
author Merten Sach <msach@mailbox.tu-berlin.de>
date Fri, 27 May 2011 12:35:40 +0200
parents 11bfe9d136ed
children efb55f1b5fb9
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>
22 #include <ucontext.h>
25 //=============================== Debug ===================================
26 //
27 //When SEQUENTIAL is defined, VMS does sequential exe in the main thread
28 // It still does co-routines and all the mechanisms are the same, it just
29 // has only a single thread and animates VPs one at a time
30 //#define SEQUENTIAL
32 //#define USE_WORK_STEALING
34 //turns on the probe-instrumentation in the application -- when not
35 // defined, the calls to the probe functions turn into comments
36 #define STATS__ENABLE_PROBES
37 //#define TURN_ON_DEBUG_PROBES
39 //These defines turn types of bug messages on and off
40 // be sure debug messages are un-commented (next block of defines)
41 #define dbgAppFlow TRUE /* Top level flow of application code -- general*/
42 #define dbgProbes FALSE /* for issues inside probes themselves*/
43 #define dbgB2BMaster FALSE /* in coreloop, back to back master VPs*/
44 #define dbgRqstHdlr FALSE /* in request handler code*/
46 //Comment or un- the substitute half to turn on/off types of debug message
47 #define DEBUG( bool, msg) \
48 // if( bool){ printf(msg); fflush(stdin);}
49 #define DEBUG1( bool, msg, param) \
50 // if(bool){printf(msg, param); fflush(stdin);}
51 #define DEBUG2( bool, msg, p1, p2) \
52 // if(bool) {printf(msg, p1, p2); fflush(stdin);}
54 #define ERROR(msg) printf(msg);
55 #define ERROR1(msg, param) printf(msg, param);
56 #define ERROR2(msg, p1, p2) printf(msg, p1, p2);
58 //=========================== STATS =======================
60 //when MEAS__TIME_STAMP_SUSP is defined, causes code to be inserted and
61 // compiled-in that saves the low part of the time stamp count just before
62 // suspending a processor and just after resuming that processor. It is
63 // saved into a field added to VirtProcr. Have to sanity-check for
64 // rollover of low portion into high portion.
65 //#define MEAS__TIME_STAMP_SUSP
66 //#define MEAS__TIME_MASTER
67 #define MEAS__TIME_PLUGIN
68 #define MEAS__TIME_MALLOC
69 //#define MEAS__TIME_MASTER_LOCK
70 #define MEAS__NUM_TIMES_TO_RUN 100000
72 //For code that calculates normalization-offset between TSC counts of
73 // different cores.
74 #define NUM_TSC_ROUND_TRIPS 10
77 //========================= Hardware related Constants =====================
78 //This value is the number of hardware threads in the shared memory
79 // machine
80 #define NUM_CORES 8
82 // tradeoff amortizing master fixed overhead vs imbalance potential
83 // when work-stealing, can make bigger, at risk of losing cache affinity
84 #define NUM_SCHED_SLOTS 5
86 #define MIN_WORK_UNIT_CYCLES 20000
88 #define MASTERLOCK_RETRIES 10000
90 // stack size in virtual processors created
91 #define VIRT_PROCR_STACK_SIZE 0x4000 /* 16K */
93 // memory for VMS__malloc
94 #define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
97 //==============================
99 #define SUCCESS 0
101 #define writeVMSQ writePrivQ
102 #define readVMSQ readPrivQ
103 #define makeVMSQ makeVMSPrivQ
104 #define numInVMSQ numInPrivQ
105 #define VMSQueueStruc PrivQueueStruc
109 //===========================================================================
110 typedef unsigned long long TSCount;
112 typedef struct _SchedSlot SchedSlot;
113 typedef struct _VMSReqst VMSReqst;
114 typedef struct _VirtProcr VirtProcr;
115 typedef struct _IntervalProbe IntervalProbe;
116 typedef struct _GateStruc GateStruc;
119 typedef VirtProcr * (*SlaveScheduler) ( void *, int ); //semEnv, coreIdx
120 typedef void (*RequestHandler) ( VirtProcr *, void * ); //prWReqst, semEnv
121 typedef void (*VirtProcrFnPtr) ( void *, VirtProcr * ); //initData, animPr
122 typedef void VirtProcrFn ( void *, VirtProcr * ); //initData, animPr
123 typedef void (*ResumePrFnPtr) ( VirtProcr *, void * );
126 //============= Requests ===========
127 //
129 enum VMSReqstType //avoid starting enums at 0, for debug reasons
130 {
131 semantic = 1,
132 createReq,
133 dissipate,
134 VMSSemantic //goes with VMSSemReqst below
135 };
137 struct _VMSReqst
138 {
139 enum VMSReqstType reqType;//used for dissipate and in future for IO requests
140 void *semReqData;
142 VMSReqst *nextReqst;
143 };
144 //VMSReqst
146 enum VMSSemReqstType //These are equivalent to semantic requests, but for
147 { // VMS's services available directly to app, like OS
148 createProbe = 1, // and probe services -- like a VMS-wide built-in lang
149 openFile,
150 otherIO
151 };
153 typedef struct
154 { enum VMSSemReqstType reqType;
155 VirtProcr *requestingPr;
156 char *nameStr; //for create probe
157 }
158 VMSSemReq;
161 //==================== Core data structures ===================
163 struct _SchedSlot
164 {
165 int workIsDone;
166 int needsProcrAssigned;
167 VirtProcr *procrAssignedToSlot;
168 };
169 //SchedSlot
171 /*WARNING: re-arranging this data structure could cause VP switching
172 * assembly code to fail -- hard-codes offsets of fields
173 */
174 struct _VirtProcr
175 { int procrID; //for debugging -- count up each time create
176 int coreAnimatedBy;
177 void *startOfStack;
178 //void *stackPtr;
179 //void *framePtr;
180 //void *nextInstrPt;
181 ucontext_t *savedVPStatus;
183 //void *coreLoopStartPt; //allows proto-runtime to be linked later
184 //void *coreLoopFramePtr; //restore before jmp back to core loop
185 //void *coreLoopStackPtr; //restore before jmp back to core loop
186 ucontext_t *savedCoreLoopStatus;
188 void *initialData;
190 SchedSlot *schedSlot;
191 VMSReqst *requests;
193 void *semanticData; //this lives here for the life of VP
194 void *dataRetFromReq;//values returned from plugin to VP go here
196 //=========== MEASUREMENT STUFF ==========
197 #ifdef MEAS__TIME_STAMP_SUSP
198 unsigned int preSuspTSCLow;
199 unsigned int postSuspTSCLow;
200 #endif
201 #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
202 unsigned int startMasterTSCLow;
203 unsigned int endMasterTSCLow;
204 #endif
205 //========================================
207 float64 createPtInSecs; //have space but don't use on some configs
208 };
209 //VirtProcr
212 /*WARNING: re-arranging this data structure could cause VP-switching
213 * assembly code to fail -- hard-codes offsets of fields
214 * (because -O3 messes with things otherwise)
215 */
216 typedef struct
217 {
218 SlaveScheduler slaveScheduler;
219 RequestHandler requestHandler;
221 SchedSlot ***allSchedSlots;
222 VMSQueueStruc **readyToAnimateQs;
223 VirtProcr **masterVPs;
225 void *semanticEnv;
226 void *OSEventStruc; //for future, when add I/O to BLIS
227 MallocProlog *freeListHead;
228 int32 amtOfOutstandingMem; //total currently allocated
230 //void *coreLoopStartPt;//addr to jump to to re-enter coreLoop
231 //void *coreLoopEndPt; //addr to jump to to shut down a coreLoop
233 int32 setupComplete;
234 volatile int32 masterLock;
236 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
237 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
238 int32 workStealingLock;
240 int32 numProcrsCreated; //gives ordering to processor creation
242 //=========== MEASUREMENT STUFF =============
243 IntervalProbe **intervalProbes;
244 PrivDynArrayInfo *dynIntervalProbesInfo;
245 HashTable *probeNameHashTbl;
246 int32 masterCreateProbeID;
247 float64 createPtInSecs;
248 Histogram **measHists;
249 PrivDynArrayInfo *measHistsInfo;
250 #ifdef MEAS__TIME_PLUGIN
251 Histogram *reqHdlrLowTimeHist;
252 Histogram *reqHdlrHighTimeHist;
253 #endif
254 #ifdef MEAS__TIME_MALLOC
255 Histogram *mallocTimeHist;
256 Histogram *freeTimeHist;
257 #endif
258 #ifdef MEAS__TIME_MASTER_LOCK
259 Histogram *masterLockLowTimeHist;
260 Histogram *masterLockHighTimeHist;
261 #endif
262 }
263 MasterEnv;
265 //========================= Extra Stuff Data Strucs =======================
266 typedef struct
267 {
269 }
270 VMSExcp;
272 struct _GateStruc
273 {
274 int32 gateClosed;
275 int32 preGateProgress;
276 int32 waitProgress;
277 int32 exitProgress;
278 };
279 //GateStruc
281 //======================= OS Thread related ===============================
283 void coreLoop( void *paramsIn ); //standard PThreads fn prototype
284 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
285 void terminateCoreLoop(VirtProcr *currPr);
286 void masterLoop( void *initData, VirtProcr *masterPr );
288 typedef struct
289 {
290 void *endThdPt;
291 unsigned int coreNum;
292 }
293 ThdParams;
295 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
296 ThdParams *coreLoopThdParams [ NUM_CORES ];
297 pthread_mutex_t suspendLock;
298 pthread_cond_t suspend_cond;
301 //===================== Global Vars ===================
303 volatile MasterEnv *_VMSMasterEnv;
308 //=========================== Function Prototypes =========================
311 //========== Setup and shutdown ==========
312 void
313 VMS__init();
315 void
316 VMS__init_Seq();
318 void
319 VMS__start_the_work_then_wait_until_done();
321 void
322 VMS__start_the_work_then_wait_until_done_Seq();
324 VirtProcr *
325 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
327 void
328 VMS__dissipate_procr( VirtProcr *procrToDissipate );
330 //Use this to create processor inside entry point & other places outside
331 // the VMS system boundary (IE, not run in slave nor Master)
332 VirtProcr *
333 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
335 void
336 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
338 void
339 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
341 void
342 VMS__shutdown();
344 void
345 VMS__cleanup_at_end_of_shutdown();
347 void *
348 VMS__give_sem_env_for( VirtProcr *animPr );
351 //============== Request Related ===============
353 void
354 VMS__suspend_procr( VirtProcr *callingPr );
356 inline void
357 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
359 inline void
360 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
362 void
363 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
365 void inline
366 VMS__send_dissipate_req( VirtProcr *prToDissipate );
368 inline void
369 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
371 VMSReqst *
372 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
374 inline void *
375 VMS__take_sem_reqst_from( VMSReqst *req );
377 //======================== STATS ======================
379 //===== RDTSC wrapper =====
381 #define saveTimeStampCountInto(low, high) \
382 asm volatile("RDTSC; \
383 movl %%eax, %0; \
384 movl %%edx, %1;" \
385 /* outputs */ : "=m" (low), "=m" (high)\
386 /* inputs */ : \
387 /* clobber */ : "%eax", "%edx" \
388 );
390 #define saveLowTimeStampCountInto(low) \
391 asm volatile("RDTSC; \
392 movl %%eax, %0;" \
393 /* outputs */ : "=m" (low) \
394 /* inputs */ : \
395 /* clobber */ : "%eax", "%edx" \
396 );
398 //====================
399 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
400 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
401 _VMSMasterEnv->measHists[idx] = \
402 makeFixedBinHist( numBins, startVal, binWidth, name );
405 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
407 //VPThread
408 #define createHistIdx 1
409 #define mutexLockHistIdx 2
410 #define mutexUnlockHistIdx 3
411 #define condWaitHistIdx 4
412 #define condSignalHistIdx 5
414 //VCilk
415 #define spawnHistIdx 1
416 #define syncHistIdx 2
418 //SSR
419 #define SendFromToHistIdx 1
420 #define SendOfTypeHistIdx 2
421 #define ReceiveFromToHistIdx 3
422 #define ReceiveOfTypeHistIdx 4
425 #define MakeTheMeasHists \
426 _VMSMasterEnv->measHistsInfo = \
427 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200);\
428 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
429 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
431 // makeAMeasHist( createHistIdx, "Create", 50, 0, 100 ) \
432 // makeAMeasHist( mutexLockHistIdx, "mutex lock", 50, 0, 100 ) \
433 // makeAMeasHist( mutexUnlockHistIdx, "mutex unlock", 50, 0, 100 ) \
434 // makeAMeasHist( condWaitHistIdx, "cond wait", 50, 0, 100 ) \
435 // makeAMeasHist( condSignalHistIdx, "cond signal", 50, 0, 100 )
437 // makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
438 // makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
439 // makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
440 // makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
443 //===========================================================================
444 //VPThread
447 #define Meas_startCreate \
448 int32 startStamp, endStamp; \
449 saveLowTimeStampCountInto( startStamp ); \
451 #define Meas_endCreate \
452 saveLowTimeStampCountInto( endStamp ); \
453 addIntervalToHist( startStamp, endStamp, \
454 _VMSMasterEnv->measHists[ createHistIdx ] );
456 #define Meas_startMutexLock \
457 int32 startStamp, endStamp; \
458 saveLowTimeStampCountInto( startStamp ); \
460 #define Meas_endMutexLock \
461 saveLowTimeStampCountInto( endStamp ); \
462 addIntervalToHist( startStamp, endStamp, \
463 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
465 #define Meas_startMutexUnlock \
466 int32 startStamp, endStamp; \
467 saveLowTimeStampCountInto( startStamp ); \
469 #define Meas_endMutexUnlock \
470 saveLowTimeStampCountInto( endStamp ); \
471 addIntervalToHist( startStamp, endStamp, \
472 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
474 #define Meas_startCondWait \
475 int32 startStamp, endStamp; \
476 saveLowTimeStampCountInto( startStamp ); \
478 #define Meas_endCondWait \
479 saveLowTimeStampCountInto( endStamp ); \
480 addIntervalToHist( startStamp, endStamp, \
481 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
483 #define Meas_startCondSignal \
484 int32 startStamp, endStamp; \
485 saveLowTimeStampCountInto( startStamp ); \
487 #define Meas_endCondSignal \
488 saveLowTimeStampCountInto( endStamp ); \
489 addIntervalToHist( startStamp, endStamp, \
490 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
492 //===========================================================================
493 // VCilk
494 #define Meas_startSpawn \
495 int32 startStamp, endStamp; \
496 saveLowTimeStampCountInto( startStamp ); \
498 #define Meas_endSpawn \
499 saveLowTimeStampCountInto( endStamp ); \
500 addIntervalToHist( startStamp, endStamp, \
501 _VMSMasterEnv->measHists[ spawnHistIdx ] );
503 #define Meas_startSync \
504 int32 startStamp, endStamp; \
505 saveLowTimeStampCountInto( startStamp ); \
507 #define Meas_endSync \
508 saveLowTimeStampCountInto( endStamp ); \
509 addIntervalToHist( startStamp, endStamp, \
510 _VMSMasterEnv->measHists[ syncHistIdx ] );
512 //===========================================================================
513 // SSR
514 #define Meas_startSendFromTo \
515 int32 startStamp, endStamp; \
516 saveLowTimeStampCountInto( startStamp ); \
518 #define Meas_endSendFromTo \
519 saveLowTimeStampCountInto( endStamp ); \
520 addIntervalToHist( startStamp, endStamp, \
521 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
523 #define Meas_startSendOfType \
524 int32 startStamp, endStamp; \
525 saveLowTimeStampCountInto( startStamp ); \
527 #define Meas_endSendOfType \
528 saveLowTimeStampCountInto( endStamp ); \
529 addIntervalToHist( startStamp, endStamp, \
530 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
532 #define Meas_startReceiveFromTo \
533 int32 startStamp, endStamp; \
534 saveLowTimeStampCountInto( startStamp ); \
536 #define Meas_endReceiveFromTo \
537 saveLowTimeStampCountInto( endStamp ); \
538 addIntervalToHist( startStamp, endStamp, \
539 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
541 #define Meas_startReceiveOfType \
542 int32 startStamp, endStamp; \
543 saveLowTimeStampCountInto( startStamp ); \
545 #define Meas_endReceiveOfType \
546 saveLowTimeStampCountInto( endStamp ); \
547 addIntervalToHist( startStamp, endStamp, \
548 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
550 //=====
552 //======= Utilities ======
553 /*
554 * This Macro makes the gcc reload all variables from Stack after this function.
555 * This is necessary because we jmp into the masterLoop and the variables are
556 * kept in Registers for optimization.
557 */
558 #define flushRegisters() \
559 asm volatile ("" /*no instr needed*/ \
560 ::: /*no output and input*/ \
561 "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi")
562 //========
564 #include "SwitchAnimators.h"
565 #include "probes.h"
566 #include "vutilities.h"
568 #endif /* _VMS_H */