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