view VSs.c @ 17:f83fff8bd4b2

finished instrumentation
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Fri, 31 Aug 2012 18:24:03 +0200
parents 1ffd5df22df9
children c9606ea7abc8
line source
1 /*
2 * Copyright 2010 OpenSourceCodeStewardshipFoundation
3 *
4 * Licensed under BSD
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <malloc.h>
11 #include "Queue_impl/PrivateQueue.h"
12 #include "Hash_impl/PrivateHash.h"
14 #include "VSs.h"
15 #include "Measurement/VSs_Counter_Recording.h"
17 //==========================================================================
19 void
20 VSs__init();
22 void
23 VSs__init_Helper();
24 //==========================================================================
28 //===========================================================================
31 /*These are the library functions *called in the application*
32 *
33 *There's a pattern for the outside sequential code to interact with the
34 * VMS_HW code.
35 *The VMS_HW system is inside a boundary.. every VSs system is in its
36 * own directory that contains the functions for each of the processor types.
37 * One of the processor types is the "seed" processor that starts the
38 * cascade of creating all the processors that do the work.
39 *So, in the directory is a file called "EntryPoint.c" that contains the
40 * function, named appropriately to the work performed, that the outside
41 * sequential code calls. This function follows a pattern:
42 *1) it calls VSs__init()
43 *2) it creates the initial data for the seed processor, which is passed
44 * in to the function
45 *3) it creates the seed VSs processor, with the data to start it with.
46 *4) it calls startVSsThenWaitUntilWorkDone
47 *5) it gets the returnValue from the transfer struc and returns that
48 * from the function
49 *
50 *For now, a new VSs system has to be created via VSs__init every
51 * time an entry point function is called -- later, might add letting the
52 * VSs system be created once, and let all the entry points just reuse
53 * it -- want to be as simple as possible now, and see by using what makes
54 * sense for later..
55 */
59 //===========================================================================
61 /*This is the "border crossing" function -- the thing that crosses from the
62 * outside world, into the VMS_HW world. It initializes and starts up the
63 * VMS system, then creates one processor from the specified function and
64 * puts it into the readyQ. From that point, that one function is resp.
65 * for creating all the other processors, that then create others, and so
66 * forth.
67 *When all the processors, including the seed, have dissipated, then this
68 * function returns. The results will have been written by side-effect via
69 * pointers read from, or written into initData.
70 *
71 *NOTE: no Threads should exist in the outside program that might touch
72 * any of the data reachable from initData passed in to here
73 */
74 void
75 VSs__create_seed_slave_and_do_work( TopLevelFnPtr fnPtr, void *initData )
76 { VSsSemEnv *semEnv;
77 SlaveVP *seedSlv;
78 VSsSemData *semData;
79 VSsTaskStub *threadTaskStub, *parentTaskStub;
81 VSs__init(); //normal multi-thd
83 semEnv = _VMSMasterEnv->semanticEnv;
85 //VSs starts with one processor, which is put into initial environ,
86 // and which then calls create() to create more, thereby expanding work
87 seedSlv = VSs__create_slave_helper( fnPtr, initData,
88 semEnv, semEnv->nextCoreToGetNewSlv++ );
90 //seed slave is a thread slave, so make a thread's task stub for it
91 // and then make another to stand for the seed's parent task. Make
92 // the parent be already ended, and have one child (the seed). This
93 // will make the dissipate handler do the right thing when the seed
94 // is dissipated.
95 threadTaskStub = create_thread_task_stub( initData );
96 parentTaskStub = create_thread_task_stub( NULL );
97 parentTaskStub->isEnded = TRUE;
98 parentTaskStub->numLiveChildThreads = 1; //so dissipate works for seed
99 threadTaskStub->parentTaskStub = parentTaskStub;
100 threadTaskStub->slaveAssignedTo = seedSlv;
102 semData = (VSsSemData *)seedSlv->semanticData;
103 //seedVP is a thread, so has a permanent task
104 semData->needsTaskAssigned = FALSE;
105 semData->taskStub = threadTaskStub;
106 semData->slaveType = ThreadSlv;
108 resume_slaveVP( seedSlv, semEnv ); //returns right away, just queues Slv
110 VMS_SS__start_the_work_then_wait_until_done(); //normal multi-thd
112 VSs__cleanup_after_shutdown();
113 }
116 int32
117 VSs__giveMinWorkUnitCycles( float32 percentOverhead )
118 {
119 return MIN_WORK_UNIT_CYCLES;
120 }
122 int32
123 VSs__giveIdealNumWorkUnits()
124 {
125 return NUM_ANIM_SLOTS * NUM_CORES;
126 }
128 int32
129 VSs__give_number_of_cores_to_schedule_onto()
130 {
131 return NUM_CORES;
132 }
134 /*For now, use TSC -- later, make these two macros with assembly that first
135 * saves jump point, and second jumps back several times to get reliable time
136 */
137 void
138 VSs__start_primitive()
139 { saveLowTimeStampCountInto( ((VSsSemEnv *)(_VMSMasterEnv->semanticEnv))->
140 primitiveStartTime );
141 }
143 /*Just quick and dirty for now -- make reliable later
144 * will want this to jump back several times -- to be sure cache is warm
145 * because don't want comm time included in calc-time measurement -- and
146 * also to throw out any "weird" values due to OS interrupt or TSC rollover
147 */
148 int32
149 VSs__end_primitive_and_give_cycles()
150 { int32 endTime, startTime;
151 //TODO: fix by repeating time-measurement
152 saveLowTimeStampCountInto( endTime );
153 startTime =((VSsSemEnv*)(_VMSMasterEnv->semanticEnv))->primitiveStartTime;
154 return (endTime - startTime);
155 }
157 //===========================================================================
159 /*Initializes all the data-structures for a VSs system -- but doesn't
160 * start it running yet!
161 *
162 *This runs in the main thread -- before VMS starts up
163 *
164 *This sets up the semantic layer over the VMS system
165 *
166 *First, calls VMS_Setup, then creates own environment, making it ready
167 * for creating the seed processor and then starting the work.
168 */
169 void
170 VSs__init()
171 {
172 VMS_SS__init();
173 //masterEnv, a global var, now is partially set up by init_VMS
174 // after this, have VMS_int__malloc and VMS_int__free available
176 VSs__init_Helper();
177 }
180 void idle_fn(void* data, SlaveVP *animatingSlv){
181 while(1){
182 VMS_int__suspend_slaveVP_and_send_req(animatingSlv);
183 }
184 }
186 void
187 VSs__init_Helper()
188 { VSsSemEnv *semanticEnv;
189 int32 i, coreNum, slotNum;
190 VSsSemData *semData;
192 //Hook up the semantic layer's plug-ins to the Master virt procr
193 _VMSMasterEnv->requestHandler = &VSs__Request_Handler;
194 _VMSMasterEnv->slaveAssigner = &VSs__assign_slaveVP_to_slot;
196 //create the semantic layer's environment (all its data) and add to
197 // the master environment
198 semanticEnv = VMS_int__malloc( sizeof( VSsSemEnv ) );
199 _VMSMasterEnv->semanticEnv = semanticEnv;
201 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
202 _VMSMasterEnv->counterHandler = &VSs__counter_handler;
203 VSs__init_counter_data_structs();
204 #endif
206 semanticEnv->shutdownInitiated = FALSE;
207 semanticEnv->coreIsDone = VMS_int__malloc( NUM_CORES * sizeof( bool32 ) );
208 //For each animation slot, there is an idle slave, and an initial
209 // slave assigned as the current-task-slave. Create them here.
210 SlaveVP *idleSlv, *slotTaskSlv;
211 for( coreNum = 0; coreNum < NUM_CORES; coreNum++ )
212 { semanticEnv->coreIsDone[coreNum] = FALSE; //use during shutdown
214 for( slotNum = 0; slotNum < NUM_ANIM_SLOTS; ++slotNum )
215 { idleSlv = VSs__create_slave_helper( &idle_fn, NULL, semanticEnv, 0);
216 idleSlv->coreAnimatedBy = coreNum;
217 idleSlv->animSlotAssignedTo =
218 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
219 semanticEnv->idleSlv[coreNum][slotNum] = idleSlv;
221 slotTaskSlv = VSs__create_slave_helper( &idle_fn, NULL, semanticEnv, 0);
222 slotTaskSlv->coreAnimatedBy = coreNum;
223 slotTaskSlv->animSlotAssignedTo =
224 _VMSMasterEnv->allAnimSlots[coreNum][slotNum];
226 semData = slotTaskSlv->semanticData;
227 semData->needsTaskAssigned = TRUE;
228 semData->slaveType = SlotTaskSlv;
229 semanticEnv->slotTaskSlvs[coreNum][slotNum] = slotTaskSlv;
230 }
231 }
233 //create the ready queues, hash tables used for matching and so forth
234 semanticEnv->slavesReadyToResumeQ = makeVMSQ();
235 semanticEnv->freeExtraTaskSlvQ = makeVMSQ();
236 semanticEnv->taskReadyQ = makeVMSQ();
238 semanticEnv->argPtrHashTbl = makeHashTable32( 16, &VMS_int__free );
239 semanticEnv->commHashTbl = makeHashTable32( 16, &VMS_int__free );
241 semanticEnv->nextCoreToGetNewSlv = 0;
244 //TODO: bug -- turn these arrays into dyn arrays to eliminate limit
245 //semanticEnv->singletonHasBeenExecutedFlags = makeDynArrayInfo( );
246 //semanticEnv->transactionStrucs = makeDynArrayInfo( );
247 for( i = 0; i < NUM_STRUCS_IN_SEM_ENV; i++ )
248 {
249 semanticEnv->fnSingletons[i].endInstrAddr = NULL;
250 semanticEnv->fnSingletons[i].hasBeenStarted = FALSE;
251 semanticEnv->fnSingletons[i].hasFinished = FALSE;
252 semanticEnv->fnSingletons[i].waitQ = makeVMSQ();
253 semanticEnv->transactionStrucs[i].waitingVPQ = makeVMSQ();
254 }
256 semanticEnv->numLiveExtraTaskSlvs = 0; //must be last
257 semanticEnv->numLiveThreadSlvs = 1; //must be last, counts the seed
259 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
260 semanticEnv->unitList = makeListOfArrays(sizeof(Unit),128);
261 semanticEnv->ctlDependenciesList = makeListOfArrays(sizeof(Dependency),128);
262 semanticEnv->commDependenciesList = makeListOfArrays(sizeof(Dependency),128);
263 semanticEnv->dynDependenciesList = makeListOfArrays(sizeof(Dependency),128);
264 semanticEnv->dataDependenciesList = makeListOfArrays(sizeof(Dependency),128);
265 semanticEnv->singletonDependenciesList = makeListOfArrays(sizeof(Dependency),128);
266 semanticEnv->warDependenciesList = makeListOfArrays(sizeof(Dependency),128);
267 semanticEnv->ntonGroupsInfo = makePrivDynArrayOfSize((void***)&(semanticEnv->ntonGroups),8);
269 semanticEnv->hwArcs = makeListOfArrays(sizeof(Dependency),128);
270 memset(semanticEnv->last_in_slot,0,sizeof(NUM_CORES * NUM_ANIM_SLOTS * sizeof(Unit)));
271 #endif
272 }
275 /*Frees any memory allocated by VSs__init() then calls VMS_int__shutdown
276 */
277 void
278 VSs__cleanup_after_shutdown()
279 { VSsSemEnv *semanticEnv;
281 semanticEnv = _VMSMasterEnv->semanticEnv;
283 #ifdef HOLISTIC__TURN_ON_OBSERVE_UCC
284 //UCC
285 FILE* output;
286 int n;
287 char filename[255];
288 for(n=0;n<255;n++)
289 {
290 sprintf(filename, "./counters/UCC.%d",n);
291 output = fopen(filename,"r");
292 if(output)
293 {
294 fclose(output);
295 }else{
296 break;
297 }
298 }
299 if(n<255){
300 printf("Saving UCC to File: %s ...\n", filename);
301 output = fopen(filename,"w+");
302 if(output!=NULL){
303 set_dependency_file(output);
304 //fprintf(output,"digraph Dependencies {\n");
305 //set_dot_file(output);
306 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
307 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
308 forAllInListOfArraysDo(semanticEnv->unitList, &print_unit_to_file);
309 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
310 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
311 forAllInListOfArraysDo( semanticEnv->dataDependenciesList, &print_data_dependency_to_file );
312 forAllInListOfArraysDo( semanticEnv->singletonDependenciesList, &print_singleton_dependency_to_file );
313 forAllInListOfArraysDo( semanticEnv->warDependenciesList, &print_war_dependency_to_file );
314 forAllInDynArrayDo(semanticEnv->ntonGroupsInfo,&print_nton_to_file);
315 //fprintf(output,"}\n");
316 fflush(output);
318 } else
319 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
320 } else {
321 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
322 }
323 //Loop Graph
324 for(n=0;n<255;n++)
325 {
326 sprintf(filename, "./counters/LoopGraph.%d",n);
327 output = fopen(filename,"r");
328 if(output)
329 {
330 fclose(output);
331 }else{
332 break;
333 }
334 }
335 if(n<255){
336 printf("Saving LoopGraph to File: %s ...\n", filename);
337 output = fopen(filename,"w+");
338 if(output!=NULL){
339 set_dependency_file(output);
340 //fprintf(output,"digraph Dependencies {\n");
341 //set_dot_file(output);
342 //FIXME: first line still depends on counters being enabled, replace w/ unit struct!
343 //forAllInDynArrayDo(_VMSMasterEnv->counter_history_array_info, &print_dot_node_info );
344 forAllInListOfArraysDo( semanticEnv->unitList, &print_unit_to_file );
345 forAllInListOfArraysDo( semanticEnv->commDependenciesList, &print_comm_dependency_to_file );
346 forAllInListOfArraysDo( semanticEnv->ctlDependenciesList, &print_ctl_dependency_to_file );
347 forAllInListOfArraysDo( semanticEnv->dataDependenciesList, &print_data_dependency_to_file );
348 forAllInListOfArraysDo( semanticEnv->singletonDependenciesList, &print_singleton_dependency_to_file );
349 forAllInListOfArraysDo( semanticEnv->dynDependenciesList, &print_dyn_dependency_to_file );
350 forAllInListOfArraysDo( semanticEnv->warDependenciesList, &print_war_dependency_to_file );
351 forAllInListOfArraysDo( semanticEnv->hwArcs, &print_hw_dependency_to_file );
352 //fprintf(output,"}\n");
353 fflush(output);
355 } else
356 printf("Opening LoopGraph file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
357 } else {
358 printf("Could not open LoopGraph file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
359 }
362 freeListOfArrays(semanticEnv->unitList);
363 freeListOfArrays(semanticEnv->commDependenciesList);
364 freeListOfArrays(semanticEnv->ctlDependenciesList);
365 freeListOfArrays(semanticEnv->dynDependenciesList);
366 freeListOfArrays(semanticEnv->dataDependenciesList);
367 freeListOfArrays(semanticEnv->warDependenciesList);
368 freeListOfArrays(semanticEnv->singletonDependenciesList);
369 freeListOfArrays(semanticEnv->hwArcs);
371 #endif
372 #ifdef HOLISTIC__TURN_ON_PERF_COUNTERS
373 for(n=0;n<255;n++)
374 {
375 sprintf(filename, "./counters/Counters.%d.csv",n);
376 output = fopen(filename,"r");
377 if(output)
378 {
379 fclose(output);
380 }else{
381 break;
382 }
383 }
384 if(n<255){
385 printf("Saving Counter measurements to File: %s ...\n", filename);
386 output = fopen(filename,"w+");
387 if(output!=NULL){
388 set_counter_file(output);
389 int i;
390 for(i=0;i<NUM_CORES;i++){
391 forAllInListOfArraysDo( semanticEnv->counterList[i], &print_counter_events_to_file );
392 fflush(output);
393 }
395 } else
396 printf("Opening UCC file failed. Please check that folder \"counters\" exists in run directory and has write permission.\n");
397 } else {
398 printf("Could not open UCC file, please clean \"counters\" folder. (Must contain less than 255 files.)\n");
399 }
401 #endif
402 /* It's all allocated inside VMS's big chunk -- that's about to be freed, so
403 * nothing to do here
406 for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
407 {
408 VMS_int__free( semanticEnv->readyVPQs[coreIdx]->startOfData );
409 VMS_int__free( semanticEnv->readyVPQs[coreIdx] );
410 }
411 VMS_int__free( semanticEnv->readyVPQs );
413 freeHashTable( semanticEnv->commHashTbl );
414 VMS_int__free( _VMSMasterEnv->semanticEnv );
415 */
416 VMS_SS__cleanup_at_end_of_shutdown();
417 }
420 //===========================================================================
422 SlaveVP *
423 VSs__create_thread( TopLevelFnPtr fnPtr, void *initData,
424 SlaveVP *creatingThd )
425 { VSsSemReq reqData;
427 //the semantic request data is on the stack and disappears when this
428 // call returns -- it's guaranteed to remain in the VP's stack for as
429 // long as the VP is suspended.
430 reqData.reqType = 0; //know type because in a VMS create req
431 reqData.fnPtr = fnPtr;
432 reqData.initData = initData;
433 reqData.callingSlv = creatingThd;
435 VMS_WL__send_create_slaveVP_req( &reqData, creatingThd );
437 return creatingThd->dataRetFromReq;
438 }
440 /*This is always the last thing done in the code animated by a thread VP.
441 * Normally, this would be the last line of the thread's top level function.
442 * But, if the thread exits from any point, it has to do so by calling
443 * this.
444 *
445 *It simply sends a dissipate request, which handles all the state cleanup.
446 */
447 void
448 VSs__end_thread( SlaveVP *thdToEnd )
449 { VSsSemData *semData;
451 VMS_WL__send_dissipate_req( thdToEnd );
452 }
456 //===========================================================================
459 //======================= task submit and end ==============================
460 /*
461 */
462 void
463 VSs__submit_task( VSsTaskType *taskType, void *args, SlaveVP *animSlv)
464 { VSsSemReq reqData;
466 reqData.reqType = submit_task;
468 reqData.taskType = taskType;
469 reqData.args = args;
470 reqData.callingSlv = animSlv;
472 reqData.taskID = NULL;
474 VMS_WL__send_sem_request( &reqData, animSlv );
475 }
477 inline int32 *
478 VSs__create_taskID_of_size( int32 numInts, SlaveVP *animSlv )
479 { int32 *taskID;
481 taskID = VMS_WL__malloc( sizeof(int32) + numInts * sizeof(int32) );
482 taskID[0] = numInts;
483 return taskID;
484 }
486 void
487 VSs__submit_task_with_ID( VSsTaskType *taskType, void *args, int32 *taskID,
488 SlaveVP *animSlv)
489 { VSsSemReq reqData;
491 reqData.reqType = submit_task;
493 reqData.taskType = taskType;
494 reqData.args = args;
495 reqData.taskID = taskID;
496 reqData.callingSlv = animSlv;
498 VMS_WL__send_sem_request( &reqData, animSlv );
499 }
502 /*This call is the last to happen in every task. It causes the slave to
503 * suspend and get the next task out of the task-queue. Notice there is no
504 * assigner here.. only one slave, no slave ReadyQ, and so on..
505 *Can either make the assigner take the next task out of the taskQ, or can
506 * leave all as it is, and make task-end take the next task.
507 *Note: this fits the case in the new VMS for no-context tasks, so will use
508 * the built-in taskQ of new VMS, and should be local and much faster.
509 *
510 *The task-stub is saved in the animSlv, so the request handler will get it
511 * from there, along with the task-type which has arg types, and so on..
512 *
513 * NOTE: if want, don't need to send the animating SlaveVP around..
514 * instead, can make a single slave per core, and coreCtrlr looks up the
515 * slave from having the core number.
516 *
517 *But, to stay compatible with all the other VMS languages, leave it in..
518 */
519 void
520 VSs__end_task( SlaveVP *animSlv )
521 { VSsSemReq reqData;
523 reqData.reqType = end_task;
524 reqData.callingSlv = animSlv;
526 VMS_WL__send_sem_request( &reqData, animSlv );
527 }
530 void
531 VSs__taskwait(SlaveVP *animSlv)
532 {
533 VSsSemReq reqData;
535 reqData.reqType = taskwait;
536 reqData.callingSlv = animSlv;
538 VMS_WL__send_sem_request( &reqData, animSlv );
539 }
543 //========================== send and receive ============================
544 //
546 inline int32 *
547 VSs__give_self_taskID( SlaveVP *animSlv )
548 {
549 return ((VSsSemData*)animSlv->semanticData)->taskStub->taskID;
550 }
552 //================================ send ===================================
554 void
555 VSs__send_of_type_to( void *msg, const int32 type, int32 *receiverID,
556 SlaveVP *senderSlv )
557 { VSsSemReq reqData;
559 reqData.reqType = send_type_to;
561 reqData.msg = msg;
562 reqData.msgType = type;
563 reqData.receiverID = receiverID;
564 reqData.senderSlv = senderSlv;
566 reqData.nextReqInHashEntry = NULL;
568 VMS_WL__send_sem_request( &reqData, senderSlv );
570 //When come back from suspend, no longer own data reachable from msg
571 }
573 void
574 VSs__send_from_to( void *msg, int32 *senderID, int32 *receiverID, SlaveVP *senderSlv )
575 { VSsSemReq reqData;
577 reqData.reqType = send_from_to;
579 reqData.msg = msg;
580 reqData.senderID = senderID;
581 reqData.receiverID = receiverID;
582 reqData.senderSlv = senderSlv;
584 reqData.nextReqInHashEntry = NULL;
586 VMS_WL__send_sem_request( &reqData, senderSlv );
587 }
590 //================================ receive ================================
592 /*The "type" version of send and receive creates a many-to-one relationship.
593 * The sender is anonymous, and many sends can stack up, waiting to be
594 * received. The same receiver can also have send from-to's
595 * waiting for it, and those will be kept separate from the "type"
596 * messages.
597 */
598 void *
599 VSs__receive_type_to( const int32 type, int32* receiverID, SlaveVP *receiverSlv )
600 { DEBUG__printf1(dbgRqstHdlr,"WL: receive type to %d",receiverID[1] );
601 VSsSemReq reqData;
603 reqData.reqType = receive_type_to;
605 reqData.msgType = type;
606 reqData.receiverID = receiverID;
607 reqData.receiverSlv = receiverSlv;
609 reqData.nextReqInHashEntry = NULL;
611 VMS_WL__send_sem_request( &reqData, receiverSlv );
613 return receiverSlv->dataRetFromReq;
614 }
618 /*Call this at the point a receiving task wants in-coming data.
619 * Use this from-to form when know senderID -- it makes a direct channel
620 * between sender and receiver.
621 */
622 void *
623 VSs__receive_from_to( int32 *senderID, int32 *receiverID, SlaveVP *receiverSlv )
624 {
625 VSsSemReq reqData;
627 reqData.reqType = receive_from_to;
629 reqData.senderID = senderID;
630 reqData.receiverID = receiverID;
631 reqData.receiverSlv = receiverSlv;
633 reqData.nextReqInHashEntry = NULL;
634 DEBUG__printf2(dbgRqstHdlr,"WL: receive from %d to: %d", reqData.senderID[1], reqData.receiverID[1]);
636 VMS_WL__send_sem_request( &reqData, receiverSlv );
638 return receiverSlv->dataRetFromReq;
639 }
644 //==========================================================================
645 //
646 /*A function singleton is a function whose body executes exactly once, on a
647 * single core, no matter how many times the fuction is called and no
648 * matter how many cores or the timing of cores calling it.
649 *
650 *A data singleton is a ticket attached to data. That ticket can be used
651 * to get the data through the function exactly once, no matter how many
652 * times the data is given to the function, and no matter the timing of
653 * trying to get the data through from different cores.
654 */
656 /*asm function declarations*/
657 void asm_save_ret_to_singleton(VSsSingleton *singletonPtrAddr);
658 void asm_write_ret_from_singleton(VSsSingleton *singletonPtrAddr);
660 /*Fn singleton uses ID as index into array of singleton structs held in the
661 * semantic environment.
662 */
663 void
664 VSs__start_fn_singleton( int32 singletonID, SlaveVP *animSlv )
665 {
666 VSsSemReq reqData;
668 //
669 reqData.reqType = singleton_fn_start;
670 reqData.singletonID = singletonID;
672 VMS_WL__send_sem_request( &reqData, animSlv );
673 if( animSlv->dataRetFromReq ) //will be 0 or addr of label in end singleton
674 {
675 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
676 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
677 }
678 }
680 /*Data singleton hands addr of loc holding a pointer to a singleton struct.
681 * The start_data_singleton makes the structure and puts its addr into the
682 * location.
683 */
684 void
685 VSs__start_data_singleton( VSsSingleton **singletonAddr, SlaveVP *animSlv )
686 {
687 VSsSemReq reqData;
689 if( *singletonAddr && (*singletonAddr)->hasFinished )
690 goto JmpToEndSingleton;
692 reqData.reqType = singleton_data_start;
693 reqData.singletonPtrAddr = singletonAddr;
695 VMS_WL__send_sem_request( &reqData, animSlv );
696 if( animSlv->dataRetFromReq ) //either 0 or end singleton's return addr
697 { //Assembly code changes the return addr on the stack to the one
698 // saved into the singleton by the end-singleton-fn
699 //The return addr is at 0x4(%%ebp)
700 JmpToEndSingleton:
701 asm_write_ret_from_singleton(*singletonAddr);
702 }
703 //now, simply return
704 //will exit either from the start singleton call or the end-singleton call
705 }
707 /*Uses ID as index into array of flags. If flag already set, resumes from
708 * end-label. Else, sets flag and resumes normally.
709 *
710 *Note, this call cannot be inlined because the instr addr at the label
711 * inside is shared by all invocations of a given singleton ID.
712 */
713 void
714 VSs__end_fn_singleton( int32 singletonID, SlaveVP *animSlv )
715 {
716 VSsSemReq reqData;
718 //don't need this addr until after at least one singleton has reached
719 // this function
720 VSsSemEnv *semEnv = VMS_int__give_sem_env_for( animSlv );
721 asm_write_ret_from_singleton(&(semEnv->fnSingletons[ singletonID]));
723 reqData.reqType = singleton_fn_end;
724 reqData.singletonID = singletonID;
726 VMS_WL__send_sem_request( &reqData, animSlv );
728 EndSingletonInstrAddr:
729 return;
730 }
732 void
733 VSs__end_data_singleton( VSsSingleton **singletonPtrAddr, SlaveVP *animSlv )
734 {
735 VSsSemReq reqData;
737 //don't need this addr until after singleton struct has reached
738 // this function for first time
739 //do assembly that saves the return addr of this fn call into the
740 // data singleton -- that data-singleton can only be given to exactly
741 // one instance in the code of this function. However, can use this
742 // function in different places for different data-singletons.
743 // (*(singletonAddr))->endInstrAddr = &&EndDataSingletonInstrAddr;
746 asm_save_ret_to_singleton(*singletonPtrAddr);
748 reqData.reqType = singleton_data_end;
749 reqData.singletonPtrAddr = singletonPtrAddr;
751 VMS_WL__send_sem_request( &reqData, animSlv );
752 }
754 /*This executes the function in the masterVP, so it executes in isolation
755 * from any other copies -- only one copy of the function can ever execute
756 * at a time.
757 *
758 *It suspends to the master, and the request handler takes the function
759 * pointer out of the request and calls it, then resumes the VP.
760 *Only very short functions should be called this way -- for longer-running
761 * isolation, use transaction-start and transaction-end, which run the code
762 * between as work-code.
763 */
764 void
765 VSs__animate_short_fn_in_isolation( PtrToAtomicFn ptrToFnToExecInMaster,
766 void *data, SlaveVP *animSlv )
767 {
768 VSsSemReq reqData;
770 //
771 reqData.reqType = atomic;
772 reqData.fnToExecInMaster = ptrToFnToExecInMaster;
773 reqData.dataForFn = data;
775 VMS_WL__send_sem_request( &reqData, animSlv );
776 }
779 /*This suspends to the master.
780 *First, it looks at the VP's data, to see the highest transactionID that VP
781 * already has entered. If the current ID is not larger, it throws an
782 * exception stating a bug in the code. Otherwise it puts the current ID
783 * there, and adds the ID to a linked list of IDs entered -- the list is
784 * used to check that exits are properly ordered.
785 *Next it is uses transactionID as index into an array of transaction
786 * structures.
787 *If the "VP_currently_executing" field is non-null, then put requesting VP
788 * into queue in the struct. (At some point a holder will request
789 * end-transaction, which will take this VP from the queue and resume it.)
790 *If NULL, then write requesting into the field and resume.
791 */
792 void
793 VSs__start_transaction( int32 transactionID, SlaveVP *animSlv )
794 {
795 VSsSemReq reqData;
797 //
798 reqData.callingSlv = animSlv;
799 reqData.reqType = trans_start;
800 reqData.transID = transactionID;
802 VMS_WL__send_sem_request( &reqData, animSlv );
803 }
805 /*This suspends to the master, then uses transactionID as index into an
806 * array of transaction structures.
807 *It looks at VP_currently_executing to be sure it's same as requesting VP.
808 * If different, throws an exception, stating there's a bug in the code.
809 *Next it looks at the queue in the structure.
810 *If it's empty, it sets VP_currently_executing field to NULL and resumes.
811 *If something in, gets it, sets VP_currently_executing to that VP, then
812 * resumes both.
813 */
814 void
815 VSs__end_transaction( int32 transactionID, SlaveVP *animSlv )
816 {
817 VSsSemReq reqData;
819 //
820 reqData.callingSlv = animSlv;
821 reqData.reqType = trans_end;
822 reqData.transID = transactionID;
824 VMS_WL__send_sem_request( &reqData, animSlv );
825 }
827 //======================== Internal ==================================
828 /*
829 */
830 SlaveVP *
831 VSs__create_slave_with( TopLevelFnPtr fnPtr, void *initData,
832 SlaveVP *creatingSlv )
833 { VSsSemReq reqData;
835 //the semantic request data is on the stack and disappears when this
836 // call returns -- it's guaranteed to remain in the VP's stack for as
837 // long as the VP is suspended.
838 reqData.reqType = 0; //know type because in a VMS create req
839 reqData.coreToAssignOnto = -1; //means round-robin assign
840 reqData.fnPtr = fnPtr;
841 reqData.initData = initData;
842 reqData.callingSlv = creatingSlv;
844 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
846 return creatingSlv->dataRetFromReq;
847 }
849 SlaveVP *
850 VSs__create_slave_with_affinity( TopLevelFnPtr fnPtr, void *initData,
851 SlaveVP *creatingSlv, int32 coreToAssignOnto )
852 { VSsSemReq reqData;
854 //the semantic request data is on the stack and disappears when this
855 // call returns -- it's guaranteed to remain in the VP's stack for as
856 // long as the VP is suspended.
857 reqData.reqType = create_slave_w_aff; //not used, May 2012
858 reqData.coreToAssignOnto = coreToAssignOnto;
859 reqData.fnPtr = fnPtr;
860 reqData.initData = initData;
861 reqData.callingSlv = creatingSlv;
863 VMS_WL__send_create_slaveVP_req( &reqData, creatingSlv );
865 return creatingSlv->dataRetFromReq;
866 }