view VMS.h @ 120:d4c881c7f03a

Added fn to send inter-master requests, and cleaned up code
author Me@portablequad
date Sat, 03 Sep 2011 20:41:51 -0700
parents efb55f1b5fb9
children 24466227d8bb
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 _InterMasterReqst InterMasterReqst;
117 typedef struct _IntervalProbe IntervalProbe;
118 typedef struct _GateStruc GateStruc;
121 typedef VirtProcr * (*SlaveScheduler) ( void *, int ); //semEnv, coreIdx
122 typedef void (*RequestHandler) ( VirtProcr *, void * ); //prWReqst, semEnv
123 typedef void (*VirtProcrFnPtr) ( void *, VirtProcr * ); //initData, animPr
124 typedef void VirtProcrFn ( void *, VirtProcr * ); //initData, animPr
125 typedef void (*ResumePrFnPtr) ( VirtProcr *, void * );
128 //============= Requests ===========
129 //
131 //VMS Request is the carrier for Slave to Master requests
132 // it has an embedded sub-type request that is pulled out
133 // inside the plugin's request handler
134 enum VMSReqstType //For Slave->Master requests
135 {
136 semantic = 1, //avoid starting enums at 0, for debug reasons
137 createReq,
138 dissipate,
139 VMSSemantic //goes with VMSSemReqst below
140 };
142 struct _VMSReqst
143 {
144 enum VMSReqstType reqType;//used for dissipate and in future for IO requests
145 void *semReqData;
147 VMSReqst *nextReqst;
148 };
149 //VMSReqst
151 //This is a sub-type of Slave->Master requests.
152 // It's for Slaves to invoke built-in VMS-core functions that have language-like
153 // behavior.
154 enum VMSSemReqstType //These are equivalent to semantic requests, but for
155 { // VMS's services available directly to app, like OS
156 createProbe = 1, // and probe services -- like a VMS-wide built-in lang
157 openFile,
158 otherIO
159 };
161 typedef struct
162 { enum VMSSemReqstType reqType;
163 VirtProcr *requestingPr;
164 char *nameStr; //for create probe
165 }
166 VMSSemReq;
168 //These are for Master to Master requests
169 // They get re-cast to the appropriate sub-type of request
170 enum InterMasterReqstType //For Master->Master
171 {
172 destVMSCore = 1, //avoid starting enums at 0, for debug reasons
173 destPlugin
174 };
176 struct _InterMasterReqst //Doing a trick to save space & time -- allocate
177 { // space for a sub-type then cast first as InterMaster then as sub-type
178 enum InterMasterReqstType reqType;
179 InterMasterReqst *nextReqst;
180 };
181 //InterMasterReqst (defined above in typedef block)
184 //These are a sub-type of InterMaster requests. The inter-master req gets
185 // re-cast to be of this type, after checking
186 //This ones for requests between internals of VMS-core.. such as malloc
187 enum InterVMSCoreReqType
188 {
189 transfer_free_ptr = 1 //avoid starting enums at 0, for debug reasons
190 };
192 typedef struct //Doing a trick to save space & time -- allocate space
193 { // for this, cast first as InterMaster then as this
194 enum InterMasterReqstType reqType; //duplicate InterMasterReqst at top
195 InterMasterReqst *nextReqst;
197 enum InterVMSCoreReqType secondReqType;
198 void *freePtr; //pile up fields, add as needed
199 }
200 InterVMSCoreReqst;
203 //This is for requests between plugins on different cores
204 // Here, after casting, the pluginReq is extracted and handed to plugin
205 typedef struct //Doing a trick to save space & time -- allocate space
206 { // for this, cast first as InterMaster then as this
207 enum InterMasterReqstType reqType; //copy InterMasterReqst at top
208 InterMasterReqst *nextReqst;
210 void *pluginReq; //plugin will cast to approp type
211 }
212 InterPluginReqst;
215 //==================== Core data structures ===================
217 struct _SchedSlot
218 {
219 int workIsDone;
220 int needsProcrAssigned;
221 VirtProcr *procrAssignedToSlot;
222 };
223 //SchedSlot
225 /*WARNING: re-arranging this data structure could cause VP switching
226 * assembly code to fail -- hard-codes offsets of fields
227 */
228 struct _VirtProcr
229 { int procrID; //for debugging -- count up each time create
230 int coreAnimatedBy;
231 void *startOfStack;
232 void *stackPtr;
233 void *framePtr;
234 void *nextInstrPt;
236 void *coreLoopStartPt; //allows proto-runtime to be linked later
237 void *coreLoopFramePtr; //restore before jmp back to core loop
238 void *coreLoopStackPtr; //restore before jmp back to core loop
240 void *initialData;
242 SchedSlot *schedSlot;
243 VMSReqst *requests;
245 void *semanticData; //this livesUSE_GNU here for the life of VP
246 void *dataRetFromReq;//values returned from plugin to VP go here
248 //=========== MEASUREMENT STUFF ==========
249 #ifdef MEAS__TIME_STAMP_SUSP
250 unsigned int preSuspTSCLow;
251 unsigned int postSuspTSCLow;
252 #endif
253 #ifdef MEAS__TIME_MASTER /* in VirtProcr because multiple masterVPs*/
254 unsigned int startMasterTSCLow;USE_GNU
255 unsigned int endMasterTSCLow;
256 #endif
257 //========================================
259 float64 createPtInSecs; //have space but don't use on some configs
260 };
261 //VirtProcr
264 /*Master Env is the only global variable -- has entry points for any other
265 * data needed.
266 */
267 typedef struct
268 {
269 SlaveScheduler slaveScheduler;
270 RequestHandler requestHandler;
272 SchedSlot ***allSchedSlots;
273 VMSQueueStruc **readyToAnimateQs;
274 VirtProcr **masterVPs;
276 void *semanticEnv;
277 void *OSEventStruc; //for future, when add I/O to BLIS
278 MallocProlog *freeListHead;
279 int32 amtOfOutstandingMem; //total currently allocated
281 void *coreLoopReturnPt;//addr to jump to to re-enter coreLoop
283 int32 setupComplete;
284 volatile int32 masterLock;
286 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
287 GateStruc *workStealingGates[ NUM_CORES ]; //concurrent work-steal
288 int32 workStealingLock;
290 int32 numProcrsCreated; //gives ordering to processor creation
292 //=========== MEASUREMENT STUFF =============
293 IntervalProbe **intervalProbes;
294 PrivDynArrayInfo *dynIntervalProbesInfo;
295 HashTable *probeNameHashTbl;
296 int32 masterCreateProbeID;
297 float64 createPtInSecs;
298 Histogram **measHists;
299 PrivDynArrayInfo *measHistsInfo;
300 #ifdef MEAS__TIME_PLUGIN
301 Histogram *reqHdlrLowTimeHist;
302 Histogram *reqHdlrHighTimeHist;
303 #endif
304 #ifdef MEAS__TIME_MALLOC
305 Histogram *mallocTimeHist;
306 Histogram *freeTimeHist;
307 #endif
308 #ifdef MEAS__TIME_MASTER_LOCK
309 Histogram *masterLockLowTimeHist;
310 Histogram *masterLockHighTimeHist;
311 #endif
312 }
313 MasterEnv;
315 //========================= Extra Stuff Data Strucs =======================
316 typedef struct
317 {
319 }
320 VMSExcp;
322 struct _GateStruc
323 {
324 int32 gateClosed;
325 int32 preGateProgress;
326 int32 waitProgress;
327 int32 exitProgress;
328 };
329 //GateStruc
331 //======================= OS Thread related ===============================
333 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
334 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
335 void masterLoop( void *initData, VirtProcr *masterPr );
338 typedef struct
339 {
340 void *endThdPt;
341 unsigned int coreNum;
342 }
343 ThdParams;
345 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
346 ThdParams *coreLoopThdParams [ NUM_CORES ];
347 pthread_mutex_t suspendLock;
348 pthread_cond_t suspend_cond;
352 //===================== Global Vars ===================
354 volatile MasterEnv *_VMSMasterEnv;
359 //=========================== Function Prototypes =========================
362 //========== Setup and shutdown ==========
363 void
364 VMS__init();
366 void
367 VMS__init_Seq();
369 void
370 VMS__start_the_work_then_wait_until_done();
372 void
373 VMS__start_the_work_then_wait_until_done_Seq();
375 inline VirtProcr *
376 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
378 void
379 VMS__dissipate_procr( VirtProcr *procrToDissipate );
381 //Use this to create processor inside entry point & other places outside
382 // the VMS system boundary (IE, not run in slave nor Master)
383 VirtProcr *
384 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
386 void
387 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
389 void
390 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
392 void
393 VMS__shutdown();
395 void
396 VMS__cleanup_at_end_of_shutdown();
398 void *
399 VMS__give_sem_env_for( VirtProcr *animPr );
402 //============== Request Related ===============
404 void
405 VMS__suspend_procr( VirtProcr *callingPr );
407 inline void
408 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
410 inline void
411 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
413 void
414 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
416 void inline
417 VMS__send_dissipate_req( VirtProcr *prToDissipate );
419 inline void
420 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
423 void inline
424 VMS__send_inter_plugin_req( void *reqData, int32 targetMaster,
425 VirtProcr *requestingMaster );
426 void inline
427 VMS__send_inter_VMSCore_req( InterVMSCoreReqst *reqData, int32 targetMaster,
428 VirtProcr *requestingMaster );
430 VMSReqst *
431 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
433 inline void *
434 VMS__take_sem_reqst_from( VMSReqst *req );
436 void inline
437 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
438 ResumePrFnPtr resumePrFnPtr );
440 //======================== STATS ======================
442 //===== RDTSC wrapper ===== //Also runs with x86_64 code
444 #define saveTimeStampCountInto(low, high) \
445 asm volatile("RDTSC; \
446 movl %%eax, %0; \
447 movl %%edx, %1;" \
448 /* outputs */ : "=m" (low), "=m" (high)\
449 /* inputs */ : \
450 /* clobber */ : "%eax", "%edx" \
451 );
453 #define saveLowTimeStampCountInto(low) \
454 asm volatile("RDTSC; \
455 movl %%eax, %0;" \
456 /* outputs */ : "=m" (low) \
457 /* inputs */ : \
458 /* clobber */ : "%eax", "%edx" \
459 );
461 //====================
462 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
463 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
464 _VMSMasterEnv->measHists[idx] = \
465 makeFixedBinHist( numBins, startVal, binWidth, name );
468 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
470 #ifdef VPTHREAD
472 //VPThread
473 #define createHistIdx 1
474 #define mutexLockHistIdx 2
475 #define mutexUnlockHistIdx 3
476 #define condWaitHistIdx 4
477 #define condSignalHistIdx 5
479 #define MakeTheMeasHists() \
480 _VMSMasterEnv->measHistsInfo = \
481 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
482 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
483 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
484 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
485 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
486 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
488 #endif
491 #ifdef VCILK
493 //VCilk
494 #define spawnHistIdx 1
495 #define syncHistIdx 2
497 #define MakeTheMeasHists() \
498 _VMSMasterEnv->measHistsInfo = \
499 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
500 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
501 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
504 #endif
506 #ifdef SSR
508 //SSR
509 #define SendFromToHistIdx 1
510 #define SendOfTypeHistIdx 2
511 #define ReceiveFromToHistIdx 3
512 #define ReceiveOfTypeHistIdx 4
514 #define MakeTheMeasHists() \
515 _VMSMasterEnv->measHistsInfo = \
516 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
517 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
518 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
519 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
520 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
522 #endif
524 //===========================================================================
525 //VPThread
528 #define Meas_startCreate \
529 int32 startStamp, endStamp; \
530 saveLowTimeStampCountInto( startStamp ); \
532 #define Meas_endCreate \
533 saveLowTimeStampCountInto( endStamp ); \
534 addIntervalToHist( startStamp, endStamp, \
535 _VMSMasterEnv->measHists[ createHistIdx ] );
537 #define Meas_startMutexLock \
538 int32 startStamp, endStamp; \
539 saveLowTimeStampCountInto( startStamp ); \
541 #define Meas_endMutexLock \
542 saveLowTimeStampCountInto( endStamp ); \
543 addIntervalToHist( startStamp, endStamp, \
544 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
546 #define Meas_startMutexUnlock \
547 int32 startStamp, endStamp; \
548 saveLowTimeStampCountInto( startStamp ); \
550 #define Meas_endMutexUnlock \
551 saveLowTimeStampCountInto( endStamp ); \
552 addIntervalToHist( startStamp, endStamp, \
553 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
555 #define Meas_startCondWait \
556 int32 startStamp, endStamp; \
557 saveLowTimeStampCountInto( startStamp ); \
559 #define Meas_endCondWait \
560 saveLowTimeStampCountInto( endStamp ); \
561 addIntervalToHist( startStamp, endStamp, \
562 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
564 #define Meas_startCondSignal \
565 int32 startStamp, endStamp; \
566 saveLowTimeStampCountInto( startStamp ); \
568 #define Meas_endCondSignal \
569 saveLowTimeStampCountInto( endStamp ); \
570 addIntervalToHist( startStamp, endStamp, \
571 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
573 //===========================================================================
574 // VCilk
575 #define Meas_startSpawn \
576 int32 startStamp, endStamp; \
577 saveLowTimeStampCountInto( startStamp ); \
579 #define Meas_endSpawn \
580 saveLowTimeStampCountInto( endStamp ); \
581 addIntervalToHist( startStamp, endStamp, \
582 _VMSMasterEnv->measHists[ spawnHistIdx ] );
584 #define Meas_startSync \
585 int32 startStamp, endStamp; \
586 saveLowTimeStampCountInto( startStamp ); \
588 #define Meas_endSync \
589 saveLowTimeStampCountInto( endStamp ); \
590 addIntervalToHist( startStamp, endStamp, \
591 _VMSMasterEnv->measHists[ syncHistIdx ] );
593 //===========================================================================
594 // SSR
595 #define Meas_startSendFromTo \
596 int32 startStamp, endStamp; \
597 saveLowTimeStampCountInto( startStamp ); \
599 #define Meas_endSendFromTo \
600 saveLowTimeStampCountInto( endStamp ); \
601 addIntervalToHist( startStamp, endStamp, \
602 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
604 #define Meas_startSendOfType \
605 int32 startStamp, endStamp; \
606 saveLowTimeStampCountInto( startStamp ); \
608 #define Meas_endSendOfType \
609 saveLowTimeStampCountInto( endStamp ); \
610 addIntervalToHist( startStamp, endStamp, \
611 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
613 #define Meas_startReceiveFromTo \
614 int32 startStamp, endStamp; \
615 saveLowTimeStampCountInto( startStamp ); \
617 #define Meas_endReceiveFromTo \
618 saveLowTimeStampCountInto( endStamp ); \
619 addIntervalToHist( startStamp, endStamp, \
620 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
622 #define Meas_startReceiveOfType \
623 int32 startStamp, endStamp; \
624 saveLowTimeStampCountInto( startStamp ); \
626 #define Meas_endReceiveOfType \
627 saveLowTimeStampCountInto( endStamp ); \
628 addIntervalToHist( startStamp, endStamp, \
629 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
631 //=====
633 #include "ProcrContext.h"
634 #include "probes.h"
635 #include "vutilities.h"
637 #endif /* _VMS_H */