view VMS.h @ 76:9ddbb071142d

make hardware independent and port to 64bit
author Merten Sach <msach@mailbox.tu-berlin.de>
date Thu, 16 Jun 2011 14:41:15 +0200
parents d8f12351f7cc
children fe5ec83f1baf
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"
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);
54 #define ERROR1(msg, param) printf(msg, param);
55 #define ERROR2(msg, p1, p2) printf(msg, p1, p2);
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 processorsrc/VPThread_lib/VMS/VMS.h:322: warning: previous declaration of ‘VMS__create_procr’ was here. 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 8
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 livesUSE_GNU 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;USE_GNU
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 *coreLoopReturnPt;//addr to jump to to re-enter coreLoop
229 int32 setupComplete;
230 volatile int32 masterLock;
232 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
233 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
234 int32 workStealingLock;
236 int32 numProcrsCreated; //gives ordering to processor creation
238 //=========== MEASUREMENT STUFF =============
239 IntervalProbe **intervalProbes;
240 PrivDynArrayInfo *dynIntervalProbesInfo;
241 HashTable *probeNameHashTbl;
242 int32 masterCreateProbeID;
243 float64 createPtInSecs;
244 Histogram **measHists;
245 PrivDynArrayInfo *measHistsInfo;
246 #ifdef MEAS__TIME_PLUGIN
247 Histogram *reqHdlrLowTimeHist;
248 Histogram *reqHdlrHighTimeHist;
249 #endif
250 #ifdef MEAS__TIME_MALLOC
251 Histogram *mallocTimeHist;
252 Histogram *freeTimeHist;
253 #endif
254 #ifdef MEAS__TIME_MASTER_LOCK
255 Histogram *masterLockLowTimeHist;
256 Histogram *masterLockHighTimeHist;
257 #endif
258 }
259 MasterEnv;
261 //========================= Extra Stuff Data Strucs =======================
262 typedef struct
263 {
265 }
266 VMSExcp;
268 struct _GateStruc
269 {
270 int32 gateClosed;
271 int32 preGateProgress;
272 int32 waitProgress;
273 int32 exitProgress;
274 };
275 //GateStruc
277 //======================= OS Thread related ===============================
279 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
280 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
281 void masterLoop( void *initData, VirtProcr *masterPr );
284 typedef struct
285 {
286 void *endThdPt;
287 unsigned int coreNum;
288 }
289 ThdParams;
291 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
292 ThdParams *coreLoopThdParams [ NUM_CORES ];
293 pthread_mutex_t suspendLock;
294 pthread_cond_t suspend_cond;
298 //===================== Global Vars ===================
300 volatile MasterEnv *_VMSMasterEnv;
305 //=========================== Function Prototypes =========================
308 //========== Setup and shutdown ==========
309 void
310 VMS__init();
312 void
313 VMS__init_Seq();
315 void
316 VMS__start_the_work_then_wait_until_done();
318 void
319 VMS__start_the_work_then_wait_until_done_Seq();
321 inline VirtProcr *
322 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
324 void
325 VMS__dissipate_procr( VirtProcr *procrToDissipate );
327 //Use this to create processor inside entry point & other places outside
328 // the VMS system boundary (IE, not run in slave nor Master)
329 VirtProcr *
330 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
332 void
333 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
335 void
336 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
338 void
339 VMS__shutdown();
341 void
342 VMS__cleanup_at_end_of_shutdown();
344 void *
345 VMS__give_sem_env_for( VirtProcr *animPr );
348 //============== Request Related ===============
350 void
351 VMS__suspend_procr( VirtProcr *callingPr );
353 inline void
354 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
356 inline void
357 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
359 void
360 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
362 void inline
363 VMS__send_dissipate_req( VirtProcr *prToDissipate );
365 inline void
366 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
368 VMSReqst *
369 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
371 inline void *
372 VMS__take_sem_reqst_from( VMSReqst *req );
374 //======================== STATS ======================
376 //===== RDTSC wrapper ===== //Also runs with x86_64 code
378 #define saveTimeStampCountInto(low, high) \
379 asm volatile("RDTSC; \
380 movl %%eax, %0; \
381 movl %%edx, %1;" \
382 /* outputs */ : "=m" (low), "=m" (high)\
383 /* inputs */ : \
384 /* clobber */ : "%eax", "%edx" \
385 );
387 #define saveLowTimeStampCountInto(low) \
388 asm volatile("RDTSC; \
389 movl %%eax, %0;" \
390 /* outputs */ : "=m" (low) \
391 /* inputs */ : \
392 /* clobber */ : "%eax", "%edx" \
393 );
395 //====================
396 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
397 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
398 _VMSMasterEnv->measHists[idx] = \
399 makeFixedBinHist( numBins, startVal, binWidth, name );
402 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
404 #ifdef VPTHREAD
406 //VPThread
407 #define createHistIdx 1
408 #define mutexLockHistIdx 2
409 #define mutexUnlockHistIdx 3
410 #define condWaitHistIdx 4
411 #define condSignalHistIdx 5
413 #define MakeTheMeasHists() \
414 _VMSMasterEnv->measHistsInfo = \
415 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
416 makeAMeasHist( createHistIdx, "Create", 50, 0, 100 ) \
417 makeAMeasHist( mutexLockHistIdx, "mutex lock", 50, 0, 100 ) \
418 makeAMeasHist( mutexUnlockHistIdx, "mutex unlock", 50, 0, 100 ) \
419 makeAMeasHist( condWaitHistIdx, "cond wait", 50, 0, 100 ) \
420 makeAMeasHist( condSignalHistIdx, "cond signal", 50, 0, 100 )
422 #endif
425 #ifdef VCILK
427 //VCilk
428 #define spawnHistIdx 1
429 #define syncHistIdx 2
431 #define MakeTheMeasHists() \
432 _VMSMasterEnv->measHistsInfo = \
433 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
434 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
435 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
438 #endif
440 #ifdef SSR
442 //SSR
443 #define SendFromToHistIdx 1
444 #define SendOfTypeHistIdx 2
445 #define ReceiveFromToHistIdx 3
446 #define ReceiveOfTypeHistIdx 4
448 #define MakeTheMeasHists() \
449 _VMSMasterEnv->measHistsInfo = \
450 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
451 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
452 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
453 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
454 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
456 #endif
458 //===========================================================================
459 //VPThread
462 #define Meas_startCreate \
463 int32 startStamp, endStamp; \
464 saveLowTimeStampCountInto( startStamp ); \
466 #define Meas_endCreate \
467 saveLowTimeStampCountInto( endStamp ); \
468 addIntervalToHist( startStamp, endStamp, \
469 _VMSMasterEnv->measHists[ createHistIdx ] );
471 #define Meas_startMutexLock \
472 int32 startStamp, endStamp; \
473 saveLowTimeStampCountInto( startStamp ); \
475 #define Meas_endMutexLock \
476 saveLowTimeStampCountInto( endStamp ); \
477 addIntervalToHist( startStamp, endStamp, \
478 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
480 #define Meas_startMutexUnlock \
481 int32 startStamp, endStamp; \
482 saveLowTimeStampCountInto( startStamp ); \
484 #define Meas_endMutexUnlock \
485 saveLowTimeStampCountInto( endStamp ); \
486 addIntervalToHist( startStamp, endStamp, \
487 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
489 #define Meas_startCondWait \
490 int32 startStamp, endStamp; \
491 saveLowTimeStampCountInto( startStamp ); \
493 #define Meas_endCondWait \
494 saveLowTimeStampCountInto( endStamp ); \
495 addIntervalToHist( startStamp, endStamp, \
496 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
498 #define Meas_startCondSignal \
499 int32 startStamp, endStamp; \
500 saveLowTimeStampCountInto( startStamp ); \
502 #define Meas_endCondSignal \
503 saveLowTimeStampCountInto( endStamp ); \
504 addIntervalToHist( startStamp, endStamp, \
505 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
507 //===========================================================================
508 // VCilk
509 #define Meas_startSpawn \
510 int32 startStamp, endStamp; \
511 saveLowTimeStampCountInto( startStamp ); \
513 #define Meas_endSpawn \
514 saveLowTimeStampCountInto( endStamp ); \
515 addIntervalToHist( startStamp, endStamp, \
516 _VMSMasterEnv->measHists[ spawnHistIdx ] );
518 #define Meas_startSync \
519 int32 startStamp, endStamp; \
520 saveLowTimeStampCountInto( startStamp ); \
522 #define Meas_endSync \
523 saveLowTimeStampCountInto( endStamp ); \
524 addIntervalToHist( startStamp, endStamp, \
525 _VMSMasterEnv->measHists[ syncHistIdx ] );
527 //===========================================================================
528 // SSR
529 #define Meas_startSendFromTo \
530 int32 startStamp, endStamp; \
531 saveLowTimeStampCountInto( startStamp ); \
533 #define Meas_endSendFromTo \
534 saveLowTimeStampCountInto( endStamp ); \
535 addIntervalToHist( startStamp, endStamp, \
536 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
538 #define Meas_startSendOfType \
539 int32 startStamp, endStamp; \
540 saveLowTimeStampCountInto( startStamp ); \
542 #define Meas_endSendOfType \
543 saveLowTimeStampCountInto( endStamp ); \
544 addIntervalToHist( startStamp, endStamp, \
545 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
547 #define Meas_startReceiveFromTo \
548 int32 startStamp, endStamp; \
549 saveLowTimeStampCountInto( startStamp ); \
551 #define Meas_endReceiveFromTo \
552 saveLowTimeStampCountInto( endStamp ); \
553 addIntervalToHist( startStamp, endStamp, \
554 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
556 #define Meas_startReceiveOfType \
557 int32 startStamp, endStamp; \
558 saveLowTimeStampCountInto( startStamp ); \
560 #define Meas_endReceiveOfType \
561 saveLowTimeStampCountInto( endStamp ); \
562 addIntervalToHist( startStamp, endStamp, \
563 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
565 //=====
567 #include "SwitchAnimators.h"
568 #include "probes.h"
569 #include "vutilities.h"
571 #endif /* _VMS_H */