MotorWare f2806x Module API Documentation

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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
15 #include "sw/modules/types/src/types.h"
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
51 uint32_t minDeltaCntObserved;
52 uint32_t avgDeltaCntObserved;
53 uint32_t maxDeltaCntObserved;
54
55 bool flag_resetStats;
56 } CPU_USAGE_Obj;
57
58
61 typedef struct _CPU_USAGE_Obj_ *CPU_USAGE_Handle;
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
207 obj->timerPeriod_cnts = timerPeriod_cnts;
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
272 obj->deltaCntAccNum = deltaCntAccNum;
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
285 obj->deltaCntAccNumMax = deltaCntAccNumMax;
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);
357 uint32_t minDeltaCntObserved = CPU_USAGE_getMinDeltaCntObserved(handle);
358 uint32_t avgDeltaCntObserved = CPU_USAGE_getAvgDeltaCntObserved(handle);
359 uint32_t maxDeltaCntObserved = CPU_USAGE_getMaxDeltaCntObserved(handle);
360 uint32_t deltaCntAcc = CPU_USAGE_getDeltaCntAcc(handle);
361 uint32_t deltaCntAccNum = CPU_USAGE_getDeltaCntAccNum(handle);
362 uint32_t deltaCntAccNumMax = CPU_USAGE_getDeltaCntAccNumMax(handle);
363 uint32_t cnt_z0 = CPU_USAGE_getCnt_z0(handle);
364 uint32_t cnt_z1 = CPU_USAGE_getCnt_z1(handle);
365
366 bool flag_resetStats = CPU_USAGE_getFlag_resetStats(handle);
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
CPU_USAGE_Handle
struct _CPU_USAGE_Obj_ * CPU_USAGE_Handle
Defines the CPU_USAGE handle.
Definition: cpu_usage.h:61
types.h
Contains the public interface to the types definitions.
CPU_USAGE_setCnt_z0
static void CPU_USAGE_setCnt_z0(CPU_USAGE_Handle handle, const uint32_t cnt)
Sets the current count value.
Definition: cpu_usage.h:216
CPU_USAGE_setDeltaCntAcc
static void CPU_USAGE_setDeltaCntAcc(CPU_USAGE_Handle handle, const uint32_t deltaCntAcc)
Sets the accumulated delta counts value.
Definition: cpu_usage.h:255
_CPU_USAGE_Obj_::cnt_z1
uint32_t cnt_z1
the previous timer count value, cnts
Definition: cpu_usage.h:44
_CPU_USAGE_Obj_::cnt_z0
uint32_t cnt_z0
the current timer count value, cnts
Definition: cpu_usage.h:43
CPU_USAGE_getCnt_z1
static uint32_t CPU_USAGE_getCnt_z1(CPU_USAGE_Handle handle)
Gets the previous count value.
Definition: cpu_usage.h:97
CPU_USAGE_getAvgDeltaCntObserved
static uint32_t CPU_USAGE_getAvgDeltaCntObserved(CPU_USAGE_Handle handle)
Gets the average delta count observed, cnts.
Definition: cpu_usage.h:163
CPU_USAGE_setFlag_resetStats
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
_CPU_USAGE_Obj_::avgDeltaCntObserved
uint32_t avgDeltaCntObserved
the average delta counts observed, cnts
Definition: cpu_usage.h:52
CPU_USAGE_getCnt_z0
static uint32_t CPU_USAGE_getCnt_z0(CPU_USAGE_Handle handle)
Gets the current count value.
Definition: cpu_usage.h:86
CPU_USAGE_getDeltaCntAccNumMax
static uint32_t CPU_USAGE_getDeltaCntAccNumMax(CPU_USAGE_Handle handle)
Gets the maximum number of accumulated delta counts, num.
Definition: cpu_usage.h:141
CPU_USAGE_setDeltaCnt
static void CPU_USAGE_setDeltaCnt(CPU_USAGE_Handle handle, const uint32_t deltaCnt)
Sets the delta count value.
Definition: cpu_usage.h:242
CPU_USAGE_setMinDeltaCntObserved
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
_CPU_USAGE_Obj_::deltaCntAccNumMax
uint32_t deltaCntAccNumMax
the maximum number of accumulated delta count values, num
Definition: cpu_usage.h:49
CPU_USAGE_setDeltaCntAccNumMax
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
_CPU_USAGE_Obj_::deltaCnt
uint32_t deltaCnt
the latest delta count value, cnts
Definition: cpu_usage.h:45
CPU_USAGE_setMaxDeltaCntObserved
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
CPU_USAGE_setTimerPeriod
static void CPU_USAGE_setTimerPeriod(CPU_USAGE_Handle handle, const uint32_t timerPeriod_cnts)
Sets the timer period, cnts.
Definition: cpu_usage.h:203
CPU_USAGE_getDeltaCntAcc
static uint32_t CPU_USAGE_getDeltaCntAcc(CPU_USAGE_Handle handle)
Gets the accumulated delta counts, cnts.
Definition: cpu_usage.h:119
CPU_USAGE_setParams
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_init
CPU_USAGE_Handle CPU_USAGE_init(void *pMemory, const size_t numBytes)
Initializes the CPU usage (CPU_USAGE) object.
Definition: cpu_usage.c:21
CPU_USAGE_getDeltaCnt
static uint32_t CPU_USAGE_getDeltaCnt(CPU_USAGE_Handle handle)
Gets the latest delta count measured, cnts.
Definition: cpu_usage.h:108
CPU_USAGE_getFlag_resetStats
static bool CPU_USAGE_getFlag_resetStats(CPU_USAGE_Handle handle)
Gets the state of the reset stats flag.
Definition: cpu_usage.h:185
CPU_USAGE_getMaxDeltaCntObserved
static uint32_t CPU_USAGE_getMaxDeltaCntObserved(CPU_USAGE_Handle handle)
Gets the maximum delta count observed, cnts.
Definition: cpu_usage.h:174
_CPU_USAGE_Obj_::minDeltaCntObserved
uint32_t minDeltaCntObserved
the minimum delta counts observed, cnts
Definition: cpu_usage.h:51
CPU_USAGE_setDeltaCntAccNum
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
CPU_USAGE_setAvgDeltaCntObserved
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
_CPU_USAGE_Obj_::timerPeriod_cnts
uint32_t timerPeriod_cnts
the timer period, cnts
Definition: cpu_usage.h:42
_CPU_USAGE_Obj_::flag_resetStats
bool flag_resetStats
a flag to reset all measured data
Definition: cpu_usage.h:55
CPU_USAGE_updateCnts
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
_CPU_USAGE_Obj_
Defines the CPU usage (CPU_USAGE) object.
Definition: cpu_usage.h:40
CPU_USAGE_getMinDeltaCntObserved
static uint32_t CPU_USAGE_getMinDeltaCntObserved(CPU_USAGE_Handle handle)
Gets the minimum delta count observed, cnts.
Definition: cpu_usage.h:152
CPU_USAGE_Obj
struct _CPU_USAGE_Obj_ CPU_USAGE_Obj
Defines the CPU usage (CPU_USAGE) object.
CPU_USAGE_getDeltaCntAccNum
static uint32_t CPU_USAGE_getDeltaCntAccNum(CPU_USAGE_Handle handle)
Gets the number of accumulated delta counts, num.
Definition: cpu_usage.h:130
_CPU_USAGE_Obj_::deltaCntAccNum
uint32_t deltaCntAccNum
the number of accumulated delta count values, num
Definition: cpu_usage.h:48
_CPU_USAGE_Obj_::deltaCntAcc
uint32_t deltaCntAcc
the accumulated delta count values, cnts
Definition: cpu_usage.h:47
CPU_USAGE_run
static void CPU_USAGE_run(CPU_USAGE_Handle handle)
Runs the CPU usage module.
Definition: cpu_usage.h:354
CPU_USAGE_getTimerPeriod
static uint32_t CPU_USAGE_getTimerPeriod(CPU_USAGE_Handle handle)
Gets the timer period, cnts.
Definition: cpu_usage.h:75
CPU_USAGE_setCnt_z1
static void CPU_USAGE_setCnt_z1(CPU_USAGE_Handle handle, const uint32_t cnt)
Sets the previous count value.
Definition: cpu_usage.h:229
_CPU_USAGE_Obj_::maxDeltaCntObserved
uint32_t maxDeltaCntObserved
the maximum delta counts observed, cnts
Definition: cpu_usage.h:53