view AnimationMaster.c @ 276:1d7ea1b0f176

Dev_ML Working in sequential mode
author Sean Halle <seanhalle@yahoo.com>
date Mon, 04 Mar 2013 00:40:38 -0800
parents 40e7625e57bd
children 2fc69e6c14ea
line source
1 /*
2 * Copyright 2012 OpenSourceResearchInstitute.org
3 *
4 * Licensed under BSD
5 */
9 #include <stdio.h>
10 #include <stddef.h>
12 #include "PR.h"
13 #include "VSs_impl/VSs.h"
15 //========================= Local Declarations ========================
16 inline PRProcess *
17 pickAProcess( AnimSlot *slot );
18 inline bool32
19 assignWork( PRProcess *process, AnimSlot *slot );
21 inline void
22 PRHandle__CreateTask( PRReqst *req, SlaveVP *slave );
23 inline void
24 PRHandle__EndTask( PRReqst *req, SlaveVP *slave );
25 inline void
26 PRHandle__CreateSlave(PRReqst *req, SlaveVP *slave );
27 inline void
28 PRHandle__EndSlave( PRReqst *req, SlaveVP *slave );
30 inline void
31 PRHandle__LangShutdown( PRReqst *req, SlaveVP *requestingSlv );
33 inline void
34 handleMakeProbe( PRServiceReq *langReq, PRLangEnv *protoLangEnv );
35 inline void
36 handleThrowException( PRServiceReq *langReq, PRLangEnv *protoLangEnv );
38 //===========================================================================
40 /*Note: there used to be a coreController that was another animation
41 * layer below both the masterVP and the slaveVPs.. in that case, the
42 * masterVP was a virtual processor whose processor-state was the same
43 * as a slaveVP's processor sate, both implemented as a SlaveVP struct.
44 * Have removed that, and
45 * changed the masterVP implementation. Instead of being a special version
46 * of a proto-runtime virtual processor, using the slaveVP stuct, the
47 * Master "virtual processor" is now implemented as a pthread pinned to
48 * a physical core.
49 */
51 /*This is the behavior of the Master. The physical processor switches
52 * between animating the master, and animating a slave. When a slave
53 * suspends, the PR "suspend" primitive switches the physical core over
54 * to animating the masterVP, which is implemented as a pinned pthread.
55 * This function is the behavior of that masterVP.
56 *This function's job is to manage processing
57 * requests and to trigger assignment of new work to the physical core,
58 * and to manage sharing the core among processes.
59 */
60 inline
61 bool32
62 masterFunction( AnimSlot *slot )
63 { //Scan the animation slots
64 int32 magicNumber;
65 SlaveVP *slave;
66 PRLangEnv *langEnv;
67 PRReqst *req;
68 PRProcess *process;
69 bool32 didAssignWork;
71 //Check if newly-done slave in slot, which will need request handled
72 //NOTE: left over from when had a coreController & MasterVP managed
73 // several slots
74 if( slot->workIsDone )
75 { slot->workIsDone = FALSE;
76 slot->needsWorkAssigned = TRUE;
78 //An Idle VP has no request to handle, so skip to assign..
79 if( slot->slaveAssignedToSlot->typeOfVP != IdleVP )
80 {
81 HOLISTIC__Record_AppResponder_start; //TODO: update to check which process for each slot
82 MEAS__startReqHdlr;
85 //process the request made by the slave (held inside slave struc)
86 slave = slot->slaveAssignedToSlot;
87 req = slave->request;
89 //If the requesting slave is a slot slave, and request is not
90 // task-end, then turn it into a free task slave & continue
91 if( slave->typeOfVP == SlotTaskSlv && req->reqType != TaskEnd )
92 PR_int__replace_with_new_slot_slv( slave );
94 //Handle task create and end first -- they're special cases..
95 switch( req->reqType )
96 { case TaskEnd:
97 { //do PR handler, which calls lang's hdlr and does recycle of
98 // free task slave if needed -- PR handler checks for free task Slv
99 PRHandle__EndTask( req, slave ); break;
100 }
101 case TaskCreate:
102 { //Do PR's create-task handler, which calls the lang's hdlr
103 // PR handler checks for free task Slv
104 PRHandle__CreateTask( req, slave ); break;
105 }
106 case SlvCreate: PRHandle__CreateSlave( req, slave ); break;
107 case SlvDissipate: PRHandle__EndSlave( req, slave ); break;
108 case Service: PRHandle__ServiceReq( slave ); break; //resumes into Service lang env
109 case Hardware: //for future expansion
110 case IO: //for future expansion
111 case OSCall: //for future expansion
112 PR_int__throw_exception("Not implemented", slave, NULL); break;
113 case LangShutdown: PRHandle__LangShutdown( req, slave ); break;
114 case Language: //normal lang request
115 { magicNumber = req->langMagicNumber;
116 langEnv = PR_PI__give_lang_env_for_slave( slave, magicNumber );
117 (*req->handler)( req->langReq, slave, langEnv );
118 }
119 }
121 MEAS__endReqHdlr;
122 HOLISTIC__Record_AppResponder_end;
123 }//if not idleVP
124 } //if have request to be handled
126 //NOTE: IF statement is leftover from when master managed many slots
127 didAssignWork = FALSE;
128 if( slot->needsWorkAssigned ) //can probably remove IF, now that only one slot
129 {
130 HOLISTIC__Record_Assigner_start;
132 //Pick a process to get this slot
133 process = pickAProcess( slot );
135 //Scan lang environs, looking for langEnv with ready work.
136 // call the Assigner for that lang Env, to get a slave for the slot
137 if( process != NULL )
138 { didAssignWork =
139 assignWork( process, slot );
140 }
141 HOLISTIC__Record_Assigner_end;
143 if( !didAssignWork ) //if no work assigned, be sure idle slave is in slot
144 { slot->slaveAssignedToSlot = _PRTopEnv->idleSlv[slot->coreSlotIsOn][0];
145 }
146 // fixme; //make into a loop that tries more processes if fails to assign
147 }//if slot needs slave assigned
149 return didAssignWork;
150 }
152 /*When several processes exist, use some pattern for picking one to give
153 * the animation slot to.
154 *First, it has to be a process that has work available.
155 *For now, just do a round-robin
156 */
157 inline
158 PRProcess *
159 pickAProcess( AnimSlot *slot )
160 { int32 idx;
161 PRProcess *process;
163 for( idx = _PRTopEnv->currProcessIdx; idx < _PRTopEnv->numProcesses; idx++)
164 {
165 process = _PRTopEnv->processes[ idx ];
166 if( process->numEnvsWithWork != 0 )
167 { _PRTopEnv->currProcessIdx = idx;
168 return process;
169 }
170 }
171 for( idx = 0; idx < _PRTopEnv->currProcessIdx; idx++)
172 {
173 process = _PRTopEnv->processes[ idx ];
174 if( process->numEnvsWithWork != 0 )
175 { _PRTopEnv->currProcessIdx = idx;
176 return process;
177 }
178 }
179 //none found
180 return NULL;
181 }
183 /*This does:
184 * 1) searches the language environments for one with work ready
185 * if finds one, asks its assigner to return work
186 * 2) checks what kind of work: new task, resuming task, resuming slave
187 * if new task, gets the slot slave and assigns task to it and returns slave
188 * else, gets the slave attached to the metaTask and returns that.
189 * 3) if no work found, then prune former task slaves waiting to be recycled.
190 * If no work and no slaves to prune, check for shutdown conditions.
191 *
192 * language env keeps its own work in its own structures, and has its own
193 * assigner. It chooses
194 * However, include a switch that switches-in an override assigner, which
195 * sees all the work in all the language env's. This is most likely
196 * generated by static tools and included in the executable. That means it
197 * has to be called via a registered pointer from here. The idea is that
198 * the static tools know which languages are grouped together.. and the
199 * override enables them to generate a custom assigner that uses info from
200 * all the languages in a unified way.. Don't really expect this to happen,
201 * but am making it possible.
202 */
203 inline
204 bool32
205 assignWork( PRProcess *process, AnimSlot *slot )
206 { int32 coreNum;
208 coreNum = slot->coreSlotIsOn;
210 if( process->overrideAssigner != NULL )
211 { if( process->numEnvsWithWork != 0 )
212 { (*process->overrideAssigner)( process, slot ); //calls PR fn that inserts work into slot
213 goto ReturnAfterAssigningWork; //quit for-loop, cause found work
214 }
215 else
216 goto NoWork;
217 }
219 //If here, then no override assigner, so search language envs for work
220 int32 envIdx, numEnvs; PRLangEnv **protoLangEnvsList, *protoLangEnv;
221 protoLangEnvsList = process->protoLangEnvsList;
222 numEnvs = process->numLangEnvs;
223 for( envIdx = 0; envIdx < numEnvs; envIdx++ ) //keep langEnvs in hash & array
224 { protoLangEnv = protoLangEnvsList[envIdx];
225 if( protoLangEnv->numReadyWork > 0 )
226 { bool32
227 didAssignWork =
228 (*protoLangEnv->workAssigner)( PR_int__give_lang_env(protoLangEnv), slot ); //assigner calls PR to put slave/task into slot
230 if(didAssignWork)
231 { protoLangEnv->numReadyWork -= 1;
232 if( protoLangEnv->numReadyWork == 0 )
233 { process->numEnvsWithWork -= 1;
234 }
235 goto ReturnAfterAssigningWork; //quit for-loop, 'cause found work
236 }
237 else
238 goto NoWork; //quit for-loop, cause found work
240 //NOTE: bad search alg -- should start where left off, then wrap around
241 }
242 }
243 //If reach here, then have searched all langEnv's & none have work..
245 NoWork: //No work, if end up here..
246 {
247 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
248 returnSlv = process->idleSlv[coreNum][0]; //only one slot now, so [0]
250 //things that would normally happen in resume(), but idle VPs
251 // never go there
252 returnSlv->numTimesAssignedToASlot++; //gives each idle unit a unique ID
253 Unit newU;
254 newU.vp = returnSlv->slaveNum;
255 newU.task = returnSlv->numTimesAssignedToASlot;
256 addToListOfArrays(Unit,newU,process->unitList);
258 if (returnSlv->numTimesAssignedToASlot > 1) //make a dependency from prev idle unit
259 { Dependency newD; // to this one
260 newD.from_vp = returnSlv->slaveNum;
261 newD.from_task = returnSlv->numTimesAssignedToASlot - 1;
262 newD.to_vp = returnSlv->slaveNum;
263 newD.to_task = returnSlv->numTimesAssignedToASlot;
264 addToListOfArrays(Dependency, newD ,process->ctlDependenciesList);
265 }
266 #endif
267 HOLISTIC__Record_Assigner_end;
268 return FALSE;
269 }
271 ReturnAfterAssigningWork: //All paths goto here.. to provide single point for holistic..
272 {
273 HOLISTIC__Record_Assigner_end;
274 return TRUE;
275 }
276 }
279 //=================================
280 //===
281 //=
282 /*Create task is a special form, that has PR behavior in addition to plugin
283 * behavior. Master calls this first, and it then calls the plugin's
284 * create task handler.
285 *
286 *Note: the requesting slave must be either generic slave or free task slave
287 */
288 inline
289 void
290 PRHandle__CreateTask( PRReqst *req, SlaveVP *slave )
291 { PRMetaTask *protoMetaTask;
292 PRProcess *process;
293 PRLangEnv *protoLangEnv;
294 void *task;
296 process = slave->processSlaveIsIn;
298 protoLangEnv = PR_int__give_proto_lang_env_for_slave( slave,
299 req->langMagicNumber );
301 //Do the langlet's create-task handler, which keeps the task
302 // inside the langlet's lang env, but returns the langMetaTask
303 // so that PR can then put stuff into the prolog
304 //typedef void * (*CreateHandler)( void *, SlaveVP *, void * ); //req, slv, langEnv
305 //
306 task =
307 (*req->createHdlr)(req->langReq, slave, PR_int__give_lang_env(protoLangEnv) );
308 protoMetaTask = PR_int__give_prolog_of_lang_meta_task( task );
309 protoMetaTask->ID = req->ID; //may be NULL
310 protoMetaTask->topLevelFn = req->topLevelFn;
311 protoMetaTask->initData = req->initData;
312 protoMetaTask->processTaskIsIn = process;
314 process->numLiveTasks += 1;
315 protoLangEnv->numLiveWork += 1; //used in wait statements -- small added overhead
317 return;
318 }
320 /*When a task ends, have two scenarios: 1) task ran to completion, or 2) task
321 * has been suspended at some point in its code.
322 *For 1, just decr count of live tasks (and check for end condition) -- the
323 * master loop will decide what goes into the slot freed up by this task end,
324 * so, here, don't worry about assigning a new task to the slot slave.
325 *For 2, the task's slot slave has been converted to a free task slave, which
326 * now has nothing more to do, so send it to the recycle Q (which includes
327 * freeing all the langData and meta task structs alloc'd for it). Then
328 * decrement the live task count and check end condition.
329 *
330 *PR has to update count of live tasks, and check end of process condition.
331 * The "main" can invoke constructs that wait for a process to end, so when
332 * end detected, have to resume what's waiting..
333 *Thing is, that wait involves the main OS thread. That means
334 * PR internals have to do OS thread signaling. Want to do that in the
335 * core controller, which has the original stack of an OS thread. So the
336 * end process handling happens in the core controller.
337 *
338 *So here, when detect process end, signal to the core controller, which will
339 * then do the condition variable notify to the OS thread that's waiting.
340 *
341 *Note: slave may be either a slot slave or a free task slave.
342 */
343 inline
344 void
345 PRHandle__EndTask( PRReqst *req, SlaveVP *requestingSlv )
346 { void *langEnv;
347 PRLangEnv *protoLangEnv;
348 PRProcess *process;
349 void *langMetaTask;
351 process = requestingSlv->processSlaveIsIn;
352 langEnv = PR_int__give_lang_env_of_req( req, requestingSlv ); //magic num in req
353 protoLangEnv = PR_int__give_proto_lang_env( langEnv );
355 langMetaTask = PR_int__give_lang_meta_task_from_slave( requestingSlv, req->langMagicNumber);
357 //Do the langlet's request handler
358 //Want to keep PR structs hidden from plugin, so extract langReq..
359 //This is supposed to free any langlet-malloc'd mem, including meta task
360 (*req->handler)( req->langReq, requestingSlv, langEnv );
362 protoLangEnv->numLiveWork -= 1; //used in wait statements -- small added overhead
363 if( protoLangEnv->numLiveWork == 0 &&
364 numInPrivQ( protoLangEnv->waitingForWorkToEndQ ) > 0 )
365 { SlaveVP *
366 waitingSlave = readPrivQ( protoLangEnv->waitingForWorkToEndQ );
367 //can't resume into langlet that just ended its last work!
368 // and don't have env that the waiter was created in, so resume
369 // into PRServ env..
370 void *
371 resumeEnv = PR_PI__give_lang_env_from_process( process, PRServ_MAGIC_NUMBER );
372 while( waitingSlave != NULL )
373 { //resume a slave that was waiting for work in this env to finish
374 PR_PI__make_slave_ready( waitingSlave, resumeEnv );
375 //get next waiting slave, repeat..
376 waitingSlave = readPrivQ( protoLangEnv->waitingForWorkToEndQ );
377 }
378 }
381 //Now that the langlet's done with it, recycle the slave if it's a freeTaskSlv
382 if( requestingSlv->typeOfVP == FreeTaskSlv )
383 PR_int__recycle_slaveVP( requestingSlv ); //Doesn't decr num live slaves
385 process->numLiveTasks -= 1;
386 //NOTE: end-task is unrelated to work available (just in case wondering)
388 //check End Of Process Condition
389 if( process->numLiveTasks == 0 &&
390 process->numLiveGenericSlvs == 0 )
391 { //Tell the core controller to do wakeup of any waiting OS thread
392 PR_SS__end_process_normally( process );
393 }
394 }
398 /*This is first thing called when creating a slave.. it hands off to the
399 * langlet's creator, then adds updates of its own..
400 *
401 *There's a question of things like lang data, meta tasks, and such..
402 *In creator, only PR related things happen, and things for the langlet whose
403 * creator construct was used.
404 *
405 *Other langlets still get a chance to create langData -- but by registering a
406 * "createLangData" handler in the langEnv. When a construct of the langlet
407 * calls "PR__give_lang_data()", if there is no langData for that langlet,
408 * the PR will call the creator in the langlet's langEnv, place whatever it
409 * makes as the langData in that slave for that langlet, and return that langData
410 *
411 *So, as far as counting things, a langlet is only allowed to count creation
412 * of slaves it creates itself.. may have to change this later.. add a way for
413 * langlet to register a trigger Fn called each time a slave gets created..
414 * need more experience with what langlets will do at create time.. think Cilk
415 * has interesting create behavior.. not sure how that will differ in light
416 * of true tasks and langlet approach. Look at it after all done and start
417 * modifying the langs to be langlets..
418 *
419 *PR itself needs to create the slave, then update numLiveSlaves in process,
420 * copy processID from requestor to newly created
421 */
422 inline
423 void
424 PRHandle__CreateSlave( PRReqst *req, SlaveVP *slave )
425 { SlaveVP *newSlv;
426 PRProcess *process;
427 PRLangEnv *protoLangEnv;
429 process = slave->processSlaveIsIn;
430 protoLangEnv = PR_int__give_proto_lang_env_for_slave( slave, req->langMagicNumber );
432 //create handler, or a future request handler will call PR_PI__make_slave_ready
433 // which will in turn handle updating which langlets and which processes have
434 // work available.
435 //NOTE: create slv has diff prototype than standard reqst hdlr
436 newSlv =
437 (*req->createHdlr)(req->langReq, slave, PR_int__give_lang_env(protoLangEnv));
439 newSlv->typeOfVP = GenericSlv;
440 newSlv->processSlaveIsIn = process;
441 newSlv->ID = req->ID;
442 process->numLiveGenericSlvs += 1; //not same as work ready!
443 protoLangEnv->numLiveWork += 1; //used in wait statements -- small added overhead
444 }
446 /*The dissipate handler has to, update the number of slaves of the type, within
447 * the process, and call the langlet handler linked into the request,
448 * and after that returns, then call the PR function that frees the slave state
449 * (or recycles the slave).
450 *
451 *The PR function that frees the slave state has to also free all of the
452 * langData in the slave.. or else reset all of the langDatas.. by, say, marking
453 * them, then in PR__give_langData( magicNum ) call the langlet registered
454 * "resetLangData" Fn.
455 */
456 inline
457 void
458 PRHandle__EndSlave( PRReqst *req, SlaveVP *slave )
459 { PRProcess *process;
460 PRLangEnv *protoLangEnv;
462 process = slave->processSlaveIsIn;
464 //do the language's dissipate handler
465 protoLangEnv = PR_int__give_proto_lang_env_for_slave( slave, slave->request->langMagicNumber );
467 if(req->handler != NULL)
468 (*req->handler)( req->langReq, slave, PR_int__give_lang_env(protoLangEnv) );
470 protoLangEnv->numLiveWork -= 1; //used in wait statements -- small added overhead
471 if( protoLangEnv->numLiveWork == 0 &&
472 numInPrivQ( protoLangEnv->waitingForWorkToEndQ ) > 0 )
473 { SlaveVP *
474 waitingSlave = readPrivQ( protoLangEnv->waitingForWorkToEndQ );
475 //can't resume into langlet that just ended its last work!
476 // and don't have env that the waiter was created in, so resume
477 // into PRServ env..
478 void *
479 resumeEnv = PR_PI__give_lang_env_from_process( process, PRServ_MAGIC_NUMBER );
480 while( waitingSlave != NULL )
481 { //resume a slave that was waiting for work in this env to finish
482 PR_PI__make_slave_ready( waitingSlave, resumeEnv );
483 //get next waiting slave, repeat..
484 waitingSlave = readPrivQ( protoLangEnv->waitingForWorkToEndQ );
485 }
486 }
488 process->numLiveGenericSlvs -= 1;
489 PR_int__recycle_slaveVP( slave );
490 //NOTE: dissipate is unrelated to work available (just in case wondering)
492 //check End Of Process Condition
493 if( process->numLiveTasks == 0 &&
494 process->numLiveGenericSlvs == 0 )
495 PR_SS__end_process_normally( process );
496 }
498 //=======================
499 //===
500 //=
501 /*Langlet shutdown triggers this, which calls the registered shutdown
502 * handler for the langlet, and removes the lang's env from the process
503 */
504 inline
505 void
506 PRHandle__LangShutdown( PRReqst *req, SlaveVP *requestingSlv )
507 { void *langEnv;
508 PRLangEnv *protoLangEnv;
509 PRProcess *process;
511 process = requestingSlv->processSlaveIsIn;
512 protoLangEnv = PR_int__give_proto_lang_env_from_process( process, req->langMagicNumber );
513 langEnv = PR_int__give_lang_env( protoLangEnv );
515 //call the langlet's registered handler
516 (*protoLangEnv->shutdownHdlr)( langEnv );
518 PR_int__remove_lang_env_from_process_and_free( langEnv ); //removes from process and frees
520 PR_PI__resume_slave_in_PRServ( requestingSlv );
521 }
524 /*This is for OS requests and PR infrastructure requests, which are not
525 * part of the PRServ language -- this is for things that have to be in the
526 * infrastructure of PR itself, such as I/O requests, which have to go through
527 * pthreads inside the core controller..
528 *
529 *As of Jan 2013, doesn't do much of anything..
530 */
531 void inline
532 PRHandle__ServiceReq( SlaveVP *requestingSlv )
533 { PRReqst *req;
534 PRServiceReq *langReq;
535 PRLangEnv *protoLangEnv;
536 int32 magicNumber;
539 req = requestingSlv->request;
541 magicNumber = req->langMagicNumber;
542 protoLangEnv = PR_int__give_proto_lang_env_for_slave( requestingSlv, magicNumber );
544 langReq = PR_PI__take_lang_reqst_from(req);
545 if( langReq == NULL ) return;
546 switch( langReq->reqType ) //lang handlers are all in other file
547 {
548 case make_probe: handleMakeProbe( langReq, protoLangEnv );
549 break;
550 case throw_excp: handleThrowException( langReq, protoLangEnv );
551 break;
552 }
553 }
556 /*These handlers are special -- they don't belong to a language, because they
557 * deal with things internal to PR, so put them here..
558 */
559 inline
560 void
561 handleMakeProbe( PRServiceReq *langReq, PRLangEnv *protoLangEnv )
562 { IntervalProbe *newProbe;
564 newProbe = PR_int__malloc( sizeof(IntervalProbe) );
565 newProbe->nameStr = PR_int__strDup( langReq->nameStr );
566 newProbe->hist = NULL;
567 newProbe->schedChoiceWasRecorded = FALSE;
569 //This runs in masterVP, so no race-condition worries
570 //BUG: move to process
571 newProbe->probeID =
572 addToDynArray( newProbe, _PRTopEnv->dynIntervalProbesInfo );
574 langReq->requestingSlv->dataRetFromReq = newProbe;
576 (*protoLangEnv->makeSlaveReadyFn)( langReq->requestingSlv, PR_int__give_lang_env(protoLangEnv) );
577 }
579 inline
580 void
581 handleThrowException( PRServiceReq *langReq, PRLangEnv *protoLangEnv )
582 {
583 PR_int__throw_exception( langReq->msgStr, langReq->requestingSlv, langReq->exceptionData );
585 (*protoLangEnv->makeSlaveReadyFn)( langReq->requestingSlv, PR_int__give_lang_env(protoLangEnv) );
586 }