Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include "debug.h"
0003 #include "evlist.h"
0004 #include "evsel.h"
0005 #include "evsel_config.h"
0006 #include "parse-events.h"
0007 #include <errno.h>
0008 #include <limits.h>
0009 #include <stdlib.h>
0010 #include <api/fs/fs.h>
0011 #include <subcmd/parse-options.h>
0012 #include <perf/cpumap.h>
0013 #include "cloexec.h"
0014 #include "util/perf_api_probe.h"
0015 #include "record.h"
0016 #include "../perf-sys.h"
0017 #include "topdown.h"
0018 #include "map_symbol.h"
0019 #include "mem-events.h"
0020 
0021 /*
0022  * evsel__config_leader_sampling() uses special rules for leader sampling.
0023  * However, if the leader is an AUX area event, then assume the event to sample
0024  * is the next event.
0025  */
0026 static struct evsel *evsel__read_sampler(struct evsel *evsel, struct evlist *evlist)
0027 {
0028     struct evsel *leader = evsel__leader(evsel);
0029 
0030     if (evsel__is_aux_event(leader) || arch_topdown_sample_read(leader) ||
0031         is_mem_loads_aux_event(leader)) {
0032         evlist__for_each_entry(evlist, evsel) {
0033             if (evsel__leader(evsel) == leader && evsel != evsel__leader(evsel))
0034                 return evsel;
0035         }
0036     }
0037 
0038     return leader;
0039 }
0040 
0041 static u64 evsel__config_term_mask(struct evsel *evsel)
0042 {
0043     struct evsel_config_term *term;
0044     struct list_head *config_terms = &evsel->config_terms;
0045     u64 term_types = 0;
0046 
0047     list_for_each_entry(term, config_terms, list) {
0048         term_types |= 1 << term->type;
0049     }
0050     return term_types;
0051 }
0052 
0053 static void evsel__config_leader_sampling(struct evsel *evsel, struct evlist *evlist)
0054 {
0055     struct perf_event_attr *attr = &evsel->core.attr;
0056     struct evsel *leader = evsel__leader(evsel);
0057     struct evsel *read_sampler;
0058     u64 term_types, freq_mask;
0059 
0060     if (!leader->sample_read)
0061         return;
0062 
0063     read_sampler = evsel__read_sampler(evsel, evlist);
0064 
0065     if (evsel == read_sampler)
0066         return;
0067 
0068     term_types = evsel__config_term_mask(evsel);
0069     /*
0070      * Disable sampling for all group members except those with explicit
0071      * config terms or the leader. In the case of an AUX area event, the 2nd
0072      * event in the group is the one that 'leads' the sampling.
0073      */
0074     freq_mask = (1 << EVSEL__CONFIG_TERM_FREQ) | (1 << EVSEL__CONFIG_TERM_PERIOD);
0075     if ((term_types & freq_mask) == 0) {
0076         attr->freq           = 0;
0077         attr->sample_freq    = 0;
0078         attr->sample_period  = 0;
0079     }
0080     if ((term_types & (1 << EVSEL__CONFIG_TERM_OVERWRITE)) == 0)
0081         attr->write_backward = 0;
0082 
0083     /*
0084      * We don't get a sample for slave events, we make them when delivering
0085      * the group leader sample. Set the slave event to follow the master
0086      * sample_type to ease up reporting.
0087      * An AUX area event also has sample_type requirements, so also include
0088      * the sample type bits from the leader's sample_type to cover that
0089      * case.
0090      */
0091     attr->sample_type = read_sampler->core.attr.sample_type |
0092                 leader->core.attr.sample_type;
0093 }
0094 
0095 void evlist__config(struct evlist *evlist, struct record_opts *opts, struct callchain_param *callchain)
0096 {
0097     struct evsel *evsel;
0098     bool use_sample_identifier = false;
0099     bool use_comm_exec;
0100     bool sample_id = opts->sample_id;
0101 
0102     /*
0103      * Set the evsel leader links before we configure attributes,
0104      * since some might depend on this info.
0105      */
0106     if (opts->group)
0107         evlist__set_leader(evlist);
0108 
0109     if (perf_cpu_map__cpu(evlist->core.user_requested_cpus, 0).cpu < 0)
0110         opts->no_inherit = true;
0111 
0112     use_comm_exec = perf_can_comm_exec();
0113 
0114     evlist__for_each_entry(evlist, evsel) {
0115         evsel__config(evsel, opts, callchain);
0116         if (evsel->tracking && use_comm_exec)
0117             evsel->core.attr.comm_exec = 1;
0118     }
0119 
0120     /* Configure leader sampling here now that the sample type is known */
0121     evlist__for_each_entry(evlist, evsel)
0122         evsel__config_leader_sampling(evsel, evlist);
0123 
0124     if (opts->full_auxtrace || opts->sample_identifier) {
0125         /*
0126          * Need to be able to synthesize and parse selected events with
0127          * arbitrary sample types, which requires always being able to
0128          * match the id.
0129          */
0130         use_sample_identifier = perf_can_sample_identifier();
0131         sample_id = true;
0132     } else if (evlist->core.nr_entries > 1) {
0133         struct evsel *first = evlist__first(evlist);
0134 
0135         evlist__for_each_entry(evlist, evsel) {
0136             if (evsel->core.attr.sample_type == first->core.attr.sample_type)
0137                 continue;
0138             use_sample_identifier = perf_can_sample_identifier();
0139             break;
0140         }
0141         sample_id = true;
0142     }
0143 
0144     if (sample_id) {
0145         evlist__for_each_entry(evlist, evsel)
0146             evsel__set_sample_id(evsel, use_sample_identifier);
0147     }
0148 
0149     evlist__set_id_pos(evlist);
0150 }
0151 
0152 static int get_max_rate(unsigned int *rate)
0153 {
0154     return sysctl__read_int("kernel/perf_event_max_sample_rate", (int *)rate);
0155 }
0156 
0157 static int record_opts__config_freq(struct record_opts *opts)
0158 {
0159     bool user_freq = opts->user_freq != UINT_MAX;
0160     bool user_interval = opts->user_interval != ULLONG_MAX;
0161     unsigned int max_rate;
0162 
0163     if (user_interval && user_freq) {
0164         pr_err("cannot set frequency and period at the same time\n");
0165         return -1;
0166     }
0167 
0168     if (user_interval)
0169         opts->default_interval = opts->user_interval;
0170     if (user_freq)
0171         opts->freq = opts->user_freq;
0172 
0173     /*
0174      * User specified count overrides default frequency.
0175      */
0176     if (opts->default_interval)
0177         opts->freq = 0;
0178     else if (opts->freq) {
0179         opts->default_interval = opts->freq;
0180     } else {
0181         pr_err("frequency and count are zero, aborting\n");
0182         return -1;
0183     }
0184 
0185     if (get_max_rate(&max_rate))
0186         return 0;
0187 
0188     /*
0189      * User specified frequency is over current maximum.
0190      */
0191     if (user_freq && (max_rate < opts->freq)) {
0192         if (opts->strict_freq) {
0193             pr_err("error: Maximum frequency rate (%'u Hz) exceeded.\n"
0194                    "       Please use -F freq option with a lower value or consider\n"
0195                    "       tweaking /proc/sys/kernel/perf_event_max_sample_rate.\n",
0196                    max_rate);
0197             return -1;
0198         } else {
0199             pr_warning("warning: Maximum frequency rate (%'u Hz) exceeded, throttling from %'u Hz to %'u Hz.\n"
0200                    "         The limit can be raised via /proc/sys/kernel/perf_event_max_sample_rate.\n"
0201                    "         The kernel will lower it when perf's interrupts take too long.\n"
0202                    "         Use --strict-freq to disable this throttling, refusing to record.\n",
0203                    max_rate, opts->freq, max_rate);
0204 
0205             opts->freq = max_rate;
0206         }
0207     }
0208 
0209     /*
0210      * Default frequency is over current maximum.
0211      */
0212     if (max_rate < opts->freq) {
0213         pr_warning("Lowering default frequency rate from %u to %u.\n"
0214                "Please consider tweaking "
0215                "/proc/sys/kernel/perf_event_max_sample_rate.\n",
0216                opts->freq, max_rate);
0217         opts->freq = max_rate;
0218     }
0219 
0220     return 0;
0221 }
0222 
0223 int record_opts__config(struct record_opts *opts)
0224 {
0225     return record_opts__config_freq(opts);
0226 }
0227 
0228 bool evlist__can_select_event(struct evlist *evlist, const char *str)
0229 {
0230     struct evlist *temp_evlist;
0231     struct evsel *evsel;
0232     int err, fd;
0233     struct perf_cpu cpu = { .cpu = 0 };
0234     bool ret = false;
0235     pid_t pid = -1;
0236 
0237     temp_evlist = evlist__new();
0238     if (!temp_evlist)
0239         return false;
0240 
0241     err = parse_event(temp_evlist, str);
0242     if (err)
0243         goto out_delete;
0244 
0245     evsel = evlist__last(temp_evlist);
0246 
0247     if (!evlist || perf_cpu_map__empty(evlist->core.user_requested_cpus)) {
0248         struct perf_cpu_map *cpus = perf_cpu_map__new(NULL);
0249 
0250         if (cpus)
0251             cpu =  perf_cpu_map__cpu(cpus, 0);
0252 
0253         perf_cpu_map__put(cpus);
0254     } else {
0255         cpu = perf_cpu_map__cpu(evlist->core.user_requested_cpus, 0);
0256     }
0257 
0258     while (1) {
0259         fd = sys_perf_event_open(&evsel->core.attr, pid, cpu.cpu, -1,
0260                      perf_event_open_cloexec_flag());
0261         if (fd < 0) {
0262             if (pid == -1 && errno == EACCES) {
0263                 pid = 0;
0264                 continue;
0265             }
0266             goto out_delete;
0267         }
0268         break;
0269     }
0270     close(fd);
0271     ret = true;
0272 
0273 out_delete:
0274     evlist__delete(temp_evlist);
0275     return ret;
0276 }
0277 
0278 int record__parse_freq(const struct option *opt, const char *str, int unset __maybe_unused)
0279 {
0280     unsigned int freq;
0281     struct record_opts *opts = opt->value;
0282 
0283     if (!str)
0284         return -EINVAL;
0285 
0286     if (strcasecmp(str, "max") == 0) {
0287         if (get_max_rate(&freq)) {
0288             pr_err("couldn't read /proc/sys/kernel/perf_event_max_sample_rate\n");
0289             return -1;
0290         }
0291         pr_info("info: Using a maximum frequency rate of %'d Hz\n", freq);
0292     } else {
0293         freq = atoi(str);
0294     }
0295 
0296     opts->user_freq = freq;
0297     return 0;
0298 }