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