1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2023. Huawei Technologies Co., Ltd */
6 #include <test_progs.h>
7 #include "htab_reuse.skel.h"
20 static void *htab_lookup_fn(void *arg)
22 struct htab_op_ctx *ctx = arg;
25 while (i++ < ctx->loop && !ctx->stop) {
26 struct htab_val value;
29 /* Use BPF_F_LOCK to use spin-lock in map value. */
31 bpf_map_lookup_elem_flags(ctx->fd, &key, &value, BPF_F_LOCK);
37 static void *htab_update_fn(void *arg)
39 struct htab_op_ctx *ctx = arg;
42 while (i++ < ctx->loop && !ctx->stop) {
43 struct htab_val value;
49 bpf_map_update_elem(ctx->fd, &key, &value, BPF_F_LOCK);
50 bpf_map_delete_elem(ctx->fd, &key);
55 bpf_map_update_elem(ctx->fd, &key, &value, BPF_F_LOCK);
56 bpf_map_delete_elem(ctx->fd, &key);
62 void test_htab_reuse(void)
64 unsigned int i, wr_nr = 1, rd_nr = 4;
65 pthread_t tids[wr_nr + rd_nr];
66 struct htab_reuse *skel;
67 struct htab_op_ctx ctx;
70 skel = htab_reuse__open_and_load();
71 if (!ASSERT_OK_PTR(skel, "htab_reuse__open_and_load"))
74 ctx.fd = bpf_map__fd(skel->maps.htab);
78 memset(tids, 0, sizeof(tids));
79 for (i = 0; i < wr_nr; i++) {
80 err = pthread_create(&tids[i], NULL, htab_update_fn, &ctx);
81 if (!ASSERT_OK(err, "pthread_create")) {
86 for (i = 0; i < rd_nr; i++) {
87 err = pthread_create(&tids[i + wr_nr], NULL, htab_lookup_fn, &ctx);
88 if (!ASSERT_OK(err, "pthread_create")) {
95 for (i = 0; i < wr_nr + rd_nr; i++) {
98 pthread_join(tids[i], NULL);
100 htab_reuse__destroy(skel);