Xenomai  3.3
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 #ifndef _COBALT_KERNEL_STAT_H
21 #define _COBALT_KERNEL_STAT_H
22 
23 #include <cobalt/kernel/clock.h>
24 
30 #ifdef CONFIG_XENO_OPT_STATS
31 
32 typedef struct xnstat_exectime {
33 
34  xnticks_t start; /* Start of execution time accumulation */
35 
36  xnticks_t total; /* Accumulated execution time */
37 
38 } xnstat_exectime_t;
39 
40 /* Return current date which can be passed to other xnstat services for
41  immediate or lazy accounting. */
42 #define xnstat_exectime_now() xnclock_core_read_raw()
43 
44 /* Accumulate exectime of the current account until the given date. */
45 #define xnstat_exectime_update(sched, date) \
46 do { \
47  xnticks_t __date = date; \
48  (sched)->current_account->total += \
49  __date - (sched)->last_account_switch; \
50  (sched)->last_account_switch = __date; \
51  /* All changes must be committed before changing the current_account \
52  reference in sched (required for xnintr_sync_stat_references) */ \
53  smp_wmb(); \
54 } while (0)
55 
56 /* Update the current account reference, returning the previous one. */
57 #define xnstat_exectime_set_current(sched, new_account) \
58 ({ \
59  xnstat_exectime_t *__prev; \
60  __prev = (xnstat_exectime_t *) \
61  atomic_long_xchg((atomic_long_t *)&(sched)->current_account, \
62  (long)(new_account)); \
63  __prev; \
64 })
65 
66 /* Return the currently active accounting entity. */
67 #define xnstat_exectime_get_current(sched) ((sched)->current_account)
68 
69 /* Finalize an account (no need to accumulate the exectime, just mark the
70  switch date and set the new account). */
71 #define xnstat_exectime_finalize(sched, new_account) \
72 do { \
73  (sched)->last_account_switch = xnclock_core_read_raw(); \
74  (sched)->current_account = (new_account); \
75 } while (0)
76 
77 /* Obtain content of xnstat_exectime_t */
78 #define xnstat_exectime_get_start(account) ((account)->start)
79 #define xnstat_exectime_get_total(account) ((account)->total)
80 
81 /* Obtain last account switch date of considered sched */
82 #define xnstat_exectime_get_last_switch(sched) ((sched)->last_account_switch)
83 
84 /* Reset statistics from inside the accounted entity (e.g. after CPU
85  migration). */
86 #define xnstat_exectime_reset_stats(stat) \
87 do { \
88  (stat)->total = 0; \
89  (stat)->start = xnclock_core_read_raw(); \
90 } while (0)
91 
92 
93 typedef struct xnstat_counter {
94  unsigned long counter;
95 } xnstat_counter_t;
96 
97 static inline unsigned long xnstat_counter_inc(xnstat_counter_t *c)
98 {
99  return c->counter++;
100 }
101 
102 static inline unsigned long xnstat_counter_get(xnstat_counter_t *c)
103 {
104  return c->counter;
105 }
106 
107 static inline void xnstat_counter_set(xnstat_counter_t *c, unsigned long value)
108 {
109  c->counter = value;
110 }
111 
112 #else /* !CONFIG_XENO_OPT_STATS */
113 typedef struct xnstat_exectime {
114 } xnstat_exectime_t;
115 
116 #define xnstat_exectime_now() ({ 0; })
117 #define xnstat_exectime_update(sched, date) do { } while (0)
118 #define xnstat_exectime_set_current(sched, new_account) ({ (void)sched; NULL; })
119 #define xnstat_exectime_get_current(sched) ({ (void)sched; NULL; })
120 #define xnstat_exectime_finalize(sched, new_account) do { } while (0)
121 #define xnstat_exectime_get_start(account) ({ 0; })
122 #define xnstat_exectime_get_total(account) ({ 0; })
123 #define xnstat_exectime_get_last_switch(sched) ({ 0; })
124 #define xnstat_exectime_reset_stats(account) do { } while (0)
125 
126 typedef struct xnstat_counter {
127 } xnstat_counter_t;
128 
129 #define xnstat_counter_inc(c) ({ do { } while(0); 0; })
130 #define xnstat_counter_get(c) ({ 0; })
131 #define xnstat_counter_set(c, value) do { } while (0)
132 #endif /* CONFIG_XENO_OPT_STATS */
133 
134 /* Account the exectime of the current account until now, switch to
135  new_account, and return the previous one. */
136 #define xnstat_exectime_switch(sched, new_account) \
137 ({ \
138  xnstat_exectime_update(sched, xnstat_exectime_now()); \
139  xnstat_exectime_set_current(sched, new_account); \
140 })
141 
142 /* Account the exectime of the current account until given start time, switch
143  to new_account, and return the previous one. */
144 #define xnstat_exectime_lazy_switch(sched, new_account, date) \
145 ({ \
146  xnstat_exectime_update(sched, date); \
147  xnstat_exectime_set_current(sched, new_account); \
148 })
149 
152 #endif /* !_COBALT_KERNEL_STAT_H */