perf-counter: use 'int' instead of size_t
[cascardo/ovs.git] / lib / perf-counter.c
1 /*
2  * Copyright (c) 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* This implementation only applies to the Linux platform.  */
18 #ifdef __linux__
19
20 #include <stddef.h>
21 #include <sys/types.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/ioctl.h>
25 #include <linux/perf_event.h>
26 #include <asm/unistd.h>
27 #include <config.h>
28 #include "dynamic-string.h"
29 #include "openvswitch/vlog.h"
30 #include "perf-counter.h"
31 #include "shash.h"
32 #include "util.h"
33
34 VLOG_DEFINE_THIS_MODULE(perf_counter);
35
36 static struct shash perf_counters;
37 static int fd__ = 0;
38
39 uint64_t
40 perf_counter_read(uint64_t *counter)
41 {
42     int size = sizeof *counter;
43
44     if (fd__ <= 0 || read(fd__, counter, size) < size) {
45         *counter = 0;
46     }
47
48     return *counter;
49 }
50
51 static long
52 perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
53                 int cpu, int group_fd, unsigned long flags)
54 {
55     int ret;
56
57     ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
58                   group_fd, flags);
59     return ret;
60 }
61
62 /* Set up perf event counters to read user space instruction counters
63  * only for this process, on all cpus.   */
64 static void
65 perf_event_setup(void)
66 {
67     struct perf_event_attr pe;
68
69     memset(&pe, 0, sizeof(struct perf_event_attr));
70     pe.type = PERF_TYPE_HARDWARE;
71     pe.size = sizeof(struct perf_event_attr);
72     pe.config = PERF_COUNT_HW_INSTRUCTIONS;
73     pe.disabled = 1;
74     pe.exclude_kernel = 1;
75     pe.exclude_hv = 1;
76
77     fd__ = perf_event_open(&pe, 0, -1, -1, 0);
78     if (fd__ == -1) {
79         VLOG_INFO("Peformance counter is not available on this platform.");
80     } else {
81         ioctl(fd__, PERF_EVENT_IOC_RESET, 0);
82         ioctl(fd__, PERF_EVENT_IOC_ENABLE, 0);
83     }
84 }
85
86 static void
87 perf_counter_init(struct perf_counter *counter)
88 {
89     counter->once = true;
90     shash_add_assert(&perf_counters, counter->name, counter);
91 }
92
93 void
94 perf_counter_accumulate(struct perf_counter *counter, uint64_t start_count)
95 {
96     uint64_t end_count;
97
98     if (!counter->once) {
99         perf_counter_init(counter);
100     }
101
102     counter->n_events++;
103     perf_counter_read(&end_count);
104     counter->total_count += end_count - start_count;
105 }
106
107 static void
108 perf_counter_to_ds(struct ds *ds, struct perf_counter *pfc)
109 {
110     double ratio;
111
112     if (pfc->n_events) {
113         ratio = (double)pfc->total_count / (double)pfc->n_events;
114     } else {
115         ratio = 0.0;
116     }
117
118     ds_put_format(ds, "%-40s%12"PRIu64"%12"PRIu64"%12.1f\n",
119                   pfc->name, pfc->n_events, pfc->total_count, ratio);
120 }
121
122 static void
123 perf_counters_to_ds(struct ds *ds)
124 {
125     const char *err_str;
126     const struct shash_node **sorted;
127     int i;
128
129     err_str = NULL;
130     if (fd__ == -1) {
131         err_str = "performance counter is not supported on this platfrom";
132     } else if (!shash_count(&perf_counters)) {
133         err_str = "performance counter has never been hit";
134     }
135
136     if (err_str) {
137         ds_put_format(ds, "%s\n", err_str);
138         return;
139     }
140
141     /* Display counters in alphabetical order.  */
142     sorted = shash_sort(&perf_counters);
143     for (i = 0; i < shash_count(&perf_counters); i++) {
144         perf_counter_to_ds(ds, sorted[i]->data);
145     }
146     free(sorted);
147 }
148
149 /*
150  * Caller is responsible for free memory.
151  */
152 char *
153 perf_counters_to_string()
154 {
155     struct ds ds;
156
157     ds_init(&ds);
158     perf_counters_to_ds(&ds);
159     return ds_steal_cstr(&ds);
160 }
161
162 void
163 perf_counters_init(void)
164 {
165     shash_init(&perf_counters);
166     perf_event_setup();
167 }
168
169 void
170 perf_counters_clear(void)
171 {
172     struct shash_node *node;
173
174     SHASH_FOR_EACH (node, &perf_counters) {
175         struct perf_counter *perf = node->data;
176
177         perf->n_events = 0;
178         perf->total_count = 0;
179     }
180 }
181
182 void
183 perf_counters_destroy()
184 {
185     struct shash_node *node, *next;
186
187     if (fd__ != -1) {
188         ioctl(fd__, PERF_EVENT_IOC_DISABLE, 0);
189         close(fd__);
190     }
191
192     SHASH_FOR_EACH_SAFE (node, next, &perf_counters) {
193         shash_delete(&perf_counters, node);
194     }
195
196     shash_destroy(&perf_counters);
197 }
198 #endif