From 0d37bc9e68af076525d5082e25d02fc8034582ce Mon Sep 17 00:00:00 2001 From: lvying6 Date: Mon, 17 Oct 2022 19:51:16 +0800 Subject: [PATCH] some enhancement for adding testcase and updating README Signed-off-by: lvying6 --- Add-stack-protector-compile-option.patch | 26 ++ ...-source-code-compilation-description.patch | 36 +++ add-test-framework-and-testcasese.patch | 263 ++++++++++++++++++ atune_bpf_collection.spec | 26 +- ...d-comment-and-bugfix-for-config-pars.patch | 72 +++++ ...linux.h-instead-of-bpftool-gen-vmlin.patch | 11 +- 6 files changed, 427 insertions(+), 7 deletions(-) create mode 100644 Add-stack-protector-compile-option.patch create mode 100644 README-add-source-code-compilation-description.patch create mode 100644 add-test-framework-and-testcasese.patch create mode 100644 common_helper-add-comment-and-bugfix-for-config-pars.patch diff --git a/Add-stack-protector-compile-option.patch b/Add-stack-protector-compile-option.patch new file mode 100644 index 0000000..e698d1a --- /dev/null +++ b/Add-stack-protector-compile-option.patch @@ -0,0 +1,26 @@ +From 7b9428779bbbff411c802e5c80a3fbdaee8608e9 Mon Sep 17 00:00:00 2001 +From: Lv Ying +Date: Tue, 8 Mar 2022 19:06:34 -0800 +Subject: [PATCH] Add stack protector compile option + +Signed-off-by: Lv Ying +--- + Makefile | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/Makefile b/Makefile +index 1e070a6..84ff9fd 100644 +--- a/Makefile ++++ b/Makefile +@@ -4,7 +4,7 @@ CLANG ?= clang -v + VMLINUX ?= vmlinux.h + + INCLUDES := -I$(OUTPUT) -I$(dir $(VMLINUX)) +-CFLAGS := -g -Wall ++CFLAGS := -g -Wall -fstack-protector-strong + ARCH := $(shell uname -m | sed 's/x86_64/x86/') + + APPS = readahead_tune +-- +2.27.0 + diff --git a/README-add-source-code-compilation-description.patch b/README-add-source-code-compilation-description.patch new file mode 100644 index 0000000..6e38166 --- /dev/null +++ b/README-add-source-code-compilation-description.patch @@ -0,0 +1,36 @@ +From 68a48eb30434b822a4827d2b533db6be362d494d Mon Sep 17 00:00:00 2001 +From: lvying6 +Date: Thu, 13 Oct 2022 15:55:11 +0800 +Subject: [PATCH] README: add source code compilation description + +Signed-off-by: lvying6 +--- + README.md | 12 ++++++++++++ + 1 file changed, 12 insertions(+) + +diff --git a/README.md b/README.md +index 0941b3d..1750627 100644 +--- a/README.md ++++ b/README.md +@@ -34,6 +34,18 @@ or + 2. 通过`start_readahead_tune`命令启动/加载`readahead_tune.bpf` BPF Program。命令使用方法可以使用`start_readahead_tune -h|--help`帮助命令。 + 3. 通过`stop_readahead_tune`命令停止/卸载`readahead_tune.bpf` BPF Program。 + ++#### 源码编译 ++1. 安装编译依赖: ++```bash ++sudo dnf install make clang llvm libbpf-devel dwarves kernel-debuginfo bpftool -y ++``` ++2. 编译: ++```bash ++make ++``` ++ ++编译过程中可能遇到`kenrl-debuginfo`rpm包提供vmlinux路径与`uname -r`不一致的情况导致编译失败。这是由于当前运行的内核版本与`kernel-debuginfo`的版本不一致。openEuler yum源的kernel版本较高,可以执行`dnf update kernel`升级当前kernel到yum源的kernel版本,重启系统,kernel版本便与`kernel-debuginfo`版本一致,再重新编译。 ++ + #### 参与贡献 + + 1. Fork 本仓库 +-- +2.33.0 + diff --git a/add-test-framework-and-testcasese.patch b/add-test-framework-and-testcasese.patch new file mode 100644 index 0000000..a4c693b --- /dev/null +++ b/add-test-framework-and-testcasese.patch @@ -0,0 +1,263 @@ +From 49f525d214694d2be5ad551c2a51ec1f50f232ed Mon Sep 17 00:00:00 2001 +From: lvying6 +Date: Sun, 16 Oct 2022 15:42:33 +0800 +Subject: [PATCH] add test framework and testcasese + +Signed-off-by: lvying6 +--- + Makefile | 8 +++++++ + README.md | 21 +++++++++++++++++ + common_helper.h | 1 + + test/Makefile | 50 ++++++++++++++++++++++++++++++++++++++++ + test/evaluate-test.sh | 16 +++++++++++++ + test/test-driver.c | 15 ++++++++++++ + test/tst-common_helper.c | 46 ++++++++++++++++++++++++++++++++++++ + test/tst-conf-file | 7 ++++++ + 8 files changed, 164 insertions(+) + create mode 100644 test/Makefile + create mode 100755 test/evaluate-test.sh + create mode 100644 test/test-driver.c + create mode 100644 test/tst-common_helper.c + create mode 100644 test/tst-conf-file + +diff --git a/Makefile b/Makefile +index f5ba4bb..cac7521 100644 +--- a/Makefile ++++ b/Makefile +@@ -6,6 +6,7 @@ PAHOLE ?= pahole + READELF ?= readelf + VMLINUX ?= /usr/lib/debug/lib/modules/`uname -r`/vmlinux + VMLINUX_HEADER ?= $(OUTPUT)/vmlinux.h ++MAKE ?= make + + BTF_PAHOLE_PROBE := $(shell $(READELF) -S $(VMLINUX) | grep .BTF 2>&1) + INCLUDES := -I$(OUTPUT) +@@ -39,10 +40,17 @@ all: $(APPS) + debug: DEBUG_FLAGS = -DBPFDEBUG + debug: all + ++export CFLAGS OUTPUT Q + .PHONY: clean + clean: + $(call msg,CLEAN) + $(Q)rm -rf $(OUTPUT) $(APPS) $(patsubst %,%.bpf.o,$(APPS)) ++ $(Q)$(MAKE) -C test clean ++ ++.PHONY: check ++check: all ++ $(call msg, CHECK) ++ $(Q)$(MAKE) -C test + + $(OUTPUT): + $(call msg,MKDIR,$@) +diff --git a/README.md b/README.md +index 1750627..de4fd9d 100644 +--- a/README.md ++++ b/README.md +@@ -46,6 +46,27 @@ make + + 编译过程中可能遇到`kenrl-debuginfo`rpm包提供vmlinux路径与`uname -r`不一致的情况导致编译失败。这是由于当前运行的内核版本与`kernel-debuginfo`的版本不一致。openEuler yum源的kernel版本较高,可以执行`dnf update kernel`升级当前kernel到yum源的kernel版本,重启系统,kernel版本便与`kernel-debuginfo`版本一致,再重新编译。 + ++#### 测试 ++##### 执行测试用例 ++```bash ++make check ++``` ++执行测试用例依赖于源码构建,会自动编译。测试用例运行结果信息示例如下所示: ++```bash ++ CHECK ++ PASS: tst-common_helper ++ Test Summary: ++ Unsupport: 0 ++ Fail: 0 ++ Pass: 1 ++``` ++会显示每个测试用例的执行结果,如`tst-common_helper`测试用例执行成功。最后以`Test Summary:`行开头总结所有测试用例执行情况。`Unsupport`表示不支持在当前测试平台上运行的测试用例数目,`Fail`表示失败的测试用例数目,`Pass`表示成功的测试用例数目。 ++ ++##### 新增测试用例 ++如果要新增对源码中目标文件中的函数进行测试,则在`test`目录下新增前缀为`tst-`,后半部分与测试目标文件名一致的测试文件。如要对`common_helper.c`中的函数进行测试,则新建名为`tst-common_helper.c`的测试文件。 ++ ++测试文件中的测试用例定义在`int do_test(void)`函数中,测试用例首先应该检查能否在测试平台上进行运行,如果不支持,则返回2(unsupport);测试用例运行失败返回1(fail);成功则返回0(pass)。在测试文件结尾添加`#include "test/test-driver.c"`语句即可。 ++ + #### 参与贡献 + + 1. Fork 本仓库 +diff --git a/common_helper.h b/common_helper.h +index 8fc52a6..2680318 100644 +--- a/common_helper.h ++++ b/common_helper.h +@@ -2,6 +2,7 @@ + #define _COMMON_HELPER_H + + #include ++#include + + #define SHASH 11 + #define SYSLOG (1 << 0) +diff --git a/test/Makefile b/test/Makefile +new file mode 100644 +index 0000000..bbbdb7a +--- /dev/null ++++ b/test/Makefile +@@ -0,0 +1,50 @@ ++.DEFAULT_GOAL := check ++INCLUDES := -I../ ++ ++evaluate-test := evaluate-test.sh ++# test source code functions in object files ++test_objs := tst-common_helper ++ ++# Build C testcases code ++$(patsubst %, %.o, $(test_objs)): %.o: %.c ++ $(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@ ++ ++$(test_objs): %: %.o ++ $(Q)$(CC) $(CFLAGS) $^ ../$(OUTPUT)/$(strip $(subst tst-, ,$@)).o -o $@ ++ ++# all the lines in the recipe be passed to a single invocation of the shell ++.ONESHELL: ++SHELL = /bin/sh ++ ++define run_tests ++ unsupport=0 ++ fail=0 ++ pass=0 ++ for testcase in $1; do ++ ./$$testcase >$$testcase.out 2>&1 ++ ret=$$? ++ $(SHELL) $(evaluate-test) $$testcase $$ret ++ if [ $$ret -eq 2 ]; then ++ unsupport=`expr $$unsupport + 1` ++ else ++ if [ $$ret -eq 0 ]; then ++ pass=`expr $$pass + 1` ++ else ++ fail=`expr $$fail + 1` ++ fi ++ fi ++ done ++ ++ echo -e " Test Summary:" ++ echo -e " Unsupport: $$unsupport" ++ echo -e " Fail: $$fail" ++ echo -e " Pass: $$pass" ++endef ++ ++.PHONY: check ++check: $(test_objs) ++ @$(call run_tests, $<) ++ ++.PHONY: clean ++clean: ++ $(Q)rm -rf $(test_objs) $(patsubst %,%.o,$(test_objs)) $(patsubst %,%.out,$(test_objs)) +diff --git a/test/evaluate-test.sh b/test/evaluate-test.sh +new file mode 100755 +index 0000000..31a8914 +--- /dev/null ++++ b/test/evaluate-test.sh +@@ -0,0 +1,16 @@ ++#!/bin/bash ++ ++test_name=$1 ++rc=$2 ++ ++if [ $rc -eq 2 ]; then ++ result="UNSUPPORTED" ++else ++ if [ $rc -eq 0 ]; then ++ result="PASS" ++ else ++ result="FAIL" ++ fi ++fi ++ ++echo -e " $result: $test_name" +diff --git a/test/test-driver.c b/test/test-driver.c +new file mode 100644 +index 0000000..6e58c9c +--- /dev/null ++++ b/test/test-driver.c +@@ -0,0 +1,15 @@ ++/* ++ * glibc style testcase: ++ * this file should be included from testcases, the testcase should define a function: ++ * int do_test(void) ++ * ++ * The do_test return 0 to indicate a passing test, ++ * 1 to indicate a failing test ++ * 2 to indicate an unsupported test ++ */ ++extern int do_test(void); ++ ++int main(int argc, char **argv) ++{ ++ return do_test(); ++} +diff --git a/test/tst-common_helper.c b/test/tst-common_helper.c +new file mode 100644 +index 0000000..1f1ce21 +--- /dev/null ++++ b/test/tst-common_helper.c +@@ -0,0 +1,46 @@ ++#include ++#include "common_helper.h" ++ ++#define CONFIG_FILE "tst-conf-file" ++ ++struct expected_option { ++ const char *name; ++ const char *expected_val; ++}; ++ ++struct expected_option test_options[] = { ++ {"space_integer", "23"}, ++ {"space_float", "3.1415926"}, ++ {"no_space_str", "hello world"}, ++ {"space_str", "hello world"}, ++ {"empty_field", ""} ++}; ++ ++static int do_test(void) ++{ ++ int ret = 0; ++ struct opt **opts = parse_init(SHASH); ++ if (!opts) { ++ log(TERM, LOG_ERR, "parse_init failed\n"); ++ return 1; ++ } ++ ++ if (parse_config_file(TERM, CONFIG_FILE, opts, SHASH)) { ++ log(TERM, LOG_ERR, "parse_config_file failed\n"); ++ return 1; ++ } ++ ++ for (int i = 0; i < sizeof(test_options) / sizeof(struct expected_option); i++) { ++ const char *res = config_opt(opts, SHASH, test_options[i].name); ++ if (strcmp(test_options[i].expected_val, res)) { ++ ret = 1; ++ goto out; ++ } ++ } ++ ++out: ++ parse_fini(opts, SHASH); ++ return ret; ++} ++ ++#include "test/test-driver.c" +diff --git a/test/tst-conf-file b/test/tst-conf-file +new file mode 100644 +index 0000000..822de28 +--- /dev/null ++++ b/test/tst-conf-file +@@ -0,0 +1,7 @@ ++# Test comments ++ space_integer = 23 ++ space_float = 3.1415926 ++no_space_str=hello world ++space_str = hello world ++ empty_field = ++ +-- +2.33.0 + diff --git a/atune_bpf_collection.spec b/atune_bpf_collection.spec index 453bdbd..d8adfc1 100644 --- a/atune_bpf_collection.spec +++ b/atune_bpf_collection.spec @@ -1,12 +1,16 @@ Name: A-Tune-BPF-Collection Version: 1.0.0 -Release: 2 +Release: 4 License: Mulan PSL v2 Summary: BPF program collection to adjust fine-grained kernel mode to get better performance URL: https://gitee.com/openeuler/A-Tune-BPF-Collection Source0: https://gitee.com/openeuler/A-Tune-BPF-Collection/repository/archive/v%{version}.tar.gz -Patch1: use-generated-vmlinux.h-instead-of-bpftool-gen-vmlin.patch +Patch1: README-add-source-code-compilation-description.patch +Patch2: common_helper-add-comment-and-bugfix-for-config-pars.patch +Patch3: add-test-framework-and-testcasese.patch +Patch4: use-generated-vmlinux.h-instead-of-bpftool-gen-vmlin.patch +Patch5: Add-stack-protector-compile-option.patch BuildRequires: clang, llvm, libbpf-devel Requires: libbpf @@ -30,6 +34,9 @@ install -D -p -m 0755 start_readahead_tune %{buildroot}/%{_sbindir}/start_readah install -D -p -m 0755 stop_readahead_tune %{buildroot}/%{_sbindir}/stop_readahead_tune install -D -p -m 0644 readahead_tune.conf %{buildroot}%{_sysconfdir}/sysconfig/readahead_tune.conf +%check +make check + %files %{_sbindir}/readahead_tune %{_sbindir}/readahead_tune.bpf.o @@ -38,6 +45,21 @@ install -D -p -m 0644 readahead_tune.conf %{buildroot}%{_sysconfdir}/sysconfig/r %config(noreplace) %{_sysconfdir}/sysconfig/readahead_tune.conf %changelog +* Mon Oct 17 lvying - 1.0.0-4 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC:some enhancement: + README: add source code compilation description + common_helper: add comment and bugfix for config parse + add test framework and testcasese + +* Wed Mar 9 2022 lvying - 1.0.0-3 +- Type:enhancement +- ID:NA +- SUG:NA +- DESC: Add stack protector compile option + * Wed Dec 8 2021 lvying - 1.0.0-2 - Type:bugfix - ID:NA diff --git a/common_helper-add-comment-and-bugfix-for-config-pars.patch b/common_helper-add-comment-and-bugfix-for-config-pars.patch new file mode 100644 index 0000000..18c4f11 --- /dev/null +++ b/common_helper-add-comment-and-bugfix-for-config-pars.patch @@ -0,0 +1,72 @@ +From 7b9208a3c969c6af9b12acad4a09da4bc60aecc9 Mon Sep 17 00:00:00 2001 +From: lvying6 +Date: Fri, 14 Oct 2022 15:38:41 +0800 +Subject: [PATCH] common_helper: add comment and bugfix for config parse + +Signed-off-by: lvying6 +--- + common_helper.c | 18 +++++++++++++++++- + 1 file changed, 17 insertions(+), 1 deletion(-) + +diff --git a/common_helper.c b/common_helper.c +index 703f19c..3ef1a71 100644 +--- a/common_helper.c ++++ b/common_helper.c +@@ -31,6 +31,7 @@ static struct opt *new_opt(char *name, char *val) + return opt; + } + ++/* alloc option hash table */ + struct opt **parse_init(unsigned int size) + { + struct opt **opts = (struct opt **)malloc(size * sizeof(struct opt *)); +@@ -39,6 +40,7 @@ struct opt **parse_init(unsigned int size) + return opts; + } + ++/* free option hash table and inside options */ + void parse_fini(struct opt **opts, unsigned int size) + { + if (!opts) { +@@ -47,9 +49,15 @@ void parse_fini(struct opt **opts, unsigned int size) + + for (unsigned int i = 0; i < size; i++) { + if (opts[i] != NULL) { +- free_opt(opts[i]); ++ struct opt *opt = opts[i]; ++ while (opt) { ++ struct opt *next_opt = opt->next; ++ free_opt(opt); ++ opt = next_opt; ++ } + } + } ++ free(opts); + } + + static int empty(char *s) +@@ -86,6 +94,13 @@ static unsigned hash(const char *str, unsigned int size) + return hash % size; + } + ++/* ++ * parse config file by key-value way from config file, the result will be added to option hash table ++ * @where: where the log output ++ * @conf_fn: config file name ++ * @opts: option hash table(option pointer array) ++ * @size: option hash table size ++ */ + int parse_config_file(unsigned int where, const char *conf_fn, struct opt **opts, unsigned int size) + { + char *line = NULL; +@@ -142,6 +157,7 @@ ret: + return ret; + } + ++/* get config value string by config name */ + char* config_opt(struct opt **opts, unsigned int size, const char *name) + { + unsigned int h = hash(name, size); +-- +2.33.0 + diff --git a/use-generated-vmlinux.h-instead-of-bpftool-gen-vmlin.patch b/use-generated-vmlinux.h-instead-of-bpftool-gen-vmlin.patch index 719dfd1..b8d1091 100644 --- a/use-generated-vmlinux.h-instead-of-bpftool-gen-vmlin.patch +++ b/use-generated-vmlinux.h-instead-of-bpftool-gen-vmlin.patch @@ -1,4 +1,4 @@ -From 3c7be0b48c5bf00d7c9a2730d414006e17753093 Mon Sep 17 00:00:00 2001 +From 8a0aef4371ccc0d16e27382635e2cf81848becbd Mon Sep 17 00:00:00 2001 From: Lv Ying Date: Fri, 10 Dec 2021 02:05:10 +0800 Subject: [PATCH] use generated vmlinux.h instead of bpftool gen vmlinux.h in @@ -15,10 +15,10 @@ Signed-off-by: Lv Ying create mode 100644 vmlinux.h diff --git a/Makefile b/Makefile -index f5ba4bb..1e070a6 100644 +index cac7521..7823f7a 100644 --- a/Makefile +++ b/Makefile -@@ -1,21 +1,12 @@ +@@ -1,22 +1,13 @@ # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) OUTPUT := .output CLANG ?= clang -v @@ -27,6 +27,7 @@ index f5ba4bb..1e070a6 100644 -READELF ?= readelf -VMLINUX ?= /usr/lib/debug/lib/modules/`uname -r`/vmlinux -VMLINUX_HEADER ?= $(OUTPUT)/vmlinux.h + MAKE ?= make +VMLINUX ?= vmlinux.h -BTF_PAHOLE_PROBE := $(shell $(READELF) -S $(VMLINUX) | grep .BTF 2>&1) @@ -42,7 +43,7 @@ index f5ba4bb..1e070a6 100644 APPS = readahead_tune CLANG_BPF_SYS_INCLUDES = $(shell $(CLANG) -v -E - &1 \ -@@ -48,15 +39,8 @@ $(OUTPUT): +@@ -56,15 +47,8 @@ $(OUTPUT): $(call msg,MKDIR,$@) $(Q)mkdir -p $@ @@ -97410,5 +97411,5 @@ index 0000000..bcb9d76 +}; + -- -2.27.0 +2.33.0