summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhangyang <[email protected]>2023-11-13 07:05:38 +0000
committerzhangyang <[email protected]>2023-11-13 07:05:38 +0000
commitbcf6aa181e1b843416324e12a9e9cbadd31740be (patch)
treec6ab143e93f7dbeb7537bf9f419c11b1560f06a8
parent0c565496afd9867de31a2568f803ec2bead8121d (diff)
hugePage
-rw-r--r--Makefile7
-rw-r--r--README_zh.md8
-rw-r--r--helloworld.c2
-rw-r--r--hptest.c62
4 files changed, 74 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index dbb8fb3..4eeebe6 100644
--- a/Makefile
+++ b/Makefile
@@ -1,18 +1,23 @@
CC = gcc
CFLAGS = -Wall
PROG = helloworld
+HPTEST = hptest
KMOD = variable_monitor
obj-m := $(KMOD).o
$(KMOD)-objs := monitor_kernel.o
-all: $(PROG) module
+all: $(PROG) $(HPTEST) module
$(PROG): helloworld.c
$(CC) $(CFLAGS) -o $(PROG) helloworld.c monitor_user.c
+$(HPTEST): hptest.c
+ $(CC) $(CFLAGS) -o $(HPTEST) hptest.c monitor_user.c
+
module:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
rm -f $(PROG)
+ rm -f $(HPTEST)
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean \ No newline at end of file
diff --git a/README_zh.md b/README_zh.md
index f2c6cfa..ec0bebb 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -118,18 +118,20 @@ watch_args = (watch_arg){
## demo
项目主文件下
+- `helloworld.c`: 测试大量变量监控
+- `hptest.c`: 测试 hugePage 挂载
```bash
# 编译加载模块
-make && insmod watch_module.ko
-./watch
+make && insmod variable_monitor.ko
+./helloworld
```
dmesg 可以看到打印的堆栈信息
```bash
# 卸载模块,清理编译文件
-rmmod watch_module.ko && make clean
+rmmod variable_monitor.ko && make clean
```
仅在 `kernel 5.17.15-1.el8.x86_64` 测试,其他内核版本未测试.
diff --git a/helloworld.c b/helloworld.c
index 7386f84..1416d5a 100644
--- a/helloworld.c
+++ b/helloworld.c
@@ -3,7 +3,7 @@
#include <unistd.h>
#include <string.h>
-#define NUM_VARS 256
+#define NUM_VARS 2049
int main()
{
diff --git a/hptest.c b/hptest.c
new file mode 100644
index 0000000..4a7e494
--- /dev/null
+++ b/hptest.c
@@ -0,0 +1,62 @@
+#include "monitor_user.h"
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#define HUGEPAGE_SIZE (1024 * 1024 * 1024) // Huge Page 大小为 1GB
+
+int main() {
+ int fd;
+ void *addr;
+ watch_arg w_arg = {0};
+
+ // 打开一个 hugetlbfs 文件
+ fd = open("/run/mrzcpd/huge_pages/hugepagefile", O_CREAT | O_RDWR, 0755);
+ if (fd < 0) {
+ perror("open");
+ return 1;
+ }
+
+ // 映射 Huge Page 内存
+ addr = mmap(0, HUGEPAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (addr == MAP_FAILED) {
+ perror("mmap");
+ close(fd);
+ return 1;
+ }
+
+ // 大页内存 int 类型变量, for ++
+ int *p = (int *)addr;
+ *p = 0;
+
+ w_arg = (watch_arg){
+ .task_id = getpid(),
+ .ptr = p,
+ .name = "hptest",
+ .length_byte = sizeof(int),
+ .threshold = 20,
+ .unsigned_flag = 0,
+ .greater_flag = 1,
+ .time_ns = 2000, // on hyper-v, 1us will block all system. 2us just fine, maybe 1us is too short for hyper-v
+ };
+ start_watch(w_arg);
+
+ for (int i = 0; i < 100; i++)
+ {
+ (*p)++;
+ printf("p = %d\n", *p);
+ sleep(1);
+ }
+
+ // 释放 Huge Page 内存
+ if (munmap(addr, HUGEPAGE_SIZE) == -1) {
+ perror("munmap");
+ close(fd);
+ return 1;
+ }
+
+ close(fd);
+
+ return 0;
+} \ No newline at end of file