Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <inttypes.h>
0003 #include <stdio.h>
0004 #include <stdbool.h>
0005 #include <linux/kernel.h>
0006 #include <linux/types.h>
0007 #include <linux/perf_event.h>
0008 #include "util/evsel_fprintf.h"
0009 
0010 struct bit_names {
0011     int bit;
0012     const char *name;
0013 };
0014 
0015 static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
0016 {
0017     bool first_bit = true;
0018     int i = 0;
0019 
0020     do {
0021         if (value & bits[i].bit) {
0022             buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
0023             first_bit = false;
0024         }
0025     } while (bits[++i].name != NULL);
0026 }
0027 
0028 static void __p_sample_type(char *buf, size_t size, u64 value)
0029 {
0030 #define bit_name(n) { PERF_SAMPLE_##n, #n }
0031     struct bit_names bits[] = {
0032         bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
0033         bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
0034         bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
0035         bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
0036         bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
0037         bit_name(WEIGHT), bit_name(PHYS_ADDR), bit_name(AUX),
0038         bit_name(CGROUP), bit_name(DATA_PAGE_SIZE), bit_name(CODE_PAGE_SIZE),
0039         bit_name(WEIGHT_STRUCT),
0040         { .name = NULL, }
0041     };
0042 #undef bit_name
0043     __p_bits(buf, size, value, bits);
0044 }
0045 
0046 static void __p_branch_sample_type(char *buf, size_t size, u64 value)
0047 {
0048 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
0049     struct bit_names bits[] = {
0050         bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
0051         bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
0052         bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
0053         bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
0054         bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
0055         bit_name(TYPE_SAVE), bit_name(HW_INDEX),
0056         { .name = NULL, }
0057     };
0058 #undef bit_name
0059     __p_bits(buf, size, value, bits);
0060 }
0061 
0062 static void __p_read_format(char *buf, size_t size, u64 value)
0063 {
0064 #define bit_name(n) { PERF_FORMAT_##n, #n }
0065     struct bit_names bits[] = {
0066         bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
0067         bit_name(ID), bit_name(GROUP),
0068         { .name = NULL, }
0069     };
0070 #undef bit_name
0071     __p_bits(buf, size, value, bits);
0072 }
0073 
0074 #define BUF_SIZE        1024
0075 
0076 #define p_hex(val)      snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
0077 #define p_unsigned(val)     snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
0078 #define p_signed(val)       snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
0079 #define p_sample_type(val)  __p_sample_type(buf, BUF_SIZE, val)
0080 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
0081 #define p_read_format(val)  __p_read_format(buf, BUF_SIZE, val)
0082 
0083 #define PRINT_ATTRn(_n, _f, _p)             \
0084 do {                            \
0085     if (attr->_f) {                 \
0086         _p(attr->_f);               \
0087         ret += attr__fprintf(fp, _n, buf, priv);\
0088     }                       \
0089 } while (0)
0090 
0091 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
0092 
0093 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
0094                  attr__fprintf_f attr__fprintf, void *priv)
0095 {
0096     char buf[BUF_SIZE];
0097     int ret = 0;
0098 
0099     PRINT_ATTRf(type, p_unsigned);
0100     PRINT_ATTRf(size, p_unsigned);
0101     PRINT_ATTRf(config, p_hex);
0102     PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
0103     PRINT_ATTRf(sample_type, p_sample_type);
0104     PRINT_ATTRf(read_format, p_read_format);
0105 
0106     PRINT_ATTRf(disabled, p_unsigned);
0107     PRINT_ATTRf(inherit, p_unsigned);
0108     PRINT_ATTRf(pinned, p_unsigned);
0109     PRINT_ATTRf(exclusive, p_unsigned);
0110     PRINT_ATTRf(exclude_user, p_unsigned);
0111     PRINT_ATTRf(exclude_kernel, p_unsigned);
0112     PRINT_ATTRf(exclude_hv, p_unsigned);
0113     PRINT_ATTRf(exclude_idle, p_unsigned);
0114     PRINT_ATTRf(mmap, p_unsigned);
0115     PRINT_ATTRf(comm, p_unsigned);
0116     PRINT_ATTRf(freq, p_unsigned);
0117     PRINT_ATTRf(inherit_stat, p_unsigned);
0118     PRINT_ATTRf(enable_on_exec, p_unsigned);
0119     PRINT_ATTRf(task, p_unsigned);
0120     PRINT_ATTRf(watermark, p_unsigned);
0121     PRINT_ATTRf(precise_ip, p_unsigned);
0122     PRINT_ATTRf(mmap_data, p_unsigned);
0123     PRINT_ATTRf(sample_id_all, p_unsigned);
0124     PRINT_ATTRf(exclude_host, p_unsigned);
0125     PRINT_ATTRf(exclude_guest, p_unsigned);
0126     PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
0127     PRINT_ATTRf(exclude_callchain_user, p_unsigned);
0128     PRINT_ATTRf(mmap2, p_unsigned);
0129     PRINT_ATTRf(comm_exec, p_unsigned);
0130     PRINT_ATTRf(use_clockid, p_unsigned);
0131     PRINT_ATTRf(context_switch, p_unsigned);
0132     PRINT_ATTRf(write_backward, p_unsigned);
0133     PRINT_ATTRf(namespaces, p_unsigned);
0134     PRINT_ATTRf(ksymbol, p_unsigned);
0135     PRINT_ATTRf(bpf_event, p_unsigned);
0136     PRINT_ATTRf(aux_output, p_unsigned);
0137     PRINT_ATTRf(cgroup, p_unsigned);
0138     PRINT_ATTRf(text_poke, p_unsigned);
0139     PRINT_ATTRf(build_id, p_unsigned);
0140     PRINT_ATTRf(inherit_thread, p_unsigned);
0141     PRINT_ATTRf(remove_on_exec, p_unsigned);
0142     PRINT_ATTRf(sigtrap, p_unsigned);
0143 
0144     PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
0145     PRINT_ATTRf(bp_type, p_unsigned);
0146     PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
0147     PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
0148     PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
0149     PRINT_ATTRf(sample_regs_user, p_hex);
0150     PRINT_ATTRf(sample_stack_user, p_unsigned);
0151     PRINT_ATTRf(clockid, p_signed);
0152     PRINT_ATTRf(sample_regs_intr, p_hex);
0153     PRINT_ATTRf(aux_watermark, p_unsigned);
0154     PRINT_ATTRf(sample_max_stack, p_unsigned);
0155     PRINT_ATTRf(aux_sample_size, p_unsigned);
0156     PRINT_ATTRf(sig_data, p_unsigned);
0157 
0158     return ret;
0159 }