sampleip_user.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * sampleip: sample instruction pointer and frequency count in a BPF map.
  3. *
  4. * Copyright 2016 Netflix, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. #include <signal.h>
  16. #include <string.h>
  17. #include <assert.h>
  18. #include <linux/perf_event.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/bpf.h>
  21. #include <sys/ioctl.h>
  22. #include "libbpf.h"
  23. #include "bpf_load.h"
  24. #include "perf-sys.h"
  25. #include "trace_helpers.h"
  26. #define DEFAULT_FREQ 99
  27. #define DEFAULT_SECS 5
  28. #define MAX_IPS 8192
  29. #define PAGE_OFFSET 0xffff880000000000
  30. static int nr_cpus;
  31. static void usage(void)
  32. {
  33. printf("USAGE: sampleip [-F freq] [duration]\n");
  34. printf(" -F freq # sample frequency (Hertz), default 99\n");
  35. printf(" duration # sampling duration (seconds), default 5\n");
  36. }
  37. static int sampling_start(int *pmu_fd, int freq)
  38. {
  39. int i;
  40. struct perf_event_attr pe_sample_attr = {
  41. .type = PERF_TYPE_SOFTWARE,
  42. .freq = 1,
  43. .sample_period = freq,
  44. .config = PERF_COUNT_SW_CPU_CLOCK,
  45. .inherit = 1,
  46. };
  47. for (i = 0; i < nr_cpus; i++) {
  48. pmu_fd[i] = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
  49. -1 /* group_fd */, 0 /* flags */);
  50. if (pmu_fd[i] < 0) {
  51. fprintf(stderr, "ERROR: Initializing perf sampling\n");
  52. return 1;
  53. }
  54. assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF,
  55. prog_fd[0]) == 0);
  56. assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0) == 0);
  57. }
  58. return 0;
  59. }
  60. static void sampling_end(int *pmu_fd)
  61. {
  62. int i;
  63. for (i = 0; i < nr_cpus; i++)
  64. close(pmu_fd[i]);
  65. }
  66. struct ipcount {
  67. __u64 ip;
  68. __u32 count;
  69. };
  70. /* used for sorting */
  71. struct ipcount counts[MAX_IPS];
  72. static int count_cmp(const void *p1, const void *p2)
  73. {
  74. return ((struct ipcount *)p1)->count - ((struct ipcount *)p2)->count;
  75. }
  76. static void print_ip_map(int fd)
  77. {
  78. struct ksym *sym;
  79. __u64 key, next_key;
  80. __u32 value;
  81. int i, max;
  82. printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT");
  83. /* fetch IPs and counts */
  84. key = 0, i = 0;
  85. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  86. bpf_map_lookup_elem(fd, &next_key, &value);
  87. counts[i].ip = next_key;
  88. counts[i++].count = value;
  89. key = next_key;
  90. }
  91. max = i;
  92. /* sort and print */
  93. qsort(counts, max, sizeof(struct ipcount), count_cmp);
  94. for (i = 0; i < max; i++) {
  95. if (counts[i].ip > PAGE_OFFSET) {
  96. sym = ksym_search(counts[i].ip);
  97. printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name,
  98. counts[i].count);
  99. } else {
  100. printf("0x%-17llx %-32s %u\n", counts[i].ip, "(user)",
  101. counts[i].count);
  102. }
  103. }
  104. if (max == MAX_IPS) {
  105. printf("WARNING: IP hash was full (max %d entries); ", max);
  106. printf("may have dropped samples\n");
  107. }
  108. }
  109. static void int_exit(int sig)
  110. {
  111. printf("\n");
  112. print_ip_map(map_fd[0]);
  113. exit(0);
  114. }
  115. int main(int argc, char **argv)
  116. {
  117. char filename[256];
  118. int *pmu_fd, opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS;
  119. /* process arguments */
  120. while ((opt = getopt(argc, argv, "F:h")) != -1) {
  121. switch (opt) {
  122. case 'F':
  123. freq = atoi(optarg);
  124. break;
  125. case 'h':
  126. default:
  127. usage();
  128. return 0;
  129. }
  130. }
  131. if (argc - optind == 1)
  132. secs = atoi(argv[optind]);
  133. if (freq == 0 || secs == 0) {
  134. usage();
  135. return 1;
  136. }
  137. /* initialize kernel symbol translation */
  138. if (load_kallsyms()) {
  139. fprintf(stderr, "ERROR: loading /proc/kallsyms\n");
  140. return 2;
  141. }
  142. /* create perf FDs for each CPU */
  143. nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
  144. pmu_fd = malloc(nr_cpus * sizeof(int));
  145. if (pmu_fd == NULL) {
  146. fprintf(stderr, "ERROR: malloc of pmu_fd\n");
  147. return 1;
  148. }
  149. /* load BPF program */
  150. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  151. if (load_bpf_file(filename)) {
  152. fprintf(stderr, "ERROR: loading BPF program (errno %d):\n",
  153. errno);
  154. if (strcmp(bpf_log_buf, "") == 0)
  155. fprintf(stderr, "Try: ulimit -l unlimited\n");
  156. else
  157. fprintf(stderr, "%s", bpf_log_buf);
  158. return 1;
  159. }
  160. signal(SIGINT, int_exit);
  161. signal(SIGTERM, int_exit);
  162. /* do sampling */
  163. printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
  164. freq, secs);
  165. if (sampling_start(pmu_fd, freq) != 0)
  166. return 1;
  167. sleep(secs);
  168. sampling_end(pmu_fd);
  169. free(pmu_fd);
  170. /* output sample counts */
  171. print_ip_map(map_fd[0]);
  172. return 0;
  173. }