2015-03-21 00:00:48 -07:00
|
|
|
/*
|
sparse: Configure target operating system and fix fallout.
cgcc, the "sparse" wrapper that OVS uses, can be told the host architecture
or the host OS or both. Until now, OVS has told it the host architecture
because it is fairly common that it doesn't guess it automatically. Until
now, OS has not told it the host OS, assuming that it would get it right.
However, it doesn't--if you tell it the host OS or the host architecture,
it doesn't really have a default for the other. This means that on Linux
(presumably the only OS where sparse works properly for OVS), it was not
defining __linux__, which caused some weird behavior.
This commit adds a flag to the cgcc invocation to make it define __linux__
on Linux, and it fixes some errors that this would otherwise cause.
Acked-by: Ilya Maximets <i.maximets@samsung.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
2019-04-23 16:42:32 -07:00
|
|
|
* Copyright (c) 2015, 2016, 2019 Nicira, Inc.
|
2015-03-21 00:00:48 -07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This implementation only applies to the Linux platform. */
|
2015-04-16 12:52:09 -07:00
|
|
|
|
|
|
|
#include <config.h>
|
sparse: Configure target operating system and fix fallout.
cgcc, the "sparse" wrapper that OVS uses, can be told the host architecture
or the host OS or both. Until now, OVS has told it the host architecture
because it is fairly common that it doesn't guess it automatically. Until
now, OS has not told it the host OS, assuming that it would get it right.
However, it doesn't--if you tell it the host OS or the host architecture,
it doesn't really have a default for the other. This means that on Linux
(presumably the only OS where sparse works properly for OVS), it was not
defining __linux__, which caused some weird behavior.
This commit adds a flag to the cgcc invocation to make it define __linux__
on Linux, and it fixes some errors that this would otherwise cause.
Acked-by: Ilya Maximets <i.maximets@samsung.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
2019-04-23 16:42:32 -07:00
|
|
|
#if defined(__linux__) && defined(HAVE_LINUX_PERF_EVENT_H) && !__CHECKER__
|
2015-03-21 00:00:48 -07:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/perf_event.h>
|
|
|
|
#include <asm/unistd.h>
|
2016-03-03 10:20:46 -08:00
|
|
|
#include "openvswitch/dynamic-string.h"
|
2015-03-21 00:00:48 -07:00
|
|
|
#include "perf-counter.h"
|
2016-07-12 16:37:34 -05:00
|
|
|
#include "openvswitch/shash.h"
|
2015-03-21 00:00:48 -07:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-07-15 13:22:00 -07:00
|
|
|
static struct shash perf_counters = SHASH_INITIALIZER(&perf_counters);
|
2015-03-21 00:00:48 -07:00
|
|
|
static int fd__ = 0;
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
perf_counter_read(uint64_t *counter)
|
|
|
|
{
|
2015-04-14 17:25:32 -07:00
|
|
|
int size = sizeof *counter;
|
2015-04-14 14:22:08 -07:00
|
|
|
|
|
|
|
if (fd__ <= 0 || read(fd__, counter, size) < size) {
|
2015-03-21 00:00:48 -07:00
|
|
|
*counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
|
|
|
|
int cpu, int group_fd, unsigned long flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
|
|
|
|
group_fd, flags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up perf event counters to read user space instruction counters
|
|
|
|
* only for this process, on all cpus. */
|
|
|
|
static void
|
|
|
|
perf_event_setup(void)
|
|
|
|
{
|
|
|
|
struct perf_event_attr pe;
|
|
|
|
|
|
|
|
memset(&pe, 0, sizeof(struct perf_event_attr));
|
|
|
|
pe.type = PERF_TYPE_HARDWARE;
|
|
|
|
pe.size = sizeof(struct perf_event_attr);
|
|
|
|
pe.config = PERF_COUNT_HW_INSTRUCTIONS;
|
|
|
|
pe.disabled = 1;
|
|
|
|
pe.exclude_kernel = 1;
|
|
|
|
pe.exclude_hv = 1;
|
2025-05-22 10:10:27 +08:00
|
|
|
pe.exclude_guest = 1;
|
2015-03-21 00:00:48 -07:00
|
|
|
|
|
|
|
fd__ = perf_event_open(&pe, 0, -1, -1, 0);
|
2015-04-16 15:19:56 -07:00
|
|
|
if (fd__ > 0) {
|
2015-03-21 00:00:48 -07:00
|
|
|
ioctl(fd__, PERF_EVENT_IOC_RESET, 0);
|
|
|
|
ioctl(fd__, PERF_EVENT_IOC_ENABLE, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
perf_counter_init(struct perf_counter *counter)
|
|
|
|
{
|
|
|
|
counter->once = true;
|
|
|
|
shash_add_assert(&perf_counters, counter->name, counter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
perf_counter_accumulate(struct perf_counter *counter, uint64_t start_count)
|
|
|
|
{
|
|
|
|
uint64_t end_count;
|
|
|
|
|
|
|
|
if (!counter->once) {
|
|
|
|
perf_counter_init(counter);
|
|
|
|
}
|
|
|
|
|
|
|
|
counter->n_events++;
|
|
|
|
perf_counter_read(&end_count);
|
|
|
|
counter->total_count += end_count - start_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
perf_counter_to_ds(struct ds *ds, struct perf_counter *pfc)
|
|
|
|
{
|
|
|
|
double ratio;
|
|
|
|
|
|
|
|
if (pfc->n_events) {
|
|
|
|
ratio = (double)pfc->total_count / (double)pfc->n_events;
|
|
|
|
} else {
|
|
|
|
ratio = 0.0;
|
|
|
|
}
|
|
|
|
|
2020-11-18 22:05:59 +01:00
|
|
|
ds_put_format(ds, "%-40s %12"PRIu64" %12"PRIu64" %12.1f\n",
|
2015-04-14 13:47:19 -07:00
|
|
|
pfc->name, pfc->n_events, pfc->total_count, ratio);
|
2015-03-21 00:00:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
perf_counters_to_ds(struct ds *ds)
|
|
|
|
{
|
|
|
|
const char *err_str;
|
|
|
|
const struct shash_node **sorted;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
err_str = NULL;
|
|
|
|
if (fd__ == -1) {
|
2022-07-08 08:53:08 +02:00
|
|
|
err_str = "performance counter is not supported on this platform";
|
2015-03-21 00:00:48 -07:00
|
|
|
} else if (!shash_count(&perf_counters)) {
|
|
|
|
err_str = "performance counter has never been hit";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err_str) {
|
|
|
|
ds_put_format(ds, "%s\n", err_str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Display counters in alphabetical order. */
|
|
|
|
sorted = shash_sort(&perf_counters);
|
|
|
|
for (i = 0; i < shash_count(&perf_counters); i++) {
|
|
|
|
perf_counter_to_ds(ds, sorted[i]->data);
|
|
|
|
}
|
|
|
|
free(sorted);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Caller is responsible for free memory.
|
|
|
|
*/
|
|
|
|
char *
|
2016-12-05 14:29:35 -08:00
|
|
|
perf_counters_to_string(void)
|
2015-03-21 00:00:48 -07:00
|
|
|
{
|
|
|
|
struct ds ds;
|
|
|
|
|
|
|
|
ds_init(&ds);
|
|
|
|
perf_counters_to_ds(&ds);
|
|
|
|
return ds_steal_cstr(&ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
perf_counters_init(void)
|
|
|
|
{
|
|
|
|
shash_init(&perf_counters);
|
|
|
|
perf_event_setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
perf_counters_clear(void)
|
|
|
|
{
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
|
|
SHASH_FOR_EACH (node, &perf_counters) {
|
|
|
|
struct perf_counter *perf = node->data;
|
|
|
|
|
|
|
|
perf->n_events = 0;
|
|
|
|
perf->total_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-12-05 14:29:35 -08:00
|
|
|
perf_counters_destroy(void)
|
2015-03-21 00:00:48 -07:00
|
|
|
{
|
2022-03-23 12:56:17 +01:00
|
|
|
struct shash_node *node;
|
2015-03-21 00:00:48 -07:00
|
|
|
|
|
|
|
if (fd__ != -1) {
|
|
|
|
ioctl(fd__, PERF_EVENT_IOC_DISABLE, 0);
|
|
|
|
close(fd__);
|
|
|
|
}
|
|
|
|
|
2022-03-23 12:56:17 +01:00
|
|
|
SHASH_FOR_EACH_SAFE (node, &perf_counters) {
|
2015-03-21 00:00:48 -07:00
|
|
|
shash_delete(&perf_counters, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
shash_destroy(&perf_counters);
|
|
|
|
}
|
|
|
|
#endif
|