From 8b12e1fefb181f02393b6dee6c4ef72e2b858144 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 9 Jul 2023 18:47:43 +0800 Subject: [PATCH] add: (linux) benchmark for inotify --- benchmark/benchmark_ipc.cpp | 83 +++++++++++++++++++++++++++++++++++++ test/ipc/test_ipc_shm.cpp | 47 ++++++++++++++++++++- 2 files changed, 129 insertions(+), 1 deletion(-) diff --git a/benchmark/benchmark_ipc.cpp b/benchmark/benchmark_ipc.cpp index 4415312..066f93c 100644 --- a/benchmark/benchmark_ipc.cpp +++ b/benchmark/benchmark_ipc.cpp @@ -5,12 +5,14 @@ #include #include #include +#include #include #include #include #include #include +#include #include @@ -247,6 +249,60 @@ struct udp_reader { } } udp_r__; +struct inotify_reader { + pid_t pid_ {-1}; + + inotify_reader() { + auto shm = ipc::shared_memory("shm-inotify_reader", sizeof(flag_t)); + shm.as()->store(false, std::memory_order_relaxed); + fclose(fopen("/tmp/shm-inotify.0", "w")); + fclose(fopen("/tmp/shm-inotify.1", "w")); + pid_ = test::subproc([this] { + auto shm = ipc::shared_memory("shm-inotify_reader", sizeof(flag_t)); + auto flag = shm.as(); + auto shm_rt = ipc::shared_memory("shm-inotify_reader.0", sizeof(flag_t)); + auto shm_wt = ipc::shared_memory("shm-inotify_reader.1", sizeof(flag_t)); + auto f_rt = shm_rt.as(); + auto f_wt = shm_wt.as(); + int ifd = inotify_init(); + int iwd = inotify_add_watch(ifd, "/tmp/shm-inotify.0", IN_OPEN | IN_CLOSE); + // auto wf = fopen("/tmp/shm-inotify.1", "w"); + printf("inotify_reader start.\n"); + while (!flag->load(std::memory_order_relaxed)) { + // read + if (!f_rt->exchange(false, std::memory_order_acquire)) { + struct inotify_event e {}; + read(ifd, &e, sizeof(e)); + } + // write + f_wt->store(true, std::memory_order_release); + fclose(fopen("/tmp/shm-inotify.1", "r")); + // char c {'A'}; + // fwrite(&c, 1, 1, wf); + // fflush(wf); + } + // fclose(wf); + inotify_rm_watch(ifd, iwd); + close(ifd); + printf("inotify_reader exit.\n"); + }); + } + + ~inotify_reader() { + auto shm = ipc::shared_memory("shm-inotify_reader", sizeof(flag_t)); + shm.as()->store(true, std::memory_order_seq_cst); + auto shm_rt = ipc::shared_memory("shm-inotify_reader.0", sizeof(flag_t)); + shm_rt.as()->store(true, std::memory_order_seq_cst); + fclose(fopen("/tmp/shm-inotify.0", "r")); + // auto rf = fopen("/tmp/shm-inotify.0", "w"); + // char c {'A'}; + // fwrite(&c, 1, 1, rf); + // fflush(rf); + // fclose(rf); + test::join_subproc(pid_); + } +} inotify_r__; + void ipc_eventfd_rtt(benchmark::State &state) { for (auto _ : state) { uint64_t n = 1; @@ -311,6 +367,32 @@ void ipc_udp_rtt(benchmark::State &state) { close(sfd); } +void ipc_inotify_rtt(benchmark::State &state) { + auto shm_rt = ipc::shared_memory("shm-inotify_reader.0", sizeof(flag_t)); + auto shm_wt = ipc::shared_memory("shm-inotify_reader.1", sizeof(flag_t)); + auto f_rt = shm_rt.as(); + auto f_wt = shm_wt.as(); + int ifd = inotify_init(); + int iwd = inotify_add_watch(ifd, "/tmp/shm-inotify.1", IN_OPEN | IN_CLOSE); + // auto rf = fopen("/tmp/shm-inotify.0", "w"); + for (auto _ : state) { + // write + f_rt->store(true, std::memory_order_release); + fclose(fopen("/tmp/shm-inotify.0", "r")); + // char c {'A'}; + // fwrite(&c, 1, 1, rf); + // fflush(rf); + // read + if (!f_wt->exchange(false, std::memory_order_acquire)) { + struct inotify_event e {}; + read(ifd, &e, sizeof(e)); + } + } + // fclose(rf); + inotify_rm_watch(ifd, iwd); + close(ifd); +} + } // namespace BENCHMARK(ipc_eventfd_rtt); @@ -318,3 +400,4 @@ BENCHMARK(ipc_mqueue_rtt); BENCHMARK(ipc_npipe_rtt); BENCHMARK(ipc_sock_rtt); BENCHMARK(ipc_udp_rtt); +BENCHMARK(ipc_inotify_rtt); diff --git a/test/ipc/test_ipc_shm.cpp b/test/ipc/test_ipc_shm.cpp index 6449431..c7fcd90 100644 --- a/test/ipc/test_ipc_shm.cpp +++ b/test/ipc/test_ipc_shm.cpp @@ -76,7 +76,9 @@ TEST(shm, shared_memory) { EXPECT_TRUE(ipc::shm_close(*shm_r)); } -#if 0 +#include + +#if defined(LIBIMP_OS_LINUX) #include #include #include @@ -132,4 +134,47 @@ TEST(shm, pipe) { test::join_subproc(r1); test::join_subproc(r2); } + +#include +#include + +TEST(shm, event) { + fclose(fopen("/tmp/shm-event", "w")); + + auto sender = test::subproc([] { + printf("sender prepared...\n"); + auto fd = fopen("/tmp/shm-event", "w"); + for (int i = 0; i < 10; ++i) { + sleep(1); + printf("\nwrite %d\n", i); + char c {'A'}; + fwrite(&c, 1, 1, fd); + fflush(fd); + } + fclose(fd); + }); + + auto reader_maker = [](int k) { + return [k] { + printf("r%d prepared...\n", k); + sleep(1); + int ifd = inotify_init(); + int iwd = inotify_add_watch(ifd, "/tmp/shm-event", IN_MODIFY); + for (int i = 0; i < 10; ++i) { + struct inotify_event e {}; + read(ifd, &e, sizeof(e)); + printf("r%d %u\n", k, e.mask); + } + inotify_rm_watch(ifd, iwd); + close(ifd); + }; + }; + + auto r1 = test::subproc(reader_maker(1)); + auto r2 = test::subproc(reader_maker(2)); + + test::join_subproc(sender); + test::join_subproc(r1); + test::join_subproc(r2); +} #endif \ No newline at end of file