hc
2024-05-10 cde9070d9970eef1f7ec2360586c802a16230ad8
kernel/samples/bpf/tracex3_user.c
....@@ -1,8 +1,5 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
2
- *
3
- * This program is free software; you can redistribute it and/or
4
- * modify it under the terms of version 2 of the GNU General Public
5
- * License as published by the Free Software Foundation.
63 */
74 #include <stdio.h>
85 #include <stdlib.h>
....@@ -10,14 +7,11 @@
107 #include <unistd.h>
118 #include <stdbool.h>
129 #include <string.h>
13
-#include <linux/bpf.h>
1410 #include <sys/resource.h>
1511
1612 #include <bpf/bpf.h>
17
-#include "bpf_load.h"
13
+#include <bpf/libbpf.h>
1814 #include "bpf_util.h"
19
-
20
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
2115
2216 #define SLOTS 100
2317
....@@ -113,21 +107,12 @@
113107
114108 int main(int ac, char **argv)
115109 {
116
- struct rlimit r = {1024*1024, RLIM_INFINITY};
110
+ struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
111
+ struct bpf_link *links[2];
112
+ struct bpf_program *prog;
113
+ struct bpf_object *obj;
117114 char filename[256];
118
- int i;
119
-
120
- snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
121
-
122
- if (setrlimit(RLIMIT_MEMLOCK, &r)) {
123
- perror("setrlimit(RLIMIT_MEMLOCK)");
124
- return 1;
125
- }
126
-
127
- if (load_bpf_file(filename)) {
128
- printf("%s", bpf_log_buf);
129
- return 1;
130
- }
115
+ int map_fd, i, j = 0;
131116
132117 for (i = 1; i < ac; i++) {
133118 if (strcmp(argv[i], "-a") == 0) {
....@@ -140,6 +125,40 @@
140125 " -t text only\n");
141126 return 1;
142127 }
128
+ }
129
+
130
+ if (setrlimit(RLIMIT_MEMLOCK, &r)) {
131
+ perror("setrlimit(RLIMIT_MEMLOCK)");
132
+ return 1;
133
+ }
134
+
135
+ snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
136
+ obj = bpf_object__open_file(filename, NULL);
137
+ if (libbpf_get_error(obj)) {
138
+ fprintf(stderr, "ERROR: opening BPF object file failed\n");
139
+ return 0;
140
+ }
141
+
142
+ /* load BPF program */
143
+ if (bpf_object__load(obj)) {
144
+ fprintf(stderr, "ERROR: loading BPF object file failed\n");
145
+ goto cleanup;
146
+ }
147
+
148
+ map_fd = bpf_object__find_map_fd_by_name(obj, "lat_map");
149
+ if (map_fd < 0) {
150
+ fprintf(stderr, "ERROR: finding a map in obj file failed\n");
151
+ goto cleanup;
152
+ }
153
+
154
+ bpf_object__for_each_program(prog, obj) {
155
+ links[j] = bpf_program__attach(prog);
156
+ if (libbpf_get_error(links[j])) {
157
+ fprintf(stderr, "ERROR: bpf_program__attach failed\n");
158
+ links[j] = NULL;
159
+ goto cleanup;
160
+ }
161
+ j++;
143162 }
144163
145164 printf(" heatmap of IO latency\n");
....@@ -158,9 +177,14 @@
158177 for (i = 0; ; i++) {
159178 if (i % 20 == 0)
160179 print_banner();
161
- print_hist(map_fd[1]);
180
+ print_hist(map_fd);
162181 sleep(2);
163182 }
164183
184
+cleanup:
185
+ for (j--; j >= 0; j--)
186
+ bpf_link__destroy(links[j]);
187
+
188
+ bpf_object__close(obj);
165189 return 0;
166190 }