From c7e7aec606a24a509eecee3716f36a9ef4fe8e7b Mon Sep 17 00:00:00 2001 From: zhuofeng <1107893276@qq.com> Date: Mon, 18 Nov 2024 09:07:48 +0000 Subject: [PATCH 1/9] print log info Signed-off-by: zhuofeng <1107893276@qq.com> --- 0001-print-log-info.patch | 126 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 0001-print-log-info.patch diff --git a/0001-print-log-info.patch b/0001-print-log-info.patch new file mode 100644 index 0000000..f304f86 --- /dev/null +++ b/0001-print-log-info.patch @@ -0,0 +1,126 @@ +From 5af1491727a31fe43119db82c56f25b64a5d333b Mon Sep 17 00:00:00 2001 +From: Your Name +Date: Mon, 18 Nov 2024 17:22:29 +0800 +Subject: [PATCH] print log info + +--- + src/c/ebpf_collector/ebpf_collector.c | 25 ++++++++++++++++++++----- + 1 file changed, 20 insertions(+), 5 deletions(-) + +diff --git a/src/c/ebpf_collector/ebpf_collector.c b/src/c/ebpf_collector/ebpf_collector.c +index af452c8..bbbca88 100644 +--- a/src/c/ebpf_collector/ebpf_collector.c ++++ b/src/c/ebpf_collector/ebpf_collector.c +@@ -118,6 +118,7 @@ char* find_device_name(dev_t dev) { + } + + static void update_io_dump(struct bpf_map *map_res, int *io_dump, int *map_size, char *stage) { ++ printf("ddddddddddd\n"); + struct time_range_io_count time_count; + u32 io_dump_key = 0; + struct sysinfo info; +@@ -125,6 +126,7 @@ static void update_io_dump(struct bpf_map *map_res, int *io_dump, int *map_size, + int count_time = 150; + u32 curr_time = info.uptime; + while (count_time >= 0) { ++ printf("ffffffffff\n"); + io_dump_key = curr_time - count_time; + int err = bpf_map_lookup_elem(map_res, &io_dump_key, &time_count); + if (err < 0) { +@@ -144,15 +146,18 @@ static void update_io_dump(struct bpf_map *map_res, int *io_dump, int *map_size, + } + } + count_time -= 1; ++ printf("eeeeeeeeeeee\n"); + } + } + + static int print_map_res(struct bpf_map *map_res, char *stage, int *map_size, int *io_dump) + { ++ printf("fffffffffff\n"); + struct stage_data counter; + int key = 0; + + for (key = 0; key < map_size; key++) { ++ printf("ggggggggggg\n"); + int err = bpf_map_lookup_elem(map_res, &key, &counter); + if (err < 0) { + fprintf(stderr, "failed to lookup %s map_res: %d\n", stage, err); +@@ -182,7 +187,7 @@ static int print_map_res(struct bpf_map *map_res, char *stage, int *map_size, in + fflush(stdout); + } + } +- ++printf("hhhhhhhhh\n"); + return 0; + } + +@@ -227,10 +232,11 @@ int main(int argc, char **argv) { + return err; + + snprintf(filename, sizeof(filename), BPF_FILE); +- ++ printf("load_bpf_file before\n"); + if (load_bpf_file(filename)) { + return 1; + } ++ printf("load_bpf_file after\n"); + + signal(SIGINT, sig_handler); + +@@ -239,17 +245,22 @@ int main(int argc, char **argv) { + printf("Failed to open /dev directory"); + return EXIT_FAILURE; + } +- ++ printf("11111111\n"); + while ((entry = readdir(dir)) != NULL) { ++ printf("2222222\n"); + if (entry->d_type == DT_BLK) { ++ printf("3333333\n"); + snprintf(path, sizeof(path), "/dev/%s", entry->d_name); + struct stat statbuf; + if (lstat(path, &statbuf) == 0) { ++ printf("4444444444\n"); + if (S_ISBLK(statbuf.st_mode)) { ++ printf("555555555555\n"); + devices[device_count].major = major(statbuf.st_rdev); + devices[device_count].minor = minor(statbuf.st_rdev); + device_count++; + if (device_count >= MAP_SIZE) { ++ printf("66666666666666\n"); + break; + } + } +@@ -258,22 +269,26 @@ int main(int argc, char **argv) { + } + + closedir(dir); +- ++printf("7777777777777\n"); + if (init_map(BLK_RES, "blk_res_map", device_count, devices) != 0) { + return 1; + } ++ printf("888888888\n"); + if (init_map(BIO_RES, "blo_res_map", device_count, devices) != 0) { + return 1; + } ++ printf("9999999999\n"); + if (init_map(WBT_RES, "wbt_res_map", device_count, devices) != 0) { + return 1; + } ++ printf("aaaaaaaaaa\n"); + if (init_map(TAG_RES, "tag_res_map", device_count, devices) != 0) { + return 1; + } ++ printf("bbbbbbbbbbbb\n"); + + for (;;) { +- ++ printf("ccccccccccc\n"); + sleep(1); + + int io_dump_blk[MAP_SIZE] = {0}; +-- +2.33.0 + -- Gitee From b44063e833457c06bbb95b9e615e44af980ec52b Mon Sep 17 00:00:00 2001 From: zhuofeng <1107893276@qq.com> Date: Mon, 18 Nov 2024 09:10:42 +0000 Subject: [PATCH 2/9] =?UTF-8?q?spec=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuofeng <1107893276@qq.com> --- sysSentry.spec | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sysSentry.spec b/sysSentry.spec index fe12957..d0eb909 100644 --- a/sysSentry.spec +++ b/sysSentry.spec @@ -4,7 +4,7 @@ Summary: System Inspection Framework Name: sysSentry Version: 1.0.2 -Release: 67 +Release: 68 License: Mulan PSL v2 Group: System Environment/Daemons Source0: https://gitee.com/openeuler/sysSentry/releases/download/v%{version}/%{name}-%{version}.tar.gz @@ -83,6 +83,7 @@ Patch70: fix-excessive-CPU-usage.patch Patch71: change-avg_block_io-config.patch Patch72: update-nvme-config.patch Patch73: fix-absolute-threshold-not-be-used.patch +Patch74: 0001-print-log-info.patch BuildRequires: cmake gcc-c++ BuildRequires: python3 python3-setuptools @@ -354,6 +355,12 @@ rm -rf %{buildroot} %attr(0550,root,root) %{python3_sitelib}/sentryCollector/__pycache__/collect_plugin* %changelog +* Fri Nov 15 2024 heyouzhi - 1.0.2-68 +- Type:bugfix +- CVE:NA +- SUG:NA +- DESC:pirnt log info + * Fri Nov 15 2024 heyouzhi - 1.0.2-67 - Type:bugfix - CVE:NA -- Gitee From 326fd58d2e67bce2588053d1f370938916ca68fc Mon Sep 17 00:00:00 2001 From: zhuofeng <1107893276@qq.com> Date: Tue, 19 Nov 2024 02:19:54 +0000 Subject: [PATCH 3/9] update Signed-off-by: zhuofeng <1107893276@qq.com> --- 0001-print-log-info.patch | 656 ++++++++++++++++++++++++++++++++++---- sysSentry.spec | 2 +- 2 files changed, 594 insertions(+), 64 deletions(-) diff --git a/0001-print-log-info.patch b/0001-print-log-info.patch index f304f86..41f66e6 100644 --- a/0001-print-log-info.patch +++ b/0001-print-log-info.patch @@ -1,126 +1,656 @@ -From 5af1491727a31fe43119db82c56f25b64a5d333b Mon Sep 17 00:00:00 2001 +From 41592c9e3f6ddd11bf149505e989b2b1119e8121 Mon Sep 17 00:00:00 2001 From: Your Name -Date: Mon, 18 Nov 2024 17:22:29 +0800 +Date: Tue, 19 Nov 2024 10:29:50 +0800 Subject: [PATCH] print log info --- - src/c/ebpf_collector/ebpf_collector.c | 25 ++++++++++++++++++++----- - 1 file changed, 20 insertions(+), 5 deletions(-) + src/c/ebpf_collector/ebpf_collector.c | 483 ++++++++++++++++++-------- + 1 file changed, 339 insertions(+), 144 deletions(-) diff --git a/src/c/ebpf_collector/ebpf_collector.c b/src/c/ebpf_collector/ebpf_collector.c -index af452c8..bbbca88 100644 +index af452c8..eecc4e3 100644 --- a/src/c/ebpf_collector/ebpf_collector.c +++ b/src/c/ebpf_collector/ebpf_collector.c -@@ -118,6 +118,7 @@ char* find_device_name(dev_t dev) { +@@ -4,15 +4,15 @@ + * Author: Zhang Nan + * Create: 2024-09-27 + */ +-#include ++#include + #include +-#include +-#include +-#include +-#include +-#include +-#include +-#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include + #include + #include + #include +@@ -22,68 +22,89 @@ + #include + #include "ebpf_collector.h" + +-#define BLK_MAP (map_fd[0]) +-#define BLK_RES (map_fd[1]) +-#define BIO_MAP (map_fd[2]) +-#define BIO_RES (map_fd[3]) +-#define WBT_MAP (map_fd[4]) +-#define WBT_RES (map_fd[5]) +-#define TAG_MAP (map_fd[7]) +-#define TAG_RES (map_fd[8]) +-#define BLK_RES_2 (map_fd[10]) +-#define BIO_RES_2 (map_fd[11]) +-#define WBT_RES_2 (map_fd[12]) +-#define TAG_RES_2 (map_fd[13]) +-#define BPF_FILE "/usr/lib/ebpf_collector.bpf.o" +- +-typedef struct { ++#define BLK_MAP (map_fd[0]) ++#define BLK_RES (map_fd[1]) ++#define BIO_MAP (map_fd[2]) ++#define BIO_RES (map_fd[3]) ++#define WBT_MAP (map_fd[4]) ++#define WBT_RES (map_fd[5]) ++#define TAG_MAP (map_fd[7]) ++#define TAG_RES (map_fd[8]) ++#define BLK_RES_2 (map_fd[10]) ++#define BIO_RES_2 (map_fd[11]) ++#define WBT_RES_2 (map_fd[12]) ++#define TAG_RES_2 (map_fd[13]) ++#define BPF_FILE "/usr/lib/ebpf_collector.bpf.o" ++ ++#define MAX_LINE_LENGTH 1024 ++#define MAX_SECTION_NAME_LENGTH 256 ++#define CONFIG_FILE "/etc/sysSentry/collector.conf" ++ ++typedef struct ++{ + int major; + int minor; + } DeviceInfo; + +-static volatile bool exiting; ++typedef enum ++{ ++ LOG_LEVEL_NONE, ++ LOG_LEVEL_DEBUG, ++ LOG_LEVEL_ERROR, ++ LOG_LEVEL_WARNING, ++ LOG_LEVEL_INFO ++} LogLevel; ++ ++LogLevel currentLogLevel = LOG_LEVEL_INFO; ++ ++static volatile bool exiting; + +-const char argp_program_doc[] = +-"Show block device I/O pattern.\n" +-"\n" +-"USAGE: ebpf_collector [--help]\n" +-"\n" +-"EXAMPLES:\n" +-" ebpf_collector # show block I/O pattern\n"; ++const char argp_program_doc[] = ++ "Show block device I/O pattern.\n" ++ "\n" ++ "USAGE: ebpf_collector [--help]\n" ++ "\n" ++ "EXAMPLES:\n" ++ " ebpf_collector # show block I/O pattern\n"; + + static const struct argp_option opts[] = { +- { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" }, ++ {NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help"}, + {}, + }; + +-static error_t parse_arg(int key, char *arg, struct argp_state *state) { +- static int pos_args; ++static error_t parse_arg(int key, char *arg, struct argp_state *state) ++{ ++ static int pos_args; + +- switch (key) { +- case 'h': +- argp_state_help(state, stderr, ARGP_HELP_STD_HELP); ++ switch (key) ++ { ++ case 'h': ++ argp_state_help(state, stderr, ARGP_HELP_STD_HELP); + break; +- default: +- return ARGP_ERR_UNKNOWN; ++ default: ++ return ARGP_ERR_UNKNOWN; + } +- return 0; ++ return 0; + } + + static void sig_handler(int sig) + { +- exiting = true; ++ exiting = true; + } + +-char* extract_device_name(const char *path) { ++char *extract_device_name(const char *path) ++{ + const char *dev_dir = "/dev/"; +- char *name = strrchr(path, '/') + 1; +- if (strncmp(dev_dir, path, strlen(dev_dir)) == 0) { +- return strdup(name); ++ char *name = strrchr(path, '/') + 1; ++ if (strncmp(dev_dir, path, strlen(dev_dir)) == 0) ++ { ++ return strdup(name); + } +- return NULL; ++ return NULL; } - static void update_io_dump(struct bpf_map *map_res, int *io_dump, int *map_size, char *stage) { -+ printf("ddddddddddd\n"); +-char* find_device_name(dev_t dev) { ++char *find_device_name(dev_t dev) ++{ + DIR *dir; + struct dirent *entry; + struct stat sb; +@@ -91,24 +112,29 @@ char* find_device_name(dev_t dev) { + char path[1024]; + + dir = opendir("/dev"); +- if (dir == NULL) { ++ if (dir == NULL) ++ { + perror("Failed to open /dev"); + return NULL; + } + +- while ((entry = readdir(dir)) != NULL) { ++ while ((entry = readdir(dir)) != NULL) ++ { + snprintf(path, sizeof(path), "/dev/%s", entry->d_name); + +- if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) { ++ if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) ++ { + continue; + } + +- if (stat(path, &sb) == -1) { +- continue; ++ if (stat(path, &sb) == -1) ++ { ++ continue; + } + +- if (major(sb.st_rdev) == major(dev) && minor(sb.st_rdev) == minor(dev)) { +- device_name = extract_device_name(path); ++ if (major(sb.st_rdev) == major(dev) && minor(sb.st_rdev) == minor(dev)) ++ { ++ device_name = extract_device_name(path); + break; + } + } +@@ -117,84 +143,108 @@ char* find_device_name(dev_t dev) { + return device_name; + } + +-static void update_io_dump(struct bpf_map *map_res, int *io_dump, int *map_size, char *stage) { ++static void update_io_dump(struct bpf_map *map_res, int *io_dump, int *map_size, char *stage) ++{ ++ logMessage(LOG_LEVEL_DEBUG, "start update io dump.\n"); struct time_range_io_count time_count; u32 io_dump_key = 0; - struct sysinfo info; -@@ -125,6 +126,7 @@ static void update_io_dump(struct bpf_map *map_res, int *io_dump, int *map_size, +- struct sysinfo info; ++ struct sysinfo info; + sysinfo(&info); int count_time = 150; u32 curr_time = info.uptime; - while (count_time >= 0) { -+ printf("ffffffffff\n"); +- while (count_time >= 0) { ++ while (count_time >= 0) ++ { ++ logMessage(LOG_LEVEL_DEBUG, "start count_time while.\n"); io_dump_key = curr_time - count_time; int err = bpf_map_lookup_elem(map_res, &io_dump_key, &time_count); - if (err < 0) { -@@ -144,15 +146,18 @@ static void update_io_dump(struct bpf_map *map_res, int *io_dump, int *map_size, - } +- if (err < 0) { +- count_time -= 1; +- continue; ++ if (err < 0) ++ { ++ count_time -= 1; ++ continue; + } +- if ((curr_time - io_dump_key) >= 2) { ++ if ((curr_time - io_dump_key) >= 2) ++ { + int isempty = 1; +- for (int key = 0; key < map_size; key++) { +- if (time_count.count[key] > 0) { ++ for (int key = 0; key < map_size; key++) ++ { ++ if (time_count.count[key] > 0) ++ { + io_dump[key] += time_count.count[key]; + isempty = 0; + } + } +- if (isempty || (curr_time - io_dump_key) > IO_DUMP_THRESHOLD) { ++ if (isempty || (curr_time - io_dump_key) > IO_DUMP_THRESHOLD) ++ { + bpf_map_delete_elem(map_res, &io_dump_key); +- } ++ } } count_time -= 1; -+ printf("eeeeeeeeeeee\n"); ++ logMessage(LOG_LEVEL_DEBUG, " finish count_time while.\n"); } } static int print_map_res(struct bpf_map *map_res, char *stage, int *map_size, int *io_dump) { -+ printf("fffffffffff\n"); - struct stage_data counter; +- struct stage_data counter; ++ logMessage(LOG_LEVEL_DEBUG, "enter print map.\n"); ++ struct stage_data counter; int key = 0; - for (key = 0; key < map_size; key++) { -+ printf("ggggggggggg\n"); - int err = bpf_map_lookup_elem(map_res, &key, &counter); - if (err < 0) { - fprintf(stderr, "failed to lookup %s map_res: %d\n", stage, err); -@@ -182,7 +187,7 @@ static int print_map_res(struct bpf_map *map_res, char *stage, int *map_size, in +- for (key = 0; key < map_size; key++) { +- int err = bpf_map_lookup_elem(map_res, &key, &counter); +- if (err < 0) { +- fprintf(stderr, "failed to lookup %s map_res: %d\n", stage, err); +- return -1; ++ for (key = 0; key < map_size; key++) ++ { ++ logMessage(LOG_LEVEL_DEBUG, "start print map for.\n"); ++ int err = bpf_map_lookup_elem(map_res, &key, &counter); ++ if (err < 0) ++ { ++ fprintf(stderr, "failed to lookup %s map_res: %d\n", stage, err); ++ return -1; + } +- ++ + size_t length = strlen(counter.io_type); + char io_type; +- if (length > 0) { ++ if (length > 0) ++ { ++ logMessage(LOG_LEVEL_DEBUG, "io_type have value.\n"); + io_type = counter.io_type[0]; +- } else { ++ } ++ else ++ { ++ logMessage(LOG_LEVEL_DEBUG, "io_type not value.\n"); + io_type = NULL; + } + int major = counter.major; + int first_minor = counter.first_minor; +- dev_t dev = makedev(major, first_minor); ++ dev_t dev = makedev(major, first_minor); + char *device_name = find_device_name(dev); +- if (device_name && io_type) { ++ logMessage(LOG_LEVEL_DEBUG, "device_name = %s\n", device_name); ++ if (device_name && io_type) ++ { + printf("%-7s %10llu %10llu %d %c %s\n", +- stage, +- counter.finish_count, +- counter.duration, +- io_dump[key], +- io_type, +- device_name +- ); ++ stage, ++ counter.finish_count, ++ counter.duration, ++ io_dump[key], ++ io_type, ++ device_name); fflush(stdout); } } -- -+printf("hhhhhhhhh\n"); - return 0; ++ logMessage(LOG_LEVEL_DEBUG, "finish print map for.\n"); + +- return 0; ++ return 0; } -@@ -227,10 +232,11 @@ int main(int argc, char **argv) { - return err; +-int init_map(int *map_fd, const char *map_name, int *map_size, DeviceInfo *devices) { ++int init_map(int *map_fd, const char *map_name, int *map_size, DeviceInfo *devices) ++{ + struct stage_data init_data = {0}; + + memset(init_data.io_type, 0, sizeof(init_data.io_type)); + +- for (int i = 0; i < map_size; i++) { ++ for (int i = 0; i < map_size; i++) ++ { + init_data.major = devices[i].major; + init_data.first_minor = devices[i].minor; +- if (bpf_map_update_elem(map_fd, &i, &init_data, BPF_ANY) != 0) { ++ if (bpf_map_update_elem(map_fd, &i, &init_data, BPF_ANY) != 0) ++ { + printf("Failed to initialize map %s at index %d\n", map_name, i); + return 1; + } +@@ -203,15 +253,113 @@ int init_map(int *map_fd, const char *map_name, int *map_size, DeviceInfo *devic + return 0; + } + +-int main(int argc, char **argv) { +- struct partitions *partitions = NULL; +- const struct partition *partition; +- static const struct argp argp = { +- .options = opts, +- .parser = parse_arg, +- .doc = argp_program_doc, ++char *read_config_value(const char *file, const char *section, const char *key) ++{ ++ FILE *fp = fopen(file, "r"); ++ if (fp == NULL) ++ { ++ perror("Failed to open config file"); ++ return NULL; ++ } ++ ++ char line[MAX_LINE_LENGTH]; ++ char current_section[MAX_SECTION_NAME_LENGTH] = {0}; ++ char *value = NULL; ++ ++ while (fgets(line, sizeof(line), fp) != NULL) ++ { ++ line[strcspn(line, "\n")] = 0; ++ ++ if (line[0] == '\0' || line[0] == ';' || line[0] == '#') ++ { ++ continue; ++ } ++ ++ if (line[0] == '[') ++ { ++ sscanf(line, "[%[^]]", current_section); ++ continue; ++ } ++ ++ if (strcmp(current_section, section) == 0) ++ { ++ char *delimiter = "="; ++ char *token = strtok(line, delimiter); ++ if (token != NULL) ++ { ++ if (strcmp(token, key) == 0) ++ { ++ token = strtok(NULL, delimiter); ++ if (token != NULL) ++ { ++ value = strdup(token); ++ break; ++ } ++ } ++ } ++ } ++ } ++ ++ fclose(fp); ++ return value; ++} ++ ++void setLogLevel(const char *levelStr) ++{ ++ if (strcmp(levelStr, "info") == 0) ++ { ++ currentLogLevel = LOG_LEVEL_INFO; ++ } ++ else if (strcmp(levelStr, "warning") == 0) ++ { ++ currentLogLevel = LOG_LEVEL_WARNING; ++ } ++ else if (strcmp(levelStr, "error") == 0) ++ { ++ currentLogLevel = LOG_LEVEL_ERROR; ++ } ++ else if (strcmp(levelStr, "debug") == 0) ++ { ++ currentLogLevel = LOG_LEVEL_DEBUG; ++ } ++ else if (strcmp(levelStr, "none") == 0) ++ { ++ currentLogLevel = LOG_LEVEL_NONE; ++ } ++ else ++ { ++ fprintf(stderr, "Unknown log level: %s\n", levelStr); ++ } ++} ++ ++void logMessage2(LogLevel level, const char *message) ++{ ++ if (level >= currentLogLevel) ++ { ++ printf("%s\n", message); ++ } ++} ++ ++void logMessage(LogLevel level, const char *format, ...) ++{ ++ va_list args; ++ if (level >= currentLogLevel) { ++ va_start(args, format); ++ vprintf(format, args); ++ va_end(args); ++ } ++} ++ ++int main(int argc, char **argv) ++{ ++ struct partitions *partitions = NULL; ++ const struct partition *partition; ++ static const struct argp argp = { ++ .options = opts, ++ .parser = parse_arg, ++ .doc = argp_program_doc, + }; +- int err; ++ int err; + char filename[256]; + DIR *dir; + struct dirent *entry; +@@ -219,37 +367,60 @@ int main(int argc, char **argv) { + int major, minor; + DeviceInfo devices[MAP_SIZE]; + int device_count = 0; +- struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; +- setrlimit(RLIMIT_MEMLOCK, &r); ++ struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; ++ setrlimit(RLIMIT_MEMLOCK, &r); ++ ++ char *level = read_config_value(CONFIG_FILE, "log", "level"); ++ if (level != NULL) ++ { ++ setLogLevel(level); ++ ; ++ free(level); ++ } + +- err = argp_parse(&argp, argc, argv, 0, NULL, NULL); +- if (err) +- return err; ++ err = argp_parse(&argp, argc, argv, 0, NULL, NULL); ++ if (err) ++ return err; snprintf(filename, sizeof(filename), BPF_FILE); -- -+ printf("load_bpf_file before\n"); - if (load_bpf_file(filename)) { - return 1; + +- if (load_bpf_file(filename)) { +- return 1; ++ logMessage(LOG_LEVEL_DEBUG, " load_bpf_file before.\n"); ++ if (load_bpf_file(filename)) ++ { ++ return 1; } -+ printf("load_bpf_file after\n"); ++ logMessage(LOG_LEVEL_DEBUG, " load_bpf_file after.\n"); signal(SIGINT, sig_handler); -@@ -239,17 +245,22 @@ int main(int argc, char **argv) { + dir = opendir("/dev"); +- if (dir == NULL) { ++ if (dir == NULL) ++ { printf("Failed to open /dev directory"); return EXIT_FAILURE; } -- -+ printf("11111111\n"); - while ((entry = readdir(dir)) != NULL) { -+ printf("2222222\n"); - if (entry->d_type == DT_BLK) { -+ printf("3333333\n"); + +- while ((entry = readdir(dir)) != NULL) { +- if (entry->d_type == DT_BLK) { ++ logMessage(LOG_LEVEL_DEBUG, " start readdir while.\n"); ++ while ((entry = readdir(dir)) != NULL) ++ { ++ logMessage(LOG_LEVEL_DEBUG, " enter readdir while.\n"); ++ if (entry->d_type == DT_BLK) ++ { ++ logMessage(LOG_LEVEL_DEBUG, " enter DT_BLK if.\n"); snprintf(path, sizeof(path), "/dev/%s", entry->d_name); struct stat statbuf; - if (lstat(path, &statbuf) == 0) { -+ printf("4444444444\n"); - if (S_ISBLK(statbuf.st_mode)) { -+ printf("555555555555\n"); +- if (lstat(path, &statbuf) == 0) { +- if (S_ISBLK(statbuf.st_mode)) { ++ if (lstat(path, &statbuf) == 0) ++ { ++ logMessage(LOG_LEVEL_DEBUG, " enter lstat if.\n"); ++ if (S_ISBLK(statbuf.st_mode)) ++ { ++ logMessage(LOG_LEVEL_DEBUG, " enter S_ISBLK if.\n"); devices[device_count].major = major(statbuf.st_rdev); devices[device_count].minor = minor(statbuf.st_rdev); device_count++; - if (device_count >= MAP_SIZE) { -+ printf("66666666666666\n"); +- if (device_count >= MAP_SIZE) { ++ if (device_count >= MAP_SIZE) ++ { ++ logMessage(LOG_LEVEL_DEBUG, " enter map_size if.\n"); break; } } -@@ -258,22 +269,26 @@ int main(int argc, char **argv) { - } +@@ -259,51 +430,75 @@ int main(int argc, char **argv) { closedir(dir); -- -+printf("7777777777777\n"); - if (init_map(BLK_RES, "blk_res_map", device_count, devices) != 0) { + +- if (init_map(BLK_RES, "blk_res_map", device_count, devices) != 0) { ++ logMessage(LOG_LEVEL_DEBUG, " start blk init.\n"); ++ if (init_map(BLK_RES, "blk_res_map", device_count, devices) != 0) ++ { return 1; } -+ printf("888888888\n"); - if (init_map(BIO_RES, "blo_res_map", device_count, devices) != 0) { +- if (init_map(BIO_RES, "blo_res_map", device_count, devices) != 0) { ++ logMessage(LOG_LEVEL_DEBUG, " finish blk init.\n"); ++ ++ logMessage(LOG_LEVEL_DEBUG, " start bio init.\n"); ++ if (init_map(BIO_RES, "blo_res_map", device_count, devices) != 0) ++ { return 1; } -+ printf("9999999999\n"); - if (init_map(WBT_RES, "wbt_res_map", device_count, devices) != 0) { +- if (init_map(WBT_RES, "wbt_res_map", device_count, devices) != 0) { ++ logMessage(LOG_LEVEL_DEBUG, " finish bio init.\n"); ++ ++ logMessage(LOG_LEVEL_DEBUG, " start wbt init.\n"); ++ if (init_map(WBT_RES, "wbt_res_map", device_count, devices) != 0) ++ { return 1; } -+ printf("aaaaaaaaaa\n"); - if (init_map(TAG_RES, "tag_res_map", device_count, devices) != 0) { +- if (init_map(TAG_RES, "tag_res_map", device_count, devices) != 0) { ++ logMessage(LOG_LEVEL_DEBUG, " finish wbt init.\n"); ++ ++ logMessage(LOG_LEVEL_DEBUG, " start tag init.\n"); ++ if (init_map(TAG_RES, "tag_res_map", device_count, devices) != 0) ++ { return 1; } -+ printf("bbbbbbbbbbbb\n"); ++ logMessage(LOG_LEVEL_DEBUG, "finish tag init.\n"); ++ ++ for (;;) ++ { ++ ++ logMessage(LOG_LEVEL_DEBUG, " enter final for.\n"); ++ sleep(1); ++ ++ logMessage(LOG_LEVEL_DEBUG, " start print blk.\n"); ++ int io_dump_blk[MAP_SIZE] = {0}; ++ update_io_dump(BLK_RES_2, io_dump_blk, device_count, "rq_driver"); ++ err = print_map_res(BLK_RES, "rq_driver", device_count, io_dump_blk); ++ if (err) ++ break; ++ logMessage(LOG_LEVEL_DEBUG, " finish print blk.\n"); ++ ++ logMessage(LOG_LEVEL_DEBUG, " start print bio.\n"); ++ int io_dump_bio[MAP_SIZE] = {0}; ++ update_io_dump(BIO_RES_2, io_dump_bio, device_count, "bio"); ++ err = print_map_res(BIO_RES, "bio", device_count, io_dump_bio); ++ if (err) ++ break; ++ logMessage(LOG_LEVEL_DEBUG, " finish print bio.\n"); ++ ++ logMessage(LOG_LEVEL_DEBUG, " start print tag.\n"); ++ int io_dump_tag[MAP_SIZE] = {0}; ++ update_io_dump(TAG_RES_2, io_dump_tag, device_count, "gettag"); ++ err = print_map_res(TAG_RES, "gettag", device_count, io_dump_tag); ++ if (err) ++ break; ++ logMessage(LOG_LEVEL_DEBUG, " finish print tag.\n"); - for (;;) { +- for (;;) { - -+ printf("ccccccccccc\n"); - sleep(1); +- sleep(1); +- +- int io_dump_blk[MAP_SIZE] = {0}; +- update_io_dump(BLK_RES_2, io_dump_blk, device_count,"rq_driver"); +- err = print_map_res(BLK_RES, "rq_driver", device_count, io_dump_blk); +- if (err) +- break; +- +- int io_dump_bio[MAP_SIZE] = {0}; +- update_io_dump(BIO_RES_2, io_dump_bio, device_count,"bio"); +- err = print_map_res(BIO_RES, "bio", device_count, io_dump_bio); +- if (err) +- break; +- +- int io_dump_tag[MAP_SIZE] = {0}; +- update_io_dump(TAG_RES_2, io_dump_tag, device_count,"gettag"); +- err = print_map_res(TAG_RES, "gettag", device_count, io_dump_tag); +- if (err) +- break; +- +- int io_dump_wbt[MAP_SIZE] = {0}; +- update_io_dump(WBT_RES_2, io_dump_wbt, device_count,"wbt"); +- err = print_map_res(WBT_RES, "wbt", device_count, io_dump_wbt); +- if (err) +- break; +- +- if (exiting) +- break; ++ logMessage(LOG_LEVEL_DEBUG, " start print wbt.\n"); ++ int io_dump_wbt[MAP_SIZE] = {0}; ++ update_io_dump(WBT_RES_2, io_dump_wbt, device_count, "wbt"); ++ err = print_map_res(WBT_RES, "wbt", device_count, io_dump_wbt); ++ if (err) ++ break; ++ logMessage(LOG_LEVEL_DEBUG, " finish print wbt.\n"); ++ ++ if (exiting) ++ break; + } - int io_dump_blk[MAP_SIZE] = {0}; +- return -err; ++ return -err; + } +- -- 2.33.0 diff --git a/sysSentry.spec b/sysSentry.spec index d0eb909..b9ce0e0 100644 --- a/sysSentry.spec +++ b/sysSentry.spec @@ -4,7 +4,7 @@ Summary: System Inspection Framework Name: sysSentry Version: 1.0.2 -Release: 68 +Release: 69 License: Mulan PSL v2 Group: System Environment/Daemons Source0: https://gitee.com/openeuler/sysSentry/releases/download/v%{version}/%{name}-%{version}.tar.gz -- Gitee From a8759ce986bdebfa71195eed047c10a8eb81b403 Mon Sep 17 00:00:00 2001 From: zhuofeng <1107893276@qq.com> Date: Tue, 19 Nov 2024 03:12:38 +0000 Subject: [PATCH 4/9] update Signed-off-by: zhuofeng <1107893276@qq.com> --- 0001-print-log-info.patch | 42 ++++++++++++++++++++++----------------- sysSentry.spec | 2 +- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/0001-print-log-info.patch b/0001-print-log-info.patch index 41f66e6..963a42e 100644 --- a/0001-print-log-info.patch +++ b/0001-print-log-info.patch @@ -1,22 +1,23 @@ -From 41592c9e3f6ddd11bf149505e989b2b1119e8121 Mon Sep 17 00:00:00 2001 +From 90ab9ddb2e6ab35575b5dfd93301b99d2bcc0305 Mon Sep 17 00:00:00 2001 From: Your Name -Date: Tue, 19 Nov 2024 10:29:50 +0800 +Date: Tue, 19 Nov 2024 11:08:57 +0800 Subject: [PATCH] print log info --- - src/c/ebpf_collector/ebpf_collector.c | 483 ++++++++++++++++++-------- - 1 file changed, 339 insertions(+), 144 deletions(-) + src/c/ebpf_collector/ebpf_collector.c | 489 ++++++++++++++++++-------- + 1 file changed, 345 insertions(+), 144 deletions(-) diff --git a/src/c/ebpf_collector/ebpf_collector.c b/src/c/ebpf_collector/ebpf_collector.c -index af452c8..eecc4e3 100644 +index af452c8..84890a5 100644 --- a/src/c/ebpf_collector/ebpf_collector.c +++ b/src/c/ebpf_collector/ebpf_collector.c -@@ -4,15 +4,15 @@ +@@ -4,15 +4,16 @@ * Author: Zhang Nan * Create: 2024-09-27 */ -#include +#include ++#include #include -#include -#include @@ -35,7 +36,7 @@ index af452c8..eecc4e3 100644 #include #include #include -@@ -22,68 +22,89 @@ +@@ -22,68 +23,89 @@ #include #include "ebpf_collector.h" @@ -164,7 +165,7 @@ index af452c8..eecc4e3 100644 DIR *dir; struct dirent *entry; struct stat sb; -@@ -91,24 +112,29 @@ char* find_device_name(dev_t dev) { +@@ -91,24 +113,29 @@ char* find_device_name(dev_t dev) { char path[1024]; dir = opendir("/dev"); @@ -201,7 +202,7 @@ index af452c8..eecc4e3 100644 break; } } -@@ -117,84 +143,108 @@ char* find_device_name(dev_t dev) { +@@ -117,84 +144,108 @@ char* find_device_name(dev_t dev) { return device_name; } @@ -343,7 +344,7 @@ index af452c8..eecc4e3 100644 printf("Failed to initialize map %s at index %d\n", map_name, i); return 1; } -@@ -203,15 +253,113 @@ int init_map(int *map_fd, const char *map_name, int *map_size, DeviceInfo *devic +@@ -203,15 +254,113 @@ int init_map(int *map_fd, const char *map_name, int *map_size, DeviceInfo *devic return 0; } @@ -437,7 +438,7 @@ index af452c8..eecc4e3 100644 +{ + if (level >= currentLogLevel) + { -+ printf("%s\n", message); ++ printf("%s", message); + } +} + @@ -465,7 +466,7 @@ index af452c8..eecc4e3 100644 char filename[256]; DIR *dir; struct dirent *entry; -@@ -219,37 +367,60 @@ int main(int argc, char **argv) { +@@ -219,37 +368,65 @@ int main(int argc, char **argv) { int major, minor; DeviceInfo devices[MAP_SIZE]; int device_count = 0; @@ -477,14 +478,19 @@ index af452c8..eecc4e3 100644 + char *level = read_config_value(CONFIG_FILE, "log", "level"); + if (level != NULL) + { -+ setLogLevel(level); -+ ; -+ free(level); -+ } ++ if (level[strlen(level) - 1] == '\r') { ++ size_t len = strlen(level); ++ level[len - 1] = '\0'; ++ } - err = argp_parse(&argp, argc, argv, 0, NULL, NULL); - if (err) - return err; ++ setLogLevel(level); ++ ; ++ free(level); ++ } ++ + err = argp_parse(&argp, argc, argv, 0, NULL, NULL); + if (err) + return err; @@ -539,7 +545,7 @@ index af452c8..eecc4e3 100644 break; } } -@@ -259,51 +430,75 @@ int main(int argc, char **argv) { +@@ -259,51 +436,75 @@ int main(int argc, char **argv) { closedir(dir); @@ -652,5 +658,5 @@ index af452c8..eecc4e3 100644 } - -- -2.33.0 +2.27.0 diff --git a/sysSentry.spec b/sysSentry.spec index b9ce0e0..d0eb909 100644 --- a/sysSentry.spec +++ b/sysSentry.spec @@ -4,7 +4,7 @@ Summary: System Inspection Framework Name: sysSentry Version: 1.0.2 -Release: 69 +Release: 68 License: Mulan PSL v2 Group: System Environment/Daemons Source0: https://gitee.com/openeuler/sysSentry/releases/download/v%{version}/%{name}-%{version}.tar.gz -- Gitee From 367255e480cea9ea3aba2e138ab58562ebfac4a3 Mon Sep 17 00:00:00 2001 From: zhuofeng <1107893276@qq.com> Date: Tue, 19 Nov 2024 06:46:59 +0000 Subject: [PATCH 5/9] update2 Signed-off-by: zhuofeng <1107893276@qq.com> --- 0001-print-log-info.patch | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/0001-print-log-info.patch b/0001-print-log-info.patch index 963a42e..37d566f 100644 --- a/0001-print-log-info.patch +++ b/0001-print-log-info.patch @@ -1,14 +1,14 @@ -From 90ab9ddb2e6ab35575b5dfd93301b99d2bcc0305 Mon Sep 17 00:00:00 2001 +From 8a1c95e596198545a386b5b8904d77db728f0e38 Mon Sep 17 00:00:00 2001 From: Your Name -Date: Tue, 19 Nov 2024 11:08:57 +0800 -Subject: [PATCH] print log info +Date: Tue, 19 Nov 2024 14:45:02 +0800 +Subject: [PATCH] print-log-info --- - src/c/ebpf_collector/ebpf_collector.c | 489 ++++++++++++++++++-------- - 1 file changed, 345 insertions(+), 144 deletions(-) + src/c/ebpf_collector/ebpf_collector.c | 491 ++++++++++++++++++-------- + 1 file changed, 346 insertions(+), 145 deletions(-) diff --git a/src/c/ebpf_collector/ebpf_collector.c b/src/c/ebpf_collector/ebpf_collector.c -index af452c8..84890a5 100644 +index af452c8..fdbd2b4 100644 --- a/src/c/ebpf_collector/ebpf_collector.c +++ b/src/c/ebpf_collector/ebpf_collector.c @@ -4,15 +4,16 @@ @@ -288,11 +288,12 @@ index af452c8..84890a5 100644 + logMessage(LOG_LEVEL_DEBUG, "io_type have value.\n"); io_type = counter.io_type[0]; - } else { +- io_type = NULL; + } + else + { + logMessage(LOG_LEVEL_DEBUG, "io_type not value.\n"); - io_type = NULL; ++ io_type = "W"; } int major = counter.major; int first_minor = counter.first_minor; -- Gitee From 6af95a0a36fb7127d3c96524e0838cbf4b170990 Mon Sep 17 00:00:00 2001 From: zhuofeng <1107893276@qq.com> Date: Tue, 19 Nov 2024 07:12:05 +0000 Subject: [PATCH 6/9] update3 Signed-off-by: zhuofeng <1107893276@qq.com> --- 0001-print-log-info.patch | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/0001-print-log-info.patch b/0001-print-log-info.patch index 37d566f..5872fec 100644 --- a/0001-print-log-info.patch +++ b/0001-print-log-info.patch @@ -1,12 +1,3 @@ -From 8a1c95e596198545a386b5b8904d77db728f0e38 Mon Sep 17 00:00:00 2001 -From: Your Name -Date: Tue, 19 Nov 2024 14:45:02 +0800 -Subject: [PATCH] print-log-info - ---- - src/c/ebpf_collector/ebpf_collector.c | 491 ++++++++++++++++++-------- - 1 file changed, 346 insertions(+), 145 deletions(-) - diff --git a/src/c/ebpf_collector/ebpf_collector.c b/src/c/ebpf_collector/ebpf_collector.c index af452c8..fdbd2b4 100644 --- a/src/c/ebpf_collector/ebpf_collector.c @@ -658,6 +649,3 @@ index af452c8..fdbd2b4 100644 + return -err; } - --- -2.27.0 - -- Gitee From 696ba48d5cdbbda969c3e87c556fa4427e3981fb Mon Sep 17 00:00:00 2001 From: zhuofeng <1107893276@qq.com> Date: Tue, 19 Nov 2024 07:22:32 +0000 Subject: [PATCH 7/9] update4 Signed-off-by: zhuofeng <1107893276@qq.com> --- 0001-print-log-info.patch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0001-print-log-info.patch b/0001-print-log-info.patch index 5872fec..d826629 100644 --- a/0001-print-log-info.patch +++ b/0001-print-log-info.patch @@ -284,7 +284,7 @@ index af452c8..fdbd2b4 100644 + else + { + logMessage(LOG_LEVEL_DEBUG, "io_type not value.\n"); -+ io_type = "W"; ++ io_type = 'W'; } int major = counter.major; int first_minor = counter.first_minor; -- Gitee From 69795237d3fddfe142c2198f9026e8f38e0fc82c Mon Sep 17 00:00:00 2001 From: zhuofeng <1107893276@qq.com> Date: Tue, 19 Nov 2024 08:54:18 +0000 Subject: [PATCH 8/9] update5 Signed-off-by: zhuofeng <1107893276@qq.com> --- 0001-print-log-info.patch | 404 +++++++++++++++++++++++++++++++++++++- sysSentry.spec | 2 +- 2 files changed, 404 insertions(+), 2 deletions(-) diff --git a/0001-print-log-info.patch b/0001-print-log-info.patch index d826629..e833730 100644 --- a/0001-print-log-info.patch +++ b/0001-print-log-info.patch @@ -1,5 +1,407 @@ +diff --git a/src/c/ebpf_collector/ebpf_collector.bpf.c b/src/c/ebpf_collector/ebpf_collector.bpf.c +index a7ea51b..7b364e2 100644 +--- a/src/c/ebpf_collector/ebpf_collector.bpf.c ++++ b/src/c/ebpf_collector/ebpf_collector.bpf.c +@@ -836,12 +836,14 @@ u32 find_matching_wbt_5_keys(int major, int minor) { + SEC("kprobe/blk_mq_start_request") + int kprobe_blk_mq_start_request(struct pt_regs *regs) + { ++ bpf_printk("rq_driver start1"); + struct request_kylin *rq = (struct request_kylin *)PT_REGS_PARM1(regs); + struct gendisk *curr_rq_disk = _(rq->rq_disk); + int major = _(curr_rq_disk->major); + int first_minor = _(curr_rq_disk->first_minor); + unsigned int cmd_flags = _(rq->cmd_flags); + ++ bpf_printk("rq_driver start cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); + struct io_counter *counterp, zero = {}; + + u32 key = find_matching_blk_1_keys(major, first_minor); +@@ -854,6 +856,7 @@ int kprobe_blk_mq_start_request(struct pt_regs *regs) + if (key >= MAP_SIZE){ + key = find_matching_blk_5_keys(major, first_minor); + if (key >= MAP_SIZE){ ++ bpf_printk("rq_driver start return 1"); + return 0; + } + } +@@ -864,10 +867,12 @@ int kprobe_blk_mq_start_request(struct pt_regs *regs) + + counterp = bpf_map_lookup_elem(&blk_map, &rq); + if (counterp || major == 0) { ++ bpf_printk("rq_driver start return 2"); + return 0; + } + long err = bpf_map_update_elem(&blk_map, &rq, &zero, BPF_NOEXIST); + if (err) { ++ bpf_printk("rq_driver start return 3"); + return 0; + } + +@@ -908,7 +913,7 @@ int kprobe_blk_mq_start_request(struct pt_regs *regs) + __sync_fetch_and_add(&curr_data_time_range->count[key], 1); + } + } +- ++ bpf_printk("rq_driver start2"); + return 0; + } + +@@ -916,11 +921,13 @@ int kprobe_blk_mq_start_request(struct pt_regs *regs) + SEC("kprobe/blk_mq_free_request") + int kprobe_blk_mq_free_request(struct pt_regs *regs) + { ++ bpf_printk("rq_driver end1"); + struct request_kylin *rq = (struct request_kylin *)PT_REGS_PARM1(regs); + struct gendisk *curr_rq_disk = _(rq->rq_disk); + int major = _(curr_rq_disk->major); + int first_minor = _(curr_rq_disk->first_minor); + unsigned int cmd_flags = _(rq->cmd_flags); ++ bpf_printk("rq_driver end cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); + + struct io_counter *counterp; + u32 key = find_matching_blk_1_keys(major, first_minor); +@@ -933,6 +940,7 @@ int kprobe_blk_mq_free_request(struct pt_regs *regs) + if (key >= MAP_SIZE){ + key = find_matching_blk_5_keys(major, first_minor); + if (key >= MAP_SIZE){ ++ bpf_printk("rq_driver end return 1"); + return 0; + } + } +@@ -943,6 +951,7 @@ int kprobe_blk_mq_free_request(struct pt_regs *regs) + counterp = bpf_map_lookup_elem(&blk_map, &rq); + + if (!counterp) { ++ bpf_printk("rq_driver end return 2"); + return 0; + } + +@@ -999,6 +1008,7 @@ int kprobe_blk_mq_free_request(struct pt_regs *regs) + } + + bpf_map_delete_elem(&blk_map, &rq); ++ bpf_printk("rq_driver end2"); + return 0; + } + +@@ -1006,12 +1016,14 @@ int kprobe_blk_mq_free_request(struct pt_regs *regs) + SEC("kprobe/blk_mq_make_request") + int kprobe_blk_mq_make_request(struct pt_regs *regs) + { ++ bpf_printk("bio start1"); + struct bio *bio = (struct bio *)PT_REGS_PARM2(regs); + struct gendisk *curr_rq_disk = _(bio->bi_disk); + int major = _(curr_rq_disk->major); + int first_minor = _(curr_rq_disk->first_minor); + unsigned int cmd_flags = _(bio->bi_opf); + ++ bpf_printk("bio start cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); + struct io_counter *counterp, zero = {}; + u32 key = find_matching_bio_1_keys(major, first_minor); + if (key >= MAP_SIZE){ +@@ -1023,6 +1035,7 @@ int kprobe_blk_mq_make_request(struct pt_regs *regs) + if (key >= MAP_SIZE){ + key = find_matching_bio_5_keys(major, first_minor); + if (key >= MAP_SIZE){ ++ bpf_printk("bio start return 1"); + return 0; + } + } +@@ -1033,12 +1046,17 @@ int kprobe_blk_mq_make_request(struct pt_regs *regs) + init_io_counter(&zero, major, first_minor); + + counterp = bpf_map_lookup_elem(&bio_map, &bio); +- if (counterp || major == 0) ++ if (counterp || major == 0) { ++ bpf_printk("bio start return 2"); + return 0; ++ } + + long err = bpf_map_update_elem(&bio_map, &bio, &zero, BPF_NOEXIST); +- if (err && err != -EEXIST) ++ if (err && err != -EEXIST) { ++ bpf_printk("bio start return 3"); + return 0; ++ } ++ + + u64 curr_start_range = zero.start_time / THRESHOLD; + +@@ -1077,6 +1095,7 @@ int kprobe_blk_mq_make_request(struct pt_regs *regs) + __sync_fetch_and_add(&curr_data_time_range->count[key], 1); + } + } ++ bpf_printk("bio start2"); + + return 0; + } +@@ -1085,12 +1104,15 @@ int kprobe_blk_mq_make_request(struct pt_regs *regs) + SEC("kprobe/bio_endio") + int kprobe_bio_endio(struct pt_regs *regs) + { ++ bpf_printk("bio end1"); + struct bio *bio = (struct bio *)PT_REGS_PARM1(regs); + struct gendisk *curr_rq_disk = _(bio->bi_disk); + int major = _(curr_rq_disk->major); + int first_minor = _(curr_rq_disk->first_minor); + unsigned int cmd_flags = _(bio->bi_opf); + ++ bpf_printk("bio end cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); ++ + struct io_counter *counterp; + void *delete_map = NULL; + u32 key = find_matching_bio_1_keys(major, first_minor); +@@ -1103,6 +1125,7 @@ int kprobe_bio_endio(struct pt_regs *regs) + if (key >= MAP_SIZE){ + key = find_matching_bio_5_keys(major, first_minor); + if (key >= MAP_SIZE){ ++ bpf_printk("bio end return 1"); + return 0; + } + } +@@ -1113,6 +1136,7 @@ int kprobe_bio_endio(struct pt_regs *regs) + counterp = bpf_map_lookup_elem(&bio_map, &bio); + + if (!counterp) { ++ bpf_printk("bio end return 2"); + return 0; + } + +@@ -1171,6 +1195,7 @@ int kprobe_bio_endio(struct pt_regs *regs) + } + + bpf_map_delete_elem(delete_map, &bio); ++ bpf_printk("bio end2"); + return 0; + } + +@@ -1178,6 +1203,7 @@ int kprobe_bio_endio(struct pt_regs *regs) + SEC("kprobe/wbt_wait") + int kprobe_wbt_wait(struct pt_regs *regs) + { ++ bpf_printk("wbt start1"); + u64 wbtkey = bpf_get_current_task(); + u64 value = (u64)PT_REGS_PARM2(regs); + (void)bpf_map_update_elem(&wbt_args, &wbtkey, &value, BPF_ANY); +@@ -1187,6 +1213,8 @@ int kprobe_wbt_wait(struct pt_regs *regs) + int first_minor = _(curr_rq_disk->first_minor); + unsigned int cmd_flags = _(bio->bi_opf); + ++ bpf_printk("wbt start cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); ++ + struct io_counter *counterp, zero = {}; + u32 key = find_matching_wbt_1_keys(major, first_minor); + if (key >= MAP_SIZE){ +@@ -1198,6 +1226,7 @@ int kprobe_wbt_wait(struct pt_regs *regs) + if (key >= MAP_SIZE){ + key = find_matching_wbt_5_keys(major, first_minor); + if (key >= MAP_SIZE){ ++ bpf_printk("wbt start return 1"); + return 0; + } + } +@@ -1209,11 +1238,16 @@ int kprobe_wbt_wait(struct pt_regs *regs) + + counterp = bpf_map_lookup_elem(&wbt_map, &wbtkey); + +- if (counterp || major == 0) ++ if (counterp || major == 0) { ++ bpf_printk("wbt start return 2"); + return 0; ++ } ++ + long err = bpf_map_update_elem(&wbt_map, &wbtkey, &zero, BPF_NOEXIST); +- if (err) ++ if (err) { ++ bpf_printk("wbt start return 3"); + return 0; ++ } + + u64 curr_start_range = zero.start_time / THRESHOLD; + +@@ -1252,7 +1286,7 @@ int kprobe_wbt_wait(struct pt_regs *regs) + __sync_fetch_and_add(&curr_data_time_range->count[key], 1); + } + } +- ++ bpf_printk("wbt start2"); + return 0; + } + +@@ -1260,12 +1294,14 @@ int kprobe_wbt_wait(struct pt_regs *regs) + SEC("kretprobe/wbt_wait") + int kretprobe_wbt_wait(struct pt_regs *regs) + { ++ bpf_printk("wbt end1"); + struct bio *bio = NULL; + u64 *wbtargs = NULL; + u64 wbtkey = bpf_get_current_task(); + wbtargs = (u64 *)bpf_map_lookup_elem(&wbt_args, &wbtkey); + if (wbtargs == NULL) { + bpf_map_delete_elem(&wbt_args, &wbtkey); ++ bpf_printk("wbt end return 1"); + return 0; + } + bio = (struct bio *)(*wbtargs); +@@ -1273,6 +1309,8 @@ int kretprobe_wbt_wait(struct pt_regs *regs) + int major = _(curr_rq_disk->major); + int first_minor = _(curr_rq_disk->first_minor); + unsigned int cmd_flags = _(bio->bi_opf); ++ ++ bpf_printk("wbt end cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); + + struct io_counter *counterp; + u32 key = find_matching_wbt_1_keys(major, first_minor); +@@ -1285,6 +1323,7 @@ int kretprobe_wbt_wait(struct pt_regs *regs) + if (key >= MAP_SIZE){ + key = find_matching_wbt_5_keys(major, first_minor); + if (key >= MAP_SIZE){ ++ bpf_printk("wbt end return 2"); + return 0; + } + } +@@ -1294,8 +1333,10 @@ int kretprobe_wbt_wait(struct pt_regs *regs) + + counterp = bpf_map_lookup_elem(&wbt_map, &wbtkey); + +- if (!counterp) ++ if (!counterp){ ++ bpf_printk("wbt end return 3"); + return 0; ++ } + + u64 duration = bpf_ktime_get_ns() - counterp->start_time; + u64 curr_start_range = counterp->start_time / THRESHOLD; +@@ -1351,6 +1392,7 @@ int kretprobe_wbt_wait(struct pt_regs *regs) + + bpf_map_delete_elem(&wbt_map, &wbtkey); + bpf_map_delete_elem(&wbt_args, &wbtkey); ++ bpf_printk("wbt end2"); + return 0; + } + +@@ -1358,17 +1400,19 @@ int kretprobe_wbt_wait(struct pt_regs *regs) + SEC("kprobe/blk_mq_get_tag") + int kprobe_blk_mq_get_tag(struct pt_regs *regs) + { ++ bpf_printk("get_tag start1"); + u64 tagkey = bpf_get_current_task(); + u64 value = (u64)PT_REGS_PARM1(regs); + (void)bpf_map_update_elem(&tag_args, &tagkey, &value, BPF_ANY); + struct blk_mq_alloc_data *bd= (struct blk_mq_alloc_data *)value; + struct request_queue *q = _(bd->q); +- struct backing_dev_info *backing_dev_info = _(q->backing_dev_info); ++ struct backing_dev_info *backing_dev_info = _(q->backing_dev_info); + struct device *owner = _(backing_dev_info->owner); + dev_t devt = _(owner->devt); + int major = MAJOR(devt); + int first_minor = MINOR(devt); + unsigned int cmd_flags = 0; ++ bpf_printk("get_tag start cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); + + struct io_counter *counterp, zero = {}; + u32 key = find_matching_tag_1_keys(major, first_minor); +@@ -1381,6 +1425,7 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) + if (key >= MAP_SIZE){ + key = find_matching_tag_5_keys(major, first_minor); + if (key >= MAP_SIZE){ ++ bpf_printk("get_tag start return 1"); + return 0; + } + } +@@ -1391,11 +1436,16 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) + init_io_counter(&zero, major, first_minor); + + counterp = bpf_map_lookup_elem(&tag_map, &tagkey); +- if (counterp || major == 0) ++ if (counterp || major == 0) { ++ bpf_printk("get_tag start return 2"); + return 0; ++ } ++ + long err = bpf_map_update_elem(&tag_map, &tagkey, &zero, BPF_NOEXIST); +- if (err) ++ if (err) { ++ bpf_printk("get_tag start return 3"); + return 0; ++ } + + u64 curr_start_range = zero.start_time / THRESHOLD; + +@@ -1434,7 +1484,7 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) + __sync_fetch_and_add(&curr_data_time_range->count[key], 1); + } + } +- ++ bpf_printk("get_tag start2"); + return 0; + } + +@@ -1442,6 +1492,7 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) + SEC("kretprobe/blk_mq_get_tag") + int kretprobe_blk_mq_get_tag(struct pt_regs *regs) + { ++ bpf_printk("get_tag end1"); + u64 tagkey = bpf_get_current_task(); + u64 *tagargs = NULL; + struct blk_mq_alloc_data *bd = NULL; +@@ -1449,17 +1500,20 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) + tagargs = (u64 *)bpf_map_lookup_elem(&tag_args, &tagkey); + if (tagargs == NULL) { + bpf_map_delete_elem(&tag_args, &tagkey); +- return 0; ++ bpf_printk("get_tag end return 1"); ++ return 0; + } + bd = (struct blk_mq_alloc_data *)(*tagargs); + struct request_queue *q = _(bd->q); +- struct backing_dev_info *backing_dev_info = _(q->backing_dev_info); ++ struct backing_dev_info *backing_dev_info = _(q->backing_dev_info); + struct device *owner = _(backing_dev_info->owner); + dev_t devt = _(owner->devt); + int major = MAJOR(devt); + int first_minor = MINOR(devt); + unsigned int cmd_flags = 0; + ++ bpf_printk("get_tag end cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); ++ + struct io_counter *counterp; + u32 key = find_matching_tag_1_keys(major, first_minor); + if (key >= MAP_SIZE){ +@@ -1471,6 +1525,7 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) + if (key >= MAP_SIZE){ + key = find_matching_tag_5_keys(major, first_minor); + if (key >= MAP_SIZE){ ++ bpf_printk("get_tag end return 2"); + return 0; + } + } +@@ -1480,8 +1535,10 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) + + counterp = bpf_map_lookup_elem(&tag_map, &tagkey); + +- if (!counterp) ++ if (!counterp) { ++ bpf_printk("get_tag end return 3"); + return 0; ++ } + + u64 duration = bpf_ktime_get_ns() - counterp->start_time; + u64 curr_start_range = counterp->start_time / THRESHOLD; +@@ -1537,9 +1594,9 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) + + bpf_map_delete_elem(&tag_map, &tagkey); + bpf_map_delete_elem(&tag_args, &tagkey); ++ bpf_printk("get_tag end2"); + return 0; + } + + char LICENSE[] SEC("license") = "Dual BSD/GPL"; + u32 _version SEC("version") = LINUX_VERSION_CODE; +- diff --git a/src/c/ebpf_collector/ebpf_collector.c b/src/c/ebpf_collector/ebpf_collector.c -index af452c8..fdbd2b4 100644 +index af452c8..7d4d76a 100644 --- a/src/c/ebpf_collector/ebpf_collector.c +++ b/src/c/ebpf_collector/ebpf_collector.c @@ -4,15 +4,16 @@ diff --git a/sysSentry.spec b/sysSentry.spec index d0eb909..b9ce0e0 100644 --- a/sysSentry.spec +++ b/sysSentry.spec @@ -4,7 +4,7 @@ Summary: System Inspection Framework Name: sysSentry Version: 1.0.2 -Release: 68 +Release: 69 License: Mulan PSL v2 Group: System Environment/Daemons Source0: https://gitee.com/openeuler/sysSentry/releases/download/v%{version}/%{name}-%{version}.tar.gz -- Gitee From c998203e5f0a7fa7d7fab0887d11884e65c94586 Mon Sep 17 00:00:00 2001 From: zhuofeng <1107893276@qq.com> Date: Sat, 7 Dec 2024 02:11:54 +0000 Subject: [PATCH 9/9] update again Signed-off-by: zhuofeng <1107893276@qq.com> --- 0001-print-log-info.patch | 1430 ++++++++++++++++--------------------- sysSentry.spec | 2 +- 2 files changed, 618 insertions(+), 814 deletions(-) diff --git a/0001-print-log-info.patch b/0001-print-log-info.patch index e833730..3439d0b 100644 --- a/0001-print-log-info.patch +++ b/0001-print-log-info.patch @@ -1,287 +1,495 @@ diff --git a/src/c/ebpf_collector/ebpf_collector.bpf.c b/src/c/ebpf_collector/ebpf_collector.bpf.c -index a7ea51b..7b364e2 100644 +index a7ea51b..d079c95 100644 --- a/src/c/ebpf_collector/ebpf_collector.bpf.c +++ b/src/c/ebpf_collector/ebpf_collector.bpf.c -@@ -836,12 +836,14 @@ u32 find_matching_wbt_5_keys(int major, int minor) { - SEC("kprobe/blk_mq_start_request") - int kprobe_blk_mq_start_request(struct pt_regs *regs) - { -+ bpf_printk("rq_driver start1"); - struct request_kylin *rq = (struct request_kylin *)PT_REGS_PARM1(regs); - struct gendisk *curr_rq_disk = _(rq->rq_disk); - int major = _(curr_rq_disk->major); - int first_minor = _(curr_rq_disk->first_minor); - unsigned int cmd_flags = _(rq->cmd_flags); - -+ bpf_printk("rq_driver start cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); - struct io_counter *counterp, zero = {}; - - u32 key = find_matching_blk_1_keys(major, first_minor); -@@ -854,6 +856,7 @@ int kprobe_blk_mq_start_request(struct pt_regs *regs) - if (key >= MAP_SIZE){ - key = find_matching_blk_5_keys(major, first_minor); - if (key >= MAP_SIZE){ -+ bpf_printk("rq_driver start return 1"); - return 0; - } - } -@@ -864,10 +867,12 @@ int kprobe_blk_mq_start_request(struct pt_regs *regs) - - counterp = bpf_map_lookup_elem(&blk_map, &rq); - if (counterp || major == 0) { -+ bpf_printk("rq_driver start return 2"); - return 0; - } - long err = bpf_map_update_elem(&blk_map, &rq, &zero, BPF_NOEXIST); - if (err) { -+ bpf_printk("rq_driver start return 3"); - return 0; - } +@@ -122,7 +122,7 @@ struct bpf_map_def SEC("maps") tag_res_2 = { + + struct blk_mq_alloc_data { + /* input parameter */ +- struct request_queue *q; ++ struct request_queue_kylin *q; + blk_mq_req_flags_t flags; + unsigned int shallow_depth; + +@@ -134,9 +134,9 @@ struct blk_mq_alloc_data { + struct request_kylin { + struct request_queue *q; + struct blk_mq_ctx *mq_ctx; ++ struct blk_mq_hw_ctx *mq_hctx; + +- int cpu; +- unsigned int cmd_flags; /* op and common flags */ ++ unsigned int cmd_flags; /* op and common flags */ + req_flags_t rq_flags; + + int internal_tag; +@@ -144,7 +144,7 @@ struct request_kylin { + /* the following two fields are internal, NEVER access directly */ + unsigned int __data_len; /* total data len */ + int tag; +- sector_t __sector; /* sector cursor */ ++ sector_t __sector; /* sector cursor */ + + struct bio *bio; + struct bio *biotail; +@@ -159,7 +159,7 @@ struct request_kylin { + * the dispatch list). + */ + union { +- struct hlist_node hash; /* merge hash */ ++ struct hlist_node hash; /* merge hash */ + struct list_head ipi_list; + }; -@@ -908,7 +913,7 @@ int kprobe_blk_mq_start_request(struct pt_regs *regs) - __sync_fetch_and_add(&curr_data_time_range->count[key], 1); - } - } +@@ -171,7 +171,7 @@ struct request_kylin { + * completion_data share space with the rb_node. + */ + union { +- struct rb_node rb_node; /* sort/lookup */ ++ struct rb_node rb_node; /* sort/lookup */ + struct bio_vec special_vec; + void *completion_data; + int error_count; /* for legacy drivers, don't use */ +@@ -198,7 +198,11 @@ struct request_kylin { + + struct gendisk *rq_disk; + struct hd_struct *part; +- /* Time that I/O was submitted to the kernel. */ ++#ifdef CONFIG_BLK_RQ_ALLOC_TIME ++ /* Time that the first bio started allocating this request. */ ++ u64 alloc_time_ns; ++#endif ++ /* Time that this request was allocated for this IO. */ + u64 start_time_ns; + /* Time that I/O was submitted to the device. */ + u64 io_start_time_ns; +@@ -223,9 +227,9 @@ struct request_kylin { + unsigned short write_hint; + unsigned short ioprio; + +- void *special; /* opaque pointer available for LLD use */ ++ void *special; /* opaque pointer available for LLD use */ + +- unsigned int extra_len; /* length of alignment and padding */ ++ unsigned int extra_len; /* length of alignment and padding */ + + enum mq_rq_state state; + refcount_t ref; +@@ -235,8 +239,6 @@ struct request_kylin { + /* access through blk_rq_set_deadline, blk_rq_deadline */ + unsigned long __deadline; + +- struct list_head timeout_list; - -+ bpf_printk("rq_driver start2"); - return 0; - } - -@@ -916,11 +921,13 @@ int kprobe_blk_mq_start_request(struct pt_regs *regs) - SEC("kprobe/blk_mq_free_request") - int kprobe_blk_mq_free_request(struct pt_regs *regs) - { -+ bpf_printk("rq_driver end1"); - struct request_kylin *rq = (struct request_kylin *)PT_REGS_PARM1(regs); - struct gendisk *curr_rq_disk = _(rq->rq_disk); - int major = _(curr_rq_disk->major); - int first_minor = _(curr_rq_disk->first_minor); - unsigned int cmd_flags = _(rq->cmd_flags); -+ bpf_printk("rq_driver end cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); - - struct io_counter *counterp; - u32 key = find_matching_blk_1_keys(major, first_minor); -@@ -933,6 +940,7 @@ int kprobe_blk_mq_free_request(struct pt_regs *regs) - if (key >= MAP_SIZE){ - key = find_matching_blk_5_keys(major, first_minor); - if (key >= MAP_SIZE){ -+ bpf_printk("rq_driver end return 1"); - return 0; - } - } -@@ -943,6 +951,7 @@ int kprobe_blk_mq_free_request(struct pt_regs *regs) - counterp = bpf_map_lookup_elem(&blk_map, &rq); - - if (!counterp) { -+ bpf_printk("rq_driver end return 2"); - return 0; - } - -@@ -999,6 +1008,7 @@ int kprobe_blk_mq_free_request(struct pt_regs *regs) - } - - bpf_map_delete_elem(&blk_map, &rq); -+ bpf_printk("rq_driver end2"); - return 0; - } - -@@ -1006,12 +1016,14 @@ int kprobe_blk_mq_free_request(struct pt_regs *regs) - SEC("kprobe/blk_mq_make_request") - int kprobe_blk_mq_make_request(struct pt_regs *regs) - { -+ bpf_printk("bio start1"); - struct bio *bio = (struct bio *)PT_REGS_PARM2(regs); - struct gendisk *curr_rq_disk = _(bio->bi_disk); - int major = _(curr_rq_disk->major); - int first_minor = _(curr_rq_disk->first_minor); - unsigned int cmd_flags = _(bio->bi_opf); - -+ bpf_printk("bio start cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); - struct io_counter *counterp, zero = {}; - u32 key = find_matching_bio_1_keys(major, first_minor); - if (key >= MAP_SIZE){ -@@ -1023,6 +1035,7 @@ int kprobe_blk_mq_make_request(struct pt_regs *regs) - if (key >= MAP_SIZE){ - key = find_matching_bio_5_keys(major, first_minor); - if (key >= MAP_SIZE){ -+ bpf_printk("bio start return 1"); - return 0; - } - } -@@ -1033,12 +1046,17 @@ int kprobe_blk_mq_make_request(struct pt_regs *regs) - init_io_counter(&zero, major, first_minor); - - counterp = bpf_map_lookup_elem(&bio_map, &bio); -- if (counterp || major == 0) -+ if (counterp || major == 0) { -+ bpf_printk("bio start return 2"); - return 0; -+ } - - long err = bpf_map_update_elem(&bio_map, &bio, &zero, BPF_NOEXIST); -- if (err && err != -EEXIST) -+ if (err && err != -EEXIST) { -+ bpf_printk("bio start return 3"); - return 0; -+ } -+ - - u64 curr_start_range = zero.start_time / THRESHOLD; - -@@ -1077,6 +1095,7 @@ int kprobe_blk_mq_make_request(struct pt_regs *regs) - __sync_fetch_and_add(&curr_data_time_range->count[key], 1); - } - } -+ bpf_printk("bio start2"); - - return 0; - } -@@ -1085,12 +1104,15 @@ int kprobe_blk_mq_make_request(struct pt_regs *regs) - SEC("kprobe/bio_endio") - int kprobe_bio_endio(struct pt_regs *regs) - { -+ bpf_printk("bio end1"); - struct bio *bio = (struct bio *)PT_REGS_PARM1(regs); - struct gendisk *curr_rq_disk = _(bio->bi_disk); - int major = _(curr_rq_disk->major); - int first_minor = _(curr_rq_disk->first_minor); - unsigned int cmd_flags = _(bio->bi_opf); - -+ bpf_printk("bio end cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); + union { + struct __call_single_data csd; + u64 fifo_time; +@@ -250,12 +252,401 @@ struct request_kylin { + + /* for bidi */ + struct request_kylin *next_rq; ++ KABI_RESERVE(1); ++ KABI_RESERVE(2); ++}; + - struct io_counter *counterp; - void *delete_map = NULL; - u32 key = find_matching_bio_1_keys(major, first_minor); -@@ -1103,6 +1125,7 @@ int kprobe_bio_endio(struct pt_regs *regs) - if (key >= MAP_SIZE){ - key = find_matching_bio_5_keys(major, first_minor); - if (key >= MAP_SIZE){ -+ bpf_printk("bio end return 1"); - return 0; - } - } -@@ -1113,6 +1136,7 @@ int kprobe_bio_endio(struct pt_regs *regs) - counterp = bpf_map_lookup_elem(&bio_map, &bio); - - if (!counterp) { -+ bpf_printk("bio end return 2"); - return 0; - } - -@@ -1171,6 +1195,7 @@ int kprobe_bio_endio(struct pt_regs *regs) - } - - bpf_map_delete_elem(delete_map, &bio); -+ bpf_printk("bio end2"); - return 0; - } - -@@ -1178,6 +1203,7 @@ int kprobe_bio_endio(struct pt_regs *regs) - SEC("kprobe/wbt_wait") - int kprobe_wbt_wait(struct pt_regs *regs) - { -+ bpf_printk("wbt start1"); - u64 wbtkey = bpf_get_current_task(); - u64 value = (u64)PT_REGS_PARM2(regs); - (void)bpf_map_update_elem(&wbt_args, &wbtkey, &value, BPF_ANY); -@@ -1187,6 +1213,8 @@ int kprobe_wbt_wait(struct pt_regs *regs) - int first_minor = _(curr_rq_disk->first_minor); - unsigned int cmd_flags = _(bio->bi_opf); - -+ bpf_printk("wbt start cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); ++struct request_queue_kylin { ++ /* ++ * Together with queue_head for cacheline sharing ++ */ ++ struct list_head queue_head; ++ struct request *last_merge; ++ struct elevator_queue *elevator; + - struct io_counter *counterp, zero = {}; - u32 key = find_matching_wbt_1_keys(major, first_minor); - if (key >= MAP_SIZE){ -@@ -1198,6 +1226,7 @@ int kprobe_wbt_wait(struct pt_regs *regs) - if (key >= MAP_SIZE){ - key = find_matching_wbt_5_keys(major, first_minor); - if (key >= MAP_SIZE){ -+ bpf_printk("wbt start return 1"); - return 0; - } - } -@@ -1209,11 +1238,16 @@ int kprobe_wbt_wait(struct pt_regs *regs) - - counterp = bpf_map_lookup_elem(&wbt_map, &wbtkey); - -- if (counterp || major == 0) -+ if (counterp || major == 0) { -+ bpf_printk("wbt start return 2"); - return 0; -+ } -+ - long err = bpf_map_update_elem(&wbt_map, &wbtkey, &zero, BPF_NOEXIST); -- if (err) -+ if (err) { -+ bpf_printk("wbt start return 3"); - return 0; -+ } - - u64 curr_start_range = zero.start_time / THRESHOLD; - -@@ -1252,7 +1286,7 @@ int kprobe_wbt_wait(struct pt_regs *regs) - __sync_fetch_and_add(&curr_data_time_range->count[key], 1); - } - } -- -+ bpf_printk("wbt start2"); - return 0; - } - -@@ -1260,12 +1294,14 @@ int kprobe_wbt_wait(struct pt_regs *regs) - SEC("kretprobe/wbt_wait") - int kretprobe_wbt_wait(struct pt_regs *regs) - { -+ bpf_printk("wbt end1"); - struct bio *bio = NULL; - u64 *wbtargs = NULL; - u64 wbtkey = bpf_get_current_task(); - wbtargs = (u64 *)bpf_map_lookup_elem(&wbt_args, &wbtkey); - if (wbtargs == NULL) { - bpf_map_delete_elem(&wbt_args, &wbtkey); -+ bpf_printk("wbt end return 1"); - return 0; - } - bio = (struct bio *)(*wbtargs); -@@ -1273,6 +1309,8 @@ int kretprobe_wbt_wait(struct pt_regs *regs) - int major = _(curr_rq_disk->major); - int first_minor = _(curr_rq_disk->first_minor); - unsigned int cmd_flags = _(bio->bi_opf); ++ struct blk_queue_stats *stats; ++ struct rq_qos *rq_qos; + -+ bpf_printk("wbt end cmd_flags = %d, major = %d, first_minor= %d\n", cmd_flags, major, first_minor); - - struct io_counter *counterp; - u32 key = find_matching_wbt_1_keys(major, first_minor); -@@ -1285,6 +1323,7 @@ int kretprobe_wbt_wait(struct pt_regs *regs) - if (key >= MAP_SIZE){ - key = find_matching_wbt_5_keys(major, first_minor); - if (key >= MAP_SIZE){ -+ bpf_printk("wbt end return 2"); - return 0; - } - } -@@ -1294,8 +1333,10 @@ int kretprobe_wbt_wait(struct pt_regs *regs) - - counterp = bpf_map_lookup_elem(&wbt_map, &wbtkey); - -- if (!counterp) -+ if (!counterp){ -+ bpf_printk("wbt end return 3"); - return 0; -+ } - - u64 duration = bpf_ktime_get_ns() - counterp->start_time; - u64 curr_start_range = counterp->start_time / THRESHOLD; -@@ -1351,6 +1392,7 @@ int kretprobe_wbt_wait(struct pt_regs *regs) - - bpf_map_delete_elem(&wbt_map, &wbtkey); - bpf_map_delete_elem(&wbt_args, &wbtkey); -+ bpf_printk("wbt end2"); - return 0; - } ++ make_request_fn *make_request_fn; ++ poll_q_fn *poll_fn; ++ dma_drain_needed_fn *dma_drain_needed; ++ ++ const struct blk_mq_ops *mq_ops; ++ ++ /* sw queues */ ++ struct blk_mq_ctx __percpu *queue_ctx; ++ unsigned int nr_queues; ++ ++ unsigned int queue_depth; ++ ++ /* hw dispatch queues */ ++ struct blk_mq_hw_ctx **queue_hw_ctx; ++ unsigned int nr_hw_queues; ++ ++ struct backing_dev_info_kylin *backing_dev_info; ++ ++ /* ++ * The queue owner gets to use this for whatever they like. ++ * ll_rw_blk doesn't touch it. ++ */ ++ void *queuedata; ++ ++ /* ++ * various queue flags, see QUEUE_* below ++ */ ++ unsigned long queue_flags; ++ /* ++ * Number of contexts that have called blk_set_pm_only(). If this ++ * counter is above zero then only RQF_PM and RQF_PREEMPT requests are ++ * processed. ++ */ ++ atomic_t pm_only; ++ ++ /* ++ * ida allocated id for this queue. Used to index queues from ++ * ioctx. ++ */ ++ int id; ++ ++ /* ++ * queue needs bounce pages for pages above this limit ++ */ ++ gfp_t bounce_gfp; ++ ++ /* ++ * protects queue structures from reentrancy. ->__queue_lock should ++ * _never_ be used directly, it is queue private. always use ++ * ->queue_lock. ++ */ ++ spinlock_t __queue_lock; ++ spinlock_t *queue_lock; ++ ++ /* ++ * queue kobject ++ */ ++ struct kobject kobj; ++ ++ /* ++ * mq queue kobject ++ */ ++ struct kobject *mq_kobj; ++ ++#ifdef CONFIG_BLK_DEV_INTEGRITY ++ struct blk_integrity integrity; ++#endif /* CONFIG_BLK_DEV_INTEGRITY */ ++ ++#ifdef CONFIG_PM ++ struct device *dev; ++ int rpm_status; ++ unsigned int nr_pending; ++#endif ++ ++ /* ++ * queue settings ++ */ ++ unsigned long nr_requests; /* Max # of requests */ + ++ unsigned int dma_drain_size; ++ void *dma_drain_buffer; ++ unsigned int dma_pad_mask; ++ unsigned int dma_alignment; ++ ++ unsigned int rq_timeout; ++ int poll_nsec; ++ ++ struct blk_stat_callback *poll_cb; ++ struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS]; ++ ++ struct timer_list timeout; ++ struct work_struct timeout_work; ++ ++ atomic_t nr_active_requests_shared_sbitmap; ++ ++ struct list_head icq_list; + #ifdef CONFIG_BLK_CGROUP +- struct request_list *rl; /* rl this rq is alloced from */ ++ DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS); ++ struct blkcg_gq *root_blkg; ++ struct list_head blkg_list; + #endif +- KABI_RESERVE(1); +- KABI_RESERVE(2); ++ ++ struct queue_limits limits; ++ ++ unsigned int required_elevator_features; ++ ++#ifdef CONFIG_BLK_DEV_ZONED ++ /* ++ * Zoned block device information for request dispatch control. ++ * nr_zones is the total number of zones of the device. This is always ++ * 0 for regular block devices. seq_zones_bitmap is a bitmap of nr_zones ++ * bits which indicates if a zone is conventional (bit clear) or ++ * sequential (bit set). seq_zones_wlock is a bitmap of nr_zones ++ * bits which indicates if a zone is write locked, that is, if a write ++ * request targeting the zone was dispatched. All three fields are ++ * initialized by the low level device driver (e.g. scsi/sd.c). ++ * Stacking drivers (device mappers) may or may not initialize ++ * these fields. ++ * ++ * Reads of this information must be protected with blk_queue_enter() / ++ * blk_queue_exit(). Modifying this information is only allowed while ++ * no requests are being processed. See also blk_mq_freeze_queue() and ++ * blk_mq_unfreeze_queue(). ++ */ ++ unsigned int nr_zones; ++ unsigned long *seq_zones_bitmap; ++ unsigned long *seq_zones_wlock; ++#endif /* CONFIG_BLK_DEV_ZONED */ ++ ++ /* ++ * sg stuff ++ */ ++ unsigned int sg_timeout; ++ unsigned int sg_reserved_size; ++ int node; ++#ifdef CONFIG_BLK_DEV_IO_TRACE ++ struct blk_trace __rcu *blk_trace; ++ struct mutex blk_trace_mutex; ++#endif ++ /* ++ * for flush operations ++ */ ++ struct blk_flush_queue *fq; ++ ++ struct list_head requeue_list; ++ spinlock_t requeue_lock; ++ struct delayed_work requeue_work; ++ ++ struct mutex sysfs_lock; ++ ++ /* ++ * for reusing dead hctx instance in case of updating ++ * nr_hw_queues ++ */ ++ struct list_head unused_hctx_list; ++ spinlock_t unused_hctx_lock; ++ ++ int mq_freeze_depth; ++ ++#if defined(CONFIG_BLK_DEV_BSG) ++ struct bsg_class_device bsg_dev; ++#endif ++ ++#ifdef CONFIG_BLK_DEV_THROTTLING ++ /* Throttle data */ ++ struct throtl_data *td; ++#endif ++ struct rcu_head rcu_head; ++ wait_queue_head_t mq_freeze_wq; ++ /* ++ * Protect concurrent access to q_usage_counter by ++ * percpu_ref_kill() and percpu_ref_reinit(). ++ */ ++ struct mutex mq_freeze_lock; ++ struct percpu_ref q_usage_counter; ++ struct list_head all_q_node; ++ ++ struct blk_mq_tag_set *tag_set; ++ struct list_head tag_set_list; ++ struct bio_set bio_split; ++ ++#ifdef CONFIG_BLK_DEBUG_FS ++ struct dentry *debugfs_dir; ++ struct dentry *sched_debugfs_dir; ++#endif ++ ++ bool mq_sysfs_init_done; ++ ++ size_t cmd_size; ++ void *rq_alloc_data; ++ ++ struct work_struct release_work; ++ ++#ifdef CONFIG_BLK_BIO_DISPATCH_ASYNC ++ /* used when QUEUE_FLAG_DISPATCH_ASYNC is set */ ++ struct cpumask dispatch_async_cpus; ++ int __percpu *last_dispatch_cpu; ++#endif ++ ++#define BLK_MAX_WRITE_HINTS 5 ++ u64 write_hints[BLK_MAX_WRITE_HINTS]; ++ ++ KABI_RESERVE(1); ++ KABI_RESERVE(2); ++ KABI_RESERVE(3); ++ KABI_RESERVE(4); ++}; ++ ++struct backing_dev_info_kylin { ++ u64 id; ++ struct rb_node rb_node; /* keyed by ->id */ ++ struct list_head bdi_list; ++ unsigned long ra_pages; /* max readahead in PAGE_SIZE units */ ++ unsigned long io_pages; /* max allowed IO size */ ++ congested_fn *congested_fn; /* Function pointer if device is md/dm */ ++ void *congested_data; /* Pointer to aux data for congested func */ ++ ++ const char *name; ++ ++ struct kref refcnt; /* Reference counter for the structure */ ++ unsigned int capabilities; /* Device capabilities */ ++ unsigned int min_ratio; ++ unsigned int max_ratio, max_prop_frac; ++ ++ /* ++ * Sum of avg_write_bw of wbs with dirty inodes. > 0 if there are ++ * any dirty wbs, which is depended upon by bdi_has_dirty(). ++ */ ++ atomic_long_t tot_write_bandwidth; ++ ++ struct bdi_writeback wb; /* the root writeback info for this bdi */ ++ struct list_head wb_list; /* list of all wbs */ ++#ifdef CONFIG_CGROUP_WRITEBACK ++ struct radix_tree_root cgwb_tree; /* radix tree of active cgroup wbs */ ++ struct rb_root cgwb_congested_tree; /* their congested states */ ++ struct mutex cgwb_release_mutex; /* protect shutdown of wb structs */ ++ struct rw_semaphore wb_switch_rwsem; /* no cgwb switch while syncing */ ++#else ++ struct bdi_writeback_congested *wb_congested; ++#endif ++ wait_queue_head_t wb_waitq; ++ ++ union { ++ struct rcu_device *rcu_dev; ++ struct device *dev; ++ }; ++ struct device *owner; ++ ++ struct timer_list laptop_mode_wb_timer; ++ ++#ifdef CONFIG_DEBUG_FS ++ struct dentry *debug_dir; ++ struct dentry *debug_stats; ++#endif ++ ++ KABI_RESERVE(1) ++ KABI_RESERVE(2) ++ KABI_RESERVE(3) ++ KABI_RESERVE(4) ++}; ++ ++struct device_kylin { ++ struct device_kylin *parent; ++ ++ struct device_private *p; ++ ++ struct kobject kobj; ++ const char *init_name; /* initial name of the device */ ++ const struct device_type *type; ++ ++ struct mutex mutex; /* mutex to synchronize calls to ++ * its driver. ++ */ ++ ++ struct bus_type *bus; /* type of bus device is on */ ++ struct device_driver *driver; /* which driver has allocated this ++ device */ ++ void *platform_data; /* Platform specific data, device ++ core doesn't touch it */ ++ void *driver_data; /* Driver data, set and get with ++ dev_set/get_drvdata */ ++ struct dev_links_info links; ++ struct dev_pm_info power; ++ struct dev_pm_domain *pm_domain; ++ ++#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN ++ struct irq_domain *msi_domain; ++#endif ++#ifdef CONFIG_PINCTRL ++ struct dev_pin_info *pins; ++#endif ++#ifdef CONFIG_GENERIC_MSI_IRQ ++ struct list_head msi_list; ++#endif ++ ++#ifdef CONFIG_NUMA ++ int numa_node; /* NUMA node this device is close to */ ++#endif ++ const struct dma_map_ops *dma_ops; ++ u64 *dma_mask; /* dma mask (if dma'able device) */ ++ u64 coherent_dma_mask;/* Like dma_mask, but for ++ alloc_coherent mappings as ++ not all hardware supports ++ 64 bit addresses for consistent ++ allocations such descriptors. */ ++ u64 bus_dma_mask; /* upstream dma_mask constraint */ ++ unsigned long dma_pfn_offset; ++ ++ struct device_dma_parameters *dma_parms; ++ ++ struct list_head dma_pools; /* dma pools (if dma'ble) */ ++ ++ struct dma_coherent_mem *dma_mem; /* internal for coherent mem ++ override */ ++#ifdef CONFIG_DMA_CMA ++ struct cma *cma_area; /* contiguous memory area for dma ++ allocations */ ++#endif ++ /* arch specific additions */ ++ struct dev_archdata archdata; ++ ++ struct device_node *of_node; /* associated device tree node */ ++ struct fwnode_handle *fwnode; /* firmware device node */ ++ ++ dev_t devt; /* dev_t, creates the sysfs "dev" */ ++ u32 id; /* device instance */ ++ ++ spinlock_t devres_lock; ++ struct list_head devres_head; ++ ++ struct klist_node knode_class; ++ struct class *class; ++ const struct attribute_group **groups; /* optional groups */ ++ ++ void (*release)(struct device *dev); ++ struct iommu_group *iommu_group; ++ struct iommu_fwspec *iommu_fwspec; ++ struct iommu_param *iommu_param; ++ ++ bool offline_disabled:1; ++ bool offline:1; ++ bool of_node_reused:1; ++#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ ++ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ ++ defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) ++ bool dma_coherent:1; ++#endif ++ ++#ifdef CONFIG_GENERIC_MSI_IRQ ++#if !defined(CONFIG_DEBUG_SPINLOCK) && !defined(CONFIG_DEBUG_LOCK_ALLOC) ++#ifndef __GENKSYMS__ ++ union { ++ raw_spinlock_t msi_lock; ++ unsigned long kabi_reserve1; ++ }; ++#else ++ KABI_RESERVE(1) ++#endif ++#else ++ raw_spinlock_t msi_lock; ++ KABI_RESERVE(1) ++#endif ++#else ++ KABI_RESERVE(1) ++#endif ++ KABI_RESERVE(2) ++ KABI_RESERVE(3) ++ KABI_RESERVE(4) ++ KABI_RESERVE(5) ++ KABI_RESERVE(6) ++ KABI_RESERVE(7) ++ KABI_RESERVE(8) ++ KABI_RESERVE(9) ++ KABI_RESERVE(10) ++ KABI_RESERVE(11) ++ KABI_RESERVE(12) ++ KABI_RESERVE(13) ++ KABI_RESERVE(14) ++ KABI_RESERVE(15) ++ KABI_RESERVE(16) + }; -@@ -1358,17 +1400,19 @@ int kretprobe_wbt_wait(struct pt_regs *regs) + static __always_inline void blk_fill_rwbs(char *rwbs, unsigned int op) +@@ -1358,17 +1749,25 @@ int kretprobe_wbt_wait(struct pt_regs *regs) SEC("kprobe/blk_mq_get_tag") int kprobe_blk_mq_get_tag(struct pt_regs *regs) { @@ -290,11 +498,18 @@ index a7ea51b..7b364e2 100644 u64 value = (u64)PT_REGS_PARM1(regs); (void)bpf_map_update_elem(&tag_args, &tagkey, &value, BPF_ANY); struct blk_mq_alloc_data *bd= (struct blk_mq_alloc_data *)value; - struct request_queue *q = _(bd->q); +- struct request_queue *q = _(bd->q); - struct backing_dev_info *backing_dev_info = _(q->backing_dev_info); -+ struct backing_dev_info *backing_dev_info = _(q->backing_dev_info); ++ struct request_queue_kylin *q = (struct request_queue_kylin *)_(bd->q); ++ struct backing_dev_info_kylin *backing_dev_info = (struct backing_dev_info_kylin *)_(q->backing_dev_info); struct device *owner = _(backing_dev_info->owner); dev_t devt = _(owner->devt); ++ bpf_printk("get_tag start devt1111111111111 = %p\n", bd); ++ bpf_printk("get_tag start devt1111111111112 = %p\n", q); ++ bpf_printk("get_tag start devt11111111111131 = %p\n", backing_dev_info); ++ bpf_printk("get_tag start devt11111111111132 = %p\n", _(q->make_request_fn)); ++ bpf_printk("get_tag start devt11111111111133 = %d\n", _(q->queue_depth)); ++ bpf_printk("get_tag start devt1111111111114 = %p\n", owner); int major = MAJOR(devt); int first_minor = MINOR(devt); unsigned int cmd_flags = 0; @@ -302,7 +517,7 @@ index a7ea51b..7b364e2 100644 struct io_counter *counterp, zero = {}; u32 key = find_matching_tag_1_keys(major, first_minor); -@@ -1381,6 +1425,7 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) +@@ -1381,6 +1780,7 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) if (key >= MAP_SIZE){ key = find_matching_tag_5_keys(major, first_minor); if (key >= MAP_SIZE){ @@ -310,11 +525,12 @@ index a7ea51b..7b364e2 100644 return 0; } } -@@ -1391,11 +1436,16 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) +@@ -1391,11 +1791,17 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) init_io_counter(&zero, major, first_minor); counterp = bpf_map_lookup_elem(&tag_map, &tagkey); - if (counterp || major == 0) ++ bpf_printk("get_tag start counterp = %p\n", counterp); + if (counterp || major == 0) { + bpf_printk("get_tag start return 2"); return 0; @@ -329,7 +545,7 @@ index a7ea51b..7b364e2 100644 u64 curr_start_range = zero.start_time / THRESHOLD; -@@ -1434,7 +1484,7 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) +@@ -1434,7 +1840,7 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) __sync_fetch_and_add(&curr_data_time_range->count[key], 1); } } @@ -338,7 +554,7 @@ index a7ea51b..7b364e2 100644 return 0; } -@@ -1442,6 +1492,7 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) +@@ -1442,6 +1848,7 @@ int kprobe_blk_mq_get_tag(struct pt_regs *regs) SEC("kretprobe/blk_mq_get_tag") int kretprobe_blk_mq_get_tag(struct pt_regs *regs) { @@ -346,7 +562,7 @@ index a7ea51b..7b364e2 100644 u64 tagkey = bpf_get_current_task(); u64 *tagargs = NULL; struct blk_mq_alloc_data *bd = NULL; -@@ -1449,17 +1500,20 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) +@@ -1449,17 +1856,22 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) tagargs = (u64 *)bpf_map_lookup_elem(&tag_args, &tagkey); if (tagargs == NULL) { bpf_map_delete_elem(&tag_args, &tagkey); @@ -355,9 +571,12 @@ index a7ea51b..7b364e2 100644 + return 0; } bd = (struct blk_mq_alloc_data *)(*tagargs); - struct request_queue *q = _(bd->q); +- struct request_queue *q = _(bd->q); - struct backing_dev_info *backing_dev_info = _(q->backing_dev_info); -+ struct backing_dev_info *backing_dev_info = _(q->backing_dev_info); ++ // struct request_queue *q = _(bd->q); ++ // struct backing_dev_info *backing_dev_info = _(q->backing_dev_info); ++ struct request_queue_kylin *q = (struct request_queue_kylin *)_(bd->q); ++ struct backing_dev_info_kylin *backing_dev_info = (struct backing_dev_info_kylin *)_(q->backing_dev_info); struct device *owner = _(backing_dev_info->owner); dev_t devt = _(owner->devt); int major = MAJOR(devt); @@ -369,7 +588,7 @@ index a7ea51b..7b364e2 100644 struct io_counter *counterp; u32 key = find_matching_tag_1_keys(major, first_minor); if (key >= MAP_SIZE){ -@@ -1471,6 +1525,7 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) +@@ -1471,6 +1883,7 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) if (key >= MAP_SIZE){ key = find_matching_tag_5_keys(major, first_minor); if (key >= MAP_SIZE){ @@ -377,7 +596,7 @@ index a7ea51b..7b364e2 100644 return 0; } } -@@ -1480,8 +1535,10 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) +@@ -1480,8 +1893,10 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) counterp = bpf_map_lookup_elem(&tag_map, &tagkey); @@ -389,7 +608,7 @@ index a7ea51b..7b364e2 100644 u64 duration = bpf_ktime_get_ns() - counterp->start_time; u64 curr_start_range = counterp->start_time / THRESHOLD; -@@ -1537,9 +1594,9 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) +@@ -1537,9 +1952,9 @@ int kretprobe_blk_mq_get_tag(struct pt_regs *regs) bpf_map_delete_elem(&tag_map, &tagkey); bpf_map_delete_elem(&tag_args, &tagkey); @@ -401,80 +620,31 @@ index a7ea51b..7b364e2 100644 u32 _version SEC("version") = LINUX_VERSION_CODE; - diff --git a/src/c/ebpf_collector/ebpf_collector.c b/src/c/ebpf_collector/ebpf_collector.c -index af452c8..7d4d76a 100644 +index af452c8..52a02bb 100644 --- a/src/c/ebpf_collector/ebpf_collector.c +++ b/src/c/ebpf_collector/ebpf_collector.c -@@ -4,15 +4,16 @@ - * Author: Zhang Nan - * Create: 2024-09-27 - */ --#include -+#include +@@ -11,6 +11,7 @@ + #include + #include + #include +#include - #include --#include --#include --#include --#include --#include --#include --#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include + #include + #include #include - #include - #include -@@ -22,68 +23,89 @@ - #include - #include "ebpf_collector.h" +@@ -36,11 +37,25 @@ + #define TAG_RES_2 (map_fd[13]) + #define BPF_FILE "/usr/lib/ebpf_collector.bpf.o" --#define BLK_MAP (map_fd[0]) --#define BLK_RES (map_fd[1]) --#define BIO_MAP (map_fd[2]) --#define BIO_RES (map_fd[3]) --#define WBT_MAP (map_fd[4]) --#define WBT_RES (map_fd[5]) --#define TAG_MAP (map_fd[7]) --#define TAG_RES (map_fd[8]) --#define BLK_RES_2 (map_fd[10]) --#define BIO_RES_2 (map_fd[11]) --#define WBT_RES_2 (map_fd[12]) --#define TAG_RES_2 (map_fd[13]) --#define BPF_FILE "/usr/lib/ebpf_collector.bpf.o" -- --typedef struct { -+#define BLK_MAP (map_fd[0]) -+#define BLK_RES (map_fd[1]) -+#define BIO_MAP (map_fd[2]) -+#define BIO_RES (map_fd[3]) -+#define WBT_MAP (map_fd[4]) -+#define WBT_RES (map_fd[5]) -+#define TAG_MAP (map_fd[7]) -+#define TAG_RES (map_fd[8]) -+#define BLK_RES_2 (map_fd[10]) -+#define BIO_RES_2 (map_fd[11]) -+#define WBT_RES_2 (map_fd[12]) -+#define TAG_RES_2 (map_fd[13]) -+#define BPF_FILE "/usr/lib/ebpf_collector.bpf.o" -+ +#define MAX_LINE_LENGTH 1024 +#define MAX_SECTION_NAME_LENGTH 256 +#define CONFIG_FILE "/etc/sysSentry/collector.conf" + -+typedef struct -+{ + typedef struct { int major; int minor; } DeviceInfo; --static volatile bool exiting; -+typedef enum -+{ ++typedef enum { + LOG_LEVEL_NONE, + LOG_LEVEL_DEBUG, + LOG_LEVEL_ERROR, @@ -484,276 +654,35 @@ index af452c8..7d4d76a 100644 + +LogLevel currentLogLevel = LOG_LEVEL_INFO; + -+static volatile bool exiting; - --const char argp_program_doc[] = --"Show block device I/O pattern.\n" --"\n" --"USAGE: ebpf_collector [--help]\n" --"\n" --"EXAMPLES:\n" --" ebpf_collector # show block I/O pattern\n"; -+const char argp_program_doc[] = -+ "Show block device I/O pattern.\n" -+ "\n" -+ "USAGE: ebpf_collector [--help]\n" -+ "\n" -+ "EXAMPLES:\n" -+ " ebpf_collector # show block I/O pattern\n"; - - static const struct argp_option opts[] = { -- { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" }, -+ {NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help"}, - {}, - }; - --static error_t parse_arg(int key, char *arg, struct argp_state *state) { -- static int pos_args; -+static error_t parse_arg(int key, char *arg, struct argp_state *state) -+{ -+ static int pos_args; - -- switch (key) { -- case 'h': -- argp_state_help(state, stderr, ARGP_HELP_STD_HELP); -+ switch (key) -+ { -+ case 'h': -+ argp_state_help(state, stderr, ARGP_HELP_STD_HELP); - break; -- default: -- return ARGP_ERR_UNKNOWN; -+ default: -+ return ARGP_ERR_UNKNOWN; - } -- return 0; -+ return 0; - } - - static void sig_handler(int sig) - { -- exiting = true; -+ exiting = true; - } - --char* extract_device_name(const char *path) { -+char *extract_device_name(const char *path) -+{ - const char *dev_dir = "/dev/"; -- char *name = strrchr(path, '/') + 1; -- if (strncmp(dev_dir, path, strlen(dev_dir)) == 0) { -- return strdup(name); -+ char *name = strrchr(path, '/') + 1; -+ if (strncmp(dev_dir, path, strlen(dev_dir)) == 0) -+ { -+ return strdup(name); - } -- return NULL; -+ return NULL; - } - --char* find_device_name(dev_t dev) { -+char *find_device_name(dev_t dev) -+{ - DIR *dir; - struct dirent *entry; - struct stat sb; -@@ -91,24 +113,29 @@ char* find_device_name(dev_t dev) { - char path[1024]; - - dir = opendir("/dev"); -- if (dir == NULL) { -+ if (dir == NULL) -+ { - perror("Failed to open /dev"); - return NULL; - } - -- while ((entry = readdir(dir)) != NULL) { -+ while ((entry = readdir(dir)) != NULL) -+ { - snprintf(path, sizeof(path), "/dev/%s", entry->d_name); - -- if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) { -+ if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) -+ { - continue; - } - -- if (stat(path, &sb) == -1) { -- continue; -+ if (stat(path, &sb) == -1) -+ { -+ continue; - } - -- if (major(sb.st_rdev) == major(dev) && minor(sb.st_rdev) == minor(dev)) { -- device_name = extract_device_name(path); -+ if (major(sb.st_rdev) == major(dev) && minor(sb.st_rdev) == minor(dev)) -+ { -+ device_name = extract_device_name(path); - break; - } - } -@@ -117,84 +144,108 @@ char* find_device_name(dev_t dev) { - return device_name; - } - --static void update_io_dump(struct bpf_map *map_res, int *io_dump, int *map_size, char *stage) { -+static void update_io_dump(struct bpf_map *map_res, int *io_dump, int *map_size, char *stage) -+{ -+ logMessage(LOG_LEVEL_DEBUG, "start update io dump.\n"); - struct time_range_io_count time_count; - u32 io_dump_key = 0; -- struct sysinfo info; -+ struct sysinfo info; - sysinfo(&info); - int count_time = 150; - u32 curr_time = info.uptime; -- while (count_time >= 0) { -+ while (count_time >= 0) -+ { -+ logMessage(LOG_LEVEL_DEBUG, "start count_time while.\n"); - io_dump_key = curr_time - count_time; - int err = bpf_map_lookup_elem(map_res, &io_dump_key, &time_count); -- if (err < 0) { -- count_time -= 1; -- continue; -+ if (err < 0) -+ { -+ count_time -= 1; -+ continue; - } -- if ((curr_time - io_dump_key) >= 2) { -+ if ((curr_time - io_dump_key) >= 2) -+ { - int isempty = 1; -- for (int key = 0; key < map_size; key++) { -- if (time_count.count[key] > 0) { -+ for (int key = 0; key < map_size; key++) -+ { -+ if (time_count.count[key] > 0) -+ { - io_dump[key] += time_count.count[key]; - isempty = 0; - } - } -- if (isempty || (curr_time - io_dump_key) > IO_DUMP_THRESHOLD) { -+ if (isempty || (curr_time - io_dump_key) > IO_DUMP_THRESHOLD) -+ { - bpf_map_delete_elem(map_res, &io_dump_key); -- } -+ } - } - count_time -= 1; -+ logMessage(LOG_LEVEL_DEBUG, " finish count_time while.\n"); - } - } - - static int print_map_res(struct bpf_map *map_res, char *stage, int *map_size, int *io_dump) - { -- struct stage_data counter; -+ logMessage(LOG_LEVEL_DEBUG, "enter print map.\n"); -+ struct stage_data counter; - int key = 0; + static volatile bool exiting; -- for (key = 0; key < map_size; key++) { -- int err = bpf_map_lookup_elem(map_res, &key, &counter); -- if (err < 0) { -- fprintf(stderr, "failed to lookup %s map_res: %d\n", stage, err); -- return -1; -+ for (key = 0; key < map_size; key++) -+ { -+ logMessage(LOG_LEVEL_DEBUG, "start print map for.\n"); -+ int err = bpf_map_lookup_elem(map_res, &key, &counter); -+ if (err < 0) -+ { -+ fprintf(stderr, "failed to lookup %s map_res: %d\n", stage, err); -+ return -1; - } -- -+ + const char argp_program_doc[] = +@@ -162,14 +177,17 @@ static int print_map_res(struct bpf_map *map_res, char *stage, int *map_size, in size_t length = strlen(counter.io_type); char io_type; -- if (length > 0) { -+ if (length > 0) -+ { + if (length > 0) { + logMessage(LOG_LEVEL_DEBUG, "io_type have value.\n"); io_type = counter.io_type[0]; -- } else { + } else { - io_type = NULL; -+ } -+ else -+ { + logMessage(LOG_LEVEL_DEBUG, "io_type not value.\n"); + io_type = 'W'; } int major = counter.major; int first_minor = counter.first_minor; -- dev_t dev = makedev(major, first_minor); -+ dev_t dev = makedev(major, first_minor); + dev_t dev = makedev(major, first_minor); char *device_name = find_device_name(dev); -- if (device_name && io_type) { -+ logMessage(LOG_LEVEL_DEBUG, "device_name = %s\n", device_name); -+ if (device_name && io_type) -+ { ++ logMessage(LOG_LEVEL_DEBUG, "device_name: %s\n", device_name); + if (device_name && io_type) { printf("%-7s %10llu %10llu %d %c %s\n", -- stage, -- counter.finish_count, -- counter.duration, -- io_dump[key], -- io_type, -- device_name -- ); -+ stage, -+ counter.finish_count, -+ counter.duration, -+ io_dump[key], -+ io_type, -+ device_name); - fflush(stdout); - } - } -+ logMessage(LOG_LEVEL_DEBUG, "finish print map for.\n"); - -- return 0; -+ return 0; - } - --int init_map(int *map_fd, const char *map_name, int *map_size, DeviceInfo *devices) { -+int init_map(int *map_fd, const char *map_name, int *map_size, DeviceInfo *devices) -+{ - struct stage_data init_data = {0}; - - memset(init_data.io_type, 0, sizeof(init_data.io_type)); - -- for (int i = 0; i < map_size; i++) { -+ for (int i = 0; i < map_size; i++) -+ { - init_data.major = devices[i].major; - init_data.first_minor = devices[i].minor; -- if (bpf_map_update_elem(map_fd, &i, &init_data, BPF_ANY) != 0) { -+ if (bpf_map_update_elem(map_fd, &i, &init_data, BPF_ANY) != 0) -+ { - printf("Failed to initialize map %s at index %d\n", map_name, i); - return 1; - } -@@ -203,15 +254,113 @@ int init_map(int *map_fd, const char *map_name, int *map_size, DeviceInfo *devic + stage, +@@ -203,6 +221,102 @@ int init_map(int *map_fd, const char *map_name, int *map_size, DeviceInfo *devic return 0; } --int main(int argc, char **argv) { -- struct partitions *partitions = NULL; -- const struct partition *partition; -- static const struct argp argp = { -- .options = opts, -- .parser = parse_arg, -- .doc = argp_program_doc, -+char *read_config_value(const char *file, const char *section, const char *key) -+{ ++char *read_config_value(const char *file, const char *section, const char *key) { + FILE *fp = fopen(file, "r"); -+ if (fp == NULL) -+ { ++ if (fp == NULL) { + perror("Failed to open config file"); + return NULL; + } @@ -762,32 +691,25 @@ index af452c8..7d4d76a 100644 + char current_section[MAX_SECTION_NAME_LENGTH] = {0}; + char *value = NULL; + -+ while (fgets(line, sizeof(line), fp) != NULL) -+ { ++ while (fgets(line, sizeof(line), fp) != NULL) { + line[strcspn(line, "\n")] = 0; + -+ if (line[0] == '\0' || line[0] == ';' || line[0] == '#') -+ { ++ if (line[0] == '\0' || line[0] == ';' || line[0] == '#') { + continue; + } + -+ if (line[0] == '[') -+ { ++ if (line[0] == '[') { + sscanf(line, "[%[^]]", current_section); + continue; + } + -+ if (strcmp(current_section, section) == 0) -+ { ++ if (strcmp(current_section, section) == 0) { + char *delimiter = "="; + char *token = strtok(line, delimiter); -+ if (token != NULL) -+ { -+ if (strcmp(token, key) == 0) -+ { ++ if (token != NULL) { ++ if (strcmp(token, key) == 0) { + token = strtok(NULL, delimiter); -+ if (token != NULL) -+ { ++ if (token != NULL) { + value = strdup(token); + break; + } @@ -800,44 +722,29 @@ index af452c8..7d4d76a 100644 + return value; +} + -+void setLogLevel(const char *levelStr) -+{ -+ if (strcmp(levelStr, "info") == 0) -+ { ++void setLogLevel(const char *levelStr) { ++ if (strcmp(levelStr, "info") == 0) { + currentLogLevel = LOG_LEVEL_INFO; + } -+ else if (strcmp(levelStr, "warning") == 0) -+ { ++ else if (strcmp(levelStr, "warning") == 0) { + currentLogLevel = LOG_LEVEL_WARNING; + } -+ else if (strcmp(levelStr, "error") == 0) -+ { ++ else if (strcmp(levelStr, "error") == 0) { + currentLogLevel = LOG_LEVEL_ERROR; + } -+ else if (strcmp(levelStr, "debug") == 0) -+ { ++ else if (strcmp(levelStr, "debug") == 0) { + currentLogLevel = LOG_LEVEL_DEBUG; + } -+ else if (strcmp(levelStr, "none") == 0) -+ { ++ else if (strcmp(levelStr, "none") == 0) { + currentLogLevel = LOG_LEVEL_NONE; + } -+ else -+ { ++ else { + fprintf(stderr, "Unknown log level: %s\n", levelStr); ++ currentLogLevel = LOG_LEVEL_INFO; + } +} + -+void logMessage2(LogLevel level, const char *message) -+{ -+ if (level >= currentLogLevel) -+ { -+ printf("%s", message); -+ } -+} -+ -+void logMessage(LogLevel level, const char *format, ...) -+{ ++void logMessage(LogLevel level, const char *format, ...){ + va_list args; + if (level >= currentLogLevel) { + va_start(args, format); @@ -846,208 +753,105 @@ index af452c8..7d4d76a 100644 + } +} + -+int main(int argc, char **argv) -+{ -+ struct partitions *partitions = NULL; -+ const struct partition *partition; -+ static const struct argp argp = { -+ .options = opts, -+ .parser = parse_arg, -+ .doc = argp_program_doc, - }; -- int err; -+ int err; - char filename[256]; - DIR *dir; - struct dirent *entry; -@@ -219,37 +368,65 @@ int main(int argc, char **argv) { - int major, minor; - DeviceInfo devices[MAP_SIZE]; - int device_count = 0; -- struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; -- setrlimit(RLIMIT_MEMLOCK, &r); -+ struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; -+ setrlimit(RLIMIT_MEMLOCK, &r); ++int check_for_device(const char *device_name) { ++ char path[256]; ++ snprintf(path, sizeof(path), "/sys/block/%s", device_name); ++ ++ DIR *dir = opendir(path); ++ if (dir == NULL) { ++ return 0; ++ } + ++ struct dirent *entry; ++ while ((entry = readdir(dir)) != NULL) { ++ struct stat statbuf; ++ if (stat(path, &statbuf) == 0) { ++ if (S_ISDIR(statbuf.st_mode)) { ++ closedir(dir); ++ return 1; ++ } ++ } ++ } ++ closedir(dir); ++ return 0; ++} ++ + int main(int argc, char **argv) { + struct partitions *partitions = NULL; + const struct partition *partition; +@@ -222,15 +336,28 @@ int main(int argc, char **argv) { + struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; + setrlimit(RLIMIT_MEMLOCK, &r); + + char *level = read_config_value(CONFIG_FILE, "log", "level"); -+ if (level != NULL) -+ { ++ if (level != NULL) { + if (level[strlen(level) - 1] == '\r') { + size_t len = strlen(level); + level[len - 1] = '\0'; + } - -- err = argp_parse(&argp, argc, argv, 0, NULL, NULL); -- if (err) -- return err; ++ + setLogLevel(level); -+ ; + free(level); + } + -+ err = argp_parse(&argp, argc, argv, 0, NULL, NULL); -+ if (err) -+ return err; + err = argp_parse(&argp, argc, argv, 0, NULL, NULL); + if (err) + return err; snprintf(filename, sizeof(filename), BPF_FILE); -- if (load_bpf_file(filename)) { -- return 1; + logMessage(LOG_LEVEL_DEBUG, " load_bpf_file before.\n"); -+ if (load_bpf_file(filename)) -+ { -+ return 1; + if (load_bpf_file(filename)) { + return 1; } + logMessage(LOG_LEVEL_DEBUG, " load_bpf_file after.\n"); signal(SIGINT, sig_handler); - dir = opendir("/dev"); -- if (dir == NULL) { -+ if (dir == NULL) -+ { - printf("Failed to open /dev directory"); +@@ -240,20 +367,28 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } -- while ((entry = readdir(dir)) != NULL) { ++ + while ((entry = readdir(dir)) != NULL) { - if (entry->d_type == DT_BLK) { -+ logMessage(LOG_LEVEL_DEBUG, " start readdir while.\n"); -+ while ((entry = readdir(dir)) != NULL) -+ { -+ logMessage(LOG_LEVEL_DEBUG, " enter readdir while.\n"); -+ if (entry->d_type == DT_BLK) -+ { -+ logMessage(LOG_LEVEL_DEBUG, " enter DT_BLK if.\n"); - snprintf(path, sizeof(path), "/dev/%s", entry->d_name); - struct stat statbuf; +- snprintf(path, sizeof(path), "/dev/%s", entry->d_name); +- struct stat statbuf; - if (lstat(path, &statbuf) == 0) { - if (S_ISBLK(statbuf.st_mode)) { -+ if (lstat(path, &statbuf) == 0) -+ { -+ logMessage(LOG_LEVEL_DEBUG, " enter lstat if.\n"); -+ if (S_ISBLK(statbuf.st_mode)) -+ { -+ logMessage(LOG_LEVEL_DEBUG, " enter S_ISBLK if.\n"); - devices[device_count].major = major(statbuf.st_rdev); - devices[device_count].minor = minor(statbuf.st_rdev); - device_count++; +- devices[device_count].major = major(statbuf.st_rdev); +- devices[device_count].minor = minor(statbuf.st_rdev); +- device_count++; - if (device_count >= MAP_SIZE) { -+ if (device_count >= MAP_SIZE) -+ { -+ logMessage(LOG_LEVEL_DEBUG, " enter map_size if.\n"); - break; - } - } -@@ -259,51 +436,75 @@ int main(int argc, char **argv) { - - closedir(dir); - -- if (init_map(BLK_RES, "blk_res_map", device_count, devices) != 0) { -+ logMessage(LOG_LEVEL_DEBUG, " start blk init.\n"); -+ if (init_map(BLK_RES, "blk_res_map", device_count, devices) != 0) -+ { - return 1; - } -- if (init_map(BIO_RES, "blo_res_map", device_count, devices) != 0) { -+ logMessage(LOG_LEVEL_DEBUG, " finish blk init.\n"); -+ -+ logMessage(LOG_LEVEL_DEBUG, " start bio init.\n"); -+ if (init_map(BIO_RES, "blo_res_map", device_count, devices) != 0) -+ { - return 1; - } -- if (init_map(WBT_RES, "wbt_res_map", device_count, devices) != 0) { -+ logMessage(LOG_LEVEL_DEBUG, " finish bio init.\n"); -+ -+ logMessage(LOG_LEVEL_DEBUG, " start wbt init.\n"); -+ if (init_map(WBT_RES, "wbt_res_map", device_count, devices) != 0) -+ { - return 1; - } -- if (init_map(TAG_RES, "tag_res_map", device_count, devices) != 0) { -+ logMessage(LOG_LEVEL_DEBUG, " finish wbt init.\n"); -+ -+ logMessage(LOG_LEVEL_DEBUG, " start tag init.\n"); -+ if (init_map(TAG_RES, "tag_res_map", device_count, devices) != 0) -+ { - return 1; - } -+ logMessage(LOG_LEVEL_DEBUG, "finish tag init.\n"); -+ -+ for (;;) -+ { -+ -+ logMessage(LOG_LEVEL_DEBUG, " enter final for.\n"); -+ sleep(1); -+ -+ logMessage(LOG_LEVEL_DEBUG, " start print blk.\n"); -+ int io_dump_blk[MAP_SIZE] = {0}; -+ update_io_dump(BLK_RES_2, io_dump_blk, device_count, "rq_driver"); -+ err = print_map_res(BLK_RES, "rq_driver", device_count, io_dump_blk); -+ if (err) -+ break; -+ logMessage(LOG_LEVEL_DEBUG, " finish print blk.\n"); -+ -+ logMessage(LOG_LEVEL_DEBUG, " start print bio.\n"); -+ int io_dump_bio[MAP_SIZE] = {0}; -+ update_io_dump(BIO_RES_2, io_dump_bio, device_count, "bio"); -+ err = print_map_res(BIO_RES, "bio", device_count, io_dump_bio); -+ if (err) -+ break; -+ logMessage(LOG_LEVEL_DEBUG, " finish print bio.\n"); -+ -+ logMessage(LOG_LEVEL_DEBUG, " start print tag.\n"); -+ int io_dump_tag[MAP_SIZE] = {0}; -+ update_io_dump(TAG_RES_2, io_dump_tag, device_count, "gettag"); -+ err = print_map_res(TAG_RES, "gettag", device_count, io_dump_tag); -+ if (err) -+ break; -+ logMessage(LOG_LEVEL_DEBUG, " finish print tag.\n"); - -- for (;;) { -- -- sleep(1); -- -- int io_dump_blk[MAP_SIZE] = {0}; -- update_io_dump(BLK_RES_2, io_dump_blk, device_count,"rq_driver"); -- err = print_map_res(BLK_RES, "rq_driver", device_count, io_dump_blk); -- if (err) -- break; -- -- int io_dump_bio[MAP_SIZE] = {0}; -- update_io_dump(BIO_RES_2, io_dump_bio, device_count,"bio"); -- err = print_map_res(BIO_RES, "bio", device_count, io_dump_bio); -- if (err) -- break; -- -- int io_dump_tag[MAP_SIZE] = {0}; -- update_io_dump(TAG_RES_2, io_dump_tag, device_count,"gettag"); -- err = print_map_res(TAG_RES, "gettag", device_count, io_dump_tag); -- if (err) -- break; -- -- int io_dump_wbt[MAP_SIZE] = {0}; -- update_io_dump(WBT_RES_2, io_dump_wbt, device_count,"wbt"); -- err = print_map_res(WBT_RES, "wbt", device_count, io_dump_wbt); -- if (err) -- break; -- -- if (exiting) -- break; -+ logMessage(LOG_LEVEL_DEBUG, " start print wbt.\n"); -+ int io_dump_wbt[MAP_SIZE] = {0}; -+ update_io_dump(WBT_RES_2, io_dump_wbt, device_count, "wbt"); -+ err = print_map_res(WBT_RES, "wbt", device_count, io_dump_wbt); -+ if (err) -+ break; -+ logMessage(LOG_LEVEL_DEBUG, " finish print wbt.\n"); -+ -+ if (exiting) +- break; +- } +- } +- } ++ if (entry->d_type != DT_BLK) { ++ continue; ++ } ++ snprintf(path, sizeof(path), "/dev/%s", entry->d_name); ++ struct stat statbuf; ++ if (lstat(path, &statbuf) != 0 && !S_ISBLK(statbuf.st_mode)) { ++ continue; ++ } ++ if (!strncmp(entry->d_name, "dm-", 3) || !strncmp(entry->d_name, "loop", 4) || ++ !strncmp(entry->d_name, "md", 2)) { ++ continue; ++ } ++ if (!check_for_device(entry->d_name)) { ++ continue; ++ } ++ devices[device_count].major = major(statbuf.st_rdev); ++ devices[device_count].minor = minor(statbuf.st_rdev); ++ device_count++; ++ if (device_count >= MAP_SIZE) { + break; + } } -- return -err; -+ return -err; +@@ -306,4 +441,3 @@ int main(int argc, char **argv) { + + return -err; } - diff --git a/sysSentry.spec b/sysSentry.spec index b9ce0e0..86d7fcf 100644 --- a/sysSentry.spec +++ b/sysSentry.spec @@ -4,7 +4,7 @@ Summary: System Inspection Framework Name: sysSentry Version: 1.0.2 -Release: 69 +Release: 70 License: Mulan PSL v2 Group: System Environment/Daemons Source0: https://gitee.com/openeuler/sysSentry/releases/download/v%{version}/%{name}-%{version}.tar.gz -- Gitee