1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
5 #include <sys/socket.h>
10 #include "bench_local_storage_create.skel.h"
18 static struct bench_local_storage_create *skel;
19 static struct thread *threads;
20 static long create_owner_errs;
21 static int storage_type = BPF_MAP_TYPE_SK_STORAGE;
22 static int batch_sz = 32;
26 ARG_STORAGE_TYPE = 9001,
29 static const struct argp_option opts[] = {
30 { "batch-size", ARG_BATCH_SZ, "BATCH_SIZE", 0,
31 "The number of storage creations in each batch" },
32 { "storage-type", ARG_STORAGE_TYPE, "STORAGE_TYPE", 0,
33 "The type of local storage to test (socket or task)" },
37 static error_t parse_arg(int key, char *arg, struct argp_state *state)
45 fprintf(stderr, "invalid batch-size\n");
50 case ARG_STORAGE_TYPE:
51 if (!strcmp(arg, "task")) {
52 storage_type = BPF_MAP_TYPE_TASK_STORAGE;
53 } else if (!strcmp(arg, "socket")) {
54 storage_type = BPF_MAP_TYPE_SK_STORAGE;
56 fprintf(stderr, "invalid storage-type (socket or task)\n");
61 return ARGP_ERR_UNKNOWN;
67 const struct argp bench_local_storage_create_argp = {
72 static void validate(void)
74 if (env.consumer_cnt != 0) {
76 "local-storage-create benchmark does not need consumer\n");
81 static void setup(void)
85 skel = bench_local_storage_create__open_and_load();
87 fprintf(stderr, "error loading skel\n");
91 skel->bss->bench_pid = getpid();
92 if (storage_type == BPF_MAP_TYPE_SK_STORAGE) {
93 if (!bpf_program__attach(skel->progs.socket_post_create)) {
94 fprintf(stderr, "Error attaching bpf program\n");
98 if (!bpf_program__attach(skel->progs.sched_process_fork)) {
99 fprintf(stderr, "Error attaching bpf program\n");
104 if (!bpf_program__attach(skel->progs.kmalloc)) {
105 fprintf(stderr, "Error attaching bpf program\n");
109 threads = calloc(env.producer_cnt, sizeof(*threads));
112 fprintf(stderr, "cannot alloc thread_res\n");
116 for (i = 0; i < env.producer_cnt; i++) {
117 struct thread *t = &threads[i];
119 if (storage_type == BPF_MAP_TYPE_SK_STORAGE) {
120 t->fds = malloc(batch_sz * sizeof(*t->fds));
122 fprintf(stderr, "cannot alloc t->fds\n");
126 t->pthds = malloc(batch_sz * sizeof(*t->pthds));
128 fprintf(stderr, "cannot alloc t->pthds\n");
131 t->pthd_results = malloc(batch_sz * sizeof(*t->pthd_results));
132 if (!t->pthd_results) {
133 fprintf(stderr, "cannot alloc t->pthd_results\n");
140 static void measure(struct bench_res *res)
142 res->hits = atomic_swap(&skel->bss->create_cnts, 0);
143 res->drops = atomic_swap(&skel->bss->kmalloc_cnts, 0);
146 static void *sk_producer(void *input)
148 struct thread *t = &threads[(long)(input)];
153 for (i = 0; i < batch_sz; i++) {
154 fds[i] = socket(AF_INET6, SOCK_DGRAM, 0);
156 atomic_inc(&create_owner_errs);
159 for (i = 0; i < batch_sz; i++) {
168 static void *thread_func(void *arg)
173 static void *task_producer(void *input)
175 struct thread *t = &threads[(long)(input)];
176 pthread_t *pthds = t->pthds;
177 int *pthd_results = t->pthd_results;
181 for (i = 0; i < batch_sz; i++) {
182 pthd_results[i] = pthread_create(&pthds[i], NULL, thread_func, NULL);
184 atomic_inc(&create_owner_errs);
187 for (i = 0; i < batch_sz; i++) {
188 if (!pthd_results[i])
189 pthread_join(pthds[i], NULL);
196 static void *producer(void *input)
198 if (storage_type == BPF_MAP_TYPE_SK_STORAGE)
199 return sk_producer(input);
201 return task_producer(input);
204 static void report_progress(int iter, struct bench_res *res, long delta_ns)
206 double creates_per_sec, kmallocs_per_create;
208 creates_per_sec = res->hits / 1000.0 / (delta_ns / 1000000000.0);
209 kmallocs_per_create = (double)res->drops / res->hits;
211 printf("Iter %3d (%7.3lfus): ",
212 iter, (delta_ns - 1000000000) / 1000.0);
213 printf("creates %8.3lfk/s (%7.3lfk/prod), ",
214 creates_per_sec, creates_per_sec / env.producer_cnt);
215 printf("%3.2lf kmallocs/create\n", kmallocs_per_create);
218 static void report_final(struct bench_res res[], int res_cnt)
220 double creates_mean = 0.0, creates_stddev = 0.0;
221 long total_creates = 0, total_kmallocs = 0;
224 for (i = 0; i < res_cnt; i++) {
225 creates_mean += res[i].hits / 1000.0 / (0.0 + res_cnt);
226 total_creates += res[i].hits;
227 total_kmallocs += res[i].drops;
231 for (i = 0; i < res_cnt; i++)
232 creates_stddev += (creates_mean - res[i].hits / 1000.0) *
233 (creates_mean - res[i].hits / 1000.0) /
235 creates_stddev = sqrt(creates_stddev);
237 printf("Summary: creates %8.3lf \u00B1 %5.3lfk/s (%7.3lfk/prod), ",
238 creates_mean, creates_stddev, creates_mean / env.producer_cnt);
239 printf("%4.2lf kmallocs/create\n", (double)total_kmallocs / total_creates);
240 if (create_owner_errs || skel->bss->create_errs)
241 printf("%s() errors %ld create_errs %ld\n",
242 storage_type == BPF_MAP_TYPE_SK_STORAGE ?
243 "socket" : "pthread_create",
245 skel->bss->create_errs);
248 /* Benchmark performance of creating bpf local storage */
249 const struct bench bench_local_storage_create = {
250 .name = "local-storage-create",
251 .argp = &bench_local_storage_create_argp,
252 .validate = validate,
254 .producer_thread = producer,
256 .report_progress = report_progress,
257 .report_final = report_final,