All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sir_timer.h
Go to the documentation of this file.
1 /*
2  * sir_timer.h
3  *
4  * Created on: Apr 3, 2014
5  * Author: root
6  */
7  // Needed by doxygen
9 
10 #ifndef SIR_TIMER_H_
11 #define SIR_TIMER_H_
12 
13 #include <cvmx.h>
14 #include <cvmx-version.h>
15 #include <cvmx-tim.h>
16 #include <cvmx-coremask.h>
17 #include <cvmx-pow.h>
18 #include <cvmx-spinlock.h>
19 #include <cvmx-bootmem.h>
20 #include <cvmx-malloc.h>
21 #include <cvmx-atomic.h>
22 
23 #include <common.h>
24 #include "common/sir_common.h"
25 
26 #define SIMULATORACCELERATOR 100ul
27 
28 #define DEFAULT_TIMER_INTERVAL 2000
29 #define DEFAULT_TIMER_TICKS 2500
30 #define DEFAULT_UNUSED_START 1
31 
32 #define SIRIUS_TIMER_POOL CVMX_FPA_WQE_POOL
33 #define SIRIUS_TIMER_POOL_SIZE CVMX_FPA_WQE_POOL_SIZE
34 
35 #define sir_timer_t uint64_t
36 
37 
44 typedef bool (*THandleUnused)(cvmx_wqe_t *ptWQE, void *pVoid); // Function pointer typedef
45 
46 struct THUStruc // Structure that holds function pointer and class pointer for unused handling
47 {
48  THandleUnused tFP; // Function pointer
49  void * pThis; // This pointer of class instance
50 };
51 
53 
57 {
58 public:
60 
63  static CSiriusTimer* Instance(void); // Singleton
65 
73  bool SetStartUnused(uint8_t ui8StartUnused);
74 
76 
85  int GetNextUnused(THandleUnused tHU, void *pThis, uint32_t ui32SizeOfStruct);
87 
95  bool Handle_Unused(cvmx_wqe_t *pWQE);
96 
99 
101  bool NowIsLater(sir_timer_t tTime);
102 
105 
108 
110  int64_t RelDiffCurrentTime(sir_timer_t tTime);
111 
114 
116  static int GetAndAddNrOfPacketsInUse(int iIncrement);
117 
119  void TimInit(void);
120 
122 
133  template <class T>
134  static bool TimLaunchPacket(T *pTimerData, uint8_t ui8Unused, uint32_t ui32Tag, uint32_t ui32Grp)
135  {
136  cvmx_wqe_t *pWQE = (cvmx_wqe_t *)cvmx_fpa_alloc(SIRIUS_TIMER_POOL);
137  if (!pWQE)
138  {
139  SIR_COMMON_DEBUG_MSG(SIR_ERROR_DEBUG_LEVEL, "LaunchTimerPacket: Could not claim memory\n");
140  return false;
141  }
142  memset(pWQE, 0, SIRIUS_TIMER_POOL_SIZE);
143  cvmx_wqe_set_grp(pWQE, ui32Grp);
144  cvmx_wqe_set_tag(pWQE, ui32Tag);
145  if (ui32Tag)
146  cvmx_wqe_set_tt(pWQE, CVMX_POW_TAG_TYPE_ATOMIC);
147  else
148  cvmx_wqe_set_tt(pWQE, CVMX_POW_TAG_TYPE_NULL);
149 
150  cvmx_wqe_set_unused8(pWQE, ui8Unused);
151 
152  uint64_t ui64Ticks = pTimerData->ui64Ticks;
153  if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM)
154  {
155  ui64Ticks /= SIMULATORACCELERATOR/4;
156  }
157  SIRIUS_ASSERT(sizeof(T) < sizeof pWQE->packet_data,("TimLaunchPacket::packet data WQE too small to contain requested data\n"));
158 
159  memcpy(pWQE->packet_data, pTimerData, sizeof(T));
160 
161  cvmx_tim_status_t tTimStat = cvmx_tim_add_entry(pWQE, ui64Ticks, NULL);
162  if (tTimStat != CVMX_TIM_STATUS_SUCCESS)
163  {
164  cvmx_fpa_free(pWQE, SIRIUS_TIMER_POOL, 0);
165  SIR_COMMON_DEBUG_MSG(SIR_ERROR_DEBUG_LEVEL, "LaunchTimerPacket: Adding timer returned error %d\n", (int)tTimStat);
166  return false;
167  }
168  cvmx_atomic_add32(&m_iNrOfTimerPackets, 1);
169  return true;
170  }
171 
173  static void TimRemovePacket(cvmx_wqe_t *pWQE)
174  {
175  if (pWQE)
176  {
177  cvmx_fpa_free(pWQE, SIRIUS_TIMER_POOL, 0);
178  cvmx_atomic_add32(&m_iNrOfTimerPackets, -1l);
179  }
180  else
181  {
182  SIR_COMMON_DEBUG_MSG(SIR_ERROR_DEBUG_LEVEL, "RemoveTimerPacket: No pointer to Work Queue Entry\n");
183  }
184  }
185 
187 
195  static bool TimReLaunchPacket(cvmx_wqe_t *pWQE, uint64_t ui64Ticks)
196  {
197  if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM)
198  {
199  ui64Ticks /= SIMULATORACCELERATOR/4;
200  }
201  cvmx_tim_status_t tTimStat = cvmx_tim_add_entry(pWQE, ui64Ticks, NULL);
202  if (tTimStat != CVMX_TIM_STATUS_SUCCESS)
203  {
204  SIR_COMMON_DEBUG_MSG(SIR_ERROR_DEBUG_LEVEL, "ReLaunchTimerPacket: Adding timer returned error %d\n", (int)tTimStat);
205  return false;
206  }
207  return true;
208  }
209 
210 private:
211  uint64_t m_ui64TimerInterval; // Interval between buckets in ms
212  uint64_t m_ui64MaxTime; // Max time distance achievable by timer unit by ms
213  uint64_t m_ui64TicksPerMs; // Processor cycles per mili second
214 
215  int m_iUnusedStart;
216  THUStruc m_tHU[256]; // Pointer array to handle_unused functions
217  CVMX_SHARED static int m_iNrOfTimerPackets; // Counts the number of timer packets active
218 
219 };
220 
221 #endif /* SIR_TIMER_H_ */
bool SetStartUnused(uint8_t ui8StartUnused)
Set start of Unused value.
#define SIMULATORACCELERATOR
To speed tings up, tick values are divided by this value when using the simulator.
Definition: sir_timer.h:26
sir_timer_t SubstractFromCurrentTime(sir_timer_t tDelta_ms)
Create a time value of tDelta_ms mili seconds in the past.
#define SIRIUS_ASSERT(exp, msg)
Definition: sir_common.h:77
#define SIRIUS_TIMER_POOL
Definition: sir_timer.h:32
THandleUnused tFP
Definition: sir_timer.h:48
void TimInit(void)
Initialize the CTimer class.
bool NowIsLater(sir_timer_t tTime)
Test if current time is later than the time value tTime.
static bool TimReLaunchPacket(cvmx_wqe_t *pWQE, uint64_t ui64Ticks)
Relaunch the timer packet.
Definition: sir_timer.h:195
static int GetAndAddNrOfPacketsInUse(int iIncrement)
DIAGNOSTIC: Get and/or increment the number of packets that are used by the CTimer class...
static void TimRemovePacket(cvmx_wqe_t *pWQE)
Delete this timer packet.
Definition: sir_timer.h:173
int64_t RelDiffCurrentTime(sir_timer_t tTime)
Calculate the releative difference between the current time and the time value tTime.
static CSiriusTimer * Instance(void)
Singleton for the CSiriusTimer class.
Sirius Timer Class.
Definition: sir_timer.h:56
static bool TimLaunchPacket(T *pTimerData, uint8_t ui8Unused, uint32_t ui32Tag, uint32_t ui32Grp)
Create a timer packet and launch it.
Definition: sir_timer.h:134
uint64_t sir_timer_t
bool(* THandleUnused)(cvmx_wqe_t *ptWQE, void *pVoid)
Function pointer for call back functions for unused-value handling.
Definition: sir_timer.h:44
#define SIR_COMMON_DEBUG_MSG(lvl, format, args...)
Definition: sir_common.h:68
int GetNextUnused(THandleUnused tHU, void *pThis, uint32_t ui32SizeOfStruct)
Get the next free Unused value.
bool Handle_Unused(cvmx_wqe_t *pWQE)
Handle a packet that has the Unused field of the Work Queue Entry set.
void * pThis
Definition: sir_timer.h:49
sir_timer_t AddToCurrentTime(sir_timer_t tDelta_ms)
Create a time value of tDelta_ms mili seconds in the future.
sir_timer_t SetToCurrentTime()
Set the time value the the current time.
#define SIRIUS_TIMER_POOL_SIZE
Definition: sir_timer.h:33
sir_timer_t AbsDiffCurrentTime(sir_timer_t tTime)
Calculate the absolute difference between the current time and the time value tTime.