From d8f9bdf25d362eb570c16c2abf39cb9a73cc5fab Mon Sep 17 00:00:00 2001 From: jinzhiguang Date: Thu, 28 Nov 2024 21:22:14 +0800 Subject: [PATCH] Added function: delete the record after printing once --- cli_parser/cli_parser.cpp | 2 ++ cli_parser/cli_parser.h | 1 + iscsi_bpf/iscsi_stats_ebpf.cpp | 6 +++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cli_parser/cli_parser.cpp b/cli_parser/cli_parser.cpp index b203da1..cebb2c0 100644 --- a/cli_parser/cli_parser.cpp +++ b/cli_parser/cli_parser.cpp @@ -19,6 +19,7 @@ using namespace std; using namespace std::filesystem; DEFINE_bool(h, false, "show short help message"); +DEFINE_bool(once, false, "show event only once"); DEFINE_int32(cid, -1, "connection id"); DEFINE_int32(sid, -1, "session id"); DEFINE_string(target, "", "target name"); @@ -63,6 +64,7 @@ bool validate_sid_cid() bool cli_parser(int argc, char** argv) { ostringstream oss; oss<<"Usage: "< +DECLARE_bool(once); DECLARE_int32(cid); DECLARE_int32(sid); DECLARE_string(target); diff --git a/iscsi_bpf/iscsi_stats_ebpf.cpp b/iscsi_bpf/iscsi_stats_ebpf.cpp index 879adb1..fee2a80 100644 --- a/iscsi_bpf/iscsi_stats_ebpf.cpp +++ b/iscsi_bpf/iscsi_stats_ebpf.cpp @@ -11,6 +11,7 @@ */ #include "iscsi_stats_ebpf.h" #include "iscsi_stats.skel.h" +#include "cli_parser.h" #include #include @@ -64,6 +65,9 @@ bool iscsi_stats_ebpf_loop(int(*handle)(struct iscsi_stats *stats)) { key = 0; while (!exiting) { err = bpf_map_get_next_key(map_fd, &key, &next_key); + if(FLAGS_once) { + bpf_map_delete_elem(map_fd, &key); + } if (err) { if (errno == ENOENT) err = 0; @@ -74,7 +78,7 @@ bool iscsi_stats_ebpf_loop(int(*handle)(struct iscsi_stats *stats)) { fprintf(stderr, "Failed to lookup map element\n"); break; } - handle(&stats); + handle(&stats); key = next_key; } } -- Gitee