/*============================================================================= Name: ffMain.c ffStart - the start command called at initialization ffStop - the stop command available via IPL, iocsh, db routine ffFreeLists - Diagnostic free list reporting fffreelistUtil - Local support routine for ffFreeLists Abs: ffMain contains the ffExec thread and the ffStart, ffRestart, and ffStop iocsh commands for the FF-IOC Auth: 21-may-2009, drogind created from micro module's slcMain.c Rev: -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: 2009 drogind used for fast feedback loop controller IOC =============================================================================*/ #include "debugPrint.h" #ifdef DEBUG_PRINT int ffMainFlag = DP_INFO; #endif /* c library includes */ #include #include #include /* epics includes */ #include /* NELEMENTS */ #include /* ioc shell stuff */ #include #include /* for registering ffRestart, ffStop */ #include /* epicsEventId */ #include /* freeListItemsAvail */ /* ff common includes */ #include "ffGlob.h" /* globals, types, */ #include "msgTypes.h" /* msgmail_ts */ #include "ffMsg.h" /* ffIOC internal msg function codes */ /* ff utility prototypes */ #include "msgQUtil.h" /* message queue utilities */ /* thread prototypes */ #include "ffMain.h" /* ffStart, ffRestart, ffStop */ #include "ffExec.h" /* ffExec thread */ #include "loopDev.h" /* loop_as */ #include /* FcomID */ /* for dbNameToAddr test #include */ /* Epics Runtime Database */ /*============================================================================= Name: ffInitialize Abs: The function ffInitialize initializes the ff-aware thread globals and prepares the map of PV names -> global memory for devSupport FbckProc PVs. Args: none Rem: This function is called once to initialize the FF global data. It is called from a startup st.cmd file, just before the iocInit call. Side: . Ret: none =============================================================================*/ void ffInitialize(void) { DEBUGPRINT(DP_DEBUG, ffMainFlag, ("ffMain: reached ffInitialize!! \n")); /* initialize the global structures - ONLY DONE ONCE RIGHT HERE */ ffGlobInitGlobals(); /* initialize devSupport memory list - ONLY DONE ONCE RIGHT HERE */ ffGlobInitDevSupport(); } /*============================================================================= Name: ffStart Abs: The function ffStart initializes the ff-aware thread globals and starts the ffExec thread. Once the ffExec thread is active, it calls the ffRestart function to tell ffExec to start all other ff-aware threads. Args: none Rem: This function is called once to start the ff-aware portion of the ffIOC. It is called from a startup file, after all other IOC initialization is done. Side: the ff-aware thread globals are initialized to known values. When this function completes successfully, all the ff-aware threads will have been started. . Ret: none =============================================================================*/ void ffStart(void) { unsigned int stackSize, threadPriority; dbExists = epicsFalse; DEBUGPRINT(DP_DEBUG, ffMainFlag, ("ffMain: reached ffStart!! \n")); if ( !epicsThreadGetId(EXEC_NAME) ) { threadPriority = epicsThreadPriorityMedium; stackSize = epicsThreadGetStackSize(epicsThreadStackBig); /* initialize the global structures - Moved to ffInitialize */ /* ffGlobInitGlobals();*/ /* create the ffExec thread */ DEBUGPRINT(DP_INFO,ffMainFlag, ("ffMain: starting exec thread\n")); ffThreads_as[ffExec].tid_ps = epicsThreadCreate(EXEC_NAME, threadPriority, stackSize, (EPICSTHREADFUNC)ffExecThread, 0); /* check for good thread start */ if (ffThreads_as[ffExec].tid_ps==NO_THREAD) { DEBUGPRINT(DP_FATAL,ffMainFlag, ("FFSTART: ffStart command failed to start exec thread!\n")); return; } while (ffThreads_as[ffExec].active==epicsFalse) epicsThreadSleep(0.5); DEBUGPRINT(DP_DEBUG,ffMainFlag, ("ffSTART: ffStart setting dbExists=true \n")); dbExists = epicsTrue; /* now ffStartLoopNum's can execute */ } else printf("Invalid Command. The ff-aware threads have already started\n"); } /*============================================================================= Name: ffStartLoopNum Abs: The function ffStartLoopNUm starts the numbered loop thread. Args: Loop Number with which to reference LOOP macro in EPICS DB Rem: This function is called once to start a FF loop thread. Side: . Ret: none =============================================================================*/ void ffStartLoopNum (const int loopNum, const char * loopString) { char loopname_ca[MAX_THREADNAME_SIZE]; /* Thread name */ msgptr_ts msg_s; msgmail_ts* sMsg_ps = NULL; loopStartMsg_ts lsMsg_s; /* potential race condition - wait until ffStart completes initing globals prior to ffStartLoopNum */ while (dbExists==epicsFalse) epicsThreadSleep(1.0); DEBUGPRINT(DP_DEBUG,ffMainFlag, ("ffSTartLoopNum reached for loopNum=%d, loopString=%s \n", loopNum, loopString)); sprintf (loopname_ca, "%s%d\n", LOOP_NAME, loopNum); if (loopNum >= 0 && loopNum <=MAX_LOOP_THREADS) { /* check to see that this loop has not already started */ if ( ffThreads_as[ffExec + LOOP_ENUM_OFFSET + loopNum].loopNum <0 ) { if ( SUCCESS(msgQGetBuffer(sizeof(msgmail_ts), (void*)&sMsg_ps)) ) { sMsg_ps->msgheader.func = IOC_LOOP_START; DEBUGPRINT(DP_DEBUG,ffMainFlag, ("ffSTartLoopNum forming message to send to ffEXEC.. \n")); /* create the IOC_LOOP_START message for sending to the ffExec thread */ memset(&msg_s,0, sizeof(msg_s)); msg_s.msgptr_p = sMsg_ps; lsMsg_s.loopNum = loopNum; memcpy(&lsMsg_s.loopString, loopString, 4); memcpy(&sMsg_ps->reqdata, &loopNum, sizeof(int)); memcpy(&sMsg_ps->reqdata, &lsMsg_s, sizeof(loopStartMsg_ts)); msg_s.msgbytes = MSG_HDRS_BYTES + sizeof(loopStartMsg_ts); DEBUGPRINT(DP_DEBUG,ffMainFlag, ("ffSTartLoopNum after forming message to send to ffEXEC.. \n")); /*send stop message to ffExe thread */ if (!SUCCESS(messageQSend(ffExec, epicsTrue, sizeof(msg_s), &msg_s))) { DEBUGPRINT(DP_ERROR,ffMainFlag, ("FFSTOP:stop failed on send to Exec thread mq.\n")); } else DEBUGPRINT(DP_DEBUG,ffMainFlag, ("ffMain.ffStartLoop successfully sent msg to ffEXEC.. \n")); } } else printf("Invalid Command. The ff loop thread %s has already started\n", loopname_ca); } else printf("Invalid Command. The loop number must be in the range of 0 and %d\n", MAX_LOOP_THREADS); } /*============================================================================= Name: ffStop Abs: Stop ff threads Args: none Rem: stopThreads issues the message to stop all threads, close all sockets. This is the main shutdown command for the ffIoc. It can be called as an iocsh command, or from the epics subroutine record. Side: a IOC_STOP message is sent to the ffExec thread message queue Ret: None ==============================================================================*/ /**procedure**/ void ffStop(void) { msgptr_ts msg_s; msgmail_ts* sMsg_ps = NULL; if (epicsThreadGetId(EXEC_NAME)) { if ( SUCCESS(msgQGetBuffer(sizeof(msgmail_ts), (void*)&sMsg_ps)) ) { sMsg_ps->msgheader.func = IOC_STOP; /* create the STOP message for the ffExec thread */ memset(&msg_s,0, sizeof(msg_s)); msg_s.msgptr_p = sMsg_ps; msg_s.msgbytes = MSG_HDRS_BYTES; /*send stop message to ffExe thread */ if (!SUCCESS(messageQSend(ffExec, epicsTrue, sizeof(msg_s), &msg_s))) { DEBUGPRINT(DP_ERROR,ffMainFlag, ("FFSTOP:stop failed on send to Exec thread mq.\n")); } } } else printf ("Invalid Command. The ff threads have not started.\n"); } /*============================================================================= Name: ffShowTimeDiags Abs: Diagnostic to dump timing information about current time delays and processing times Args: none Rem: Called as an iocsh command Side: Ret: None ==============================================================================*/ /**procedure**/ void ffShowTimeDiags(const int loop) { if (epicsThreadGetId(EXEC_NAME)) { DEBUGPRINT(DP_INFO,ffMainFlag,("Timer delay=%dusec\n", (unsigned int) ffDelay_in_usec) ); /* show meas diagnostics */ if (loopRT_as[loop].meas.diag_s.iterations>0) { DEBUGPRINT(DP_INFO,ffMainFlag, ("latest time from fiducial to getBlobs = %d nsec + %d sec\n", loopRT_as[loop].timeFromFiducial.nsec, loopRT_as[loop].timeFromFiducial.secPastEpoch)); DEBUGPRINT(DP_INFO,ffMainFlag, ("latest processing time in getBlobs = %d nsec + %d sec\n", loopRT_as[loop].timeLoopProcess.nsec, loopRT_as[loop].timeLoopProcess.secPastEpoch)); } } } /*============================================================================= Name: ffShowConfig Abs: Diagnostic to dump config information for the given loop Args: none Rem: Called as an iocsh command Side: Ret: None ==============================================================================*/ /**procedure**/ void ffShowConfig(const int loop) { int i; if (epicsThreadGetId(EXEC_NAME)) { DEBUGPRINT(DP_INFO,ffMainFlag,("Timer delay=%dusec\n", (unsigned int) ffDelay_in_usec) ); /* show loop config stuff */ DEBUGPRINT(DP_INFO, ffMainFlag, ("Loop State: %d\n", loopRT_as[loop].loopState) ); DEBUGPRINT(DP_INFO, ffMainFlag, ("Loop Mode: %d\n", loopRT_as[loop].loopMode) ); DEBUGPRINT(DP_INFO, ffMainFlag, ("Loop pGain: %f\n", loopRT_as[loop].state.pgain) ); DEBUGPRINT(DP_INFO, ffMainFlag, ("Loop iGain: %f\n", loopRT_as[loop].state.igain) ); /* show meas config */ DEBUGPRINT(DP_INFO,ffMainFlag, (" number of measurements: %d\n", loop_as[loop].measNum) ); for (i=0; i0) { DEBUGPRINT(DP_INFO,ffMainFlag, ("per %ld iterations with %d measurements: good datasets = %ld\n", loopRT_as[loop].meas.diag_s.iterations, loop_as[loop].measNum, loopRT_as[loop].meas.numgood)); DEBUGPRINT(DP_INFO,ffMainFlag, ("per %ld iterations with %d measurements: old pkts = %ld, good pkts = %ld, late pkts= %ld, fcom errs = %ld, total pkts = %ld\n", loopRT_as[loop].meas.diag_s.iterations, loop_as[loop].measNum, loopRT_as[loop].meas.diag_s.old, loopRT_as[loop].meas.diag_s.good, loopRT_as[loop].meas.diag_s.late, loopRT_as[loop].meas.diag_s.fcom_err, (loopRT_as[loop].meas.diag_s.good+ loopRT_as[loop].meas.diag_s.old+ loopRT_as[loop].meas.diag_s.late+ loopRT_as[loop].meas.diag_s.fcom_err))); for (i=0; ifid.ID (rollover?)\n")); } /* show states diagnostics */ if (loop_as[loop].stateNum>0) { DEBUGPRINT(DP_INFO,ffMainFlag, ("per %ld iterations with %d states:good states = %ld, late states= %ld, fcom errs = %ld, total pkts = %ld\n", loopRT_as[loop].state.diag_s.iterations, loop_as[loop].stateNum, loopRT_as[loop].state.diag_s.good, loopRT_as[loop].state.diag_s.late, loopRT_as[loop].state.diag_s.fcom_err, (loopRT_as[loop].state.diag_s.good+ loopRT_as[loop].state.diag_s.late+ loopRT_as[loop].state.diag_s.fcom_err))); } /* show acts diagnostics */ if (loop_as[loop].actNum>0) { DEBUGPRINT(DP_INFO,ffMainFlag, ("per %ld iterations with %d actuators:good acts=%ld, late acts=%ld, really late acts=%ld, fcom errs=%ld, total pkts=%ld\n", loopRT_as[loop].act.diag_s.iterations, loop_as[loop].actNum, loopRT_as[loop].act.diag_s.good, loopRT_as[loop].act.diag_s.late, loopRT_as[loop].meas.diag_s.old, loopRT_as[loop].act.diag_s.fcom_err, (loopRT_as[loop].act.diag_s.good+ loopRT_as[loop].meas.diag_s.old+ loopRT_as[loop].act.diag_s.late+ loopRT_as[loop].act.diag_s.fcom_err))); } } else printf ("Invalid Command. The ff threads have not started.\n"); } /*============================================================================= Name: fffreelistUtil Abs: Diagnostic to dump information about various ioc free list items available called by ffFreeLists, below Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- const void * freeList_ps R pointer to free list to diagnose const int numLists R number of lists in free list const int sizeEaList R size of each list in free list const char * message R string describing name of free list Rem: Called as an iocsh command Side: Uses epics libCom freeListLib utilities freeListItemsAvail to view the number of lists available to the ioc. freeListLib memory allocates the free list after the first list request, and not upon initialization. Thus, if the 'ffFreeLists' shell routine is called prior to any programmatic requests, (as may be the case with dblist or message lists), this routine will go ahead and request a free list, then free it right away in order for the entire free list structure to get allocated. It then reports the items avail, total # lists in free list, and size of each list. Ret: None ==============================================================================*/ /**procedure**/ void fffreelistUtil(const void * freeList_ps, const int numLists, const int sizeEaList, const char * message) { size_t itemsAvail = 0; void * dblist_ps = NULL; if (freeList_ps) { itemsAvail=freeListItemsAvail((void *)freeList_ps); if (itemsAvail==0) { /* freelistLib allocates entire space after first list request */ /* so, make request, then free */ dblist_ps= freeListCalloc((void *)freeList_ps); freeListFree((void *)freeList_ps, dblist_ps); itemsAvail=freeListItemsAvail((void *)freeList_ps); } printf("Free lists for %s avail: %d (out of %d; list size=%d bytes)\n", message, (int)itemsAvail, numLists, sizeEaList); } else { printf ("Free lists for %s do not exist; error \n", message); } } /*============================================================================= Name: ffFreeLists Abs: Diagnostic to dump information about various ioc free list items available Args: none Rem: Called as an iocsh command Free list pointers must be global in order to report upon them Calls local utility fffreelistUtil with free list args in order to output to console Feel free to insert your free lists here Side: Ret: None ==============================================================================*/ /**procedure**/ void ffFreeLists(void) { if (epicsThreadGetId(EXEC_NAME)) { /* the msg service lists do not need the database */ fffreelistUtil(ffSmMsgPvt_p, SMALL_MSGPOOL_ELEMENTS, sizeof(msgmail_ts), "smallmsgFreeList" ); fffreelistUtil(ffLrgMsgPvt_p, LARGE_MSGPOOL_ELEMENTS, sizeof(lrgmail_ts), "largemsgFreeList" ); } else printf(" Invalid Command. The ff threads have not started.\n"); } /*============================================================================= Name: ffdbAddr Abs: Diagnostic to dump information about various ioc free list items available Args: none Rem: Called as an iocsh command Tests out dbNametoAddr calls that are failing on linux in the ff aware code. Side: Ret: None ==============================================================================*/ /**procedure**/ void ffdbAddr(void) { /* DBADDR pvAddr_s; mgntHdlrInit(); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_CTRL); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_EPICSHSTA); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_STAT); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_TTIM); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_ITRY); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_BDES); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_BACT); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_BMON); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_IDES); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_IACT); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_RIPL); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_IPRV); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_IMON); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_IMAX); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_CTIM); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_KTIM); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); sprintf (pvname_ca, "LGPS:XX02:1:%s", PV_ZTIM); if ((dbNameToAddr(pvname_ca, &pvAddr_s))) printf ("dbNameToAddr of %s failed\n", (char *)pvname_ca); */ }