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_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 "openvswitch/vlog.h"
31 #include "perf-counter.h"
35 VLOG_DEFINE_THIS_MODULE(perf_counter
);
37 static struct shash perf_counters
;
41 perf_counter_read(uint64_t *counter
)
43 int size
= sizeof *counter
;
45 if (fd__
<= 0 || read(fd__
, counter
, size
) < size
) {
53 perf_event_open(struct perf_event_attr
*hw_event
, pid_t pid
,
54 int cpu
, int group_fd
, unsigned long flags
)
58 ret
= syscall(__NR_perf_event_open
, hw_event
, pid
, cpu
,
63 /* Set up perf event counters to read user space instruction counters
64 * only for this process, on all cpus. */
66 perf_event_setup(void)
68 struct perf_event_attr pe
;
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
;
75 pe
.exclude_kernel
= 1;
78 fd__
= perf_event_open(&pe
, 0, -1, -1, 0);
80 VLOG_INFO("Peformance counter is not available on this platform.");
82 ioctl(fd__
, PERF_EVENT_IOC_RESET
, 0);
83 ioctl(fd__
, PERF_EVENT_IOC_ENABLE
, 0);
88 perf_counter_init(struct perf_counter
*counter
)
91 shash_add_assert(&perf_counters
, counter
->name
, counter
);
95 perf_counter_accumulate(struct perf_counter
*counter
, uint64_t start_count
)
100 perf_counter_init(counter
);
104 perf_counter_read(&end_count
);
105 counter
->total_count
+= end_count
- start_count
;
109 perf_counter_to_ds(struct ds
*ds
, struct perf_counter
*pfc
)
114 ratio
= (double)pfc
->total_count
/ (double)pfc
->n_events
;
119 ds_put_format(ds
, "%-40s%12"PRIu64
"%12"PRIu64
"%12.1f\n",
120 pfc
->name
, pfc
->n_events
, pfc
->total_count
, ratio
);
124 perf_counters_to_ds(struct ds
*ds
)
127 const struct shash_node
**sorted
;
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";
138 ds_put_format(ds
, "%s\n", err_str
);
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
);
151 * Caller is responsible for free memory.
154 perf_counters_to_string()
159 perf_counters_to_ds(&ds
);
160 return ds_steal_cstr(&ds
);
164 perf_counters_init(void)
166 shash_init(&perf_counters
);
171 perf_counters_clear(void)
173 struct shash_node
*node
;
175 SHASH_FOR_EACH (node
, &perf_counters
) {
176 struct perf_counter
*perf
= node
->data
;
179 perf
->total_count
= 0;
184 perf_counters_destroy()
186 struct shash_node
*node
, *next
;
189 ioctl(fd__
, PERF_EVENT_IOC_DISABLE
, 0);
193 SHASH_FOR_EACH_SAFE (node
, next
, &perf_counters
) {
194 shash_delete(&perf_counters
, node
);
197 shash_destroy(&perf_counters
);