Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view VMS.h @ 132:dbfc8382d546
distributed memory allocation interface - unfinished
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Fri, 16 Sep 2011 14:25:49 +0200 |
| parents | 24466227d8bb |
| children | a9b72021f053 |
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 */
8 #ifndef _VMS_H
9 #define _VMS_H
10 #define _GNU_SOURCE
12 #include "VMS_primitive_data_types.h"
13 #include "Queue_impl/PrivateQueue.h"
14 #include "Histogram/Histogram.h"
15 #include "DynArray/DynArray.h"
16 #include "Hash_impl/PrivateHash.h"
17 #include "vmalloc.h"
19 #include <pthread.h>
20 #include <sys/time.h>
23 //=============================== Debug ===================================
24 //
25 //When SEQUENTIAL is defined, VMS does sequential exe in the main thread
26 // It still does co-routines and all the mechanisms are the same, it just
27 // has only a single thread and animates VPs one at a time
28 //#define SEQUENTIAL
30 //#define USE_WORK_STEALING
32 //turns on the probe-instrumentation in the application -- when not
33 // defined, the calls to the probe functions turn into comments
34 #define STATS__ENABLE_PROBES
35 //#define TURN_ON_DEBUG_PROBES
37 //These defines turn types of bug messages on and off
38 // be sure debug messages are un-commented (next block of defines)
39 #define dbgAppFlow TRUE /* Top level flow of application code -- general*/
40 #define dbgProbes FALSE /* for issues inside probes themselves*/
41 #define dbgB2BMaster FALSE /* in coreloop, back to back master VPs*/
42 #define dbgRqstHdlr FALSE /* in request handler code*/
44 //Comment or un- the substitute half to turn on/off types of debug message
45 #define DEBUG( bool, msg) \
46 // if( bool){ printf(msg); fflush(stdin);}
47 #define DEBUG1( bool, msg, param) \
48 // if(bool){printf(msg, param); fflush(stdin);}
49 #define DEBUG2( bool, msg, p1, p2) \
50 // if(bool) {printf(msg, p1, p2); fflush(stdin);}
52 #define ERROR(msg) printf(msg);
53 #define ERROR1(msg, param) printf(msg, param);
54 #define ERROR2(msg, p1, p2) printf(msg, p1, p2);
56 //=========================== STATS =======================
58 //when MEAS__TIME_STAMP_SUSP is defined, causes code to be inserted and
59 // compiled-in that saves the low part of the time stamp count just before
60 // 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
61 // saved into a field added to VirtProcr. Have to sanity-check for
62 // rollover of low portion into high portion.
63 //#define MEAS__TIME_STAMP_SUSP
64 //#define MEAS__TIME_MASTER
65 #define MEAS__TIME_PLUGIN
66 #define MEAS__TIME_MALLOC
67 //#define MEAS__TIME_MASTER_LOCK
68 #define MEAS__NUM_TIMES_TO_RUN 100000
70 //For code that calculates normalization-offset between TSC counts of
71 // different cores.
72 #define NUM_TSC_ROUND_TRIPS 10
75 //========================= Hardware related Constants =====================
76 //This value is the number of hardware threads in the shared memory
77 // machine
78 #define NUM_CORES 8
80 // tradeoff amortizing master fixed overhead vs imbalance potential
81 // when work-stealing, can make bigger, at risk of losing cache affinity
82 #define NUM_SCHED_SLOTS 5
84 #define MIN_WORK_UNIT_CYCLES 20000
86 #define MASTERLOCK_RETRIES 10000
88 // stack size in virtual processors created
89 #define VIRT_PROCR_STACK_SIZE 0x8000 /* 32K */
91 // memory for VMS__malloc
92 #define MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE 0x10000000 /* 256M */
94 #define CACHE_LINE 64
95 #define PAGE_SIZE 4096
98 //==============================
100 #define SUCCESS 0
102 #define writeVMSQ writePrivQ
103 #define readVMSQ readPrivQ
104 #define makeVMSQ makeVMSPrivQ
105 #define numInVMSQ numInPrivQ
106 #define VMSQueueStruc PrivQueueStruc
110 //===========================================================================
111 typedef unsigned long long TSCount;
113 typedef struct _VMSReqst VMSReqst;
114 typedef struct _InterMasterReqst InterMasterReqst;
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 //VMS Request is the carrier for Slave to Master requests
130 // it has an embedded sub-type request that is pulled out
131 // inside the plugin's request handler
132 enum VMSReqstType //For Slave->Master requests
133 {
134 semantic = 1, //avoid starting enums at 0, for debug reasons
135 createReq,
136 dissipate,
137 VMSSemantic //goes with VMSSemReqst below
138 };
140 struct _VMSReqst
141 {
142 enum VMSReqstType reqType;//used for dissipate and in future for IO requests
143 void *semReqData;
145 VMSReqst *nextReqst;
146 };
147 //VMSReqst
149 //This is a sub-type of Slave->Master requests.
150 // It's for Slaves to invoke built-in VMS-core functions that have language-like
151 // behavior.
152 enum VMSSemReqstType //These are equivalent to semantic requests, but for
153 { // VMS's services available directly to app, like OS
154 createProbe = 1, // and probe services -- like a VMS-wide built-in lang
155 openFile,
156 otherIO
157 };
159 typedef struct
160 { enum VMSSemReqstType reqType;
161 VirtProcr *requestingPr;
162 char *nameStr; //for create probe
163 }
164 VMSSemReq;
166 //These are for Master to Master requests
167 // They get re-cast to the appropriate sub-type of request
168 enum InterMasterReqstType //For Master->Master
169 {
170 destVMSCore = 1, //avoid starting enums at 0, for debug reasons
171 destPlugin
172 };
174 struct _InterMasterReqst //Doing a trick to save space & time -- allocate
175 { // space for a sub-type then cast first as InterMaster then as sub-type
176 enum InterMasterReqstType reqType;
177 InterMasterReqst *nextReqst;
178 };
179 //InterMasterReqst (defined above in typedef block)
182 //These are a sub-type of InterMaster requests. The inter-master req gets
183 // re-cast to be of this type, after checking
184 //This ones for requests between internals of VMS-core.. such as malloc
185 enum InterVMSCoreReqType
186 {
187 transfer_free_ptr = 1 //avoid starting enums at 0, for debug reasons
188 };
190 //Doing a trick to save space & time -- allocate space
191 // for this, cast first as InterMaster then as this
192 typedef struct
193 {
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 } InterVMSCoreReqst;
201 //This is for requests between plugins on different cores
202 // Here, after casting, the pluginReq is extracted and handed to plugin
203 //Doing a trick to save space & time -- allocate space
204 // for this, cast first as InterMaster then as this
205 typedef struct
206 {
207 enum InterMasterReqstType reqType; //copy InterMasterReqst at top
208 InterMasterReqst *nextReqst;
210 void *pluginReq; //plugin will cast to approp type
211 } InterPluginReqst;
213 //==================== Core data structures ===================
215 /*Master Env is the only global variable -- has entry points for any other
216 * data needed.
217 */
218 typedef struct
219 {
220 SlaveScheduler slaveScheduler;
221 RequestHandler requestHandler;
223 SchedSlot ***allSchedSlots;
224 VMSQueueStruc **readyToAnimateQs;
225 VirtProcr **masterVPs;
227 void *semanticEnv;
228 void *OSEventStruc; //for future, when add I/O to BLIS
230 void *coreLoopReturnPt;//addr to jump to to re-enter coreLoop
232 int32 setupComplete;
233 volatile int32 masterLock;
235 MallocProlog *freeListHead[NUM_CORES];
236 int32 amtOfOutstandingMem; //total currently allocated
238 int32 numMasterInARow[NUM_CORES];//detect back-to-back masterVP
239 GateStruc *workStealingGates[NUM_CORES]; //concurrent work-steal
240 int32 workStealingLock;
242 InterMasterReqst* interMasterRequestsFor[NUM_CORES];
243 RequestHandler interPluginReqHdlr;
245 int32 numProcrsCreated; //gives ordering to processor creation
247 int32 currentMasterProcrID;
249 //=========== MEASUREMENT STUFF =============
250 IntervalProbe **intervalProbes;
251 PrivDynArrayInfo *dynIntervalProbesInfo;
252 HashTable *probeNameHashTbl;
253 int32 masterCreateProbeID;
254 float64 createPtInSecs;
255 Histogram **measHists;
256 PrivDynArrayInfo *measHistsInfo;
257 #ifdef MEAS__TIME_PLUGIN
258 Histogram *reqHdlrLowTimeHist;
259 Histogram *reqHdlrHighTimeHist;
260 #endif
261 #ifdef MEAS__TIME_MALLOC
262 Histogram *mallocTimeHist;
263 Histogram *freeTimeHist;
264 #endif
265 #ifdef MEAS__TIME_MASTER_LOCK
266 Histogram *masterLockLowTimeHist;
267 Histogram *masterLockHighTimeHist;
268 #endif
269 }
270 MasterEnv;
272 //========================= Extra Stuff Data Strucs =======================
273 typedef struct
274 {
276 }
277 VMSExcp;
279 struct _GateStruc
280 {
281 int32 gateClosed;
282 int32 preGateProgress;
283 int32 waitProgress;
284 int32 exitProgress;
285 };
286 //GateStruc
288 //======================= OS Thread related ===============================
290 void * coreLoop( void *paramsIn ); //standard PThreads fn prototype
291 void * coreLoop_Seq( void *paramsIn ); //standard PThreads fn prototype
292 void masterLoop( void *initData, VirtProcr *masterPr );
295 typedef struct
296 {
297 void *endThdPt;
298 unsigned int coreNum;
299 }
300 ThdParams;
302 pthread_t coreLoopThdHandles[ NUM_CORES ]; //pthread's virt-procr state
303 ThdParams *coreLoopThdParams [ NUM_CORES ];
304 pthread_mutex_t suspendLock;
305 pthread_cond_t suspend_cond;
309 //===================== Global Vars ===================
311 volatile MasterEnv *_VMSMasterEnv;
316 //=========================== Function Prototypes =========================
319 //========== Setup and shutdown ==========
320 void
321 VMS__init();
323 void
324 VMS__init_Seq();
326 void
327 VMS__start_the_work_then_wait_until_done();
329 void
330 VMS__start_the_work_then_wait_until_done_Seq();
332 inline VirtProcr *
333 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
335 void
336 VMS__dissipate_procr( VirtProcr *procrToDissipate );
338 //Use this to create processor inside entry point & other places outside
339 // the VMS system boundary (IE, not run in slave nor Master)
340 VirtProcr *
341 VMS_ext__create_procr( VirtProcrFnPtr fnPtr, void *initialData );
343 void
344 VMS_ext__dissipate_procr( VirtProcr *procrToDissipate );
346 void
347 VMS__throw_exception( char *msgStr, VirtProcr *reqstPr, VMSExcp *excpData );
349 void
350 VMS__shutdown();
352 void
353 VMS__cleanup_at_end_of_shutdown();
355 void *
356 VMS__give_sem_env_for( VirtProcr *animPr );
359 //============== Request Related ===============
361 void
362 VMS__suspend_procr( VirtProcr *callingPr );
364 inline void
365 VMS__add_sem_request_in_mallocd_VMSReqst( void *semReqData, VirtProcr *callingPr );
367 inline void
368 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr );
370 void
371 VMS__send_create_procr_req( void *semReqData, VirtProcr *reqstingPr );
373 void inline
374 VMS__send_dissipate_req( VirtProcr *prToDissipate );
376 inline void
377 VMS__send_VMSSem_request( void *semReqData, VirtProcr *callingPr );
380 void inline
381 VMS__send_inter_plugin_req( void *reqData, int32 targetMaster,
382 VirtProcr *requestingMaster );
383 void inline
384 VMS__send_inter_VMSCore_req( InterVMSCoreReqst *reqData, int32 targetMaster,
385 VirtProcr *requestingMaster );
387 VMSReqst *
388 VMS__take_next_request_out_of( VirtProcr *procrWithReq );
390 inline void *
391 VMS__take_sem_reqst_from( VMSReqst *req );
393 void inline
394 VMS__handle_VMSSemReq( VMSReqst *req, VirtProcr *requestingPr, void *semEnv,
395 ResumePrFnPtr resumePrFnPtr );
397 //======================== STATS ======================
399 //===== RDTSC wrapper ===== //Also runs with x86_64 code
401 #define saveTimeStampCountInto(low, high) \
402 asm volatile("RDTSC; \
403 movl %%eax, %0; \
404 movl %%edx, %1;" \
405 /* outputs */ : "=m" (low), "=m" (high)\
406 /* inputs */ : \
407 /* clobber */ : "%eax", "%edx" \
408 );
410 #define saveLowTimeStampCountInto(low) \
411 asm volatile("RDTSC; \
412 movl %%eax, %0;" \
413 /* outputs */ : "=m" (low) \
414 /* inputs */ : \
415 /* clobber */ : "%eax", "%edx" \
416 );
418 //====================
419 #define makeAMeasHist( idx, name, numBins, startVal, binWidth ) \
420 makeHighestDynArrayIndexBeAtLeast( _VMSMasterEnv->measHistsInfo, idx ); \
421 _VMSMasterEnv->measHists[idx] = \
422 makeFixedBinHist( numBins, startVal, binWidth, name );
425 #define MEAS__SUB_CREATE /*turn on/off subtraction of create from plugin*/
427 #ifdef VPTHREAD
429 //VPThread
430 #define createHistIdx 1
431 #define mutexLockHistIdx 2
432 #define mutexUnlockHistIdx 3
433 #define condWaitHistIdx 4
434 #define condSignalHistIdx 5
436 #define MakeTheMeasHists() \
437 _VMSMasterEnv->measHistsInfo = \
438 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
439 makeAMeasHist( createHistIdx, "create", 250, 0, 100 ) \
440 makeAMeasHist( mutexLockHistIdx, "mutex_lock", 50, 0, 100 ) \
441 makeAMeasHist( mutexUnlockHistIdx, "mutex_unlock", 50, 0, 100 ) \
442 makeAMeasHist( condWaitHistIdx, "cond_wait", 50, 0, 100 ) \
443 makeAMeasHist( condSignalHistIdx, "cond_signal", 50, 0, 100 )
445 #endif
448 #ifdef VCILK
450 //VCilk
451 #define spawnHistIdx 1
452 #define syncHistIdx 2
454 #define MakeTheMeasHists() \
455 _VMSMasterEnv->measHistsInfo = \
456 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
457 makeAMeasHist( spawnHistIdx, "Spawn", 50, 0, 200 ) \
458 makeAMeasHist( syncHistIdx, "Sync", 50, 0, 200 )
461 #endif
463 #ifdef SSR
465 //SSR
466 #define SendFromToHistIdx 1
467 #define SendOfTypeHistIdx 2
468 #define ReceiveFromToHistIdx 3
469 #define ReceiveOfTypeHistIdx 4
471 #define MakeTheMeasHists() \
472 _VMSMasterEnv->measHistsInfo = \
473 makePrivDynArrayOfSize( (void***)&(_VMSMasterEnv->measHists), 200); \
474 makeAMeasHist( SendFromToHistIdx, "SendFromTo", 50, 0, 100 ) \
475 makeAMeasHist( SendOfTypeHistIdx, "SendOfType", 50, 0, 100 ) \
476 makeAMeasHist( ReceiveFromToHistIdx,"ReceiveFromTo", 50, 0, 100 ) \
477 makeAMeasHist( ReceiveOfTypeHistIdx,"ReceiveOfType", 50, 0, 100 )
479 #endif
481 //===========================================================================
482 //VPThread
485 #define Meas_startCreate \
486 int32 startStamp, endStamp; \
487 saveLowTimeStampCountInto( startStamp ); \
489 #define Meas_endCreate \
490 saveLowTimeStampCountInto( endStamp ); \
491 addIntervalToHist( startStamp, endStamp, \
492 _VMSMasterEnv->measHists[ createHistIdx ] );
494 #define Meas_startMutexLock \
495 int32 startStamp, endStamp; \
496 saveLowTimeStampCountInto( startStamp ); \
498 #define Meas_endMutexLock \
499 saveLowTimeStampCountInto( endStamp ); \
500 addIntervalToHist( startStamp, endStamp, \
501 _VMSMasterEnv->measHists[ mutexLockHistIdx ] );
503 #define Meas_startMutexUnlock \
504 int32 startStamp, endStamp; \
505 saveLowTimeStampCountInto( startStamp ); \
507 #define Meas_endMutexUnlock \
508 saveLowTimeStampCountInto( endStamp ); \
509 addIntervalToHist( startStamp, endStamp, \
510 _VMSMasterEnv->measHists[ mutexUnlockHistIdx ] );
512 #define Meas_startCondWait \
513 int32 startStamp, endStamp; \
514 saveLowTimeStampCountInto( startStamp ); \
516 #define Meas_endCondWait \
517 saveLowTimeStampCountInto( endStamp ); \
518 addIntervalToHist( startStamp, endStamp, \
519 _VMSMasterEnv->measHists[ condWaitHistIdx ] );
521 #define Meas_startCondSignal \
522 int32 startStamp, endStamp; \
523 saveLowTimeStampCountInto( startStamp ); \
525 #define Meas_endCondSignal \
526 saveLowTimeStampCountInto( endStamp ); \
527 addIntervalToHist( startStamp, endStamp, \
528 _VMSMasterEnv->measHists[ condSignalHistIdx ] );
530 //===========================================================================
531 // VCilk
532 #define Meas_startSpawn \
533 int32 startStamp, endStamp; \
534 saveLowTimeStampCountInto( startStamp ); \
536 #define Meas_endSpawn \
537 saveLowTimeStampCountInto( endStamp ); \
538 addIntervalToHist( startStamp, endStamp, \
539 _VMSMasterEnv->measHists[ spawnHistIdx ] );
541 #define Meas_startSync \
542 int32 startStamp, endStamp; \
543 saveLowTimeStampCountInto( startStamp ); \
545 #define Meas_endSync \
546 saveLowTimeStampCountInto( endStamp ); \
547 addIntervalToHist( startStamp, endStamp, \
548 _VMSMasterEnv->measHists[ syncHistIdx ] );
550 //===========================================================================
551 // SSR
552 #define Meas_startSendFromTo \
553 int32 startStamp, endStamp; \
554 saveLowTimeStampCountInto( startStamp ); \
556 #define Meas_endSendFromTo \
557 saveLowTimeStampCountInto( endStamp ); \
558 addIntervalToHist( startStamp, endStamp, \
559 _VMSMasterEnv->measHists[ SendFromToHistIdx ] );
561 #define Meas_startSendOfType \
562 int32 startStamp, endStamp; \
563 saveLowTimeStampCountInto( startStamp ); \
565 #define Meas_endSendOfType \
566 saveLowTimeStampCountInto( endStamp ); \
567 addIntervalToHist( startStamp, endStamp, \
568 _VMSMasterEnv->measHists[ SendOfTypeHistIdx ] );
570 #define Meas_startReceiveFromTo \
571 int32 startStamp, endStamp; \
572 saveLowTimeStampCountInto( startStamp ); \
574 #define Meas_endReceiveFromTo \
575 saveLowTimeStampCountInto( endStamp ); \
576 addIntervalToHist( startStamp, endStamp, \
577 _VMSMasterEnv->measHists[ ReceiveFromToHistIdx ] );
579 #define Meas_startReceiveOfType \
580 int32 startStamp, endStamp; \
581 saveLowTimeStampCountInto( startStamp ); \
583 #define Meas_endReceiveOfType \
584 saveLowTimeStampCountInto( endStamp ); \
585 addIntervalToHist( startStamp, endStamp, \
586 _VMSMasterEnv->measHists[ReceiveOfTypeHistIdx ] );
588 //=====
590 #include "ProcrContext.h"
591 #include "probes.h"
592 #include "vutilities.h"
594 #endif /* _VMS_H */
