/*
 * Copyright 2010  OpenSourceStewardshipFoundation
 *
 * Licensed under BSD
 */

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#include "VMS.h"
#include "Queue_impl/BlockingQueue.h"
#include "Histogram/Histogram.h"


#define thdAttrs NULL

//===========================================================================
void
shutdownFn( void *dummy, VirtProcr *dummy2 );

SchedSlot **
create_sched_slots();

void
create_masterEnv();

void
create_the_coreLoop_OS_threads();

pthread_mutex_t suspendLock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  suspend_cond  = PTHREAD_COND_INITIALIZER;

//===========================================================================

/*Setup has two phases:
 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
 *    the master virt procr into the work-queue, ready for first "call"
 * 2) Semantic layer then does its own init, which creates the seed virt
 *    procr inside the semantic layer, ready to schedule it when
 *    asked by the first run of the masterLoop.
 *
 *This part is bit weird because VMS really wants to be "always there", and
 * have applications attach and detach..  for now, this VMS is part of
 * the app, so the VMS system starts up as part of running the app.
 *
 *The semantic layer is isolated from the VMS internals by making the
 * semantic layer do setup to a state that it's ready with its
 * initial virt procrs, ready to schedule them to slots when the masterLoop
 * asks.  Without this pattern, the semantic layer's setup would
 * have to modify slots directly to assign the initial virt-procrs, and put
 * them into the readyToAnimateQ itself, breaking the isolation completely.
 *
 * 
 *The semantic layer creates the initial virt procr(s), and adds its
 * own environment to masterEnv, and fills in the pointers to
 * the requestHandler and slaveScheduler plug-in functions
 */

/*This allocates VMS data structures, populates the master VMSProc,
 * and master environment, and returns the master environment to the semantic
 * layer.
 */
void
VMS__init()
 {
   create_masterEnv();
   create_the_coreLoop_OS_threads();
 }

/*To initialize the sequential version, just don't create the threads
 */
void
VMS__init_Seq()
 {
   create_masterEnv();
 }

void
create_masterEnv()
 { MasterEnv       *masterEnv;
   SRSWQueueStruc **readyToAnimateQs;
   int              coreIdx;
   VirtProcr      **masterVPs;
   SchedSlot     ***allSchedSlots; //ptr to array of ptrs
   
      //Make the master env, which holds everything else
   _VMSMasterEnv = malloc( sizeof(MasterEnv) );
   masterEnv     = _VMSMasterEnv;
      //Need to set start pt here 'cause used by seed procr, which is created
      // before the first core loop starts up. -- not sure how yet..
//   masterEnv->coreLoopStartPt = ;
//   masterEnv->coreLoopEndPt   = ;
   
      //Make a readyToAnimateQ for each core loop
   readyToAnimateQs = malloc( NUM_CORES * sizeof(SRSWQueueStruc *) );
   masterVPs        = malloc( NUM_CORES * sizeof(VirtProcr *) );

      //One array for each core, 3 in array, core's masterVP scheds all
   allSchedSlots    = malloc( NUM_CORES * sizeof(SchedSlot *) );

   for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
    {
      readyToAnimateQs[ coreIdx ] = makeSRSWQ();
      
         //Q: should give masterVP core-specific into as its init data?
      masterVPs[ coreIdx ] = VMS__create_procr( &masterLoop, masterEnv );
      masterVPs[ coreIdx ]->coreAnimatedBy = coreIdx;
      allSchedSlots[ coreIdx ] = create_sched_slots(); //makes for one core
    }
   _VMSMasterEnv->readyToAnimateQs = readyToAnimateQs;
   _VMSMasterEnv->masterVPs        = masterVPs;
   _VMSMasterEnv->allSchedSlots    = allSchedSlots;



      //Aug 19, 2010:  no longer need to place initial masterVP into queue
      // because coreLoop now controls -- animates its masterVP when no work


   //==================== malloc substitute ========================
   //
   //Testing whether malloc is using thread-local storage and therefore
   // causing unreliable behavior.
   //Just allocate a massive chunk of memory and roll own malloc/free and
   // make app use VMS__malloc_to, which will suspend and perform malloc
   // in the master, taking from this massive chunk.

//   initFreeList();

 }

/*
void
initMasterMalloc()
 {
   _VMSMasterEnv->mallocChunk = malloc( MASSIVE_MALLOC_SIZE );

      //The free-list element is the first several locations of an
      // allocated chunk -- the address given to the application is pre-
      // pended with both the ownership structure and the free-list struc.
      //So, write the values of these into the first locations of
      // mallocChunk -- which marks it as free & puts in its size.
   listElem = (FreeListElem *)_VMSMasterEnv->mallocChunk;
   listElem->size = MASSIVE_MALLOC_SIZE - NUM_PREPEND_BYTES
   listElem->next = NULL;
 }

void
dissipateMasterMalloc()
 {
      //Just foo code -- to get going -- doing as if free list were link-list
   currElem = _VMSMasterEnv->freeList;
   while( currElem != NULL )
    {
      nextElem = currElem->next;
      masterFree( currElem );
      currElem = nextElem;
    }
   free( _VMSMasterEnv->freeList );
 }
 */

SchedSlot **
create_sched_slots()
 { SchedSlot  **schedSlots;
   int i;

   schedSlots  = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );

   for( i = 0; i < NUM_SCHED_SLOTS; i++ )
    {
      schedSlots[i] = malloc( sizeof(SchedSlot) );

         //Set state to mean "handling requests done, slot needs filling"
      schedSlots[i]->workIsDone         = FALSE;
      schedSlots[i]->needsProcrAssigned = TRUE;
    }
   return schedSlots;
 }


void
freeSchedSlots( SchedSlot **schedSlots )
 { int i;
   for( i = 0; i < NUM_SCHED_SLOTS; i++ )
    {
      free( schedSlots[i] );
    }
   free( schedSlots );
 }


void
create_the_coreLoop_OS_threads()
 {
   //========================================================================
   //                      Create the Threads
   int coreIdx, retCode;

      //Need the threads to be created suspended, and wait for a signal
      // before proceeding -- gives time after creating to initialize other
      // stuff before the coreLoops set off.
   _VMSMasterEnv->setupComplete = 0;

      //Make the threads that animate the core loops
   for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
    { coreLoopThdParams[coreIdx]          = malloc( sizeof(ThdParams) );
      coreLoopThdParams[coreIdx]->coreNum = coreIdx;

      retCode =
      pthread_create( &(coreLoopThdHandles[coreIdx]),
                        thdAttrs,
                       &coreLoop,
               (void *)(coreLoopThdParams[coreIdx]) );
      if(retCode){printf("ERROR creating thread: %d\n", retCode); exit(0);}
    }
 }

/*Semantic layer calls this when it want the system to start running..
 *
 *This starts the core loops running then waits for them to exit.
 */
void
VMS__start_the_work_then_wait_until_done()
 { int coreIdx;
      //Start the core loops running
//===========================================================================
   TSCount  startCount, endCount;
   unsigned long long count = 0, freq = 0;
   double   runTime;

      startCount = getTSCount();
   
      //tell the core loop threads that setup is complete
      //get lock, to lock out any threads still starting up -- they'll see
      // that setupComplete is true before entering while loop, and so never
      // wait on the condition
   pthread_mutex_lock(     &suspendLock );
   _VMSMasterEnv->setupComplete = 1;
   pthread_mutex_unlock(   &suspendLock );
   pthread_cond_broadcast( &suspend_cond );
   
   
      //wait for all to complete
   for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
    {
      pthread_join( coreLoopThdHandles[coreIdx], NULL );
    }
   
      //NOTE: do not clean up VMS env here -- semantic layer has to have
      // a chance to clean up its environment first, then do a call to free
      // the Master env and rest of VMS locations


      endCount = getTSCount();
      count = endCount - startCount;

      runTime = (double)count / (double)TSCOUNT_FREQ;

      printf("\n Time startup to shutdown: %f\n", runTime); fflush( stdin );
 }

/*Only difference between version with an OS thread pinned to each core and
 * the sequential version of VMS is VMS__init_Seq, this, and coreLoop_Seq.
 */
void
VMS__start_the_work_then_wait_until_done_Seq()
 {
         //Instead of un-suspending threads, just call the one and only
         // core loop (sequential version), in the main thread.
      coreLoop_Seq( NULL );

 }



/*Create stack, then create __cdecl structure on it and put initialData and
 * pointer to the new structure instance into the parameter positions on
 * the stack
 *Then put function pointer into nextInstrPt -- the stack is setup in std
 * call structure, so jumping to function ptr is same as a GCC generated
 * function call
 *No need to save registers on old stack frame, because there's no old
 * animator state to return to --
 *
 */
VirtProcr *
VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
 { VirtProcr *newPr;
   char      *stackLocs, *stackPtr;

   newPr              = malloc( sizeof(VirtProcr) );
   newPr->procrID     = numProcrsCreated++;
   newPr->nextInstrPt = fnPtr;
   newPr->initialData = initialData;
   newPr->requests    = NULL;
   newPr->schedSlot   = NULL;
//   newPr->coreLoopStartPt = _VMSMasterEnv->coreLoopStartPt;

      //fnPtr takes two params -- void *initData & void *animProcr
      //alloc stack locations, make stackPtr be the highest addr minus room
      // for 2 params + return addr.  Return addr (NULL) is in loc pointed to
      // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
   stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
   if(stackLocs == 0)
   {perror("malloc stack"); exit(1);}
   newPr->startOfStack = stackLocs;
   stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
      //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
   *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
   *( (int *)stackPtr + 1 ) = (int) initialData;  //next  param to left
   newPr->stackPtr = stackPtr; //core loop will switch to this, then
   newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr

   return newPr;
 }


/*there is a label inside this function -- save the addr of this label in
 * the callingPr struc, as the pick-up point from which to start the next
 * work-unit for that procr.  If turns out have to save registers, then
 * save them in the procr struc too.  Then do assembly jump to the CoreLoop's
 * "done with work-unit" label.  The procr struc is in the request in the
 * slave that animated the just-ended work-unit, so all the state is saved
 * there, and will get passed along, inside the request handler, to the
 * next work-unit for that procr.
 */
void
VMS__suspend_procr( VirtProcr *animatingPr )
 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
   void *coreLoopFramePtr;

      //The request to master will cause this suspended virt procr to get
      // scheduled again at some future point -- to resume, core loop jumps
      // to the resume point (below), which causes restore of saved regs and
      // "return" from this call.
   animatingPr->nextInstrPt = &&ResumePt;

      //return ownership of the virt procr and sched slot to Master virt pr
   animatingPr->schedSlot->workIsDone = TRUE;
//   coreIdx = callingPr->coreAnimatedBy;

   stackPtrAddr      = &(animatingPr->stackPtr);
   framePtrAddr      = &(animatingPr->framePtr);

   jmpPt             = _VMSMasterEnv->coreLoopStartPt;
   coreLoopFramePtr  = animatingPr->coreLoopFramePtr;//need this only
   coreLoopStackPtr  = animatingPr->coreLoopStackPtr;//safety

      //Save the virt procr's stack and frame ptrs,
   asm volatile("movl %0,     %%eax;  \
                 movl %%esp, (%%eax); \
                 movl %1,     %%eax;  \
                 movl %%ebp, (%%eax) "\
   /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
   /* inputs  */ :        \
   /* clobber */ : "%eax" \
                );

   //===========================  Measurement stuff ========================
   #ifdef MEAS__TIME_STAMP_SUSP
      //record time stamp: compare to time-stamp recorded below
   saveLowTimeStampCountInto( animatingPr->preSuspTSCLow );
   #endif
   //=======================================================================

      //restore coreloop's frame ptr, then jump back to "start" of core loop
      //Note, GCC compiles to assembly that saves esp and ebp in the stack
      // frame -- so have to explicitly do assembly that saves to memory
   asm volatile("movl %0, %%eax;      \
                 movl %1, %%esp;      \
                 movl %2, %%ebp;      \
                 jmp  %%eax    "      \
   /* outputs */ :                    \
   /* inputs  */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi"  \
                ); //list everything as clobbered to force GCC to save all
                   // live vars that are in regs on stack before this
                   // assembly, so that stack pointer is correct, before jmp

ResumePt:
   #ifdef MEAS__TIME_STAMP_SUSP
      //NOTE: only take low part of count -- do sanity check when take diff
   saveLowTimeStampCountInto( animatingPr->postSuspTSCLow );
   #endif

   return;
 }




/*
 *This adds a request to dissipate, then suspends the processor so that the
 * request handler will receive the request.  The request handler is what
 * does the work of freeing memory and removing the processor from the
 * semantic environment's data structures.
 *The request handler also is what figures out when to shutdown the VMS
 * system -- which causes all the core loop threads to die, and returns from
 * the call that started up VMS to perform the work.
 *
 *This form is a bit misleading to understand if one is trying to figure out
 * how VMS works -- it looks like a normal function call, but inside it
 * sends a request to the request handler and suspends the processor, which
 * jumps out of the VMS__dissipate_procr function, and out of all nestings
 * above it, transferring the work of dissipating to the request handler,
 * which then does the actual work -- causing the processor that animated
 * the call of this function to disappear and the "hanging" state of this
 * function to just poof into thin air -- the virtual processor's trace
 * never returns from this call, but instead the virtual processor's trace
 * gets suspended in this call and all the virt processor's state disap-
 * pears -- making that suspend the last thing in the virt procr's trace.
 */
void
VMS__dissipate_procr( VirtProcr *procrToDissipate )
 { VMSReqst *req;

   req = malloc( sizeof(VMSReqst) );
//   req->virtProcrFrom      = callingPr;
   req->reqType               = dissipate;
   req->nextReqst             = procrToDissipate->requests;
   procrToDissipate->requests = req;
   
   VMS__suspend_procr( procrToDissipate );
}


/*This inserts the semantic-layer's request data into standard VMS carrier
 */
inline void
VMS__add_sem_request( void *semReqData, VirtProcr *callingPr )
 { VMSReqst *req;

   req = malloc( sizeof(VMSReqst) );
//   req->virtProcrFrom      = callingPr;
   req->reqType        = semantic;
   req->semReqData     = semReqData;
   req->nextReqst      = callingPr->requests;
   callingPr->requests = req;
 }


/*Use this to get first request before starting request handler's loop
 */
VMSReqst *
VMS__take_top_request_from( VirtProcr *procrWithReq )
 { VMSReqst *req;

   req = procrWithReq->requests;
   if( req == NULL ) return req;

   procrWithReq->requests = procrWithReq->requests->nextReqst;
   return req;
 }

/*A subtle bug due to freeing then accessing "next" after freed caused this
 * form of call to be put in -- so call this at end of request handler loop
 * that iterates through the requests.
 */
VMSReqst *
VMS__free_top_and_give_next_request_from( VirtProcr *procrWithReq )
 { VMSReqst *req;

   req = procrWithReq->requests;
   if( req == NULL ) return NULL;

   procrWithReq->requests = procrWithReq->requests->nextReqst;
   VMS__free_request( req );
   return procrWithReq->requests;
 }


//TODO: add a semantic-layer supplied "freer" for the semantic-data portion
// of a request -- IE call with both a virt procr and a fn-ptr to request
// freer (also maybe put sem request freer as a field in virt procr?)
//MeasVMS relies right now on this only freeing VMS layer of request -- the
// semantic portion of request is alloc'd and freed by request handler
void
VMS__free_request( VMSReqst *req )
 {
   free( req );
 }



inline int
VMS__isSemanticReqst( VMSReqst *req )
 {
   return ( req->reqType == semantic );
 }


inline void *
VMS__take_sem_reqst_from( VMSReqst *req )
 {
   return req->semReqData;
 }

inline int
VMS__isDissipateReqst( VMSReqst *req )
 {
   return ( req->reqType == dissipate );
 }

inline int
VMS__isCreateReqst( VMSReqst *req )
 {
   return ( req->reqType == regCreated );
 }

void
VMS__send_req_to_register_new_procr(VirtProcr *newPr, VirtProcr *reqstingPr)
 { VMSReqst *req;

   req                  = malloc( sizeof(VMSReqst) );
   req->reqType         = regCreated;
   req->semReqData      = newPr;
   req->nextReqst       = reqstingPr->requests;
   reqstingPr->requests = req;

   VMS__suspend_procr( reqstingPr );
 }



/*This must be called by the request handler plugin -- it cannot be called
 * from the semantic library "dissipate processor" function -- instead, the
 * semantic layer has to generate a request for the plug-in to call this
 * function.
 *The reason is that this frees the virtual processor's stack -- which is
 * still in use inside semantic library calls!
 *
 *This frees or recycles all the state owned by and comprising the VMS
 * portion of the animating virtual procr.  The request handler must first
 * free any semantic data created for the processor that didn't use the
 * VMS_malloc mechanism.  Then it calls this, which first asks the malloc
 * system to disown any state that did use VMS_malloc, and then frees the
 * statck and the processor-struct itself.
 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
 * state, then that state gets freed (or sent to recycling) as a side-effect
 * of dis-owning it.
 */
void
VMS__handle_dissipate_reqst( VirtProcr *animatingPr )
 {
      //dis-own all locations owned by this processor, causing to be freed
      // any locations that it is (was) sole owner of
//TODO: implement VMS__malloc system, including "give up ownership"

      //The dissipate request might still be attached, so remove and free it
   VMS__free_top_and_give_next_request_from( animatingPr );

      //NOTE: initialData was given to the processor, so should either have
      // been alloc'd with VMS__malloc, or freed by the level above animPr.
      //So, all that's left to free here is the stack and the VirtProcr struc
      // itself
   free( animatingPr->startOfStack );
   free( animatingPr );
 }


//TODO: re-architect so that have clean separation between request handler
// and master loop, for dissipate, create, shutdown, and other non-semantic
// requests.  Issue is chain: one removes requests from AppVP, one dispatches
// on type of request, and one handles each type..  but some types require
// action from both request handler and master loop -- maybe just give the
// request handler calls like:  VMS__handle_X_request_type

void
endOSThreadFn( void *initData, VirtProcr *animatingPr );

/*This is called by the semantic layer's request handler when it decides its
 * time to shut down the VMS system.  Calling this causes the core loop OS
 * threads to exit, which unblocks the entry-point function that started up
 * VMS, and allows it to grab the result and return to the original single-
 * threaded application.
 * 
 *The _VMSMasterEnv is needed by this shut down function, so the create-seed-
 * and-wait function has to free a bunch of stuff after it detects the
 * threads have all died: the masterEnv, the thread-related locations,
 * masterVP any AppVPs that might still be allocated and sitting in the
 * semantic environment, or have been orphaned in the _VMSWorkQ.
 * 
 *NOTE: the semantic plug-in is expected to use VMS__malloc_to to get all the
 * locations it needs, and give ownership to masterVP.  Then, they will be
 * automatically freed when the masterVP is dissipated.  (This happens after
 * the core loop threads have all exited)
 *
 *In here,create one core-loop shut-down processor for each core loop and put
 * them all directly into the readyToAnimateQ.
 *Note, this function can ONLY be called after the semantic environment no
 * longer cares if AppVPs get animated after the point this is called.  In
 * other words, this can be used as an abort, or else it should only be
 * called when all AppVPs have finished dissipate requests -- only at that
 * point is it sure that all results have completed.
 */
void
VMS__handle_shutdown_reqst( void *dummy, VirtProcr *animatingPr )
 { int coreIdx;
   VirtProcr *shutDownPr;

      //create the shutdown processors, one for each core loop -- put them
      // directly into the Q -- each core will die when gets one
   for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
    {
      shutDownPr = VMS__create_procr( &endOSThreadFn, NULL );
      writeSRSWQ( shutDownPr, _VMSMasterEnv->readyToAnimateQs[coreIdx] );
    }

 }


/*Am trying to be cute, avoiding IF statement in coreLoop that checks for
 * a special shutdown procr.  Ended up with extra-complex shutdown sequence.
 *This function has the sole purpose of setting the stack and framePtr
 * to the coreLoop's stack and framePtr.. it does that then jumps to the
 * core loop's shutdown point -- might be able to just call Pthread_exit
 * from here, but am going back to the pthread's stack and setting everything
 * up just as if it never jumped out, before calling pthread_exit.
 *The end-point of core loop will free the stack and so forth of the
 * processor that animates this function, (this fn is transfering the
 * animator of the AppVP that is in turn animating this function over
 * to core loop function -- note that this slices out a level of virtual
 * processors).
 */
void
endOSThreadFn( void *initData, VirtProcr *animatingPr )
 { void *jmpPt, *coreLoopStackPtr, *coreLoopFramePtr;

   jmpPt             = _VMSMasterEnv->coreLoopEndPt;
   coreLoopStackPtr  = animatingPr->coreLoopStackPtr;
   coreLoopFramePtr  = animatingPr->coreLoopFramePtr;


   asm volatile("movl %0, %%eax;      \
                 movl %1, %%esp;      \
                 movl %2, %%ebp;      \
                 jmp  %%eax    "      \
   /* outputs */ :                    \
   /* inputs  */ : "m" (jmpPt), "m"(coreLoopStackPtr), "m"(coreLoopFramePtr)\
   /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi"  \
                );
 }


/*This is called after the threads have shut down and control has returned
 * to the semantic layer, in the entry point function in the main thread.
 * It has to free anything allocated during VMS_init, and any other alloc'd
 * locations that might be left over.
 */
void
VMS__cleanup_after_shutdown()
 { 
   SRSWQueueStruc **readyToAnimateQs;
   int              coreIdx;
   VirtProcr      **masterVPs;
   SchedSlot     ***allSchedSlots; //ptr to array of ptrs

   readyToAnimateQs = _VMSMasterEnv->readyToAnimateQs;
   masterVPs        = _VMSMasterEnv->masterVPs;
   allSchedSlots    = _VMSMasterEnv->allSchedSlots;
   
   for( coreIdx = 0; coreIdx < NUM_CORES; coreIdx++ )
    {
      freeSRSWQ( readyToAnimateQs[ coreIdx ] );

      VMS__handle_dissipate_reqst( masterVPs[ coreIdx ] );
      
      freeSchedSlots( allSchedSlots[ coreIdx ] );
    }
   
   free( _VMSMasterEnv->readyToAnimateQs );
   free( _VMSMasterEnv->masterVPs );
   free( _VMSMasterEnv->allSchedSlots );

   free( _VMSMasterEnv );
 }


//===========================================================================

inline TSCount getTSCount()
 { unsigned int low, high;
   TSCount  out;

   saveTimeStampCountInto( low, high );
   out = high;
   out = (out << 32) + low;
   return out;
 }

