]> git.proxmox.com Git - mirror_ovs.git/blob - lib/perf-counter.c
autoconf: check for linux/perf_event.h
[mirror_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
19 #include <config.h>
20 #if defined(__linux__) && defined(HAVE_PERF_EVENT_H)
21
22 #include <stddef.h>
23 #include <sys/types.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/ioctl.h>
27 #include <linux/perf_event.h>
28 #include <asm/unistd.h>
29 #include "dynamic-string.h"
30 #include "openvswitch/vlog.h"
31 #include "perf-counter.h"
32 #include "shash.h"
33 #include "util.h"
34
35 VLOG_DEFINE_THIS_MODULE(perf_counter);
36
37 static struct shash perf_counters;
38 static int fd__ = 0;
39
40 uint64_t
41 perf_counter_read(uint64_t *counter)
42 {
43 int size = sizeof *counter;
44
45 if (fd__ <= 0 || read(fd__, counter, size) < size) {
46 *counter = 0;
47 }
48
49 return *counter;
50 }
51
52 static long
53 perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
54 int cpu, int group_fd, unsigned long flags)
55 {
56 int ret;
57
58 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
59 group_fd, flags);
60 return ret;
61 }
62
63 /* Set up perf event counters to read user space instruction counters
64 * only for this process, on all cpus. */
65 static void
66 perf_event_setup(void)
67 {
68 struct perf_event_attr pe;
69
70 memset(&pe, 0, sizeof(struct perf_event_attr));
71 pe.type = PERF_TYPE_HARDWARE;
72 pe.size = sizeof(struct perf_event_attr);
73 pe.config = PERF_COUNT_HW_INSTRUCTIONS;
74 pe.disabled = 1;
75 pe.exclude_kernel = 1;
76 pe.exclude_hv = 1;
77
78 fd__ = perf_event_open(&pe, 0, -1, -1, 0);
79 if (fd__ == -1) {
80 VLOG_INFO("Peformance counter is not available on this platform.");
81 } else {
82 ioctl(fd__, PERF_EVENT_IOC_RESET, 0);
83 ioctl(fd__, PERF_EVENT_IOC_ENABLE, 0);
84 }
85 }
86
87 static void
88 perf_counter_init(struct perf_counter *counter)
89 {
90 counter->once = true;
91 shash_add_assert(&perf_counters, counter->name, counter);
92 }
93
94 void
95 perf_counter_accumulate(struct perf_counter *counter, uint64_t start_count)
96 {
97 uint64_t end_count;
98
99 if (!counter->once) {
100 perf_counter_init(counter);
101 }
102
103 counter->n_events++;
104 perf_counter_read(&end_count);
105 counter->total_count += end_count - start_count;
106 }
107
108 static void
109 perf_counter_to_ds(struct ds *ds, struct perf_counter *pfc)
110 {
111 double ratio;
112
113 if (pfc->n_events) {
114 ratio = (double)pfc->total_count / (double)pfc->n_events;
115 } else {
116 ratio = 0.0;
117 }
118
119 ds_put_format(ds, "%-40s%12"PRIu64"%12"PRIu64"%12.1f\n",
120 pfc->name, pfc->n_events, pfc->total_count, ratio);
121 }
122
123 static void
124 perf_counters_to_ds(struct ds *ds)
125 {
126 const char *err_str;
127 const struct shash_node **sorted;
128 int i;
129
130 err_str = NULL;
131 if (fd__ == -1) {
132 err_str = "performance counter is not supported on this platfrom";
133 } else if (!shash_count(&perf_counters)) {
134 err_str = "performance counter has never been hit";
135 }
136
137 if (err_str) {
138 ds_put_format(ds, "%s\n", err_str);
139 return;
140 }
141
142 /* Display counters in alphabetical order. */
143 sorted = shash_sort(&perf_counters);
144 for (i = 0; i < shash_count(&perf_counters); i++) {
145 perf_counter_to_ds(ds, sorted[i]->data);
146 }
147 free(sorted);
148 }
149
150 /*
151 * Caller is responsible for free memory.
152 */
153 char *
154 perf_counters_to_string()
155 {
156 struct ds ds;
157
158 ds_init(&ds);
159 perf_counters_to_ds(&ds);
160 return ds_steal_cstr(&ds);
161 }
162
163 void
164 perf_counters_init(void)
165 {
166 shash_init(&perf_counters);
167 perf_event_setup();
168 }
169
170 void
171 perf_counters_clear(void)
172 {
173 struct shash_node *node;
174
175 SHASH_FOR_EACH (node, &perf_counters) {
176 struct perf_counter *perf = node->data;
177
178 perf->n_events = 0;
179 perf->total_count = 0;
180 }
181 }
182
183 void
184 perf_counters_destroy()
185 {
186 struct shash_node *node, *next;
187
188 if (fd__ != -1) {
189 ioctl(fd__, PERF_EVENT_IOC_DISABLE, 0);
190 close(fd__);
191 }
192
193 SHASH_FOR_EACH_SAFE (node, next, &perf_counters) {
194 shash_delete(&perf_counters, node);
195 }
196
197 shash_destroy(&perf_counters);
198 }
199 #endif