view VMS.h @ 127:24466227d8bb

bugs fixed, code is now compiling
author Merten Sach <msach@mailbox.tu-berlin.de>
date Wed, 07 Sep 2011 17:45:05 +0200
parents d4c881c7f03a
children dbfc8382d546
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 0x8000 /* 32K */
92 // memory for VMS__malloc
93 #define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
95 #define CACHE_LINE 64
96 #define PAGE_SIZE 4096
99 //==============================
101 #define SUCCESS 0
103 #define writeVMSQ writePrivQ
104 #define readVMSQ readPrivQ
105 #define makeVMSQ makeVMSPrivQ
106 #define numInVMSQ numInPrivQ
107 #define VMSQueueStruc PrivQueueStruc
111 //===========================================================================
112 typedef unsigned long long TSCount;
114 typedef struct _SchedSlot SchedSlot;
115 typedef struct _VMSReqst VMSReqst;
116 typedef struct _VirtProcr VirtProcr;
117 typedef struct _InterMasterReqst InterMasterReqst;
118 typedef struct _IntervalProbe IntervalProbe;
119 typedef struct _GateStruc GateStruc;
122 typedef VirtProcr * (*SlaveScheduler) ( void *, int ); //semEnv, coreIdx
123 typedef void (*RequestHandler) ( VirtProcr *, void * ); //prWReqst, semEnv
124 typedef void (*VirtProcrFnPtr) ( void *, VirtProcr * ); //initData, animPr
125 typedef void VirtProcrFn ( void *, VirtProcr * ); //initData, animPr
126 typedef void (*ResumePrFnPtr) ( VirtProcr *, void * );
129 //============= Requests ===========
130 //
132 //VMS Request is the carrier for Slave to Master requests
133 // it has an embedded sub-type request that is pulled out
134 // inside the plugin's request handler
135 enum VMSReqstType //For Slave->Master requests
136 {
137 semantic = 1, //avoid starting enums at 0, for debug reasons
138 createReq,
139 dissipate,
140 VMSSemantic //goes with VMSSemReqst below
141 };
143 struct _VMSReqst
144 {
145 enum VMSReqstType reqType;//used for dissipate and in future for IO requests
146 void *semReqData;
148 VMSReqst *nextReqst;
149 };
150 //VMSReqst
152 //This is a sub-type of Slave->Master requests.
153 // It's for Slaves to invoke built-in VMS-core functions that have language-like
154 // behavior.
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;
169 //These are for Master to Master requests
170 // They get re-cast to the appropriate sub-type of request
171 enum InterMasterReqstType //For Master->Master
172 {
173 destVMSCore = 1, //avoid starting enums at 0, for debug reasons
174 destPlugin
175 };
177 struct _InterMasterReqst //Doing a trick to save space & time -- allocate
178 { // space for a sub-type then cast first as InterMaster then as sub-type
179 enum InterMasterReqstType reqType;
180 InterMasterReqst *nextReqst;
181 };
182 //InterMasterReqst (defined above in typedef block)
185 //These are a sub-type of InterMaster requests. The inter-master req gets
186 // re-cast to be of this type, after checking
187 //This ones for requests between internals of VMS-core.. such as malloc
188 enum InterVMSCoreReqType
189 {
190 transfer_free_ptr = 1 //avoid starting enums at 0, for debug reasons
191 };
193 //Doing a trick to save space & time -- allocate space
194 // for this, cast first as InterMaster then as this
195 typedef struct
196 {
197 enum InterMasterReqstType reqType; //duplicate InterMasterReqst at top
198 InterMasterReqst *nextReqst;
200 enum InterVMSCoreReqType secondReqType;
201 void *freePtr; //pile up fields, add as needed
202 } InterVMSCoreReqst;
204 //This is for requests between plugins on different cores
205 // Here, after casting, the pluginReq is extracted and handed to plugin
206 //Doing a trick to save space & time -- allocate space
207 // for this, cast first as InterMaster then as this
208 typedef struct
209 {
210 enum InterMasterReqstType reqType; //copy InterMasterReqst at top
211 InterMasterReqst *nextReqst;
213 void *pluginReq; //plugin will cast to approp type
214 } InterPluginReqst;
216 //==================== Core data structures ===================
218 struct _SchedSlot
219 {
220 int workIsDone;
221 int needsProcrAssigned;
222 VirtProcr *procrAssignedToSlot;
223 };
224 //SchedSlot
226 /*WARNING: re-arranging this data structure could cause VP switching
227 * assembly code to fail -- hard-codes offsets of fields
228 */
229 struct _VirtProcr
230 { int procrID; //for debugging -- count up each time create
231 int coreAnimatedBy;
232 void *startOfStack;
233 void *stackPtr;
234 void *framePtr;
235 void *nextInstrPt;
237 void *coreLoopStartPt; //allows proto-runtime to be linked later
238 void *coreLoopFramePtr; //restore before jmp back to core loop
239 void *coreLoopStackPtr; //restore before jmp back to core loop
241 void *initialData;
243 SchedSlot *schedSlot;
244 VMSReqst *requests;
246 void *semanticData; //this livesUSE_GNU here for the life of VP
247 void *dataRetFromReq;//values returned from plugin to VP go here
249 //=========== MEASUREMENT STUFF ==========
250 #ifdef MEAS__TIME_STAMP_SUSP
251 unsigned int preSuspTSCLow;
252 unsigned int postSuspTSCLow;
253 #endif
254 #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
255 unsigned int startMasterTSCLow;USE_GNU
256 unsigned int endMasterTSCLow;
257 #endif
258 //========================================
260 float64 createPtInSecs; //have space but don't use on some configs
261 };
262 //VirtProcr
265 /*Master Env is the only global variable -- has entry points for any other
266 * data needed.
267 */
268 typedef struct
269 {
270 SlaveScheduler slaveScheduler;
271 RequestHandler requestHandler;
273 SchedSlot ***allSchedSlots;
274 VMSQueueStruc **readyToAnimateQs;
275 VirtProcr **masterVPs;
277 void *semanticEnv;
278 void *OSEventStruc; //for future, when add I/O to BLIS
279 MallocProlog *freeListHead;
280 int32 amtOfOutstandingMem; //total currently allocated
282 void *coreLoopReturnPt;//addr to jump to to re-enter coreLoop
284 int32 setupComplete;
285 volatile int32 masterLock;
287 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
288 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
289 int32 workStealingLock;
291 InterMasterReqst* interMasterRequestsFor[NUM_CORES];
292 RequestHandler interPluginReqHdlr;
294 int32 numProcrsCreated; //gives ordering to processor creation
296 //=========== MEASUREMENT STUFF =============
297 IntervalProbe **intervalProbes;
298 PrivDynArrayInfo *dynIntervalProbesInfo;
299 HashTable *probeNameHashTbl;
300 int32 masterCreateProbeID;
301 float64 createPtInSecs;
302 Histogram **measHists;
303 PrivDynArrayInfo *measHistsInfo;
304 #ifdef MEAS__TIME_PLUGIN
305 Histogram *reqHdlrLowTimeHist;
306 Histogram *reqHdlrHighTimeHist;
307 #endif
308 #ifdef MEAS__TIME_MALLOC
309 Histogram *mallocTimeHist;
310 Histogram *freeTimeHist;
311 #endif
312 #ifdef MEAS__TIME_MASTER_LOCK
313 Histogram *masterLockLowTimeHist;
314 Histogram *masterLockHighTimeHist;
315 #endif
316 }
317 MasterEnv;
319 //========================= Extra Stuff Data Strucs =======================
320 typedef struct
321 {
323 }
324 VMSExcp;
326 struct _GateStruc
327 {
328 int32 gateClosed;
329 int32 preGateProgress;
330 int32 waitProgress;
331 int32 exitProgress;
332 };
333 //GateStruc
335 //======================= OS Thread related ===============================
337 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
338 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
339 void masterLoop( void *initData, VirtProcr *masterPr );
342 typedef struct
343 {
344 void *endThdPt;
345 unsigned int coreNum;
346 }
347 ThdParams;
349 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
350 ThdParams *coreLoopThdParams [ NUM_CORES ];
351 pthread_mutex_t suspendLock;
352 pthread_cond_t suspend_cond;
356 //===================== Global Vars ===================
358 volatile MasterEnv *_VMSMasterEnv;
363 //=========================== Function Prototypes =========================
366 //========== Setup and shutdown ==========
367 void
368 VMS__init();
370 void
371 VMS__init_Seq();
373 void
374 VMS__start_the_work_then_wait_until_done();
376 void
377 VMS__start_the_work_then_wait_until_done_Seq();
379 inline VirtProcr *
380 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
382 void
383 VMS__dissipate_procr( VirtProcr *procrToDissipate );
385 //Use this to create processor inside entry point & other places outside
386 // the VMS system boundary (IE, not run in slave nor Master)
387 VirtProcr *
388 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
390 void
391 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
393 void
394 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
396 void
397 VMS__shutdown();
399 void
400 VMS__cleanup_at_end_of_shutdown();
402 void *
403 VMS__give_sem_env_for( VirtProcr *animPr );
406 //============== Request Related ===============
408 void
409 VMS__suspend_procr( VirtProcr *callingPr );
411 inline void
412 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
414 inline void
415 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
417 void
418 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
420 void inline
421 VMS__send_dissipate_req( VirtProcr *prToDissipate );
423 inline void
424 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
427 void inline
428 VMS__send_inter_plugin_req( void *reqData, int32 targetMaster,
429 VirtProcr *requestingMaster );
430 void inline
431 VMS__send_inter_VMSCore_req( InterVMSCoreReqst *reqData, int32 targetMaster,
432 VirtProcr *requestingMaster );
434 VMSReqst *
435 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
437 inline void *
438 VMS__take_sem_reqst_from( VMSReqst *req );
440 void inline
441 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
442 ResumePrFnPtr resumePrFnPtr );
444 //======================== STATS ======================
446 //===== RDTSC wrapper ===== //Also runs with x86_64 code
448 #define saveTimeStampCountInto(low, high) \
449 asm volatile("RDTSC; \
450 movl %%eax, %0; \
451 movl %%edx, %1;" \
452 /* outputs */ : "=m" (low), "=m" (high)\
453 /* inputs */ : \
454 /* clobber */ : "%eax", "%edx" \
455 );
457 #define saveLowTimeStampCountInto(low) \
458 asm volatile("RDTSC; \
459 movl %%eax, %0;" \
460 /* outputs */ : "=m" (low) \
461 /* inputs */ : \
462 /* clobber */ : "%eax", "%edx" \
463 );
465 //====================
466 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
467 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
468 _VMSMasterEnv->measHists[idx] = \
469 makeFixedBinHist( numBins, startVal, binWidth, name );
472 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
474 #ifdef VPTHREAD
476 //VPThread
477 #define createHistIdx 1
478 #define mutexLockHistIdx 2
479 #define mutexUnlockHistIdx 3
480 #define condWaitHistIdx 4
481 #define condSignalHistIdx 5
483 #define MakeTheMeasHists() \
484 _VMSMasterEnv->measHistsInfo = \
485 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
486 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
487 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
488 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
489 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
490 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
492 #endif
495 #ifdef VCILK
497 //VCilk
498 #define spawnHistIdx 1
499 #define syncHistIdx 2
501 #define MakeTheMeasHists() \
502 _VMSMasterEnv->measHistsInfo = \
503 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
504 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
505 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
508 #endif
510 #ifdef SSR
512 //SSR
513 #define SendFromToHistIdx 1
514 #define SendOfTypeHistIdx 2
515 #define ReceiveFromToHistIdx 3
516 #define ReceiveOfTypeHistIdx 4
518 #define MakeTheMeasHists() \
519 _VMSMasterEnv->measHistsInfo = \
520 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
521 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
522 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
523 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
524 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
526 #endif
528 //===========================================================================
529 //VPThread
532 #define Meas_startCreate \
533 int32 startStamp, endStamp; \
534 saveLowTimeStampCountInto( startStamp ); \
536 #define Meas_endCreate \
537 saveLowTimeStampCountInto( endStamp ); \
538 addIntervalToHist( startStamp, endStamp, \
539 _VMSMasterEnv->measHists[ createHistIdx ] );
541 #define Meas_startMutexLock \
542 int32 startStamp, endStamp; \
543 saveLowTimeStampCountInto( startStamp ); \
545 #define Meas_endMutexLock \
546 saveLowTimeStampCountInto( endStamp ); \
547 addIntervalToHist( startStamp, endStamp, \
548 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
550 #define Meas_startMutexUnlock \
551 int32 startStamp, endStamp; \
552 saveLowTimeStampCountInto( startStamp ); \
554 #define Meas_endMutexUnlock \
555 saveLowTimeStampCountInto( endStamp ); \
556 addIntervalToHist( startStamp, endStamp, \
557 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
559 #define Meas_startCondWait \
560 int32 startStamp, endStamp; \
561 saveLowTimeStampCountInto( startStamp ); \
563 #define Meas_endCondWait \
564 saveLowTimeStampCountInto( endStamp ); \
565 addIntervalToHist( startStamp, endStamp, \
566 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
568 #define Meas_startCondSignal \
569 int32 startStamp, endStamp; \
570 saveLowTimeStampCountInto( startStamp ); \
572 #define Meas_endCondSignal \
573 saveLowTimeStampCountInto( endStamp ); \
574 addIntervalToHist( startStamp, endStamp, \
575 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
577 //===========================================================================
578 // VCilk
579 #define Meas_startSpawn \
580 int32 startStamp, endStamp; \
581 saveLowTimeStampCountInto( startStamp ); \
583 #define Meas_endSpawn \
584 saveLowTimeStampCountInto( endStamp ); \
585 addIntervalToHist( startStamp, endStamp, \
586 _VMSMasterEnv->measHists[ spawnHistIdx ] );
588 #define Meas_startSync \
589 int32 startStamp, endStamp; \
590 saveLowTimeStampCountInto( startStamp ); \
592 #define Meas_endSync \
593 saveLowTimeStampCountInto( endStamp ); \
594 addIntervalToHist( startStamp, endStamp, \
595 _VMSMasterEnv->measHists[ syncHistIdx ] );
597 //===========================================================================
598 // SSR
599 #define Meas_startSendFromTo \
600 int32 startStamp, endStamp; \
601 saveLowTimeStampCountInto( startStamp ); \
603 #define Meas_endSendFromTo \
604 saveLowTimeStampCountInto( endStamp ); \
605 addIntervalToHist( startStamp, endStamp, \
606 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
608 #define Meas_startSendOfType \
609 int32 startStamp, endStamp; \
610 saveLowTimeStampCountInto( startStamp ); \
612 #define Meas_endSendOfType \
613 saveLowTimeStampCountInto( endStamp ); \
614 addIntervalToHist( startStamp, endStamp, \
615 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
617 #define Meas_startReceiveFromTo \
618 int32 startStamp, endStamp; \
619 saveLowTimeStampCountInto( startStamp ); \
621 #define Meas_endReceiveFromTo \
622 saveLowTimeStampCountInto( endStamp ); \
623 addIntervalToHist( startStamp, endStamp, \
624 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
626 #define Meas_startReceiveOfType \
627 int32 startStamp, endStamp; \
628 saveLowTimeStampCountInto( startStamp ); \
630 #define Meas_endReceiveOfType \
631 saveLowTimeStampCountInto( endStamp ); \
632 addIntervalToHist( startStamp, endStamp, \
633 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
635 //=====
637 #include "ProcrContext.h"
638 #include "probes.h"
639 #include "vutilities.h"
641 #endif /* _VMS_H */