.. | .. |
---|
1912 | 1912 | profile_perf_event_cnt = 0; |
---|
1913 | 1913 | } |
---|
1914 | 1914 | |
---|
| 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 | + |
---|
1915 | 1943 | static int profile_open_perf_events(struct profiler_bpf *obj) |
---|
1916 | 1944 | { |
---|
1917 | 1945 | unsigned int cpu, m; |
---|
1918 | | - int map_fd, pmu_fd; |
---|
| 1946 | + int map_fd; |
---|
1919 | 1947 | |
---|
1920 | 1948 | profile_perf_events = calloc( |
---|
1921 | 1949 | sizeof(int), obj->rodata->num_cpu * obj->rodata->num_metric); |
---|
.. | .. |
---|
1934 | 1962 | if (!metrics[m].selected) |
---|
1935 | 1963 | continue; |
---|
1936 | 1964 | 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)) { |
---|
1943 | 1966 | p_err("failed to create event %s on cpu %d", |
---|
1944 | 1967 | metrics[m].name, cpu); |
---|
1945 | 1968 | return -1; |
---|
1946 | 1969 | } |
---|
1947 | | - profile_perf_events[profile_perf_event_cnt++] = pmu_fd; |
---|
1948 | 1970 | } |
---|
1949 | 1971 | } |
---|
1950 | 1972 | return 0; |
---|