2 * Copyright (c) 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* This implementation only applies to the Linux platform. */
20 #if defined(__linux__) && defined(HAVE_LINUX_PERF_EVENT_H)
23 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include <linux/perf_event.h>
28 #include <asm/unistd.h>
29 #include "dynamic-string.h"
30 #include "perf-counter.h"
34 static struct shash perf_counters;
38 perf_counter_read(uint64_t *counter)
40 int size = sizeof *counter;
42 if (fd__ <= 0 || read(fd__, counter, size) < size) {
50 perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
51 int cpu, int group_fd, unsigned long flags)
55 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
60 /* Set up perf event counters to read user space instruction counters
61 * only for this process, on all cpus. */
63 perf_event_setup(void)
65 struct perf_event_attr pe;
67 memset(&pe, 0, sizeof(struct perf_event_attr));
68 pe.type = PERF_TYPE_HARDWARE;
69 pe.size = sizeof(struct perf_event_attr);
70 pe.config = PERF_COUNT_HW_INSTRUCTIONS;
72 pe.exclude_kernel = 1;
75 fd__ = perf_event_open(&pe, 0, -1, -1, 0);
77 ioctl(fd__, PERF_EVENT_IOC_RESET, 0);
78 ioctl(fd__, PERF_EVENT_IOC_ENABLE, 0);
83 perf_counter_init(struct perf_counter *counter)
86 shash_add_assert(&perf_counters, counter->name, counter);
90 perf_counter_accumulate(struct perf_counter *counter, uint64_t start_count)
95 perf_counter_init(counter);
99 perf_counter_read(&end_count);
100 counter->total_count += end_count - start_count;
104 perf_counter_to_ds(struct ds *ds, struct perf_counter *pfc)
109 ratio = (double)pfc->total_count / (double)pfc->n_events;
114 ds_put_format(ds, "%-40s%12"PRIu64"%12"PRIu64"%12.1f\n",
115 pfc->name, pfc->n_events, pfc->total_count, ratio);
119 perf_counters_to_ds(struct ds *ds)
122 const struct shash_node **sorted;
127 err_str = "performance counter is not supported on this platfrom";
128 } else if (!shash_count(&perf_counters)) {
129 err_str = "performance counter has never been hit";
133 ds_put_format(ds, "%s\n", err_str);
137 /* Display counters in alphabetical order. */
138 sorted = shash_sort(&perf_counters);
139 for (i = 0; i < shash_count(&perf_counters); i++) {
140 perf_counter_to_ds(ds, sorted[i]->data);
146 * Caller is responsible for free memory.
149 perf_counters_to_string()
154 perf_counters_to_ds(&ds);
155 return ds_steal_cstr(&ds);
159 perf_counters_init(void)
161 shash_init(&perf_counters);
166 perf_counters_clear(void)
168 struct shash_node *node;
170 SHASH_FOR_EACH (node, &perf_counters) {
171 struct perf_counter *perf = node->data;
174 perf->total_count = 0;
179 perf_counters_destroy()
181 struct shash_node *node, *next;
184 ioctl(fd__, PERF_EVENT_IOC_DISABLE, 0);
188 SHASH_FOR_EACH_SAFE (node, next, &perf_counters) {
189 shash_delete(&perf_counters, node);
192 shash_destroy(&perf_counters);