📄 rticore.c
字号:
/*---------------------------------------------------------------------------*//* RTI core implementation. *//* Author(s): Steve Ferenci, Thom McLean, Kalyan Perumalla. *//* $Revision: 1.11 $ $Name: v26apr05 $ $Date: 2004/07/14 17:03:57 $ *//*---------------------------------------------------------------------------*/#include <stdlib.h>#include <string.h>#include <stdio.h>#include <assert.h>#include "rticore.h"#include "mcast.h"/*----------------------------------------------------------------------------*/static const char *fdkErrorMessages[] = { /* * General error messages */ /* fdkSUCCEEDED = 0 */ "Succeeded.", /* fdkINCOMPLETE */ "Succeeded, but incomplete.", /* fdkFAILED */ "Failed.", /* fdkOUTOFMEMORY */ "Out of Memory.", /* fdkBADFD */ "Bad File Descriptor.", /* * RTICore specific error messages */ /* fdkNLBTSERROR */ "Attempt to defer more than 1 LBTS computation.", /* fdkCNSLBTS */ "Could not start LBTS computation.", /* fdkLMLTLBTS */ "Local Min less than LBTS.", /* fdkTMPENDING */ "Time Management Request pending.", /* fdkTIMEPASSED */ "Requested time already passed.", /* fdkTMESMALL */ "TM_EPSILON too small.", /* fdkTMINCON */ "State of time managment is inconsistent.", /* registration errors (certain functions must be registered) */ /* fdkDOMNR */ "DeliverOneMessage is not registered.", /* fdkDTSOENR */ "DeliverTSOEvents is not registered.", /* fdkDROENR */ "DeliverROEvents is not registered.", /* fdkTAGNR */ "TimeAdvanceGrant is not registered.", /* fdkTSOMNR */ "TSOMin is not registered.", /* fdkTSOPNR */ "TSOPop is not registered.", /* * RTI errors for DeliverOneMessage, DeliverROEvents, * and DeliverTSOEvents */ /* fdkDOMIMT */ "Invalid message type.", /* * GuardValue */ /* fdkMAXERROR */ "Invalid fdkErrorCode."};/*----------------------------------------------------------------------------*/const char *fdkErrorMessage(fdkErrorCode ec){ if (ec > fdkMAXERROR) ec = fdkMAXERROR; return(fdkErrorMessages[ec]);}/*----------------------------------------------------------------------------*//* Returned by Core_RetractionHandle Compare, used internally.*/#define EQUAL 0#define NOTEQUAL 1/*----------------------------------------------------------------------------*//* If LBTS fails to advance TM_LBTSFAILCOUNT times then it is an error. */#define TM_LBTSFAILCOUNT 10000000/*----------------------------------------------------------------------------*//* These defines are used as short cuts for accessing retraction information. */#define CRH coreRHInfo#define SRH CRH.SenderRH#define RRH CRH.ReceiverRH/*----------------------------------------------------------------------------*//* These defines are short cuts to the output file descriptors used when printing error messages and user messages.*/#define rd CRH.dout #define cd CRTI.dout/*----------------------------------------------------------------------------*//*----------------------------------------------------------------------------*//* Holds retraction informantion, retraction handle, its timestamp, and its multi-cast handle. NOTE: RTI may alter the multi-cast handle during the time it is stored in the RHM thus making the multi-cast invalid when it is returned to the RTI via RHM_IsRetractable.*/typedef struct{ CoreRetractionHandle rh; TM_Time ts; MCAST_Handle mh;} coreSenderRHHashEntry;/*----------------------------------------------------------------------------*//* This hash table supports fast look ups given a retraction handle. When a message is sent the retraction handle is stored in here. When a message is about to be retracted this table can be consulted to determin if the message is retractable (ie TS >= LBTS + Lookahead).*/typedef struct { coreSenderRHHashEntry *sRHHash; int sRHHashSize; int sRHHashIncr; unsigned int sRHHashBegining; unsigned int sRHHashEnd; CoreRetractionNumber nextSRNum;} CoreSenderRetractionHandles;/*----------------------------------------------------------------------------*/typedef struct { CoreRetractionHandle rh; int Qloc; TM_Time ts; long MsgType;}CoreReceiverRHHashEntry;/*----------------------------------------------------------------------------*/typedef struct { CoreRetractionHandle rh; TM_Time ts; MCAST_Addr Msg; int MsgSize; long MsgType;}CoreReceiverRHQueEntry; /*----------------------------------------------------------------------------*/typedef struct { CoreReceiverRHQueEntry *rRHQue; int rRHQueHead; int rRHQueSize; int rRHQueIncr; }CoreReceiverRHQue;/*----------------------------------------------------------------------------*/typedef struct { CoreReceiverRHHashEntry *rRHHash; int rRHHashSize; int rRHHashIncr;}CoreReceiverRHHash;/*----------------------------------------------------------------------------*/typedef struct { CoreReceiverRHQue rQ; CoreReceiverRHHash rH;} CoreReceiverRetractionHandles;/*----------------------------------------------------------------------------*/typedef struct { CoreSenderRetractionHandles SenderRH; CoreReceiverRetractionHandles ReceiverRH; int debug; FILE *dout;} CoreRHInfo;/*----------------------------------------------------------------------------*/struct InternalStatitics{ int SRTableResizes; /* number of times sender retraction table resized*/ int SRTableFinalSize; /*Final Size of Sender Retraction Table */ int SRTableInitialSize; /* Initial size of sender retraction table */ int RRTableResizes; /* number of times receiver retraction table resized */ int RRTableFinalSize; /*Final size of receiver retraction table */ int RRTableInitialSize; /* initial size of receiver retraction table */}Stats;/*----------------------------------------------------------------------------*/typedef enum { NO_REQ = 0, NER, TAR, FQR, NERA, TARA } CoreRequest;/*----------------------------------------------------------------------------*/typedef struct Core_RTIState{ TM_Time CurrentTime; /* current logical time of federate */ TM_TimeQual CurrentQual; /* Qual parameter of Current Time */ TM_Time LookAhead; /* federate's lookahead */ TM_Time TargetLA; /* Requested LA, used for LA reductions */ CoreRequest PendingRequest; /* The Pending Request, if any */ TM_Time PendingTime; /* time parameter of pending request */ TM_TimeQual PendingQual; /* Qual parameter of pending request */ TM_Time MinDelivered; /* Min time of the message delivered(for FQR)*/ TM_Time LocalMin; /* Most recently reported MinTS */ TM_TimeQual LocalMinQual; /* Most recently reported MinTSQual */ TM_Time LBTS; /* last computed LBTS value */ TM_TimeQual LBTSQual; /* last computed LBTS Qual value */ BOOLEAN LBTSEnabled; long NPendingLBTS; /* known # LBTS computations in progress */ int NLBTSDeferred; int RecentLBTSDoneTransaction;/* Trans# of most recently completed LBTSDone*/ long FailCount; /* # consecutive times LBTS didn't advance */ struct { BOOLEAN ner_fastpath; /* KALYAN: Optimize for NER fastpath */ BOOLEAN min_nlbts; /* KALYAN: Minimize number of LBTSs */ } optimize; int debug; FILE *dout; } CoreRTIState;/*----------------------------------------------------------------------------*//* Time Management function prototypes*/static fdkErrorCode core_DoEventDelivery();static fdkErrorCode core_FlushQueueRequestDelivery();static int core_TryToRelease(TM_Time *, TM_TimeQual *);static fdkErrorCode core_StartLBTS ();static fdkErrorCode core_Compute_Local_Min();static long core_MyLBTSStarted(long, TM_Time*, TM_TimeQual*, TM_LBTSDoneProc *);static void core_MyLBTSDone(TM_Time, TM_TimeQual, long);/*----------------------------------------------------------------------------*//* Retraction Handle Management function prototypes*/static fdkErrorCode Core_InitRHM(void);static int Core_RetractionHandleCompare(const CoreRetractionHandle *h1, const CoreRetractionHandle *h2);/* Sender Side */static fdkErrorCode core_InitSenderRetraction(int initial_table_size, int increment_size);static coreSenderRHHashEntry *core_makeRetractionHash(int);static fdkErrorCode core_ResizeSenderRHHash(void);static int core_SenderHashFunction(CoreRetractionHandle *rh);static fdkErrorCode core_GetNextRetractionHashEntry(TM_Time, MCAST_Handle, coreSenderRHHashEntry *);static fdkErrorCode core_SaveSenderRHHashEntry(coreSenderRHHashEntry);static int core_VerifyRetractionHashEntry(coreSenderRHHashEntry);/* Receiver Side */static fdkErrorCode core_InitReceiverRetraction(int size, int incr); static fdkErrorCode core_ResizeReceiverRetraction(void); static int core_ReceiverHashFunction(CoreRetractionHandle *rh);static fdkErrorCode core_SaveERHQue(CoreRetractionHandle RH, TM_Time TS, MCAST_Addr Msg, int MsgSize, long MsgType, int *loc);static fdkErrorCode core_SaveERHHash(CoreRetractionHandle RH, TM_Time TS, int QLoc, long MsgType); static fdkErrorCode core_SendRemainingRetracts(void);static TM_Time core_MinRetract(void);/*----------------------------------------------------------------------------*//* Registered RTI functions. The RTI must register all of the functions below.*/static DeliverOneMessageProc RTI_DeliverOneMessage=NULL;static DeliverTSOEventsProc RTI_DeliverTSOEvents=NULL;static DeliverROEventsProc RTI_DeliverROEvents=NULL;static TimeAdvanceGrantProc RTI_TimeAdvanceGrant=NULL;static TSOMinProc RTI_TSOMin=NULL;static TSOPopProc RTI_TSOPop=NULL;/*----------------------------------------------------------------------------*//* */static CoreRTIState CRTI;static CoreRHInfo coreRHInfo;/*----------------------------------------------------------------------------*//******************************** ID Functions ********************************//*----------------------------------------------------------------------------*/int Core_nodeID(void){ return(RTIKIT_nodeid);}int Core_federateID(void){ return(RTIKIT_nodeid);}/*----------------------------------------------------------------------------*//************************** Time Management Functions *************************//*----------------------------------------------------------------------------*//*----------------------------------------------------------------------------*//* INTERFACE *//*----------------------------------------------------------------------------*/fdkErrorCode Core_InitRTI(void){ fdkErrorCode retCode; CRTI.CurrentTime = TM_ZERO; CRTI.CurrentQual = TM_TIME_QUAL_INCL; CRTI.LookAhead = TM_ZERO; CRTI.TargetLA = TM_ZERO; CRTI.PendingRequest = NO_REQ; CRTI.PendingTime = TM_ZERO; CRTI.PendingQual = TM_TIME_QUAL_INCL; CRTI.MinDelivered = TM_ZERO; CRTI.LocalMin = TM_ZERO; CRTI.LocalMinQual = TM_TIME_QUAL_INCL; CRTI.LBTS = TM_ZERO; CRTI.LBTSQual = TM_TIME_QUAL_INCL; CRTI.LBTSEnabled = TRUE; CRTI.NPendingLBTS = 0; CRTI.NLBTSDeferred = 0; CRTI.RecentLBTSDoneTransaction = -1; CRTI.FailCount = 0; /*KALYAN: Optimization defaults & overrides*/ { char *estr = 0; CRTI.optimize.ner_fastpath = (estr=getenv("RTIC_OPTNER"))?atoi(estr):TRUE; CRTI.optimize.min_nlbts = (estr=getenv("RTIC_OPTMINLBTS"))?atoi(estr):TRUE; } /* set procedure to be called when an LBTS computation is started elsewhere */ TM_SetLBTSStartProc (core_MyLBTSStarted); /* Init the debug, retraction manager and RHM debug */ if ((retCode = Core_InitDebug(stdout)) != fdkSUCCEEDED) { return(retCode); } if ((retCode = Core_InitRHMDebug(stdout)) != fdkSUCCEEDED) { return(retCode); } if ((retCode = Core_InitRHM()) != fdkSUCCEEDED) { return(retCode); } /* Check to make certain required callbacks have been registered */ if (RTI_DeliverOneMessage==NULL) { return(fdkDOMNR); } if (RTI_DeliverTSOEvents==NULL) { return(fdkDTSOENR); } if (RTI_DeliverROEvents==NULL) { return(fdkDROENR); } if (RTI_TimeAdvanceGrant==NULL) { return(fdkTAGNR); } if (RTI_TSOMin==NULL) { return(fdkTSOMNR); } if (RTI_TSOPop==NULL) { return(fdkTSOPNR); } return(fdkSUCCEEDED); }/*----------------------------------------------------------------------------*//* FUNCTION REGISTRATION FUNCTIONS *//*----------------------------------------------------------------------------*//* These functions are to be called during the initialization of the RTI. These functions perform necessary state initialization and function registration.*/fdkErrorCode Core_RegisterDeliverOneMessage(DeliverOneMessageProc p){ RTI_DeliverOneMessage = p;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -