mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
add: (linux) benchmark for inotify
This commit is contained in:
parent
f00e11699e
commit
8b12e1fefb
@ -5,12 +5,14 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/inotify.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <mqueue.h>
|
#include <mqueue.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
||||||
@ -247,6 +249,60 @@ struct udp_reader {
|
|||||||
}
|
}
|
||||||
} udp_r__;
|
} udp_r__;
|
||||||
|
|
||||||
|
struct inotify_reader {
|
||||||
|
pid_t pid_ {-1};
|
||||||
|
|
||||||
|
inotify_reader() {
|
||||||
|
auto shm = ipc::shared_memory("shm-inotify_reader", sizeof(flag_t));
|
||||||
|
shm.as<flag_t>()->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<flag_t>();
|
||||||
|
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<flag_t>();
|
||||||
|
auto f_wt = shm_wt.as<flag_t>();
|
||||||
|
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<flag_t>()->store(true, std::memory_order_seq_cst);
|
||||||
|
auto shm_rt = ipc::shared_memory("shm-inotify_reader.0", sizeof(flag_t));
|
||||||
|
shm_rt.as<flag_t>()->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) {
|
void ipc_eventfd_rtt(benchmark::State &state) {
|
||||||
for (auto _ : state) {
|
for (auto _ : state) {
|
||||||
uint64_t n = 1;
|
uint64_t n = 1;
|
||||||
@ -311,6 +367,32 @@ void ipc_udp_rtt(benchmark::State &state) {
|
|||||||
close(sfd);
|
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<flag_t>();
|
||||||
|
auto f_wt = shm_wt.as<flag_t>();
|
||||||
|
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
|
} // namespace
|
||||||
|
|
||||||
BENCHMARK(ipc_eventfd_rtt);
|
BENCHMARK(ipc_eventfd_rtt);
|
||||||
@ -318,3 +400,4 @@ BENCHMARK(ipc_mqueue_rtt);
|
|||||||
BENCHMARK(ipc_npipe_rtt);
|
BENCHMARK(ipc_npipe_rtt);
|
||||||
BENCHMARK(ipc_sock_rtt);
|
BENCHMARK(ipc_sock_rtt);
|
||||||
BENCHMARK(ipc_udp_rtt);
|
BENCHMARK(ipc_udp_rtt);
|
||||||
|
BENCHMARK(ipc_inotify_rtt);
|
||||||
|
|||||||
@ -76,7 +76,9 @@ TEST(shm, shared_memory) {
|
|||||||
EXPECT_TRUE(ipc::shm_close(*shm_r));
|
EXPECT_TRUE(ipc::shm_close(*shm_r));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
#include <libimp/detect_plat.h>
|
||||||
|
|
||||||
|
#if defined(LIBIMP_OS_LINUX)
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -132,4 +134,47 @@ TEST(shm, pipe) {
|
|||||||
test::join_subproc(r1);
|
test::join_subproc(r1);
|
||||||
test::join_subproc(r2);
|
test::join_subproc(r2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <sys/inotify.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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
|
#endif
|
||||||
Loading…
x
Reference in New Issue
Block a user