perf_event_attr_fprintf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <inttypes.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <linux/perf_event.h>
  8. #include "util/evsel_fprintf.h"
  9. struct bit_names {
  10. int bit;
  11. const char *name;
  12. };
  13. static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
  14. {
  15. bool first_bit = true;
  16. int i = 0;
  17. do {
  18. if (value & bits[i].bit) {
  19. buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
  20. first_bit = false;
  21. }
  22. } while (bits[++i].name != NULL);
  23. }
  24. static void __p_sample_type(char *buf, size_t size, u64 value)
  25. {
  26. #define bit_name(n) { PERF_SAMPLE_##n, #n }
  27. struct bit_names bits[] = {
  28. bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
  29. bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
  30. bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
  31. bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
  32. bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
  33. bit_name(WEIGHT), bit_name(PHYS_ADDR), bit_name(AUX),
  34. bit_name(CGROUP),
  35. { .name = NULL, }
  36. };
  37. #undef bit_name
  38. __p_bits(buf, size, value, bits);
  39. }
  40. static void __p_branch_sample_type(char *buf, size_t size, u64 value)
  41. {
  42. #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
  43. struct bit_names bits[] = {
  44. bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
  45. bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
  46. bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
  47. bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
  48. bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
  49. bit_name(HW_INDEX),
  50. { .name = NULL, }
  51. };
  52. #undef bit_name
  53. __p_bits(buf, size, value, bits);
  54. }
  55. static void __p_read_format(char *buf, size_t size, u64 value)
  56. {
  57. #define bit_name(n) { PERF_FORMAT_##n, #n }
  58. struct bit_names bits[] = {
  59. bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
  60. bit_name(ID), bit_name(GROUP),
  61. { .name = NULL, }
  62. };
  63. #undef bit_name
  64. __p_bits(buf, size, value, bits);
  65. }
  66. #define BUF_SIZE 1024
  67. #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
  68. #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
  69. #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
  70. #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
  71. #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
  72. #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
  73. #define PRINT_ATTRn(_n, _f, _p) \
  74. do { \
  75. if (attr->_f) { \
  76. _p(attr->_f); \
  77. ret += attr__fprintf(fp, _n, buf, priv);\
  78. } \
  79. } while (0)
  80. #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
  81. int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
  82. attr__fprintf_f attr__fprintf, void *priv)
  83. {
  84. char buf[BUF_SIZE];
  85. int ret = 0;
  86. PRINT_ATTRf(type, p_unsigned);
  87. PRINT_ATTRf(size, p_unsigned);
  88. PRINT_ATTRf(config, p_hex);
  89. PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
  90. PRINT_ATTRf(sample_type, p_sample_type);
  91. PRINT_ATTRf(read_format, p_read_format);
  92. PRINT_ATTRf(disabled, p_unsigned);
  93. PRINT_ATTRf(inherit, p_unsigned);
  94. PRINT_ATTRf(pinned, p_unsigned);
  95. PRINT_ATTRf(exclusive, p_unsigned);
  96. PRINT_ATTRf(exclude_user, p_unsigned);
  97. PRINT_ATTRf(exclude_kernel, p_unsigned);
  98. PRINT_ATTRf(exclude_hv, p_unsigned);
  99. PRINT_ATTRf(exclude_idle, p_unsigned);
  100. PRINT_ATTRf(mmap, p_unsigned);
  101. PRINT_ATTRf(comm, p_unsigned);
  102. PRINT_ATTRf(freq, p_unsigned);
  103. PRINT_ATTRf(inherit_stat, p_unsigned);
  104. PRINT_ATTRf(enable_on_exec, p_unsigned);
  105. PRINT_ATTRf(task, p_unsigned);
  106. PRINT_ATTRf(watermark, p_unsigned);
  107. PRINT_ATTRf(precise_ip, p_unsigned);
  108. PRINT_ATTRf(mmap_data, p_unsigned);
  109. PRINT_ATTRf(sample_id_all, p_unsigned);
  110. PRINT_ATTRf(exclude_host, p_unsigned);
  111. PRINT_ATTRf(exclude_guest, p_unsigned);
  112. PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
  113. PRINT_ATTRf(exclude_callchain_user, p_unsigned);
  114. PRINT_ATTRf(mmap2, p_unsigned);
  115. PRINT_ATTRf(comm_exec, p_unsigned);
  116. PRINT_ATTRf(use_clockid, p_unsigned);
  117. PRINT_ATTRf(context_switch, p_unsigned);
  118. PRINT_ATTRf(write_backward, p_unsigned);
  119. PRINT_ATTRf(namespaces, p_unsigned);
  120. PRINT_ATTRf(ksymbol, p_unsigned);
  121. PRINT_ATTRf(bpf_event, p_unsigned);
  122. PRINT_ATTRf(aux_output, p_unsigned);
  123. PRINT_ATTRf(cgroup, p_unsigned);
  124. PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
  125. PRINT_ATTRf(bp_type, p_unsigned);
  126. PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
  127. PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
  128. PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
  129. PRINT_ATTRf(sample_regs_user, p_hex);
  130. PRINT_ATTRf(sample_stack_user, p_unsigned);
  131. PRINT_ATTRf(clockid, p_signed);
  132. PRINT_ATTRf(sample_regs_intr, p_hex);
  133. PRINT_ATTRf(aux_watermark, p_unsigned);
  134. PRINT_ATTRf(sample_max_stack, p_unsigned);
  135. PRINT_ATTRf(aux_sample_size, p_unsigned);
  136. PRINT_ATTRf(text_poke, p_unsigned);
  137. return ret;
  138. }