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 *)atomic_long_xchg(&(sched)->current_account, (long)(new_account)); \
62  __prev; \
63 })
64 
65 /* Return the currently active accounting entity. */
66 #define xnstat_exectime_get_current(sched) ((sched)->current_account)
67 
68 /* Finalize an account (no need to accumulate the exectime, just mark the
69  switch date and set the new account). */
70 #define xnstat_exectime_finalize(sched, new_account) \
71 do { \
72  (sched)->last_account_switch = xnclock_core_read_raw(); \
73  (sched)->current_account = (new_account); \
74 } while (0)
75 
76 /* Obtain content of xnstat_exectime_t */
77 #define xnstat_exectime_get_start(account) ((account)->start)
78 #define xnstat_exectime_get_total(account) ((account)->total)
79 
80 /* Obtain last account switch date of considered sched */
81 #define xnstat_exectime_get_last_switch(sched) ((sched)->last_account_switch)
82 
83 /* Reset statistics from inside the accounted entity (e.g. after CPU
84  migration). */
85 #define xnstat_exectime_reset_stats(stat) \
86 do { \
87  (stat)->total = 0; \
88  (stat)->start = xnclock_core_read_raw(); \
89 } while (0)
90 
91 
92 typedef struct xnstat_counter {
93  unsigned long counter;
94 } xnstat_counter_t;
95 
96 static inline unsigned long xnstat_counter_inc(xnstat_counter_t *c)
97 {
98  return c->counter++;
99 }
100 
101 static inline unsigned long xnstat_counter_get(xnstat_counter_t *c)
102 {
103  return c->counter;
104 }
105 
106 static inline void xnstat_counter_set(xnstat_counter_t *c, unsigned long value)
107 {
108  c->counter = value;
109 }
110 
111 #else /* !CONFIG_XENO_OPT_STATS */
112 typedef struct xnstat_exectime {
113 } xnstat_exectime_t;
114 
115 #define xnstat_percpu_data NULL
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 */