MotorWare f2806x Module API Documentation
cpu_usage.h
Go to the documentation of this file.
1 #ifndef _CPU_USAGE_H_
2 #define _CPU_USAGE_H_
3 
9 
10 
11 // **************************************************************************
12 // the includes
13 #include <math.h>
14 
16 
17 
20 
23 
24 
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 
31 // **************************************************************************
32 // the defines
33 
34 
35 // **************************************************************************
36 // the typedefs
37 
40 typedef struct _CPU_USAGE_Obj_
41 {
42  uint32_t timerPeriod_cnts;
43  uint32_t cnt_z0;
44  uint32_t cnt_z1;
45  uint32_t deltaCnt;
46 
47  uint32_t deltaCntAcc;
48  uint32_t deltaCntAccNum;
49  uint32_t deltaCntAccNumMax;
50 
54 
57 
58 
62 
63 
64 // **************************************************************************
65 // the globals
66 
67 
68 // **************************************************************************
69 // the function prototypes
70 
71 
75 static inline uint32_t CPU_USAGE_getTimerPeriod(CPU_USAGE_Handle handle)
76 {
77  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
78 
79  return(obj->timerPeriod_cnts);
80 } // end of CPU_USAGE_getTimerPeriod() function
81 
82 
86 static inline uint32_t CPU_USAGE_getCnt_z0(CPU_USAGE_Handle handle)
87 {
88  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
89 
90  return(obj->cnt_z0);
91 } // end of CPU_USAGE_getCnt_z0() function
92 
93 
97 static inline uint32_t CPU_USAGE_getCnt_z1(CPU_USAGE_Handle handle)
98 {
99  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
100 
101  return(obj->cnt_z1);
102 } // end of CPU_USAGE_getCnt_z1() function
103 
104 
108 static inline uint32_t CPU_USAGE_getDeltaCnt(CPU_USAGE_Handle handle)
109 {
110  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
111 
112  return(obj->deltaCnt);
113 } // end of CPU_USAGE_getDeltaCnt() function
114 
115 
119 static inline uint32_t CPU_USAGE_getDeltaCntAcc(CPU_USAGE_Handle handle)
120 {
121  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
122 
123  return(obj->deltaCntAcc);
124 } // end of CPU_USAGE_getDeltaCntAcc() function
125 
126 
130 static inline uint32_t CPU_USAGE_getDeltaCntAccNum(CPU_USAGE_Handle handle)
131 {
132  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
133 
134  return(obj->deltaCntAccNum);
135 } // end of CPU_USAGE_getDeltaCntAccNum() function
136 
137 
141 static inline uint32_t CPU_USAGE_getDeltaCntAccNumMax(CPU_USAGE_Handle handle)
142 {
143  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
144 
145  return(obj->deltaCntAccNumMax);
146 } // end of CPU_USAGE_getDeltaCntAccNumMax() function
147 
148 
152 static inline uint32_t CPU_USAGE_getMinDeltaCntObserved(CPU_USAGE_Handle handle)
153 {
154  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
155 
156  return(obj->minDeltaCntObserved);
157 } // end of CPU_USAGE_getMinDeltaCntObserved() function
158 
159 
163 static inline uint32_t CPU_USAGE_getAvgDeltaCntObserved(CPU_USAGE_Handle handle)
164 {
165  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
166 
167  return(obj->avgDeltaCntObserved);
168 } // end of CPU_USAGE_getAvgDeltaCntObserved() function
169 
170 
174 static inline uint32_t CPU_USAGE_getMaxDeltaCntObserved(CPU_USAGE_Handle handle)
175 {
176  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
177 
178  return(obj->maxDeltaCntObserved);
179 } // end of CPU_USAGE_getMaxDeltaCntObserved() function
180 
181 
185 static inline bool CPU_USAGE_getFlag_resetStats(CPU_USAGE_Handle handle)
186 {
187  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
188 
189  return(obj->flag_resetStats);
190 } // end of CPU_USAGE_getFlag_resetStats() function
191 
192 
197 extern CPU_USAGE_Handle CPU_USAGE_init(void *pMemory,const size_t numBytes);
198 
199 
203 static inline void CPU_USAGE_setTimerPeriod(CPU_USAGE_Handle handle,const uint32_t timerPeriod_cnts)
204 {
205  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
206 
208 
209  return;
210 } // end of CPU_USAGE_setTimerPeriod() function
211 
212 
216 static inline void CPU_USAGE_setCnt_z0(CPU_USAGE_Handle handle,const uint32_t cnt)
217 {
218  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
219 
220  obj->cnt_z0 = cnt;
221 
222  return;
223 } // end of CPU_USAGE_setCnt_z0() function
224 
225 
229 static inline void CPU_USAGE_setCnt_z1(CPU_USAGE_Handle handle,const uint32_t cnt)
230 {
231  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
232 
233  obj->cnt_z1 = cnt;
234 
235  return;
236 } // end of CPU_USAGE_setCnt_z1() function
237 
238 
242 static inline void CPU_USAGE_setDeltaCnt(CPU_USAGE_Handle handle,const uint32_t deltaCnt)
243 {
244  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
245 
246  obj->deltaCnt = deltaCnt;
247 
248  return;
249 } // end of CPU_USAGE_setDeltaCnt() function
250 
251 
255 static inline void CPU_USAGE_setDeltaCntAcc(CPU_USAGE_Handle handle,const uint32_t deltaCntAcc)
256 {
257  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
258 
259  obj->deltaCntAcc = deltaCntAcc;
260 
261  return;
262 } // end of CPU_USAGE_setDeltaCntAcc() function
263 
264 
268 static inline void CPU_USAGE_setDeltaCntAccNum(CPU_USAGE_Handle handle,const uint32_t deltaCntAccNum)
269 {
270  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
271 
273 
274  return;
275 } // end of CPU_USAGE_setDeltaCntAccNum() function
276 
277 
281 static inline void CPU_USAGE_setDeltaCntAccNumMax(CPU_USAGE_Handle handle,const uint32_t deltaCntAccNumMax)
282 {
283  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
284 
286 
287  return;
288 } // end of CPU_USAGE_setDeltaCntAccNumMax() function
289 
290 
294 static inline void CPU_USAGE_setMinDeltaCntObserved(CPU_USAGE_Handle handle,const uint32_t minDeltaCnt)
295 {
296  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
297 
298  obj->minDeltaCntObserved = minDeltaCnt;
299 
300  return;
301 } // end of CPU_USAGE_setMinDeltaCntObserved() function
302 
303 
307 static inline void CPU_USAGE_setAvgDeltaCntObserved(CPU_USAGE_Handle handle,const uint32_t avgDeltaCnt)
308 {
309  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
310 
311  obj->avgDeltaCntObserved = avgDeltaCnt;
312 
313  return;
314 } // end of CPU_USAGE_setAvgDeltaCntObserved() function
315 
316 
320 static inline void CPU_USAGE_setMaxDeltaCntObserved(CPU_USAGE_Handle handle,const uint32_t maxDeltaCnt)
321 {
322  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
323 
324  obj->maxDeltaCntObserved = maxDeltaCnt;
325 
326  return;
327 } // end of CPU_USAGE_setMaxDeltaCntObserved() function
328 
329 
333 static inline void CPU_USAGE_setFlag_resetStats(CPU_USAGE_Handle handle,const bool state)
334 {
335  CPU_USAGE_Obj *obj = (CPU_USAGE_Obj *)handle;
336 
337  obj->flag_resetStats = state;
338 
339  return;
340 } // end of CPU_USAGE_setFlag_resetStats() function
341 
342 
347 void CPU_USAGE_setParams(CPU_USAGE_Handle handle,
348  const uint32_t timerPeriod_cnts,
349  const uint32_t numDeltaCntsAvg);
350 
351 
354 static inline void CPU_USAGE_run(CPU_USAGE_Handle handle)
355 {
356  uint32_t timerPeriod = CPU_USAGE_getTimerPeriod(handle);
360  uint32_t deltaCntAcc = CPU_USAGE_getDeltaCntAcc(handle);
361  uint32_t deltaCntAccNum = CPU_USAGE_getDeltaCntAccNum(handle);
363  uint32_t cnt_z0 = CPU_USAGE_getCnt_z0(handle);
364  uint32_t cnt_z1 = CPU_USAGE_getCnt_z1(handle);
365 
367 
368  uint32_t deltaCnt;
369 
370 
371  // compute the actual delta counts
372  // handle wrap around of the timer count
373  // NOTE: count down timer
374  if(cnt_z0 > cnt_z1)
375  {
376  deltaCnt = cnt_z1 + timerPeriod - cnt_z0 + 1;
377  }
378  else
379  {
380  deltaCnt = cnt_z1 - cnt_z0 + 1;
381  }
382 
383  // accumulate delta counts for the average calculation
384  deltaCntAcc += deltaCnt;
385 
386  // increment the number of accumulations
387  deltaCntAccNum++;
388 
389  // calculate average counts if number of accumulations matches the max number
390  if(deltaCntAccNum >= deltaCntAccNumMax)
391  {
392  // compute the average
393  avgDeltaCntObserved = deltaCntAcc/deltaCntAccNum;
394 
395  // reset accumulation and number of accumulations
396  deltaCntAcc = 0;
397  deltaCntAccNum = 0;
398  }
399 
400 
401  // keep track of the minimum delta count observed
402  if(deltaCnt < minDeltaCntObserved)
403  {
404  minDeltaCntObserved = deltaCnt;
405  }
406 
407  // keep track of the maximum delta count observed
408  if(deltaCnt > maxDeltaCntObserved)
409  {
410  maxDeltaCntObserved = deltaCnt;
411  }
412 
413  // reset statistics if reset flag is true
414  if(flag_resetStats == true)
415  {
416  cnt_z0 = 0;
417  cnt_z1 = 0;
418  deltaCnt = 0;
419 
420  deltaCntAcc = 0;
421  deltaCntAccNum = 0;
422 
423  minDeltaCntObserved = timerPeriod;
424  avgDeltaCntObserved = 0;
425  maxDeltaCntObserved = 0;
426 
427  flag_resetStats = false;
428  }
429 
430 
431  // store the values
432  CPU_USAGE_setDeltaCnt(handle,deltaCnt);
433  CPU_USAGE_setDeltaCntAcc(handle,deltaCntAcc);
434  CPU_USAGE_setDeltaCntAccNum(handle,deltaCntAccNum);
435  CPU_USAGE_setMinDeltaCntObserved(handle,minDeltaCntObserved);
436  CPU_USAGE_setAvgDeltaCntObserved(handle,avgDeltaCntObserved);
437  CPU_USAGE_setMaxDeltaCntObserved(handle,maxDeltaCntObserved);
438  CPU_USAGE_setFlag_resetStats(handle, flag_resetStats);
439 
440 
441  return;
442 } // end of FEM_run() function
443 
444 
448 static inline void CPU_USAGE_updateCnts(CPU_USAGE_Handle handle,const uint32_t cnt)
449 {
450  uint32_t cnt_z0 = CPU_USAGE_getCnt_z0(handle);
451 
452  CPU_USAGE_setCnt_z1(handle,cnt_z0);
453  CPU_USAGE_setCnt_z0(handle,cnt);
454 
455  return;
456 } // end of CPU_USAGE_updateCnts() function
457 
458 
459 #ifdef __cplusplus
460 }
461 #endif // extern "C"
462 
464 
465 #endif // end of _CPU_USAGE_H_ definition
466 
467 
struct _CPU_USAGE_Obj_ * CPU_USAGE_Handle
Defines the CPU_USAGE handle.
Definition: cpu_usage.h:61
Contains the public interface to the types definitions.
static void CPU_USAGE_setCnt_z0(CPU_USAGE_Handle handle, const uint32_t cnt)
Sets the current count value.
Definition: cpu_usage.h:216
static void CPU_USAGE_setDeltaCntAcc(CPU_USAGE_Handle handle, const uint32_t deltaCntAcc)
Sets the accumulated delta counts value.
Definition: cpu_usage.h:255
uint32_t cnt_z1
the previous timer count value, cnts
Definition: cpu_usage.h:44
uint32_t cnt_z0
the current timer count value, cnts
Definition: cpu_usage.h:43
static uint32_t CPU_USAGE_getCnt_z1(CPU_USAGE_Handle handle)
Gets the previous count value.
Definition: cpu_usage.h:97
static uint32_t CPU_USAGE_getAvgDeltaCntObserved(CPU_USAGE_Handle handle)
Gets the average delta count observed, cnts.
Definition: cpu_usage.h:163
static void CPU_USAGE_setFlag_resetStats(CPU_USAGE_Handle handle, const bool state)
Sets the state of the reset stats flag.
Definition: cpu_usage.h:333
uint32_t avgDeltaCntObserved
the average delta counts observed, cnts
Definition: cpu_usage.h:52
static uint32_t CPU_USAGE_getCnt_z0(CPU_USAGE_Handle handle)
Gets the current count value.
Definition: cpu_usage.h:86
static uint32_t CPU_USAGE_getDeltaCntAccNumMax(CPU_USAGE_Handle handle)
Gets the maximum number of accumulated delta counts, num.
Definition: cpu_usage.h:141
static void CPU_USAGE_setDeltaCnt(CPU_USAGE_Handle handle, const uint32_t deltaCnt)
Sets the delta count value.
Definition: cpu_usage.h:242
static void CPU_USAGE_setMinDeltaCntObserved(CPU_USAGE_Handle handle, const uint32_t minDeltaCnt)
Sets the minimum delta count observed, cnts.
Definition: cpu_usage.h:294
uint32_t deltaCntAccNumMax
the maximum number of accumulated delta count values, num
Definition: cpu_usage.h:49
static void CPU_USAGE_setDeltaCntAccNumMax(CPU_USAGE_Handle handle, const uint32_t deltaCntAccNumMax)
Sets the maximum number of accumulated delta counts.
Definition: cpu_usage.h:281
uint32_t deltaCnt
the latest delta count value, cnts
Definition: cpu_usage.h:45
static void CPU_USAGE_setMaxDeltaCntObserved(CPU_USAGE_Handle handle, const uint32_t maxDeltaCnt)
Sets the maximum delta count observed, cnts.
Definition: cpu_usage.h:320
static void CPU_USAGE_setTimerPeriod(CPU_USAGE_Handle handle, const uint32_t timerPeriod_cnts)
Sets the timer period, cnts.
Definition: cpu_usage.h:203
static uint32_t CPU_USAGE_getDeltaCntAcc(CPU_USAGE_Handle handle)
Gets the accumulated delta counts, cnts.
Definition: cpu_usage.h:119
void CPU_USAGE_setParams(CPU_USAGE_Handle handle, const uint32_t timerPeriod_cnts, const uint32_t numDeltaCntsAvg)
Sets the CPU usage module parameters.
Definition: cpu_usage.c:34
CPU_USAGE_Handle CPU_USAGE_init(void *pMemory, const size_t numBytes)
Initializes the CPU usage (CPU_USAGE) object.
Definition: cpu_usage.c:21
static uint32_t CPU_USAGE_getDeltaCnt(CPU_USAGE_Handle handle)
Gets the latest delta count measured, cnts.
Definition: cpu_usage.h:108
static bool CPU_USAGE_getFlag_resetStats(CPU_USAGE_Handle handle)
Gets the state of the reset stats flag.
Definition: cpu_usage.h:185
static uint32_t CPU_USAGE_getMaxDeltaCntObserved(CPU_USAGE_Handle handle)
Gets the maximum delta count observed, cnts.
Definition: cpu_usage.h:174
uint32_t minDeltaCntObserved
the minimum delta counts observed, cnts
Definition: cpu_usage.h:51
static void CPU_USAGE_setDeltaCntAccNum(CPU_USAGE_Handle handle, const uint32_t deltaCntAccNum)
Sets the number of accumulated delta counts.
Definition: cpu_usage.h:268
static void CPU_USAGE_setAvgDeltaCntObserved(CPU_USAGE_Handle handle, const uint32_t avgDeltaCnt)
Sets the average delta count observed, cnts.
Definition: cpu_usage.h:307
uint32_t timerPeriod_cnts
the timer period, cnts
Definition: cpu_usage.h:42
bool flag_resetStats
a flag to reset all measured data
Definition: cpu_usage.h:55
static void CPU_USAGE_updateCnts(CPU_USAGE_Handle handle, const uint32_t cnt)
Updates the current and previous count values.
Definition: cpu_usage.h:448
Defines the CPU usage (CPU_USAGE) object.
Definition: cpu_usage.h:40
static uint32_t CPU_USAGE_getMinDeltaCntObserved(CPU_USAGE_Handle handle)
Gets the minimum delta count observed, cnts.
Definition: cpu_usage.h:152
struct _CPU_USAGE_Obj_ CPU_USAGE_Obj
Defines the CPU usage (CPU_USAGE) object.
static uint32_t CPU_USAGE_getDeltaCntAccNum(CPU_USAGE_Handle handle)
Gets the number of accumulated delta counts, num.
Definition: cpu_usage.h:130
uint32_t deltaCntAccNum
the number of accumulated delta count values, num
Definition: cpu_usage.h:48
uint32_t deltaCntAcc
the accumulated delta count values, cnts
Definition: cpu_usage.h:47
static void CPU_USAGE_run(CPU_USAGE_Handle handle)
Runs the CPU usage module.
Definition: cpu_usage.h:354
static uint32_t CPU_USAGE_getTimerPeriod(CPU_USAGE_Handle handle)
Gets the timer period, cnts.
Definition: cpu_usage.h:75
static void CPU_USAGE_setCnt_z1(CPU_USAGE_Handle handle, const uint32_t cnt)
Sets the previous count value.
Definition: cpu_usage.h:229
uint32_t maxDeltaCntObserved
the maximum delta counts observed, cnts
Definition: cpu_usage.h:53