summaryrefslogtreecommitdiff
path: root/testcase
diff options
context:
space:
mode:
authorzy <[email protected]>2023-11-16 13:11:45 +0800
committerzy <[email protected]>2023-11-16 13:11:45 +0800
commit4c63686513c0cce1b64f7aca2d6a9f2a2a379e98 (patch)
treed6e6d5c09c01e0d1f539cd1941f1e65bbe36c7e0 /testcase
parentb0365d12e761d268e47881c4a218681e78da3221 (diff)
Reorganize the source codeHEADmaster
Diffstat (limited to 'testcase')
-rw-r--r--testcase/helloworld.c51
-rw-r--r--testcase/hptest.c62
2 files changed, 113 insertions, 0 deletions
diff --git a/testcase/helloworld.c b/testcase/helloworld.c
new file mode 100644
index 0000000..554b258
--- /dev/null
+++ b/testcase/helloworld.c
@@ -0,0 +1,51 @@
+#include "../user/monitor_user.h"
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+#define NUM_VARS 2049
+
+int main()
+{
+ int i = 0;
+ int temps[NUM_VARS] = {0};
+ watch_arg watch_args[NUM_VARS] = {0};
+
+ cancel_watch();
+
+ for (i = 0; i < NUM_VARS; i++)
+ {
+ temps[i] = 100;
+
+ watch_args[i] = (watch_arg){
+ .task_id = getpid(),
+ .ptr = &temps[i],
+ .name = "temp",
+ .length_byte = sizeof(int),
+ .threshold = 150 + i,
+ .unsigned_flag = 0,
+ .greater_flag = 1,
+ .time_ns = 2000 + (i / 33) * 5000, // on hyper-v, 1us will block all system. 2us just fine, maybe 1us is too short for hyper-v
+ };
+ char name[20];
+ snprintf(name, sizeof(name), "temp%d", i);
+ // 拷贝字符串
+ strncpy(watch_args[i].name, name, (MAX_NAME_LEN + 1));
+
+ start_watch(watch_args[i]);
+ }
+
+ while (temps[NUM_VARS - 1] < 205)
+ {
+ for (i = 0; i < NUM_VARS; i++)
+ {
+ temps[i]++;
+ }
+ printf("Value of variable %d: %d", i, temps[0]);
+ printf("\n");
+ sleep(1);
+ }
+
+ cancel_watch();
+ return 0;
+} \ No newline at end of file
diff --git a/testcase/hptest.c b/testcase/hptest.c
new file mode 100644
index 0000000..ab2f0a8
--- /dev/null
+++ b/testcase/hptest.c
@@ -0,0 +1,62 @@
+#include "../user/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