Xenomai API  2.6.5
stat.h
1 /*
2  * Copyright (C) 2006 Jan Kiszka <[email protected]>.
3  * Copyright (C) 2006 Dmitry Adamushko <[email protected]>.
4  *
5  * Xenomai is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 2 of the License,
8  * or (at your option) any later version.
9  *
10  * Xenomai is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Xenomai; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 
21 #ifndef _XENO_NUCLEUS_STAT_H
22 #define _XENO_NUCLEUS_STAT_H
23 
24 #include <nucleus/types.h> /* This pulls in linux/config.h with legacy kernels. */
25 
26 #ifdef CONFIG_XENO_OPT_STATS
27 
28 typedef struct xnstat_exectime {
29 
30  xnticks_t start; /* Start of execution time accumulation */
31 
32  xnticks_t total; /* Accumulated execution time */
33 
34 } xnstat_exectime_t;
35 
36 /* Return current date which can be passed to other xnstat services for
37  immediate or lazy accounting. */
38 #define xnstat_exectime_now() xnarch_get_cpu_tsc()
39 
40 /* Accumulate exectime of the current account until the given date. */
41 #define xnstat_exectime_update(sched, date) \
42 do { \
43  (sched)->current_account->total += \
44  date - (sched)->last_account_switch; \
45  (sched)->last_account_switch = date; \
46  /* All changes must be committed before changing the current_account \
47  reference in sched (required for xnintr_sync_stat_references) */ \
48  xnarch_memory_barrier(); \
49 } while (0)
50 
51 /* Update the current account reference, returning the previous one. */
52 #define xnstat_exectime_set_current(sched, new_account) \
53 ({ \
54  xnstat_exectime_t *__prev; \
55  __prev = xnarch_atomic_xchg(&(sched)->current_account, (new_account)); \
56  __prev; \
57 })
58 
59 /* Return the currently active accounting entity. */
60 #define xnstat_exectime_get_current(sched) ((sched)->current_account)
61 
62 /* Finalize an account (no need to accumulate the exectime, just mark the
63  switch date and set the new account). */
64 #define xnstat_exectime_finalize(sched, new_account) \
65 do { \
66  (sched)->last_account_switch = xnarch_get_cpu_tsc(); \
67  (sched)->current_account = (new_account); \
68 } while (0)
69 
70 /* Obtain content of xnstat_exectime_t */
71 #define xnstat_exectime_get_start(account) ((account)->start)
72 #define xnstat_exectime_get_total(account) ((account)->total)
73 
74 /* Obtain last account switch date of considered sched */
75 #define xnstat_exectime_get_last_switch(sched) ((sched)->last_account_switch)
76 
77 /* Reset statistics from inside the accounted entity (e.g. after CPU
78  migration). */
79 #define xnstat_exectime_reset_stats(stat) \
80 do { \
81  (stat)->total = 0; \
82  (stat)->start = xnarch_get_cpu_tsc(); \
83 } while (0)
84 
85 
86 typedef struct xnstat_counter {
87  unsigned long counter;
88 } xnstat_counter_t;
89 
90 static inline unsigned long xnstat_counter_inc(xnstat_counter_t *c)
91 {
92  return c->counter++;
93 }
94 
95 static inline unsigned long xnstat_counter_get(xnstat_counter_t *c)
96 {
97  return c->counter;
98 }
99 
100 static inline void xnstat_counter_set(xnstat_counter_t *c, unsigned long value)
101 {
102  c->counter = value;
103 }
104 
105 #else /* !CONFIG_XENO_OPT_STATS */
106 typedef struct xnstat_exectime {
107 #ifdef __XENO_SIM__
108  int dummy;
109 #endif /* __XENO_SIM__ */
110 } xnstat_exectime_t;
111 
112 #define xnstat_exectime_now() ({ 0; })
113 #define xnstat_exectime_update(sched, date) do { } while (0)
114 #define xnstat_exectime_set_current(sched, new_account) ({ (void)sched; NULL; })
115 #define xnstat_exectime_get_current(sched) ({ (void)sched; NULL; })
116 #define xnstat_exectime_finalize(sched, new_account) do { } while (0)
117 #define xnstat_exectime_get_start(account) ({ 0; })
118 #define xnstat_exectime_get_total(account) ({ 0; })
119 #define xnstat_exectime_get_last_switch(sched) ({ 0; })
120 #define xnstat_exectime_reset_stats(account) do { } while (0)
121 
122 typedef struct xnstat_counter {
123 #ifdef __XENO_SIM__
124  int dummy;
125 #endif /* __XENO_SIM__ */
126 } xnstat_counter_t;
127 
128 #define xnstat_counter_inc(c) ({ do { } while(0); 0; })
129 #define xnstat_counter_get(c) ({ 0; })
130 #define xnstat_counter_set(c, value) do { } while (0)
131 #endif /* CONFIG_XENO_OPT_STATS */
132 
133 /* Account the exectime of the current account until now, switch to
134  new_account, and return the previous one. */
135 #define xnstat_exectime_switch(sched, new_account) \
136 ({ \
137  xnstat_exectime_update(sched, xnstat_exectime_now()); \
138  xnstat_exectime_set_current(sched, new_account); \
139 })
140 
141 /* Account the exectime of the current account until given start time, switch
142  to new_account, and return the previous one. */
143 #define xnstat_exectime_lazy_switch(sched, new_account, date) \
144 ({ \
145  xnstat_exectime_update(sched, date); \
146  xnstat_exectime_set_current(sched, new_account); \
147 })
148 
149 #endif /* !_XENO_NUCLEUS_STAT_H */