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. */
21 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <linux/perf_event.h>
26 #include <asm/unistd.h>
28 #include "dynamic-string.h"
29 #include "openvswitch/vlog.h"
30 #include "perf-counter.h"
34 VLOG_DEFINE_THIS_MODULE(perf_counter);
36 static struct shash perf_counters;
40 perf_counter_read(uint64_t *counter)
42 int size = sizeof *counter;
44 if (fd__ <= 0 || read(fd__, counter, size) < size) {
52 perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
53 int cpu, int group_fd, unsigned long flags)
57 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
62 /* Set up perf event counters to read user space instruction counters
63 * only for this process, on all cpus. */
65 perf_event_setup(void)
67 struct perf_event_attr pe;
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;
74 pe.exclude_kernel = 1;
77 fd__ = perf_event_open(&pe, 0, -1, -1, 0);
79 VLOG_INFO("Peformance counter is not available on this platform.");
81 ioctl(fd__, PERF_EVENT_IOC_RESET, 0);
82 ioctl(fd__, PERF_EVENT_IOC_ENABLE, 0);
87 perf_counter_init(struct perf_counter *counter)
90 shash_add_assert(&perf_counters, counter->name, counter);
94 perf_counter_accumulate(struct perf_counter *counter, uint64_t start_count)
99 perf_counter_init(counter);
103 perf_counter_read(&end_count);
104 counter->total_count += end_count - start_count;
108 perf_counter_to_ds(struct ds *ds, struct perf_counter *pfc)
113 ratio = (double)pfc->total_count / (double)pfc->n_events;
118 ds_put_format(ds, "%-40s%12"PRIu64"%12"PRIu64"%12.1f\n",
119 pfc->name, pfc->n_events, pfc->total_count, ratio);
123 perf_counters_to_ds(struct ds *ds)
126 const struct shash_node **sorted;
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";
137 ds_put_format(ds, "%s\n", err_str);
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);
150 * Caller is responsible for free memory.
153 perf_counters_to_string()
158 perf_counters_to_ds(&ds);
159 return ds_steal_cstr(&ds);
163 perf_counters_init(void)
165 shash_init(&perf_counters);
170 perf_counters_clear(void)
172 struct shash_node *node;
174 SHASH_FOR_EACH (node, &perf_counters) {
175 struct perf_counter *perf = node->data;
178 perf->total_count = 0;
183 perf_counters_destroy()
185 struct shash_node *node, *next;
188 ioctl(fd__, PERF_EVENT_IOC_DISABLE, 0);
192 SHASH_FOR_EACH_SAFE (node, next, &perf_counters) {
193 shash_delete(&perf_counters, node);
196 shash_destroy(&perf_counters);