1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2022 Cisco Systems, Inc.
5 #ifndef included_perfmon_perfmon_h
6 #define included_perfmon_perfmon_h
8 #include <vppinfra/cpu.h>
10 #include <sys/ioctl.h>
11 #include <linux/perf_event.h>
14 #define CLIB_PERFMON_MAX_EVENTS 7
19 u64 config[CLIB_PERFMON_MAX_EVENTS];
22 format_function_t *format_fn;
23 char **column_headers;
24 } clib_perfmon_bundle_t;
30 u64 data[CLIB_PERFMON_MAX_EVENTS];
34 } clib_perfmon_capture_t;
40 } clib_perfmon_capture_group_t;
46 clib_perfmon_bundle_t *bundle;
50 clib_perfmon_capture_t *captures;
51 clib_perfmon_capture_group_t *capture_groups;
55 typedef struct clib_perfmon_bundle_reg
57 clib_perfmon_bundle_t *bundle;
58 struct clib_perfmon_bundle_reg *next;
59 } clib_perfmon_bundle_reg_t;
63 clib_perfmon_bundle_reg_t *bundle_regs;
64 } clib_perfmon_main_t;
66 extern clib_perfmon_main_t clib_perfmon_main;
68 static_always_inline void
69 clib_perfmon_ioctl (int fd, u32 req)
72 asm volatile("syscall"
74 : "D"(fd), "S"(req), "a"(__NR_ioctl), "d"(PERF_IOC_FLAG_GROUP)
75 : "rcx", "r11" /* registers modified by kernel */);
77 ioctl (fd, req, PERF_IOC_FLAG_GROUP);
81 clib_error_t *clib_perfmon_init_by_bundle_name (clib_perfmon_ctx_t *ctx,
83 void clib_perfmon_free (clib_perfmon_ctx_t *ctx);
84 void clib_perfmon_warmup (clib_perfmon_ctx_t *ctx);
85 void clib_perfmon_clear (clib_perfmon_ctx_t *ctx);
86 u64 *clib_perfmon_capture (clib_perfmon_ctx_t *ctx, u32 n_ops, char *fmt, ...);
87 void clib_perfmon_capture_group (clib_perfmon_ctx_t *ctx, char *fmt, ...);
88 format_function_t format_perfmon_bundle;
90 static_always_inline void
91 clib_perfmon_reset (clib_perfmon_ctx_t *ctx)
93 clib_perfmon_ioctl (ctx->group_fd, PERF_EVENT_IOC_RESET);
95 static_always_inline void
96 clib_perfmon_enable (clib_perfmon_ctx_t *ctx)
98 clib_perfmon_ioctl (ctx->group_fd, PERF_EVENT_IOC_ENABLE);
100 static_always_inline void
101 clib_perfmon_disable (clib_perfmon_ctx_t *ctx)
103 clib_perfmon_ioctl (ctx->group_fd, PERF_EVENT_IOC_DISABLE);
106 #define CLIB_PERFMON_BUNDLE(x) \
107 static clib_perfmon_bundle_reg_t clib_perfmon_bundle_reg_##x; \
108 static clib_perfmon_bundle_t clib_perfmon_bundle_##x; \
109 static void __clib_constructor clib_perfmon_bundle_reg_fn_##x (void) \
111 clib_perfmon_bundle_reg_##x.bundle = &clib_perfmon_bundle_##x; \
112 clib_perfmon_bundle_reg_##x.next = clib_perfmon_main.bundle_regs; \
113 clib_perfmon_main.bundle_regs = &clib_perfmon_bundle_reg_##x; \
115 static clib_perfmon_bundle_t clib_perfmon_bundle_##x