sampleip_user.c 4.2 KB

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