hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/tools/bpf/bpftool/prog.c
....@@ -1912,10 +1912,38 @@
19121912 profile_perf_event_cnt = 0;
19131913 }
19141914
1915
+static int profile_open_perf_event(int mid, int cpu, int map_fd)
1916
+{
1917
+ int pmu_fd;
1918
+
1919
+ pmu_fd = syscall(__NR_perf_event_open, &metrics[mid].attr,
1920
+ -1 /*pid*/, cpu, -1 /*group_fd*/, 0);
1921
+ if (pmu_fd < 0) {
1922
+ if (errno == ENODEV) {
1923
+ p_info("cpu %d may be offline, skip %s profiling.",
1924
+ cpu, metrics[mid].name);
1925
+ profile_perf_event_cnt++;
1926
+ return 0;
1927
+ }
1928
+ return -1;
1929
+ }
1930
+
1931
+ if (bpf_map_update_elem(map_fd,
1932
+ &profile_perf_event_cnt,
1933
+ &pmu_fd, BPF_ANY) ||
1934
+ ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) {
1935
+ close(pmu_fd);
1936
+ return -1;
1937
+ }
1938
+
1939
+ profile_perf_events[profile_perf_event_cnt++] = pmu_fd;
1940
+ return 0;
1941
+}
1942
+
19151943 static int profile_open_perf_events(struct profiler_bpf *obj)
19161944 {
19171945 unsigned int cpu, m;
1918
- int map_fd, pmu_fd;
1946
+ int map_fd;
19191947
19201948 profile_perf_events = calloc(
19211949 sizeof(int), obj->rodata->num_cpu * obj->rodata->num_metric);
....@@ -1934,17 +1962,11 @@
19341962 if (!metrics[m].selected)
19351963 continue;
19361964 for (cpu = 0; cpu < obj->rodata->num_cpu; cpu++) {
1937
- pmu_fd = syscall(__NR_perf_event_open, &metrics[m].attr,
1938
- -1/*pid*/, cpu, -1/*group_fd*/, 0);
1939
- if (pmu_fd < 0 ||
1940
- bpf_map_update_elem(map_fd, &profile_perf_event_cnt,
1941
- &pmu_fd, BPF_ANY) ||
1942
- ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) {
1965
+ if (profile_open_perf_event(m, cpu, map_fd)) {
19431966 p_err("failed to create event %s on cpu %d",
19441967 metrics[m].name, cpu);
19451968 return -1;
19461969 }
1947
- profile_perf_events[profile_perf_event_cnt++] = pmu_fd;
19481970 }
19491971 }
19501972 return 0;