annotate VMS.c @ 22:1dbc7f6e3e67

Full VMS test -- works
author Me
date Wed, 30 Jun 2010 13:10:59 -0700
parents 734c665500e4
children 2b161e1a50ee
rev   line source
Me@0 1 /*
Me@0 2 * Copyright 2010 OpenSourceCodeStewardshipFoundation
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@0 13
Me@0 14
Me@22 15 //===========================================================================
Me@22 16 void
Me@22 17 shutdownFn( void *dummy, VirtProcr *dummy2 );
Me@22 18
Me@22 19 void
Me@22 20 create_sched_slots( MasterEnv *masterEnv );
Me@22 21
Me@22 22 //===========================================================================
Me@22 23
Me@0 24 /*Setup has two phases:
Me@0 25 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
Me@8 26 * the master virt procr into the work-queue, ready for first "call"
Me@8 27 * 2) Semantic layer then does its own init, which creates the seed virt
Me@8 28 * procr inside the semantic layer, ready to schedule it when
Me@0 29 * asked by the first run of the masterLoop.
Me@0 30 *
Me@0 31 *This part is bit weird because VMS really wants to be "always there", and
Me@0 32 * have applications attach and detach.. for now, this VMS is part of
Me@0 33 * the app, so the VMS system starts up as part of running the app.
Me@0 34 *
Me@8 35 *The semantic layer is isolated from the VMS internals by making the
Me@8 36 * semantic layer do setup to a state that it's ready with its
Me@8 37 * initial virt procrs, ready to schedule them to slots when the masterLoop
Me@0 38 * asks. Without this pattern, the semantic layer's setup would
Me@8 39 * have to modify slots directly to assign the initial virt-procrs, and put
Me@0 40 * them into the workQ itself, breaking the isolation completely.
Me@0 41 *
Me@0 42 *
Me@8 43 *The semantic layer creates the initial virt procr(s), and adds its
Me@8 44 * own environment to masterEnv, and fills in the pointers to
Me@0 45 * the requestHandler and slaveScheduler plug-in functions
Me@8 46 */
Me@8 47
Me@8 48 /*This allocates VMS data structures, populates the master VMSProc,
Me@0 49 * and master environment, and returns the master environment to the semantic
Me@0 50 * layer.
Me@0 51 */
Me@8 52 void
Me@8 53 VMS__init()
Me@1 54 { MasterEnv *masterEnv;
Me@12 55 CASQueueStruc *workQ;
Me@1 56
Me@0 57 //Make the central work-queue
Me@12 58 _VMSWorkQ = makeCASQ();
Me@1 59 workQ = _VMSWorkQ;
Me@0 60
Me@1 61 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
Me@1 62 masterEnv = _VMSMasterEnv;
Me@0 63
Me@8 64 //create the master virtual processor
Me@8 65 masterEnv->masterVirtPr = VMS__create_procr( &masterLoop, masterEnv );
Me@0 66
Me@1 67 create_sched_slots( masterEnv );
Me@0 68
Me@8 69 //Set slot 0 to be the master virt procr & set flags just in case
Me@8 70 masterEnv->schedSlots[0]->needsProcrAssigned = FALSE; //says don't touch
Me@8 71 masterEnv->schedSlots[0]->workIsDone = FALSE; //says don't touch
Me@1 72 masterEnv->schedSlots[0]->procrAssignedToSlot = masterEnv->masterVirtPr;
Me@22 73 masterEnv->masterVirtPr->schedSlot = masterEnv->schedSlots[0];
Me@22 74
Me@1 75 //First core loop to start up gets this, which will schedule seed Pr
Me@1 76 //TODO: debug: check address of masterVirtPr
Me@22 77 writeCASQ( masterEnv->masterVirtPr, workQ );
Me@12 78
Me@12 79 numProcrsCreated = 1;
Me@0 80 }
Me@0 81
Me@0 82
Me@0 83 void
Me@1 84 create_sched_slots( MasterEnv *masterEnv )
Me@8 85 { SchedSlot **schedSlots, **filledSlots;
Me@0 86 int i;
Me@0 87
Me@8 88 schedSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
Me@8 89 filledSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
Me@8 90 masterEnv->schedSlots = schedSlots;
Me@8 91 masterEnv->filledSlots = filledSlots;
Me@8 92
Me@1 93 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
Me@0 94 {
Me@8 95 schedSlots[i] = malloc( sizeof(SchedSlot) );
Me@8 96
Me@1 97 //Set state to mean "handling requests done, slot needs filling"
Me@8 98 schedSlots[i]->workIsDone = FALSE;
Me@8 99 schedSlots[i]->needsProcrAssigned = TRUE;
Me@0 100 }
Me@0 101 }
Me@0 102
Me@8 103
Me@0 104 /*Semantic layer calls this when it want the system to start running..
Me@0 105 *
Me@0 106 *This creates the core loops, pins them to physical cores, gives them the
Me@0 107 * pointer to the workQ, and starts them running.
Me@0 108 */
Me@12 109 void
Me@0 110 VMS__start()
Me@12 111 { int coreIdx;
Me@0 112
Me@14 113 //TODO: Save "orig" stack pointer and frame ptr -- restore in VMS__end()
Me@8 114 //Create the win threads that animate the core loops
Me@8 115 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
Me@8 116 {
Me@12 117 coreLoopThdParams[coreIdx] = (ThdParams *)malloc( sizeof(ThdParams) );
Me@12 118 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
Me@0 119
Me@12 120 coreLoopThdHandles[coreIdx] =
Me@12 121 CreateThread ( NULL, // Security attributes
Me@12 122 0, // Stack size
Me@12 123 coreLoop,
Me@12 124 coreLoopThdParams[coreIdx],
Me@12 125 CREATE_SUSPENDED,
Me@12 126 &(coreLoopThdIds[coreIdx])
Me@12 127 );
Me@12 128 ResumeThread( coreLoopThdHandles[coreIdx] ); //starts thread
Me@8 129 }
Me@8 130 }
Me@0 131
Me@0 132
Me@0 133
Me@8 134 /*Create stack, then create __cdecl structure on it and put initialData and
Me@8 135 * pointer to the new structure instance into the parameter positions on
Me@8 136 * the stack
Me@8 137 *Then put function pointer into nextInstrPt -- the stack is setup in std
Me@8 138 * call structure, so jumping to function ptr is same as a GCC generated
Me@8 139 * function call
Me@8 140 *No need to save registers on old stack frame, because there's no old
Me@8 141 * animator state to return to --
Me@8 142 *
Me@8 143 */
Me@8 144 VirtProcr *
Me@8 145 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
Me@8 146 { VirtProcr *newPr;
Me@8 147 char *stackLocs, *stackPtr;
Me@8 148
Me@8 149 newPr = malloc( sizeof(VirtProcr) );
Me@12 150 newPr->procrID = numProcrsCreated++;
Me@8 151 newPr->nextInstrPt = fnPtr;
Me@8 152 newPr->initialData = initialData;
Me@8 153
Me@14 154 //fnPtr takes two params -- void *initData & void *animProcr
Me@8 155 //alloc stack locations, make stackPtr be the highest addr minus room
Me@14 156 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
Me@14 157 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
Me@22 158 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
Me@22 159 newPr->startOfStack = stackLocs;
Me@22 160 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
Me@8 161 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
Me@22 162 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
Me@14 163 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
Me@8 164 newPr->stackPtr = stackPtr; //core loop will switch to this, then
Me@8 165 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
Me@8 166
Me@8 167 return newPr;
Me@8 168 }
Me@8 169
Me@8 170
Me@0 171 /*there is a label inside this function -- save the addr of this label in
Me@0 172 * the callingPr struc, as the pick-up point from which to start the next
Me@0 173 * work-unit for that procr. If turns out have to save registers, then
Me@0 174 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
Me@0 175 * "done with work-unit" label. The procr struc is in the request in the
Me@0 176 * slave that animated the just-ended work-unit, so all the state is saved
Me@0 177 * there, and will get passed along, inside the request handler, to the
Me@0 178 * next work-unit for that procr.
Me@0 179 */
Me@8 180 void
Me@22 181 VMS__suspend_procr( VirtProcr *callingPr )
Me@14 182 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
Me@14 183 void *coreLoopFramePtr;
Me@0 184
Me@14 185 //The request to master will cause this suspended virt procr to get
Me@14 186 // scheduled again at some future point -- to resume, core loop jumps
Me@14 187 // to the resume point (below), which causes restore of saved regs and
Me@14 188 // "return" from this call.
Me@1 189 callingPr->nextInstrPt = &&ResumePt;
Me@1 190
Me@1 191 //return ownership of the virt procr and sched slot to Master virt pr
Me@1 192 callingPr->schedSlot->workIsDone = TRUE;
Me@14 193 // coreIdx = callingPr->coreAnimatedBy;
Me@1 194
Me@18 195 stackPtrAddr = &(callingPr->stackPtr);
Me@18 196 framePtrAddr = &(callingPr->framePtr);
Me@14 197
Me@14 198 jmpPt = callingPr->coreLoopStartPt;
Me@14 199 coreLoopFramePtr = callingPr->coreLoopFramePtr;//need this only
Me@18 200 coreLoopStackPtr = callingPr->coreLoopStackPtr;//shouldn't need -- safety
Me@1 201
Me@14 202 //Save the virt procr's stack and frame ptrs, restore coreloop's frame
Me@14 203 // ptr, then jump back to "start" of core loop
Me@22 204 //Note, GCC compiles to assembly that saves esp and ebp in the stack
Me@22 205 // frame -- so have to explicitly do assembly that saves to memory
Me@18 206 asm volatile("movl %0, %%eax; \
Me@18 207 movl %%esp, (%%eax); \
Me@18 208 movl %1, %%eax; \
Me@18 209 movl %%ebp, (%%eax); \
Me@18 210 movl %2, %%eax; \
Me@18 211 movl %3, %%esp; \
Me@18 212 movl %4, %%ebp; \
Me@18 213 jmp %%eax " \
Me@18 214 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
Me@18 215 /* inputs */ : "g" (jmpPt), "g"(coreLoopStackPtr), "g"(coreLoopFramePtr)\
Me@18 216 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
Me@12 217 ); //list everything as clobbered to force GCC to save all
Me@12 218 // live vars that are in regs on stack before this
Me@12 219 // assembly, so that stack pointer is correct, before jmp
Me@1 220
Me@1 221 ResumePt:
Me@0 222 return;
Me@0 223 }
Me@0 224
Me@22 225
Me@22 226
Me@22 227 /*This is equivalent to "jump back to core loop" -- it's mainly only used
Me@22 228 * just after adding dissipate request to a processor -- so the semantic
Me@22 229 * layer is the only place it will be seen and/or used.
Me@22 230 *
Me@22 231 *It does almost the same thing as suspend, except don't need to save the
Me@22 232 * stack nor set the nextInstrPt
Me@22 233 *
Me@22 234 *As of June 30, 2010 just implementing as a call to suspend -- just sugar
Me@22 235 */
Me@8 236 void
Me@22 237 VMS__return_from_fn( VirtProcr *animatingPr )
Me@8 238 {
Me@22 239 VMS__suspend_procr( animatingPr );
Me@1 240 }
Me@1 241
Me@22 242
Me@22 243 /*Not sure yet the form going to put "dissipate" in, so this is the third
Me@22 244 * possibility -- the semantic layer can just make a macro that looks like
Me@22 245 * a call to its name, then expands to a call to this.
Me@8 246 *
Me@22 247 *As of June 30, 2010 this looks like the top choice..
Me@8 248 *
Me@22 249 *This adds a request to dissipate, then suspends the processor so that the
Me@22 250 * request handler will receive the request. The request handler is what
Me@22 251 * does the work of freeing memory and removing the processor from the
Me@22 252 * semantic environment's data structures.
Me@22 253 *The request handler also is what figures out when to shutdown the VMS
Me@22 254 * system -- which causes all the core loop threads to die, and returns from
Me@22 255 * the call that started up VMS to perform the work.
Me@22 256 *
Me@22 257 *This form is a bit misleading to understand if one is trying to figure out
Me@22 258 * how VMS works -- it looks like a normal function call, but inside it
Me@22 259 * sends a request to the request handler and suspends the processor, which
Me@22 260 * jumps out of the VMS__dissipate_procr function, and out of all nestings
Me@22 261 * above it, transferring the work of dissipating to the request handler,
Me@22 262 * which then does the actual work -- causing the processor that animated
Me@22 263 * the call of this function to disappear and the "hanging" state of this
Me@22 264 * function to just poof into thin air -- the virtual processor's trace
Me@22 265 * never returns from this call, but instead the virtual processor's trace
Me@22 266 * gets suspended in this call and all the virt processor's state disap-
Me@22 267 * pears -- making that suspend the last thing in the virt procr's trace.
Me@8 268 */
Me@8 269 void
Me@22 270 VMS__dissipate_procr( VirtProcr *procrToDissipate )
Me@22 271 { VMSReqst *req;
Me@22 272
Me@22 273 req = malloc( sizeof(VMSReqst) );
Me@22 274 // req->virtProcrFrom = callingPr;
Me@22 275 req->reqType = dissipate;
Me@22 276 req->nextReqst = procrToDissipate->requests;
Me@22 277 procrToDissipate->requests = req;
Me@22 278
Me@22 279 VMS__suspend_procr( procrToDissipate );
Me@22 280 }
Me@22 281
Me@22 282
Me@22 283 /*This inserts the semantic-layer's request data into standard VMS carrier
Me@22 284 */
Me@22 285 inline void
Me@22 286 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr )
Me@22 287 { VMSReqst *req;
Me@22 288
Me@22 289 req = malloc( sizeof(VMSReqst) );
Me@22 290 // req->virtProcrFrom = callingPr;
Me@22 291 req->reqType = semantic;
Me@22 292 req->semReqData = semReqData;
Me@22 293 req->nextReqst = callingPr->requests;
Me@22 294 callingPr->requests = req;
Me@22 295 }
Me@22 296
Me@22 297
Me@22 298 /*This creates a request of type "dissipate" -- which will cause the virt
Me@22 299 * processor's state and owned locations to be freed
Me@22 300 */
Me@22 301 inline void
Me@22 302 VMS__send_dissipate_request( VirtProcr *procrToDissipate )
Me@22 303 { VMSReqst *req;
Me@22 304
Me@22 305 req = malloc( sizeof(VMSReqst) );
Me@22 306 // req->virtProcrFrom = callingPr;
Me@22 307 req->reqType = dissipate;
Me@22 308 req->nextReqst = procrToDissipate->requests;
Me@22 309 procrToDissipate->requests = req;
Me@22 310 }
Me@22 311
Me@22 312
Me@22 313 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
Me@22 314 // of a request -- IE call with both a virt procr and a fn-ptr to request
Me@22 315 // freer (or maybe put request freer as a field in virt procr?)
Me@22 316 void
Me@22 317 VMS__remove_and_free_top_request( VirtProcr *procrWithReq )
Me@22 318 { VMSReqst *req;
Me@22 319
Me@22 320 req = procrWithReq->requests;
Me@22 321 procrWithReq->requests = procrWithReq->requests->nextReqst;
Me@22 322 free( req );
Me@22 323 }
Me@22 324
Me@22 325 /*This must be called by the request handler plugin -- it cannot be called
Me@22 326 * from the semantic library "dissipate processor" function -- instead, the
Me@22 327 * semantic layer has to generate a request for the plug-in to call this
Me@22 328 * function.
Me@22 329 *The reason is that this frees the virtual processor's stack -- which is
Me@22 330 * still in use inside semantic library calls!
Me@22 331 *
Me@22 332 *This frees or recycles all the state owned by and comprising the animating
Me@22 333 * virtual procr. It frees any state that was malloc'd by the VMS system
Me@22 334 * itself, and asks the VMS system to dis-own any VMS__malloc'd locations.
Me@22 335 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
Me@22 336 * state, then that state gets freed (or sent to recycling) as a side-effect
Me@22 337 * of dis-owning it.
Me@22 338 */
Me@22 339 void
Me@22 340 VMS__free_procr_locs( VirtProcr *animatingPr )
Me@22 341 {
Me@22 342 //dis-own all locations owned by this processor, causing to be freed
Me@22 343 // any locations that it is (was) sole owner of
Me@22 344 //TODO: implement VMS__malloc system, including "give up ownership"
Me@22 345
Me@22 346 VMS__remove_and_free_top_request( animatingPr );
Me@22 347 free( animatingPr->startOfStack );
Me@22 348
Me@22 349 //NOTE: animatingPr->semanticData should either have been allocated
Me@22 350 // with VMS__malloc, or else freed in the request handler plug-in.
Me@22 351 //NOTE: initialData was given to the processor, so should either have
Me@22 352 // been alloc'd with VMS__malloc, or freed by the level above animPr.
Me@22 353 //So, all that's left to free here is the VirtProcr struc itself
Me@22 354 free( animatingPr );
Me@22 355 }
Me@22 356
Me@22 357
Me@22 358 /*The semantic layer figures out when the work is done ( perhaps by a call
Me@22 359 * in the application to "work all done", or perhaps all the virtual
Me@22 360 * processors have dissipated.. a.s.o. )
Me@22 361 *
Me@22 362 *The semantic layer is responsible for making sure all work has fully
Me@22 363 * completed before using this to shutdown the VMS system.
Me@22 364 *
Me@22 365 *After the semantic layer has determined it wants to shut down, the
Me@22 366 * next time the Master Loop calls the scheduler plug-in, the scheduler
Me@22 367 * then calls this function and returns the virtual processor it gets back.
Me@22 368 *
Me@22 369 *When the shut-down processor runs, it first frees all locations malloc'd to
Me@22 370 * the VMS system (that wasn't
Me@22 371 * specified as return-locations). Then it creates one core-loop shut-down
Me@22 372 * processor for each core loop and puts them all into the workQ. When a
Me@22 373 * core loop animates a core loop shut-down processor, it causes exit-thread
Me@22 374 * to run, and when all core loop threads have exited, then the "wait for
Me@22 375 * work to finish" in the main thread is woken, and the function-call that
Me@22 376 * started all the work returns.
Me@22 377 *
Me@22 378 *The function animated by this processor performs the shut-down work.
Me@22 379 */
Me@22 380 VirtProcr *
Me@22 381 VMS__create_the_shutdown_procr()
Me@22 382 {
Me@22 383 return VMS__create_procr( &shutdownFn, NULL );
Me@22 384 }
Me@22 385
Me@22 386
Me@22 387 /*This is the function run by the special "shut-down" processor
Me@22 388 *
Me@22 389 *The _VMSMasterEnv is needed by this shut down function, so the "wait"
Me@22 390 * function run in the main loop has to free it, and the thread-related
Me@22 391 * locations (coreLoopThdParams a.s.o.).
Me@22 392 *However, the semantic environment and all data malloc'd to VMS can be
Me@22 393 * freed here.
Me@22 394 *
Me@22 395 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
Me@22 396 * locations it needs -- they will be automatically freed by the standard
Me@22 397 * "free all owned locations"
Me@22 398 *
Me@22 399 *Free any locations malloc'd to the VMS system (that weren't
Me@22 400 * specified as return-locations).
Me@22 401 *Then create one core-loop shut-down processor for each core loop and puts
Me@22 402 * them all into the workQ.
Me@22 403 */
Me@22 404 void
Me@22 405 shutdownFn( void *dummy, VirtProcr *animatingPr )
Me@8 406 { int coreIdx;
Me@14 407 VirtProcr *shutDownPr;
Me@22 408 CASQueueStruc *workQ = _VMSWorkQ;
Me@22 409
Me@22 410 //free all the locations owned within the VMS system
Me@22 411 //TODO: write VMS__malloc and free.. -- take the DKU malloc as starting pt
Me@22 412
Me@22 413 //make the core loop shut-down processors and put them into the workQ
Me@8 414 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
Me@8 415 {
Me@14 416 shutDownPr = VMS__create_procr( NULL, NULL );
Me@14 417 shutDownPr->nextInstrPt = _VMSMasterEnv->coreLoopShutDownPt;
Me@22 418 writeCASQ( shutDownPr, workQ );
Me@8 419 }
Me@22 420
Me@22 421 //This is an issue: the animating processor of this function may not
Me@22 422 // get its request handled before all the cores have shutdown.
Me@22 423 //TODO: after all the threads stop, clean out the MasterEnv, the
Me@22 424 // SemanticEnv, and the workQ before returning.
Me@22 425 VMS__send_dissipate_request( animatingPr );
Me@22 426 VMS__suspend_procr( animatingPr ); //will never come back from this
Me@12 427 }
Me@12 428
Me@12 429
Me@12 430
Me@12 431 inline TSCount getTSCount()
Me@12 432 { unsigned int low, high;
Me@12 433 TSCount out;
Me@12 434
Me@12 435 saveTimeStampCountInto( low, high );
Me@12 436 out = high;
Me@12 437 out = (out << 32) + low;
Me@12 438 return out;
Me@12 439 }
Me@12 440