| rev |
line source |
|
Me@0
|
1 /*
|
|
Me@38
|
2 * Copyright 2010 OpenSourceStewardshipFoundation
|
|
Me@0
|
3 *
|
|
Me@0
|
4 * Licensed under BSD
|
|
Me@0
|
5 */
|
|
Me@0
|
6
|
|
Me@0
|
7 #include <stdio.h>
|
|
Me@0
|
8 #include <stdlib.h>
|
|
Me@0
|
9 #include <malloc.h>
|
|
Me@0
|
10
|
|
Me@0
|
11 #include "VMS.h"
|
|
Me@0
|
12 #include "Queue_impl/BlockingQueue.h"
|
|
Me@38
|
13 #include "Histogram/Histogram.h"
|
|
Me@0
|
14
|
|
Me@0
|
15
|
|
Me@26
|
16 #define thdAttrs NULL
|
|
Me@26
|
17
|
|
Me@22
|
18 //===========================================================================
|
|
Me@22
|
19 void
|
|
Me@22
|
20 shutdownFn( void *dummy, VirtProcr *dummy2 );
|
|
Me@22
|
21
|
|
Me@31
|
22 SchedSlot **
|
|
Me@31
|
23 create_sched_slots();
|
|
Me@22
|
24
|
|
Me@28
|
25 void
|
|
Me@28
|
26 create_masterEnv();
|
|
Me@28
|
27
|
|
Me@28
|
28 void
|
|
Me@28
|
29 create_the_coreLoop_OS_threads();
|
|
Me@28
|
30
|
|
Me@26
|
31 pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
|
|
Me@26
|
32 pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
|
|
Me@26
|
33
|
|
Me@22
|
34 //===========================================================================
|
|
Me@22
|
35
|
|
Me@0
|
36 /*Setup has two phases:
|
|
Me@0
|
37 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
|
|
Me@8
|
38 * the master virt procr into the work-queue, ready for first "call"
|
|
Me@8
|
39 * 2) Semantic layer then does its own init, which creates the seed virt
|
|
Me@8
|
40 * procr inside the semantic layer, ready to schedule it when
|
|
Me@0
|
41 * asked by the first run of the masterLoop.
|
|
Me@0
|
42 *
|
|
Me@0
|
43 *This part is bit weird because VMS really wants to be "always there", and
|
|
Me@0
|
44 * have applications attach and detach.. for now, this VMS is part of
|
|
Me@0
|
45 * the app, so the VMS system starts up as part of running the app.
|
|
Me@0
|
46 *
|
|
Me@8
|
47 *The semantic layer is isolated from the VMS internals by making the
|
|
Me@8
|
48 * semantic layer do setup to a state that it's ready with its
|
|
Me@8
|
49 * initial virt procrs, ready to schedule them to slots when the masterLoop
|
|
Me@0
|
50 * asks. Without this pattern, the semantic layer's setup would
|
|
Me@8
|
51 * have to modify slots directly to assign the initial virt-procrs, and put
|
|
Me@31
|
52 * them into the readyToAnimateQ itself, breaking the isolation completely.
|
|
Me@0
|
53 *
|
|
Me@0
|
54 *
|
|
Me@8
|
55 *The semantic layer creates the initial virt procr(s), and adds its
|
|
Me@8
|
56 * own environment to masterEnv, and fills in the pointers to
|
|
Me@0
|
57 * the requestHandler and slaveScheduler plug-in functions
|
|
Me@8
|
58 */
|
|
Me@8
|
59
|
|
Me@8
|
60 /*This allocates VMS data structures, populates the master VMSProc,
|
|
Me@0
|
61 * and master environment, and returns the master environment to the semantic
|
|
Me@0
|
62 * layer.
|
|
Me@0
|
63 */
|
|
Me@8
|
64 void
|
|
Me@8
|
65 VMS__init()
|
|
Me@28
|
66 {
|
|
Me@28
|
67 create_masterEnv();
|
|
Me@28
|
68 create_the_coreLoop_OS_threads();
|
|
Me@28
|
69 }
|
|
Me@28
|
70
|
|
Me@28
|
71 /*To initialize the sequential version, just don't create the threads
|
|
Me@28
|
72 */
|
|
Me@28
|
73 void
|
|
Me@28
|
74 VMS__init_Seq()
|
|
Me@28
|
75 {
|
|
Me@28
|
76 create_masterEnv();
|
|
Me@28
|
77 }
|
|
Me@28
|
78
|
|
Me@28
|
79 void
|
|
Me@28
|
80 create_masterEnv()
|
|
Me@31
|
81 { MasterEnv *masterEnv;
|
|
Me@47
|
82 VMSQueueStruc **readyToAnimateQs;
|
|
Me@31
|
83 int coreIdx;
|
|
Me@31
|
84 VirtProcr **masterVPs;
|
|
Me@31
|
85 SchedSlot ***allSchedSlots; //ptr to array of ptrs
|
|
Me@31
|
86
|
|
Me@31
|
87 //Make the master env, which holds everything else
|
|
Me@1
|
88 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
|
|
Me@1
|
89 masterEnv = _VMSMasterEnv;
|
|
Me@31
|
90 //Need to set start pt here 'cause used by seed procr, which is created
|
|
Me@31
|
91 // before the first core loop starts up. -- not sure how yet..
|
|
Me@31
|
92 // masterEnv->coreLoopStartPt = ;
|
|
Me@31
|
93 // masterEnv->coreLoopEndPt = ;
|
|
Me@31
|
94
|
|
Me@31
|
95 //Make a readyToAnimateQ for each core loop
|
|
Me@47
|
96 readyToAnimateQs = malloc( NUM_CORES * sizeof(VMSQueueStruc *) );
|
|
Me@31
|
97 masterVPs = malloc( NUM_CORES * sizeof(VirtProcr *) );
|
|
Me@0
|
98
|
|
Me@31
|
99 //One array for each core, 3 in array, core's masterVP scheds all
|
|
Me@31
|
100 allSchedSlots = malloc( NUM_CORES * sizeof(SchedSlot *) );
|
|
Me@0
|
101
|
|
Me@31
|
102 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
|
|
Me@31
|
103 {
|
|
Me@31
|
104 readyToAnimateQs[ coreIdx ] = makeSRSWQ();
|
|
Me@31
|
105
|
|
Me@31
|
106 //Q: should give masterVP core-specific into as its init data?
|
|
Me@31
|
107 masterVPs[ coreIdx ] = VMS__create_procr( &masterLoop, masterEnv );
|
|
Me@31
|
108 masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
|
|
Me@31
|
109 allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
|
|
Me@31
|
110 }
|
|
Me@31
|
111 _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
|
|
Me@31
|
112 _VMSMasterEnv->masterVPs = masterVPs;
|
|
Me@31
|
113 _VMSMasterEnv->allSchedSlots = allSchedSlots;
|
|
Me@0
|
114
|
|
Me@28
|
115
|
|
Me@12
|
116
|
|
Me@31
|
117 //Aug 19, 2010: no longer need to place initial masterVP into queue
|
|
Me@31
|
118 // because coreLoop now controls -- animates its masterVP when no work
|
|
Me@31
|
119
|
|
Me@30
|
120
|
|
Me@30
|
121 //==================== malloc substitute ========================
|
|
Me@30
|
122 //
|
|
Me@30
|
123 //Testing whether malloc is using thread-local storage and therefore
|
|
Me@30
|
124 // causing unreliable behavior.
|
|
Me@30
|
125 //Just allocate a massive chunk of memory and roll own malloc/free and
|
|
Me@30
|
126 // make app use VMS__malloc_to, which will suspend and perform malloc
|
|
Me@30
|
127 // in the master, taking from this massive chunk.
|
|
Me@30
|
128
|
|
Me@30
|
129 // initFreeList();
|
|
Me@38
|
130
|
|
Me@0
|
131 }
|
|
Me@0
|
132
|
|
Me@30
|
133 /*
|
|
Me@30
|
134 void
|
|
Me@30
|
135 initMasterMalloc()
|
|
Me@30
|
136 {
|
|
Me@30
|
137 _VMSMasterEnv->mallocChunk = malloc( MASSIVE_MALLOC_SIZE );
|
|
Me@30
|
138
|
|
Me@30
|
139 //The free-list element is the first several locations of an
|
|
Me@30
|
140 // allocated chunk -- the address given to the application is pre-
|
|
Me@30
|
141 // pended with both the ownership structure and the free-list struc.
|
|
Me@30
|
142 //So, write the values of these into the first locations of
|
|
Me@30
|
143 // mallocChunk -- which marks it as free & puts in its size.
|
|
Me@30
|
144 listElem = (FreeListElem *)_VMSMasterEnv->mallocChunk;
|
|
Me@30
|
145 listElem->size = MASSIVE_MALLOC_SIZE - NUM_PREPEND_BYTES
|
|
Me@30
|
146 listElem->next = NULL;
|
|
Me@30
|
147 }
|
|
Me@30
|
148
|
|
Me@30
|
149 void
|
|
Me@30
|
150 dissipateMasterMalloc()
|
|
Me@30
|
151 {
|
|
Me@30
|
152 //Just foo code -- to get going -- doing as if free list were link-list
|
|
Me@30
|
153 currElem = _VMSMasterEnv->freeList;
|
|
Me@30
|
154 while( currElem != NULL )
|
|
Me@30
|
155 {
|
|
Me@30
|
156 nextElem = currElem->next;
|
|
Me@30
|
157 masterFree( currElem );
|
|
Me@30
|
158 currElem = nextElem;
|
|
Me@30
|
159 }
|
|
Me@30
|
160 free( _VMSMasterEnv->freeList );
|
|
Me@30
|
161 }
|
|
Me@30
|
162 */
|
|
Me@30
|
163
|
|
Me@31
|
164 SchedSlot **
|
|
Me@31
|
165 create_sched_slots()
|
|
Me@31
|
166 { SchedSlot **schedSlots;
|
|
Me@0
|
167 int i;
|
|
Me@0
|
168
|
|
Me@8
|
169 schedSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
|
|
Me@8
|
170
|
|
Me@1
|
171 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
|
|
Me@0
|
172 {
|
|
Me@8
|
173 schedSlots[i] = malloc( sizeof(SchedSlot) );
|
|
Me@8
|
174
|
|
Me@1
|
175 //Set state to mean "handling requests done, slot needs filling"
|
|
Me@8
|
176 schedSlots[i]->workIsDone = FALSE;
|
|
Me@8
|
177 schedSlots[i]->needsProcrAssigned = TRUE;
|
|
Me@0
|
178 }
|
|
Me@31
|
179 return schedSlots;
|
|
Me@31
|
180 }
|
|
Me@31
|
181
|
|
Me@31
|
182
|
|
Me@31
|
183 void
|
|
Me@31
|
184 freeSchedSlots( SchedSlot **schedSlots )
|
|
Me@31
|
185 { int i;
|
|
Me@31
|
186 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
|
|
Me@31
|
187 {
|
|
Me@31
|
188 free( schedSlots[i] );
|
|
Me@31
|
189 }
|
|
Me@31
|
190 free( schedSlots );
|
|
Me@0
|
191 }
|
|
Me@0
|
192
|
|
Me@8
|
193
|
|
Me@28
|
194 void
|
|
Me@28
|
195 create_the_coreLoop_OS_threads()
|
|
Me@28
|
196 {
|
|
Me@28
|
197 //========================================================================
|
|
Me@28
|
198 // Create the Threads
|
|
Me@47
|
199 int coreIdx, retCode, i;
|
|
Me@47
|
200
|
|
Me@47
|
201 //create the arrays used to measure TSC offsets between cores
|
|
Me@47
|
202 pongNums = malloc( NUM_CORES * sizeof( int ) );
|
|
Me@47
|
203 pingTimes = malloc( NUM_CORES * NUM_TSC_ROUND_TRIPS * sizeof( TSCount ) );
|
|
Me@47
|
204 pongTimes = malloc( NUM_CORES * NUM_TSC_ROUND_TRIPS * sizeof( TSCount ) );
|
|
Me@47
|
205
|
|
Me@47
|
206 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
|
|
Me@47
|
207 {
|
|
Me@47
|
208 pongNums[ coreIdx ] = 0;
|
|
Me@47
|
209 for( i = 0; i < NUM_TSC_ROUND_TRIPS; i++ )
|
|
Me@47
|
210 {
|
|
Me@47
|
211 pingTimes[ coreIdx * NUM_TSC_ROUND_TRIPS + i ] = (TSCount) 0;
|
|
Me@47
|
212 pingTimes[ coreIdx * NUM_TSC_ROUND_TRIPS + i ] = (TSCount) 0;
|
|
Me@47
|
213 }
|
|
Me@47
|
214 }
|
|
Me@28
|
215
|
|
Me@28
|
216 //Need the threads to be created suspended, and wait for a signal
|
|
Me@28
|
217 // before proceeding -- gives time after creating to initialize other
|
|
Me@28
|
218 // stuff before the coreLoops set off.
|
|
Me@28
|
219 _VMSMasterEnv->setupComplete = 0;
|
|
Me@28
|
220
|
|
Me@28
|
221 //Make the threads that animate the core loops
|
|
Me@28
|
222 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
|
|
Me@28
|
223 { coreLoopThdParams[coreIdx] = malloc( sizeof(ThdParams) );
|
|
Me@28
|
224 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
|
|
Me@28
|
225
|
|
Me@28
|
226 retCode =
|
|
Me@28
|
227 pthread_create( &(coreLoopThdHandles[coreIdx]),
|
|
Me@28
|
228 thdAttrs,
|
|
Me@28
|
229 &coreLoop,
|
|
Me@28
|
230 (void *)(coreLoopThdParams[coreIdx]) );
|
|
Me@28
|
231 if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(0);}
|
|
Me@28
|
232 }
|
|
Me@28
|
233 }
|
|
Me@28
|
234
|
|
Me@0
|
235 /*Semantic layer calls this when it want the system to start running..
|
|
Me@0
|
236 *
|
|
Me@24
|
237 *This starts the core loops running then waits for them to exit.
|
|
Me@0
|
238 */
|
|
Me@12
|
239 void
|
|
Me@24
|
240 VMS__start_the_work_then_wait_until_done()
|
|
Me@12
|
241 { int coreIdx;
|
|
Me@24
|
242 //Start the core loops running
|
|
Me@24
|
243 //===========================================================================
|
|
Me@25
|
244 TSCount startCount, endCount;
|
|
Me@24
|
245 unsigned long long count = 0, freq = 0;
|
|
Me@25
|
246 double runTime;
|
|
Me@0
|
247
|
|
Me@47
|
248 startCount = getTSC();
|
|
Me@25
|
249
|
|
Me@25
|
250 //tell the core loop threads that setup is complete
|
|
Me@25
|
251 //get lock, to lock out any threads still starting up -- they'll see
|
|
Me@25
|
252 // that setupComplete is true before entering while loop, and so never
|
|
Me@25
|
253 // wait on the condition
|
|
Me@26
|
254 pthread_mutex_lock( &suspendLock );
|
|
Me@25
|
255 _VMSMasterEnv->setupComplete = 1;
|
|
Me@26
|
256 pthread_mutex_unlock( &suspendLock );
|
|
Me@26
|
257 pthread_cond_broadcast( &suspend_cond );
|
|
Me@25
|
258
|
|
Me@25
|
259
|
|
Me@24
|
260 //wait for all to complete
|
|
Me@8
|
261 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
|
|
Me@8
|
262 {
|
|
Me@25
|
263 pthread_join( coreLoopThdHandles[coreIdx], NULL );
|
|
Me@24
|
264 }
|
|
Me@25
|
265
|
|
Me@24
|
266 //NOTE: do not clean up VMS env here -- semantic layer has to have
|
|
Me@24
|
267 // a chance to clean up its environment first, then do a call to free
|
|
Me@24
|
268 // the Master env and rest of VMS locations
|
|
Me@24
|
269
|
|
Me@24
|
270
|
|
Me@47
|
271 endCount = getTSC();
|
|
Me@25
|
272 count = endCount - startCount;
|
|
Me@24
|
273
|
|
Me@25
|
274 runTime = (double)count / (double)TSCOUNT_FREQ;
|
|
Me@25
|
275
|
|
Me@25
|
276 printf("\n Time startup to shutdown: %f\n", runTime); fflush( stdin );
|
|
Me@8
|
277 }
|
|
Me@0
|
278
|
|
Me@28
|
279 /*Only difference between version with an OS thread pinned to each core and
|
|
Me@28
|
280 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
|
|
Me@28
|
281 */
|
|
Me@28
|
282 void
|
|
Me@28
|
283 VMS__start_the_work_then_wait_until_done_Seq()
|
|
Me@28
|
284 {
|
|
Me@28
|
285 //Instead of un-suspending threads, just call the one and only
|
|
Me@28
|
286 // core loop (sequential version), in the main thread.
|
|
Me@28
|
287 coreLoop_Seq( NULL );
|
|
Me@28
|
288
|
|
Me@28
|
289 }
|
|
Me@28
|
290
|
|
Me@0
|
291
|
|
Me@0
|
292
|
|
Me@8
|
293 /*Create stack, then create __cdecl structure on it and put initialData and
|
|
Me@8
|
294 * pointer to the new structure instance into the parameter positions on
|
|
Me@8
|
295 * the stack
|
|
Me@8
|
296 *Then put function pointer into nextInstrPt -- the stack is setup in std
|
|
Me@8
|
297 * call structure, so jumping to function ptr is same as a GCC generated
|
|
Me@8
|
298 * function call
|
|
Me@8
|
299 *No need to save registers on old stack frame, because there's no old
|
|
Me@8
|
300 * animator state to return to --
|
|
Me@8
|
301 *
|
|
Me@8
|
302 */
|
|
Me@8
|
303 VirtProcr *
|
|
Me@8
|
304 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
|
|
Me@8
|
305 { VirtProcr *newPr;
|
|
Me@8
|
306 char *stackLocs, *stackPtr;
|
|
Me@8
|
307
|
|
Me@8
|
308 newPr = malloc( sizeof(VirtProcr) );
|
|
Me@12
|
309 newPr->procrID = numProcrsCreated++;
|
|
Me@8
|
310 newPr->nextInstrPt = fnPtr;
|
|
Me@8
|
311 newPr->initialData = initialData;
|
|
Me@31
|
312 newPr->requests = NULL;
|
|
Me@38
|
313 newPr->schedSlot = NULL;
|
|
Me@31
|
314 // newPr->coreLoopStartPt = _VMSMasterEnv->coreLoopStartPt;
|
|
Me@8
|
315
|
|
Me@14
|
316 //fnPtr takes two params -- void *initData & void *animProcr
|
|
Me@8
|
317 //alloc stack locations, make stackPtr be the highest addr minus room
|
|
Me@14
|
318 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
|
|
Me@14
|
319 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
|
|
Me@22
|
320 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
|
|
Me@47
|
321 if(stackLocs == 0) {perror("error: malloc stack"); exit(1);}
|
|
Me@22
|
322 newPr->startOfStack = stackLocs;
|
|
Me@22
|
323 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
|
|
Me@8
|
324 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
|
|
Me@22
|
325 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
|
|
Me@14
|
326 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
|
|
Me@8
|
327 newPr->stackPtr = stackPtr; //core loop will switch to this, then
|
|
Me@8
|
328 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
|
|
Me@8
|
329
|
|
Me@8
|
330 return newPr;
|
|
Me@8
|
331 }
|
|
Me@8
|
332
|
|
Me@8
|
333
|
|
Me@26
|
334 /*there is a label inside this function -- save the addr of this label in
|
|
Me@0
|
335 * the callingPr struc, as the pick-up point from which to start the next
|
|
Me@0
|
336 * work-unit for that procr. If turns out have to save registers, then
|
|
Me@0
|
337 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
|
|
Me@0
|
338 * "done with work-unit" label. The procr struc is in the request in the
|
|
Me@0
|
339 * slave that animated the just-ended work-unit, so all the state is saved
|
|
Me@0
|
340 * there, and will get passed along, inside the request handler, to the
|
|
Me@0
|
341 * next work-unit for that procr.
|
|
Me@0
|
342 */
|
|
Me@8
|
343 void
|
|
Me@38
|
344 VMS__suspend_procr( VirtProcr *animatingPr )
|
|
Me@14
|
345 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
|
|
Me@14
|
346 void *coreLoopFramePtr;
|
|
Me@0
|
347
|
|
Me@14
|
348 //The request to master will cause this suspended virt procr to get
|
|
Me@14
|
349 // scheduled again at some future point -- to resume, core loop jumps
|
|
Me@14
|
350 // to the resume point (below), which causes restore of saved regs and
|
|
Me@14
|
351 // "return" from this call.
|
|
Me@38
|
352 animatingPr->nextInstrPt = &&ResumePt;
|
|
Me@1
|
353
|
|
Me@1
|
354 //return ownership of the virt procr and sched slot to Master virt pr
|
|
Me@38
|
355 animatingPr->schedSlot->workIsDone = TRUE;
|
|
Me@14
|
356 // coreIdx = callingPr->coreAnimatedBy;
|
|
Me@1
|
357
|
|
Me@38
|
358 stackPtrAddr = &(animatingPr->stackPtr);
|
|
Me@38
|
359 framePtrAddr = &(animatingPr->framePtr);
|
|
Me@26
|
360
|
|
Me@31
|
361 jmpPt = _VMSMasterEnv->coreLoopStartPt;
|
|
Me@38
|
362 coreLoopFramePtr = animatingPr->coreLoopFramePtr;//need this only
|
|
Me@38
|
363 coreLoopStackPtr = animatingPr->coreLoopStackPtr;//safety
|
|
Me@1
|
364
|
|
Me@26
|
365 //Save the virt procr's stack and frame ptrs,
|
|
Me@18
|
366 asm volatile("movl %0, %%eax; \
|
|
Me@18
|
367 movl %%esp, (%%eax); \
|
|
Me@18
|
368 movl %1, %%eax; \
|
|
Me@26
|
369 movl %%ebp, (%%eax) "\
|
|
Me@26
|
370 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
|
|
Me@26
|
371 /* inputs */ : \
|
|
Me@26
|
372 /* clobber */ : "%eax" \
|
|
Me@26
|
373 );
|
|
Me@26
|
374
|
|
Me@41
|
375 //=========================== Measurement stuff ========================
|
|
Me@38
|
376 #ifdef MEAS__TIME_STAMP_SUSP
|
|
Me@41
|
377 //record time stamp: compare to time-stamp recorded below
|
|
Me@38
|
378 saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
|
|
Me@38
|
379 #endif
|
|
Me@41
|
380 //=======================================================================
|
|
Me@38
|
381
|
|
Me@26
|
382 //restore coreloop's frame ptr, then jump back to "start" of core loop
|
|
Me@26
|
383 //Note, GCC compiles to assembly that saves esp and ebp in the stack
|
|
Me@26
|
384 // frame -- so have to explicitly do assembly that saves to memory
|
|
Me@26
|
385 asm volatile("movl %0, %%eax; \
|
|
Me@26
|
386 movl %1, %%esp; \
|
|
Me@26
|
387 movl %2, %%ebp; \
|
|
Me@18
|
388 jmp %%eax " \
|
|
Me@26
|
389 /* outputs */ : \
|
|
Me@26
|
390 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
|
|
Me@18
|
391 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
|
|
Me@12
|
392 ); //list everything as clobbered to force GCC to save all
|
|
Me@12
|
393 // live vars that are in regs on stack before this
|
|
Me@12
|
394 // assembly, so that stack pointer is correct, before jmp
|
|
Me@1
|
395
|
|
Me@1
|
396 ResumePt:
|
|
Me@38
|
397 #ifdef MEAS__TIME_STAMP_SUSP
|
|
Me@41
|
398 //NOTE: only take low part of count -- do sanity check when take diff
|
|
Me@38
|
399 saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
|
|
Me@38
|
400 #endif
|
|
Me@38
|
401
|
|
Me@0
|
402 return;
|
|
Me@0
|
403 }
|
|
Me@0
|
404
|
|
Me@22
|
405
|
|
Me@22
|
406
|
|
Me@22
|
407
|
|
Me@38
|
408 /*
|
|
Me@22
|
409 *This adds a request to dissipate, then suspends the processor so that the
|
|
Me@22
|
410 * request handler will receive the request. The request handler is what
|
|
Me@22
|
411 * does the work of freeing memory and removing the processor from the
|
|
Me@22
|
412 * semantic environment's data structures.
|
|
Me@22
|
413 *The request handler also is what figures out when to shutdown the VMS
|
|
Me@22
|
414 * system -- which causes all the core loop threads to die, and returns from
|
|
Me@22
|
415 * the call that started up VMS to perform the work.
|
|
Me@22
|
416 *
|
|
Me@22
|
417 *This form is a bit misleading to understand if one is trying to figure out
|
|
Me@22
|
418 * how VMS works -- it looks like a normal function call, but inside it
|
|
Me@22
|
419 * sends a request to the request handler and suspends the processor, which
|
|
Me@22
|
420 * jumps out of the VMS__dissipate_procr function, and out of all nestings
|
|
Me@22
|
421 * above it, transferring the work of dissipating to the request handler,
|
|
Me@22
|
422 * which then does the actual work -- causing the processor that animated
|
|
Me@22
|
423 * the call of this function to disappear and the "hanging" state of this
|
|
Me@22
|
424 * function to just poof into thin air -- the virtual processor's trace
|
|
Me@22
|
425 * never returns from this call, but instead the virtual processor's trace
|
|
Me@22
|
426 * gets suspended in this call and all the virt processor's state disap-
|
|
Me@22
|
427 * pears -- making that suspend the last thing in the virt procr's trace.
|
|
Me@8
|
428 */
|
|
Me@8
|
429 void
|
|
Me@22
|
430 VMS__dissipate_procr( VirtProcr *procrToDissipate )
|
|
Me@22
|
431 { VMSReqst *req;
|
|
Me@22
|
432
|
|
Me@22
|
433 req = malloc( sizeof(VMSReqst) );
|
|
Me@22
|
434 // req->virtProcrFrom = callingPr;
|
|
Me@22
|
435 req->reqType = dissipate;
|
|
Me@22
|
436 req->nextReqst = procrToDissipate->requests;
|
|
Me@22
|
437 procrToDissipate->requests = req;
|
|
Me@22
|
438
|
|
Me@22
|
439 VMS__suspend_procr( procrToDissipate );
|
|
Me@22
|
440 }
|
|
Me@22
|
441
|
|
Me@22
|
442
|
|
Me@22
|
443 /*This inserts the semantic-layer's request data into standard VMS carrier
|
|
Me@22
|
444 */
|
|
Me@22
|
445 inline void
|
|
Me@24
|
446 VMS__add_sem_request( void *semReqData, VirtProcr *callingPr )
|
|
Me@22
|
447 { VMSReqst *req;
|
|
Me@22
|
448
|
|
Me@22
|
449 req = malloc( sizeof(VMSReqst) );
|
|
Me@22
|
450 // req->virtProcrFrom = callingPr;
|
|
Me@22
|
451 req->reqType = semantic;
|
|
Me@22
|
452 req->semReqData = semReqData;
|
|
Me@22
|
453 req->nextReqst = callingPr->requests;
|
|
Me@22
|
454 callingPr->requests = req;
|
|
Me@22
|
455 }
|
|
Me@22
|
456
|
|
Me@22
|
457
|
|
Me@38
|
458 /*Use this to get first request before starting request handler's loop
|
|
Me@38
|
459 */
|
|
Me@24
|
460 VMSReqst *
|
|
Me@24
|
461 VMS__take_top_request_from( VirtProcr *procrWithReq )
|
|
Me@24
|
462 { VMSReqst *req;
|
|
Me@24
|
463
|
|
Me@24
|
464 req = procrWithReq->requests;
|
|
Me@24
|
465 if( req == NULL ) return req;
|
|
Me@31
|
466
|
|
Me@24
|
467 procrWithReq->requests = procrWithReq->requests->nextReqst;
|
|
Me@24
|
468 return req;
|
|
Me@24
|
469 }
|
|
Me@24
|
470
|
|
Me@38
|
471 /*A subtle bug due to freeing then accessing "next" after freed caused this
|
|
Me@38
|
472 * form of call to be put in -- so call this at end of request handler loop
|
|
Me@38
|
473 * that iterates through the requests.
|
|
Me@38
|
474 */
|
|
Me@31
|
475 VMSReqst *
|
|
Me@31
|
476 VMS__free_top_and_give_next_request_from( VirtProcr *procrWithReq )
|
|
Me@31
|
477 { VMSReqst *req;
|
|
Me@31
|
478
|
|
Me@31
|
479 req = procrWithReq->requests;
|
|
Me@38
|
480 if( req == NULL ) return NULL;
|
|
Me@31
|
481
|
|
Me@31
|
482 procrWithReq->requests = procrWithReq->requests->nextReqst;
|
|
Me@31
|
483 VMS__free_request( req );
|
|
Me@31
|
484 return procrWithReq->requests;
|
|
Me@31
|
485 }
|
|
Me@31
|
486
|
|
Me@38
|
487
|
|
Me@38
|
488 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
|
|
Me@38
|
489 // of a request -- IE call with both a virt procr and a fn-ptr to request
|
|
Me@38
|
490 // freer (also maybe put sem request freer as a field in virt procr?)
|
|
Me@38
|
491 //MeasVMS relies right now on this only freeing VMS layer of request -- the
|
|
Me@38
|
492 // semantic portion of request is alloc'd and freed by request handler
|
|
Me@38
|
493 void
|
|
Me@38
|
494 VMS__free_request( VMSReqst *req )
|
|
Me@38
|
495 {
|
|
Me@38
|
496 free( req );
|
|
Me@38
|
497 }
|
|
Me@38
|
498
|
|
Me@38
|
499
|
|
Me@38
|
500
|
|
Me@24
|
501 inline int
|
|
Me@24
|
502 VMS__isSemanticReqst( VMSReqst *req )
|
|
Me@22
|
503 {
|
|
Me@24
|
504 return ( req->reqType == semantic );
|
|
Me@24
|
505 }
|
|
Me@22
|
506
|
|
Me@24
|
507
|
|
Me@24
|
508 inline void *
|
|
Me@24
|
509 VMS__take_sem_reqst_from( VMSReqst *req )
|
|
Me@24
|
510 {
|
|
Me@24
|
511 return req->semReqData;
|
|
Me@24
|
512 }
|
|
Me@24
|
513
|
|
Me@24
|
514 inline int
|
|
Me@24
|
515 VMS__isDissipateReqst( VMSReqst *req )
|
|
Me@24
|
516 {
|
|
Me@24
|
517 return ( req->reqType == dissipate );
|
|
Me@24
|
518 }
|
|
Me@24
|
519
|
|
Me@24
|
520 inline int
|
|
Me@24
|
521 VMS__isCreateReqst( VMSReqst *req )
|
|
Me@24
|
522 {
|
|
Me@24
|
523 return ( req->reqType == regCreated );
|
|
Me@24
|
524 }
|
|
Me@24
|
525
|
|
Me@24
|
526 void
|
|
Me@38
|
527 VMS__send_req_to_register_new_procr(VirtProcr *newPr, VirtProcr *reqstingPr)
|
|
Me@24
|
528 { VMSReqst *req;
|
|
Me@24
|
529
|
|
Me@24
|
530 req = malloc( sizeof(VMSReqst) );
|
|
Me@24
|
531 req->reqType = regCreated;
|
|
Me@24
|
532 req->semReqData = newPr;
|
|
Me@24
|
533 req->nextReqst = reqstingPr->requests;
|
|
Me@24
|
534 reqstingPr->requests = req;
|
|
Me@24
|
535
|
|
Me@24
|
536 VMS__suspend_procr( reqstingPr );
|
|
Me@22
|
537 }
|
|
Me@22
|
538
|
|
Me@22
|
539
|
|
Me@22
|
540
|
|
Me@24
|
541 /*This must be called by the request handler plugin -- it cannot be called
|
|
Me@24
|
542 * from the semantic library "dissipate processor" function -- instead, the
|
|
Me@24
|
543 * semantic layer has to generate a request for the plug-in to call this
|
|
Me@24
|
544 * function.
|
|
Me@24
|
545 *The reason is that this frees the virtual processor's stack -- which is
|
|
Me@24
|
546 * still in use inside semantic library calls!
|
|
Me@24
|
547 *
|
|
Me@24
|
548 *This frees or recycles all the state owned by and comprising the VMS
|
|
Me@24
|
549 * portion of the animating virtual procr. The request handler must first
|
|
Me@24
|
550 * free any semantic data created for the processor that didn't use the
|
|
Me@24
|
551 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
|
|
Me@24
|
552 * system to disown any state that did use VMS_malloc, and then frees the
|
|
Me@24
|
553 * statck and the processor-struct itself.
|
|
Me@24
|
554 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
|
|
Me@24
|
555 * state, then that state gets freed (or sent to recycling) as a side-effect
|
|
Me@24
|
556 * of dis-owning it.
|
|
Me@24
|
557 */
|
|
Me@24
|
558 void
|
|
Me@29
|
559 VMS__handle_dissipate_reqst( VirtProcr *animatingPr )
|
|
Me@24
|
560 {
|
|
Me@24
|
561 //dis-own all locations owned by this processor, causing to be freed
|
|
Me@24
|
562 // any locations that it is (was) sole owner of
|
|
Me@29
|
563 //TODO: implement VMS__malloc system, including "give up ownership"
|
|
Me@24
|
564
|
|
Me@24
|
565 //The dissipate request might still be attached, so remove and free it
|
|
Me@38
|
566 VMS__free_top_and_give_next_request_from( animatingPr );
|
|
Me@24
|
567
|
|
Me@24
|
568 //NOTE: initialData was given to the processor, so should either have
|
|
Me@24
|
569 // been alloc'd with VMS__malloc, or freed by the level above animPr.
|
|
Me@24
|
570 //So, all that's left to free here is the stack and the VirtProcr struc
|
|
Me@24
|
571 // itself
|
|
Me@24
|
572 free( animatingPr->startOfStack );
|
|
Me@24
|
573 free( animatingPr );
|
|
Me@24
|
574 }
|
|
Me@24
|
575
|
|
Me@24
|
576
|
|
Me@29
|
577 //TODO: re-architect so that have clean separation between request handler
|
|
Me@29
|
578 // and master loop, for dissipate, create, shutdown, and other non-semantic
|
|
Me@29
|
579 // requests. Issue is chain: one removes requests from AppVP, one dispatches
|
|
Me@29
|
580 // on type of request, and one handles each type.. but some types require
|
|
Me@29
|
581 // action from both request handler and master loop -- maybe just give the
|
|
Me@29
|
582 // request handler calls like: VMS__handle_X_request_type
|
|
Me@24
|
583
|
|
Me@29
|
584 void
|
|
Me@29
|
585 endOSThreadFn( void *initData, VirtProcr *animatingPr );
|
|
Me@29
|
586
|
|
Me@29
|
587 /*This is called by the semantic layer's request handler when it decides its
|
|
Me@29
|
588 * time to shut down the VMS system. Calling this causes the core loop OS
|
|
Me@29
|
589 * threads to exit, which unblocks the entry-point function that started up
|
|
Me@29
|
590 * VMS, and allows it to grab the result and return to the original single-
|
|
Me@29
|
591 * threaded application.
|
|
Me@22
|
592 *
|
|
Me@29
|
593 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
|
|
Me@29
|
594 * and-wait function has to free a bunch of stuff after it detects the
|
|
Me@29
|
595 * threads have all died: the masterEnv, the thread-related locations,
|
|
Me@29
|
596 * masterVP any AppVPs that might still be allocated and sitting in the
|
|
Me@29
|
597 * semantic environment, or have been orphaned in the _VMSWorkQ.
|
|
Me@29
|
598 *
|
|
Me@29
|
599 *NOTE: the semantic plug-in is expected to use VMS__malloc_to to get all the
|
|
Me@29
|
600 * locations it needs, and give ownership to masterVP. Then, they will be
|
|
Me@29
|
601 * automatically freed when the masterVP is dissipated. (This happens after
|
|
Me@29
|
602 * the core loop threads have all exited)
|
|
Me@22
|
603 *
|
|
Me@29
|
604 *In here,create one core-loop shut-down processor for each core loop and put
|
|
Me@31
|
605 * them all directly into the readyToAnimateQ.
|
|
Me@29
|
606 *Note, this function can ONLY be called after the semantic environment no
|
|
Me@29
|
607 * longer cares if AppVPs get animated after the point this is called. In
|
|
Me@29
|
608 * other words, this can be used as an abort, or else it should only be
|
|
Me@29
|
609 * called when all AppVPs have finished dissipate requests -- only at that
|
|
Me@29
|
610 * point is it sure that all results have completed.
|
|
Me@22
|
611 */
|
|
Me@22
|
612 void
|
|
Me@29
|
613 VMS__handle_shutdown_reqst( void *dummy, VirtProcr *animatingPr )
|
|
Me@8
|
614 { int coreIdx;
|
|
Me@14
|
615 VirtProcr *shutDownPr;
|
|
Me@22
|
616
|
|
Me@29
|
617 //create the shutdown processors, one for each core loop -- put them
|
|
Me@31
|
618 // directly into the Q -- each core will die when gets one
|
|
Me@8
|
619 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
|
|
Me@8
|
620 {
|
|
Me@29
|
621 shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
|
|
Me@31
|
622 writeSRSWQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
|
|
Me@8
|
623 }
|
|
Me@22
|
624
|
|
Me@12
|
625 }
|
|
Me@12
|
626
|
|
Me@12
|
627
|
|
Me@29
|
628 /*Am trying to be cute, avoiding IF statement in coreLoop that checks for
|
|
Me@29
|
629 * a special shutdown procr. Ended up with extra-complex shutdown sequence.
|
|
Me@29
|
630 *This function has the sole purpose of setting the stack and framePtr
|
|
Me@29
|
631 * to the coreLoop's stack and framePtr.. it does that then jumps to the
|
|
Me@29
|
632 * core loop's shutdown point -- might be able to just call Pthread_exit
|
|
Me@30
|
633 * from here, but am going back to the pthread's stack and setting everything
|
|
Me@29
|
634 * up just as if it never jumped out, before calling pthread_exit.
|
|
Me@29
|
635 *The end-point of core loop will free the stack and so forth of the
|
|
Me@29
|
636 * processor that animates this function, (this fn is transfering the
|
|
Me@29
|
637 * animator of the AppVP that is in turn animating this function over
|
|
Me@29
|
638 * to core loop function -- note that this slices out a level of virtual
|
|
Me@29
|
639 * processors).
|
|
Me@29
|
640 */
|
|
Me@29
|
641 void
|
|
Me@29
|
642 endOSThreadFn( void *initData, VirtProcr *animatingPr )
|
|
Me@29
|
643 { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;
|
|
Me@29
|
644
|
|
Me@29
|
645 jmpPt = _VMSMasterEnv->coreLoopEndPt;
|
|
Me@29
|
646 coreLoopStackPtr = animatingPr->coreLoopStackPtr;
|
|
Me@29
|
647 coreLoopFramePtr = animatingPr->coreLoopFramePtr;
|
|
Me@29
|
648
|
|
Me@29
|
649
|
|
Me@29
|
650 asm volatile("movl %0, %%eax; \
|
|
Me@29
|
651 movl %1, %%esp; \
|
|
Me@29
|
652 movl %2, %%ebp; \
|
|
Me@29
|
653 jmp %%eax " \
|
|
Me@29
|
654 /* outputs */ : \
|
|
Me@29
|
655 /* inputs */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
|
|
Me@29
|
656 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
|
|
Me@29
|
657 );
|
|
Me@29
|
658 }
|
|
Me@29
|
659
|
|
Me@29
|
660
|
|
Me@31
|
661 /*This is called after the threads have shut down and control has returned
|
|
Me@30
|
662 * to the semantic layer, in the entry point function in the main thread.
|
|
Me@30
|
663 * It has to free anything allocated during VMS_init, and any other alloc'd
|
|
Me@24
|
664 * locations that might be left over.
|
|
Me@24
|
665 */
|
|
Me@24
|
666 void
|
|
Me@29
|
667 VMS__cleanup_after_shutdown()
|
|
Me@31
|
668 {
|
|
Me@47
|
669 VMSQueueStruc **readyToAnimateQs;
|
|
Me@31
|
670 int coreIdx;
|
|
Me@31
|
671 VirtProcr **masterVPs;
|
|
Me@31
|
672 SchedSlot ***allSchedSlots; //ptr to array of ptrs
|
|
Me@31
|
673
|
|
Me@31
|
674 readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
|
|
Me@31
|
675 masterVPs = _VMSMasterEnv->masterVPs;
|
|
Me@31
|
676 allSchedSlots = _VMSMasterEnv->allSchedSlots;
|
|
Me@31
|
677
|
|
Me@31
|
678 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
|
|
Me@24
|
679 {
|
|
Me@31
|
680 freeSRSWQ( readyToAnimateQs[ coreIdx ] );
|
|
Me@31
|
681
|
|
Me@31
|
682 VMS__handle_dissipate_reqst( masterVPs[ coreIdx ] );
|
|
Me@31
|
683
|
|
Me@31
|
684 freeSchedSlots( allSchedSlots[ coreIdx ] );
|
|
Me@24
|
685 }
|
|
Me@31
|
686
|
|
Me@31
|
687 free( _VMSMasterEnv->readyToAnimateQs );
|
|
Me@31
|
688 free( _VMSMasterEnv->masterVPs );
|
|
Me@31
|
689 free( _VMSMasterEnv->allSchedSlots );
|
|
Me@24
|
690
|
|
Me@24
|
691 free( _VMSMasterEnv );
|
|
Me@24
|
692 }
|
|
Me@24
|
693
|
|
Me@24
|
694
|
|
Me@24
|
695 //===========================================================================
|
|
Me@12
|
696
|
|
Me@47
|
697 inline TSCount getTSC()
|
|
Me@12
|
698 { unsigned int low, high;
|
|
Me@12
|
699 TSCount out;
|
|
Me@12
|
700
|
|
Me@12
|
701 saveTimeStampCountInto( low, high );
|
|
Me@12
|
702 out = high;
|
|
Me@12
|
703 out = (out << 32) + low;
|
|
Me@12
|
704 return out;
|
|
Me@12
|
705 }
|
|
Me@12
|
706
|