]>
git.proxmox.com Git - ovs.git/blob - lib/perf-counter.h
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 #ifndef __PERF_COUNTER_H
18 #define __PERF_COUNTER_H 1
23 * It is sometimes desirable to gain performance insights of a program
24 * by using hardware counters. Recent Linux kernels started to support
25 * a set of portable API for configuring and access those counter across
28 * APIs provided by perf-counter.h provides a set of APIs that are
29 * semi-integrated into OVS user spaces. The infrastructure that initializes,
30 * cleanup, display and clear them at run time is provided. However the
31 * sample points are not. A programmer needs insert sample points when needed.
33 * Since there is no pre configured sample points, there is no run time
34 * over head for the released product.
38 * - Hard coded to sample CPU cycle count in user space only.
39 * - Only one counter is sampled.
40 * - Useful macros are only provided for function profiling.
41 * - show and clear command applies to all counters, there is no way
42 * to select a sub-set of counter.
44 * Those are not fundamental limits, but only limited by current
50 * Adding performance counter is easy. Simply use the following macro to
51 * wrap around the expression you are interested in measuring.
55 * The 'expr' is a set of C expressions you are interested in measuring.
56 * 'name' is the counter name.
58 * For example, if we are interested in performance of perf_func():
68 * PERF("perf_func", rt = perf_func());
74 * This will maintain the number of times 'perf_func()' is called, total
75 * number of instructions '<implementation>' plus function call overhead
80 #if defined(__linux__) && defined(HAVE_LINUX_PERF_EVENT_H)
88 #define PERF_COUNTER_ONCE_INITIALIZER(name) \
96 void perf_counters_init(void);
97 void perf_counters_destroy(void);
98 void perf_counters_clear(void);
100 uint64_t perf_counter_read(uint64_t *counter
);
101 void perf_counter_accumulate(struct perf_counter
*counter
,
102 uint64_t start_count
);
103 char *perf_counters_to_string(void);
105 /* User access macros. */
106 #define PERF(name, expr) \
108 static struct perf_counter c = PERF_COUNTER_ONCE_INITIALIZER(name);\
109 uint64_t start_count = perf_counter_read(&start_count); \
113 perf_counter_accumulate(&c, start_count); \
116 #define PERF(name, expr) { expr; }
118 static inline void perf_counters_init(void) {}
119 static inline void perf_counters_destroy(void) {}
120 static inline void perf_counters_clear(void) {}
122 perf_counters_to_string(void)
124 return xstrdup("Not Supported on this platform. Only available on Linux (version >= 2.6.32)");