annotate MasterLoop.c @ 53:42dd44df1bb0

Init changed to only use VMS__malloc & uses VMS__malloc versions of utilities
author Me
date Mon, 01 Nov 2010 21:21:32 -0700
parents 8f7141a9272e
children 3bac84e4e56e
rev   line source
Me@0 1 /*
Me@38 2 * Copyright 2010 OpenSourceStewardshipFoundation
Me@43 3 *
Me@0 4 * Licensed under BSD
Me@0 5 */
Me@0 6
Me@0 7
Me@0 8
Me@0 9 #include <stdio.h>
Me@9 10 #include <stddef.h>
Me@0 11
Me@0 12 #include "VMS.h"
Me@0 13
Me@0 14
Me@0 15
Me@0 16 /*This code is animated by the virtual Master processor.
Me@0 17 *
Me@11 18 *Polls each sched slot exactly once, hands any requests made by a newly
Me@11 19 * done slave to the "request handler" plug-in function
Me@0 20 *
Me@11 21 *Any slots that need a virt procr assigned are given to the "schedule"
Me@11 22 * plug-in function, which tries to assign a virt procr (slave) to it.
Me@0 23 *
Me@11 24 *When all slots needing a processor have been given to the schedule plug-in,
Me@11 25 * a fraction of the procrs successfully scheduled are put into the
Me@11 26 * work queue, then a continuation of this function is put in, then the rest
Me@11 27 * of the virt procrs that were successfully scheduled.
Me@0 28 *
Me@11 29 *The first thing the continuation does is busy-wait until the previous
Me@11 30 * animation completes. This is because an (unlikely) continuation may
Me@11 31 * sneak through queue before previous continuation is done putting second
Me@11 32 * part of scheduled slaves in, which is the only race condition.
Me@0 33 *
Me@0 34 */
Me@0 35
Me@4 36 /*May 29, 2010 -- birth a Master during init so that first core loop to
Me@11 37 * start running gets it and does all the stuff for a newly born --
Me@11 38 * from then on, will be doing continuation, but do suspension self
Me@4 39 * directly at end of master loop
Me@4 40 *So VMS__init just births the master virtual processor same way it births
Me@4 41 * all the others -- then does any extra setup needed and puts it into the
Me@4 42 * work queue.
Me@4 43 *However means have to make masterEnv a global static volatile the same way
Me@31 44 * did with readyToAnimateQ in core loop. -- for performance, put the
Me@11 45 * jump to the core loop directly in here, and have it directly jump back.
Me@31 46 *
Me@31 47 *
Me@31 48 *Aug 18, 2010 -- Going to a separate MasterVP for each core, to see if this
Me@31 49 * avoids the suspected bug in the system stack that causes bizarre faults
Me@31 50 * at random places in the system code.
Me@31 51 *
Me@31 52 *So, this function is coupled to each of the MasterVPs, -- meaning this
Me@31 53 * function can't rely on a particular stack and frame -- each MasterVP that
Me@31 54 * animates this function has a different one.
Me@31 55 *
Me@31 56 *At this point, the masterLoop does not write itself into the queue anymore,
Me@31 57 * instead, the coreLoop acquires the masterLock when it has nothing to
Me@31 58 * animate, and then animates its own masterLoop. However, still try to put
Me@31 59 * several AppVPs into the queue to amortize the startup cost of switching
Me@31 60 * to the MasterVP. Note, don't have to worry about latency of requests much
Me@31 61 * because most requests generate work for same core -- only latency issue
Me@31 62 * is case when other cores starved and one core's requests generate work
Me@31 63 * for them -- so keep max in queue to 3 or 4..
Me@4 64 */
Me@31 65 void masterLoop( void *initData, VirtProcr *animatingPr )
Me@21 66 {
Me@31 67 int slotIdx;
Me@21 68 VirtProcr *schedVirtPr;
Me@31 69 SchedSlot *currSlot, **schedSlots;
Me@0 70 MasterEnv *masterEnv;
Me@31 71 VMSQueueStruc *readyToAnimateQ;
Me@4 72
Me@0 73 SlaveScheduler slaveScheduler;
Me@0 74 RequestHandler requestHandler;
Me@31 75 void *semanticEnv;
Me@0 76
Me@31 77 int thisCoresIdx;
Me@31 78 VirtProcr *masterPr;
Me@31 79 volatile VirtProcr *volatileMasterPr;
Me@31 80
Me@31 81 volatileMasterPr = animatingPr;
Me@31 82 masterPr = volatileMasterPr; //used to force re-define after jmp
Me@31 83
Me@31 84 //First animation of each MasterVP will in turn animate this part
Me@31 85 // of setup code.. (VP creator sets up the stack as if this function
Me@31 86 // was called normally, but actually get here by jmp)
Me@31 87 //So, setup values about stack ptr, jmp pt and all that
Me@4 88 masterPr->nextInstrPt = &&masterLoopStartPt;
Me@0 89
Me@26 90
Me@31 91 //Note, got rid of writing the stack and frame ptr up here, because
Me@31 92 // only one
Me@31 93 // core can ever animate a given MasterVP, so don't need to communicate
Me@31 94 // new frame and stack ptr to the MasterVP storage before a second
Me@31 95 // version of that MasterVP can get animated on a different core.
Me@31 96 //Also got rid of the busy-wait.
Me@26 97
Me@31 98
Me@4 99 masterLoopStartPt:
Me@38 100 //============================= MEASUREMENT STUFF ========================
Me@38 101 #ifdef MEAS__TIME_MASTER
Me@38 102 //Total Master time includes one coreloop time -- just assume the core
Me@38 103 // loop time is same for Master as for AppVPs, even though it will be
Me@41 104 // smaller due to high predictability of the fixed jmp.
Me@38 105 saveLowTimeStampCountInto( masterPr->startMasterTSCLow );
Me@38 106 #endif
Me@38 107 //========================================================================
Me@0 108
Me@31 109 masterEnv = _VMSMasterEnv;
Me@4 110
Me@31 111 //TODO: check that compiles so that always re-define from frame-storage
Me@31 112 masterPr = volatileMasterPr; //just to make sure after jmp
Me@31 113 thisCoresIdx = masterPr->coreAnimatedBy;
Me@31 114 readyToAnimateQ = masterEnv->readyToAnimateQs[thisCoresIdx];
Me@31 115 schedSlots = masterEnv->allSchedSlots[thisCoresIdx];
Me@4 116
Me@0 117 requestHandler = masterEnv->requestHandler;
Me@0 118 slaveScheduler = masterEnv->slaveScheduler;
Me@21 119 semanticEnv = masterEnv->semanticEnv;
Me@0 120
Me@0 121
Me@31 122 //Poll each slot's Done flag
Me@26 123 for( slotIdx = 0; slotIdx < NUM_SCHED_SLOTS; slotIdx++)
Me@0 124 {
Me@4 125 currSlot = schedSlots[ slotIdx ];
Me@0 126
Me@4 127 if( currSlot->workIsDone )
Me@0 128 {
Me@4 129 currSlot->workIsDone = FALSE;
Me@4 130 currSlot->needsProcrAssigned = TRUE;
Me@0 131
Me@0 132 //process requests from slave to master
Me@21 133 (*requestHandler)( currSlot->procrAssignedToSlot, semanticEnv );
Me@0 134 }
Me@4 135 if( currSlot->needsProcrAssigned )
Me@4 136 { //give slot a new virt procr
Me@21 137 schedVirtPr =
Me@31 138 (*slaveScheduler)( semanticEnv, thisCoresIdx );
Me@0 139
Me@21 140 if( schedVirtPr != NULL )
Me@21 141 { currSlot->procrAssignedToSlot = schedVirtPr;
Me@26 142 schedVirtPr->schedSlot = currSlot;
Me@26 143 currSlot->needsProcrAssigned = FALSE;
Me@4 144
Me@31 145 writeSRSWQ( schedVirtPr, readyToAnimateQ );
Me@0 146 }
Me@0 147 }
Me@0 148 }
Me@0 149
Me@26 150
Me@31 151 //Save stack ptr and frame, restore CoreLoop's stack and frame,
Me@31 152 // and clear the MasterLock
Me@21 153 //TODO: cafefully verify don't need to force saving anything to stack
Me@21 154 // before jumping back to core loop.
Me@31 155 void *stackPtrAddr, *framePtrAddr, *masterLockAddr;
Me@31 156 void *jmpPt, *coreLoopFramePtr, *coreLoopStackPtr;
Me@31 157
Me@21 158 stackPtrAddr = &(masterPr->stackPtr);
Me@21 159 framePtrAddr = &(masterPr->framePtr);
Me@31 160 masterLockAddr = &(_VMSMasterEnv->masterLock);
Me@21 161
Me@31 162 jmpPt = _VMSMasterEnv->coreLoopStartPt;
Me@21 163 coreLoopFramePtr = masterPr->coreLoopFramePtr;//need this only
Me@21 164 coreLoopStackPtr = masterPr->coreLoopStackPtr;//shouldn't need -- safety
Me@21 165
Me@38 166 #ifdef MEAS__TIME_MASTER
Me@38 167 saveLowTimeStampCountInto( masterPr->endMasterTSCLow );
Me@38 168 #endif
Me@38 169
Me@21 170 asm volatile("movl %0, %%eax; \
Me@21 171 movl %%esp, (%%eax); \
Me@21 172 movl %1, %%eax; \
Me@21 173 movl %%ebp, (%%eax); \
Me@21 174 movl %2, %%ebx; \
Me@21 175 movl %3, %%eax; \
Me@21 176 movl %4, %%esp; \
Me@21 177 movl %5, %%ebp; \
Me@21 178 movl $0x0, (%%ebx); \
Me@30 179 jmp %%eax;" \
Me@21 180 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr), \
Me@38 181 "=g"(masterLockAddr) \
Me@21 182 /* inputs */ : "g" (jmpPt), "g"(coreLoopStackPtr), "g"(coreLoopFramePtr)\
Me@21 183 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
Me@21 184 );//can probably make clobber list empty -- but safe for now
Me@0 185 }
Me@0 186