bench_ringbufs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2020 Facebook */
  3. #include <asm/barrier.h>
  4. #include <linux/perf_event.h>
  5. #include <linux/ring_buffer.h>
  6. #include <sys/epoll.h>
  7. #include <sys/mman.h>
  8. #include <argp.h>
  9. #include <stdlib.h>
  10. #include "bench.h"
  11. #include "ringbuf_bench.skel.h"
  12. #include "perfbuf_bench.skel.h"
  13. static struct {
  14. bool back2back;
  15. int batch_cnt;
  16. bool sampled;
  17. int sample_rate;
  18. int ringbuf_sz; /* per-ringbuf, in bytes */
  19. bool ringbuf_use_output; /* use slower output API */
  20. int perfbuf_sz; /* per-CPU size, in pages */
  21. } args = {
  22. .back2back = false,
  23. .batch_cnt = 500,
  24. .sampled = false,
  25. .sample_rate = 500,
  26. .ringbuf_sz = 512 * 1024,
  27. .ringbuf_use_output = false,
  28. .perfbuf_sz = 128,
  29. };
  30. enum {
  31. ARG_RB_BACK2BACK = 2000,
  32. ARG_RB_USE_OUTPUT = 2001,
  33. ARG_RB_BATCH_CNT = 2002,
  34. ARG_RB_SAMPLED = 2003,
  35. ARG_RB_SAMPLE_RATE = 2004,
  36. };
  37. static const struct argp_option opts[] = {
  38. { "rb-b2b", ARG_RB_BACK2BACK, NULL, 0, "Back-to-back mode"},
  39. { "rb-use-output", ARG_RB_USE_OUTPUT, NULL, 0, "Use bpf_ringbuf_output() instead of bpf_ringbuf_reserve()"},
  40. { "rb-batch-cnt", ARG_RB_BATCH_CNT, "CNT", 0, "Set BPF-side record batch count"},
  41. { "rb-sampled", ARG_RB_SAMPLED, NULL, 0, "Notification sampling"},
  42. { "rb-sample-rate", ARG_RB_SAMPLE_RATE, "RATE", 0, "Notification sample rate"},
  43. {},
  44. };
  45. static error_t parse_arg(int key, char *arg, struct argp_state *state)
  46. {
  47. switch (key) {
  48. case ARG_RB_BACK2BACK:
  49. args.back2back = true;
  50. break;
  51. case ARG_RB_USE_OUTPUT:
  52. args.ringbuf_use_output = true;
  53. break;
  54. case ARG_RB_BATCH_CNT:
  55. args.batch_cnt = strtol(arg, NULL, 10);
  56. if (args.batch_cnt < 0) {
  57. fprintf(stderr, "Invalid batch count.");
  58. argp_usage(state);
  59. }
  60. break;
  61. case ARG_RB_SAMPLED:
  62. args.sampled = true;
  63. break;
  64. case ARG_RB_SAMPLE_RATE:
  65. args.sample_rate = strtol(arg, NULL, 10);
  66. if (args.sample_rate < 0) {
  67. fprintf(stderr, "Invalid perfbuf sample rate.");
  68. argp_usage(state);
  69. }
  70. break;
  71. default:
  72. return ARGP_ERR_UNKNOWN;
  73. }
  74. return 0;
  75. }
  76. /* exported into benchmark runner */
  77. const struct argp bench_ringbufs_argp = {
  78. .options = opts,
  79. .parser = parse_arg,
  80. };
  81. /* RINGBUF-LIBBPF benchmark */
  82. static struct counter buf_hits;
  83. static inline void bufs_trigger_batch(void)
  84. {
  85. (void)syscall(__NR_getpgid);
  86. }
  87. static void bufs_validate(void)
  88. {
  89. if (env.consumer_cnt != 1) {
  90. fprintf(stderr, "rb-libbpf benchmark needs one consumer!\n");
  91. exit(1);
  92. }
  93. if (args.back2back && env.producer_cnt > 1) {
  94. fprintf(stderr, "back-to-back mode makes sense only for single-producer case!\n");
  95. exit(1);
  96. }
  97. }
  98. static void *bufs_sample_producer(void *input)
  99. {
  100. if (args.back2back) {
  101. /* initial batch to get everything started */
  102. bufs_trigger_batch();
  103. return NULL;
  104. }
  105. while (true)
  106. bufs_trigger_batch();
  107. return NULL;
  108. }
  109. static struct ringbuf_libbpf_ctx {
  110. struct ringbuf_bench *skel;
  111. struct ring_buffer *ringbuf;
  112. } ringbuf_libbpf_ctx;
  113. static void ringbuf_libbpf_measure(struct bench_res *res)
  114. {
  115. struct ringbuf_libbpf_ctx *ctx = &ringbuf_libbpf_ctx;
  116. res->hits = atomic_swap(&buf_hits.value, 0);
  117. res->drops = atomic_swap(&ctx->skel->bss->dropped, 0);
  118. }
  119. static struct ringbuf_bench *ringbuf_setup_skeleton(void)
  120. {
  121. struct ringbuf_bench *skel;
  122. setup_libbpf();
  123. skel = ringbuf_bench__open();
  124. if (!skel) {
  125. fprintf(stderr, "failed to open skeleton\n");
  126. exit(1);
  127. }
  128. skel->rodata->batch_cnt = args.batch_cnt;
  129. skel->rodata->use_output = args.ringbuf_use_output ? 1 : 0;
  130. if (args.sampled)
  131. /* record data + header take 16 bytes */
  132. skel->rodata->wakeup_data_size = args.sample_rate * 16;
  133. bpf_map__set_max_entries(skel->maps.ringbuf, args.ringbuf_sz);
  134. if (ringbuf_bench__load(skel)) {
  135. fprintf(stderr, "failed to load skeleton\n");
  136. exit(1);
  137. }
  138. return skel;
  139. }
  140. static int buf_process_sample(void *ctx, void *data, size_t len)
  141. {
  142. atomic_inc(&buf_hits.value);
  143. return 0;
  144. }
  145. static void ringbuf_libbpf_setup(void)
  146. {
  147. struct ringbuf_libbpf_ctx *ctx = &ringbuf_libbpf_ctx;
  148. struct bpf_link *link;
  149. ctx->skel = ringbuf_setup_skeleton();
  150. ctx->ringbuf = ring_buffer__new(bpf_map__fd(ctx->skel->maps.ringbuf),
  151. buf_process_sample, NULL, NULL);
  152. if (!ctx->ringbuf) {
  153. fprintf(stderr, "failed to create ringbuf\n");
  154. exit(1);
  155. }
  156. link = bpf_program__attach(ctx->skel->progs.bench_ringbuf);
  157. if (!link) {
  158. fprintf(stderr, "failed to attach program!\n");
  159. exit(1);
  160. }
  161. }
  162. static void *ringbuf_libbpf_consumer(void *input)
  163. {
  164. struct ringbuf_libbpf_ctx *ctx = &ringbuf_libbpf_ctx;
  165. while (ring_buffer__poll(ctx->ringbuf, -1) >= 0) {
  166. if (args.back2back)
  167. bufs_trigger_batch();
  168. }
  169. fprintf(stderr, "ringbuf polling failed!\n");
  170. return NULL;
  171. }
  172. /* RINGBUF-CUSTOM benchmark */
  173. struct ringbuf_custom {
  174. __u64 *consumer_pos;
  175. __u64 *producer_pos;
  176. __u64 mask;
  177. void *data;
  178. int map_fd;
  179. };
  180. static struct ringbuf_custom_ctx {
  181. struct ringbuf_bench *skel;
  182. struct ringbuf_custom ringbuf;
  183. int epoll_fd;
  184. struct epoll_event event;
  185. } ringbuf_custom_ctx;
  186. static void ringbuf_custom_measure(struct bench_res *res)
  187. {
  188. struct ringbuf_custom_ctx *ctx = &ringbuf_custom_ctx;
  189. res->hits = atomic_swap(&buf_hits.value, 0);
  190. res->drops = atomic_swap(&ctx->skel->bss->dropped, 0);
  191. }
  192. static void ringbuf_custom_setup(void)
  193. {
  194. struct ringbuf_custom_ctx *ctx = &ringbuf_custom_ctx;
  195. const size_t page_size = getpagesize();
  196. struct bpf_link *link;
  197. struct ringbuf_custom *r;
  198. void *tmp;
  199. int err;
  200. ctx->skel = ringbuf_setup_skeleton();
  201. ctx->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
  202. if (ctx->epoll_fd < 0) {
  203. fprintf(stderr, "failed to create epoll fd: %d\n", -errno);
  204. exit(1);
  205. }
  206. r = &ctx->ringbuf;
  207. r->map_fd = bpf_map__fd(ctx->skel->maps.ringbuf);
  208. r->mask = args.ringbuf_sz - 1;
  209. /* Map writable consumer page */
  210. tmp = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
  211. r->map_fd, 0);
  212. if (tmp == MAP_FAILED) {
  213. fprintf(stderr, "failed to mmap consumer page: %d\n", -errno);
  214. exit(1);
  215. }
  216. r->consumer_pos = tmp;
  217. /* Map read-only producer page and data pages. */
  218. tmp = mmap(NULL, page_size + 2 * args.ringbuf_sz, PROT_READ, MAP_SHARED,
  219. r->map_fd, page_size);
  220. if (tmp == MAP_FAILED) {
  221. fprintf(stderr, "failed to mmap data pages: %d\n", -errno);
  222. exit(1);
  223. }
  224. r->producer_pos = tmp;
  225. r->data = tmp + page_size;
  226. ctx->event.events = EPOLLIN;
  227. err = epoll_ctl(ctx->epoll_fd, EPOLL_CTL_ADD, r->map_fd, &ctx->event);
  228. if (err < 0) {
  229. fprintf(stderr, "failed to epoll add ringbuf: %d\n", -errno);
  230. exit(1);
  231. }
  232. link = bpf_program__attach(ctx->skel->progs.bench_ringbuf);
  233. if (!link) {
  234. fprintf(stderr, "failed to attach program\n");
  235. exit(1);
  236. }
  237. }
  238. #define RINGBUF_BUSY_BIT (1 << 31)
  239. #define RINGBUF_DISCARD_BIT (1 << 30)
  240. #define RINGBUF_META_LEN 8
  241. static inline int roundup_len(__u32 len)
  242. {
  243. /* clear out top 2 bits */
  244. len <<= 2;
  245. len >>= 2;
  246. /* add length prefix */
  247. len += RINGBUF_META_LEN;
  248. /* round up to 8 byte alignment */
  249. return (len + 7) / 8 * 8;
  250. }
  251. static void ringbuf_custom_process_ring(struct ringbuf_custom *r)
  252. {
  253. unsigned long cons_pos, prod_pos;
  254. int *len_ptr, len;
  255. bool got_new_data;
  256. cons_pos = smp_load_acquire(r->consumer_pos);
  257. while (true) {
  258. got_new_data = false;
  259. prod_pos = smp_load_acquire(r->producer_pos);
  260. while (cons_pos < prod_pos) {
  261. len_ptr = r->data + (cons_pos & r->mask);
  262. len = smp_load_acquire(len_ptr);
  263. /* sample not committed yet, bail out for now */
  264. if (len & RINGBUF_BUSY_BIT)
  265. return;
  266. got_new_data = true;
  267. cons_pos += roundup_len(len);
  268. atomic_inc(&buf_hits.value);
  269. }
  270. if (got_new_data)
  271. smp_store_release(r->consumer_pos, cons_pos);
  272. else
  273. break;
  274. }
  275. }
  276. static void *ringbuf_custom_consumer(void *input)
  277. {
  278. struct ringbuf_custom_ctx *ctx = &ringbuf_custom_ctx;
  279. int cnt;
  280. do {
  281. if (args.back2back)
  282. bufs_trigger_batch();
  283. cnt = epoll_wait(ctx->epoll_fd, &ctx->event, 1, -1);
  284. if (cnt > 0)
  285. ringbuf_custom_process_ring(&ctx->ringbuf);
  286. } while (cnt >= 0);
  287. fprintf(stderr, "ringbuf polling failed!\n");
  288. return 0;
  289. }
  290. /* PERFBUF-LIBBPF benchmark */
  291. static struct perfbuf_libbpf_ctx {
  292. struct perfbuf_bench *skel;
  293. struct perf_buffer *perfbuf;
  294. } perfbuf_libbpf_ctx;
  295. static void perfbuf_measure(struct bench_res *res)
  296. {
  297. struct perfbuf_libbpf_ctx *ctx = &perfbuf_libbpf_ctx;
  298. res->hits = atomic_swap(&buf_hits.value, 0);
  299. res->drops = atomic_swap(&ctx->skel->bss->dropped, 0);
  300. }
  301. static struct perfbuf_bench *perfbuf_setup_skeleton(void)
  302. {
  303. struct perfbuf_bench *skel;
  304. setup_libbpf();
  305. skel = perfbuf_bench__open();
  306. if (!skel) {
  307. fprintf(stderr, "failed to open skeleton\n");
  308. exit(1);
  309. }
  310. skel->rodata->batch_cnt = args.batch_cnt;
  311. if (perfbuf_bench__load(skel)) {
  312. fprintf(stderr, "failed to load skeleton\n");
  313. exit(1);
  314. }
  315. return skel;
  316. }
  317. static enum bpf_perf_event_ret
  318. perfbuf_process_sample_raw(void *input_ctx, int cpu,
  319. struct perf_event_header *e)
  320. {
  321. switch (e->type) {
  322. case PERF_RECORD_SAMPLE:
  323. atomic_inc(&buf_hits.value);
  324. break;
  325. case PERF_RECORD_LOST:
  326. break;
  327. default:
  328. return LIBBPF_PERF_EVENT_ERROR;
  329. }
  330. return LIBBPF_PERF_EVENT_CONT;
  331. }
  332. static void perfbuf_libbpf_setup(void)
  333. {
  334. struct perfbuf_libbpf_ctx *ctx = &perfbuf_libbpf_ctx;
  335. struct perf_event_attr attr;
  336. struct bpf_link *link;
  337. ctx->skel = perfbuf_setup_skeleton();
  338. memset(&attr, 0, sizeof(attr));
  339. attr.config = PERF_COUNT_SW_BPF_OUTPUT;
  340. attr.type = PERF_TYPE_SOFTWARE;
  341. attr.sample_type = PERF_SAMPLE_RAW;
  342. /* notify only every Nth sample */
  343. if (args.sampled) {
  344. attr.sample_period = args.sample_rate;
  345. attr.wakeup_events = args.sample_rate;
  346. } else {
  347. attr.sample_period = 1;
  348. attr.wakeup_events = 1;
  349. }
  350. if (args.sample_rate > args.batch_cnt) {
  351. fprintf(stderr, "sample rate %d is too high for given batch count %d\n",
  352. args.sample_rate, args.batch_cnt);
  353. exit(1);
  354. }
  355. ctx->perfbuf = perf_buffer__new_raw(bpf_map__fd(ctx->skel->maps.perfbuf),
  356. args.perfbuf_sz, &attr,
  357. perfbuf_process_sample_raw, NULL, NULL);
  358. if (!ctx->perfbuf) {
  359. fprintf(stderr, "failed to create perfbuf\n");
  360. exit(1);
  361. }
  362. link = bpf_program__attach(ctx->skel->progs.bench_perfbuf);
  363. if (!link) {
  364. fprintf(stderr, "failed to attach program\n");
  365. exit(1);
  366. }
  367. }
  368. static void *perfbuf_libbpf_consumer(void *input)
  369. {
  370. struct perfbuf_libbpf_ctx *ctx = &perfbuf_libbpf_ctx;
  371. while (perf_buffer__poll(ctx->perfbuf, -1) >= 0) {
  372. if (args.back2back)
  373. bufs_trigger_batch();
  374. }
  375. fprintf(stderr, "perfbuf polling failed!\n");
  376. return NULL;
  377. }
  378. /* PERFBUF-CUSTOM benchmark */
  379. /* copies of internal libbpf definitions */
  380. struct perf_cpu_buf {
  381. struct perf_buffer *pb;
  382. void *base; /* mmap()'ed memory */
  383. void *buf; /* for reconstructing segmented data */
  384. size_t buf_size;
  385. int fd;
  386. int cpu;
  387. int map_key;
  388. };
  389. struct perf_buffer {
  390. perf_buffer_event_fn event_cb;
  391. perf_buffer_sample_fn sample_cb;
  392. perf_buffer_lost_fn lost_cb;
  393. void *ctx; /* passed into callbacks */
  394. size_t page_size;
  395. size_t mmap_size;
  396. struct perf_cpu_buf **cpu_bufs;
  397. struct epoll_event *events;
  398. int cpu_cnt; /* number of allocated CPU buffers */
  399. int epoll_fd; /* perf event FD */
  400. int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
  401. };
  402. static void *perfbuf_custom_consumer(void *input)
  403. {
  404. struct perfbuf_libbpf_ctx *ctx = &perfbuf_libbpf_ctx;
  405. struct perf_buffer *pb = ctx->perfbuf;
  406. struct perf_cpu_buf *cpu_buf;
  407. struct perf_event_mmap_page *header;
  408. size_t mmap_mask = pb->mmap_size - 1;
  409. struct perf_event_header *ehdr;
  410. __u64 data_head, data_tail;
  411. size_t ehdr_size;
  412. void *base;
  413. int i, cnt;
  414. while (true) {
  415. if (args.back2back)
  416. bufs_trigger_batch();
  417. cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, -1);
  418. if (cnt <= 0) {
  419. fprintf(stderr, "perf epoll failed: %d\n", -errno);
  420. exit(1);
  421. }
  422. for (i = 0; i < cnt; ++i) {
  423. cpu_buf = pb->events[i].data.ptr;
  424. header = cpu_buf->base;
  425. base = ((void *)header) + pb->page_size;
  426. data_head = ring_buffer_read_head(header);
  427. data_tail = header->data_tail;
  428. while (data_head != data_tail) {
  429. ehdr = base + (data_tail & mmap_mask);
  430. ehdr_size = ehdr->size;
  431. if (ehdr->type == PERF_RECORD_SAMPLE)
  432. atomic_inc(&buf_hits.value);
  433. data_tail += ehdr_size;
  434. }
  435. ring_buffer_write_tail(header, data_tail);
  436. }
  437. }
  438. return NULL;
  439. }
  440. const struct bench bench_rb_libbpf = {
  441. .name = "rb-libbpf",
  442. .argp = &bench_ringbufs_argp,
  443. .validate = bufs_validate,
  444. .setup = ringbuf_libbpf_setup,
  445. .producer_thread = bufs_sample_producer,
  446. .consumer_thread = ringbuf_libbpf_consumer,
  447. .measure = ringbuf_libbpf_measure,
  448. .report_progress = hits_drops_report_progress,
  449. .report_final = hits_drops_report_final,
  450. };
  451. const struct bench bench_rb_custom = {
  452. .name = "rb-custom",
  453. .argp = &bench_ringbufs_argp,
  454. .validate = bufs_validate,
  455. .setup = ringbuf_custom_setup,
  456. .producer_thread = bufs_sample_producer,
  457. .consumer_thread = ringbuf_custom_consumer,
  458. .measure = ringbuf_custom_measure,
  459. .report_progress = hits_drops_report_progress,
  460. .report_final = hits_drops_report_final,
  461. };
  462. const struct bench bench_pb_libbpf = {
  463. .name = "pb-libbpf",
  464. .argp = &bench_ringbufs_argp,
  465. .validate = bufs_validate,
  466. .setup = perfbuf_libbpf_setup,
  467. .producer_thread = bufs_sample_producer,
  468. .consumer_thread = perfbuf_libbpf_consumer,
  469. .measure = perfbuf_measure,
  470. .report_progress = hits_drops_report_progress,
  471. .report_final = hits_drops_report_final,
  472. };
  473. const struct bench bench_pb_custom = {
  474. .name = "pb-custom",
  475. .argp = &bench_ringbufs_argp,
  476. .validate = bufs_validate,
  477. .setup = perfbuf_libbpf_setup,
  478. .producer_thread = bufs_sample_producer,
  479. .consumer_thread = perfbuf_custom_consumer,
  480. .measure = perfbuf_measure,
  481. .report_progress = hits_drops_report_progress,
  482. .report_final = hits_drops_report_final,
  483. };