1 #ifndef __XEN_PERFC_H__
2 #define __XEN_PERFC_H__
3 
4 #ifdef CONFIG_PERF_COUNTERS
5 
6 #include <xen/macros.h>
7 #include <xen/percpu.h>
8 
9 /*
10  * NOTE: new counters must be defined in perfc_defn.h
11  *
12  * Counter declarations:
13  * PERFCOUNTER (counter, string)              define a new performance counter
14  * PERFCOUNTER_ARRAY (counter, string, size)  define an array of counters
15  *
16  * Unlike counters, status variables do not reset:
17  * PERFSTATUS (counter, string)               define a new performance stauts
18  * PERFSTATUS_ARRAY (counter, string, size)   define an array of status vars
19  *
20  * unsigned long perfc_value  (counter)        get value of a counter
21  * unsigned long perfc_valuea (counter, index) get value of an array counter
22  * unsigned long perfc_set  (counter, val)     set value of a counter
23  * unsigned long perfc_seta (counter, index, val) set value of an array counter
24  * void perfc_incr  (counter)                  increment a counter
25  * void perfc_decr  (counter)                  decrement a status
26  * void perfc_incra (counter, index)           increment an array counter
27  * void perfc_add   (counter, value)           add a value to a counter
28  * void perfc_adda  (counter, index, value)    add a value to array counter
29  * void perfc_print (counter)                  print out the counter
30  */
31 
32 #define PERFCOUNTER( name, descr ) \
33   PERFC_##name,
34 #define PERFCOUNTER_ARRAY( name, descr, size ) \
35   PERFC_##name,                                \
36   PERFC_LAST_##name = PERFC_ ## name + (size) - sizeof(char[2 * !!(size) - 1]),
37 
38 #define PERFSTATUS       PERFCOUNTER
39 #define PERFSTATUS_ARRAY PERFCOUNTER_ARRAY
40 
41 enum {
42 #include <xen/perfc_defn.h>
43 	NUM_PERFCOUNTERS
44 };
45 
46 #undef PERFCOUNTER
47 #undef PERFCOUNTER_ARRAY
48 #undef PERFSTATUS
49 #undef PERFSTATUS_ARRAY
50 
51 typedef unsigned int perfc_t;
52 
53 DECLARE_PER_CPU(perfc_t[NUM_PERFCOUNTERS], perfcounters);
54 
55 #define perfc_value(x)    this_cpu(perfcounters)[PERFC_ ## x]
56 #define perfc_valuea(x,y)                                               \
57     ( (y) <= PERFC_LAST_ ## x - PERFC_ ## x ?                           \
58 	 this_cpu(perfcounters)[PERFC_ ## x + (y)] : 0 )
59 #define perfc_set(x,v)    (this_cpu(perfcounters)[PERFC_ ## x] = (v))
60 #define perfc_seta(x,y,v)                                               \
61     ( (y) <= PERFC_LAST_ ## x - PERFC_ ## x ?                           \
62 	 this_cpu(perfcounters)[PERFC_ ## x + (y)] = (v) : (v) )
63 #define perfc_incr(x)     (++this_cpu(perfcounters)[PERFC_ ## x])
64 #define perfc_decr(x)     (--this_cpu(perfcounters)[PERFC_ ## x])
65 #define perfc_incra(x,y)                                                \
66     ( (y) <= PERFC_LAST_ ## x - PERFC_ ## x ?                           \
67 	 ++this_cpu(perfcounters)[PERFC_ ## x + (y)] : 0 )
68 #define perfc_add(x,v)    (this_cpu(perfcounters)[PERFC_ ## x] += (v))
69 #define perfc_adda(x,y,v)                                               \
70     ( (y) <= PERFC_LAST_ ## x - PERFC_ ## x ?                           \
71 	 this_cpu(perfcounters)[PERFC_ ## x + (y)] = (v) : (v) )
72 
73 /*
74  * Histogram: special treatment for 0 and 1 count. After that equally spaced
75  * with last bucket taking the rest.
76  */
77 #ifdef CONFIG_PERF_ARRAYS
78 #define perfc_incr_histo(x,v)                                           \
79     do {                                                                \
80         if ( (v) == 0 )                                                 \
81             perfc_incra(x, 0);                                          \
82         else if ( (v) == 1 )                                            \
83             perfc_incra(x, 1);                                          \
84         else if ( (((v) - 2) / PERFC_ ## x ## _BUCKET_SIZE) <           \
85                   (PERFC_LAST_ ## x - PERFC_ ## x - 2) )                \
86             perfc_incra(x, (((v) - 2) / PERFC_ ## x ## _BUCKET_SIZE) + 2); \
87         else                                                            \
88             perfc_incra(x, PERFC_LAST_ ## x - PERFC_ ## x);             \
89     } while ( 0 )
90 #else
91 #define perfc_incr_histo(x,v) ((void)0)
92 #endif
93 
94 struct xen_sysctl_perfc_op;
95 int perfc_control(struct xen_sysctl_perfc_op *pc);
96 
97 extern void cf_check perfc_printall(unsigned char key);
98 extern void cf_check perfc_reset(unsigned char key);
99 
100 
101 #else /* CONFIG_PERF_COUNTERS */
102 
103 #define perfc_value(x)    (0)
104 #define perfc_valuea(x,y) (0)
105 #define perfc_set(x,v)    ((void)0)
106 #define perfc_seta(x,y,v) ((void)0)
107 #define perfc_incr(x)     ((void)0)
108 #define perfc_decr(x)     ((void)0)
109 #define perfc_incra(x,y)  ((void)0)
110 #define perfc_decra(x,y)  ((void)0)
111 #define perfc_add(x,y)    ((void)0)
112 #define perfc_adda(x,y,z) ((void)0)
113 #define perfc_incr_histo(x,y,z) ((void)0)
114 
115 #endif /* CONFIG_PERF_COUNTERS */
116 
117 #endif /* __XEN_PERFC_H__ */
118