summaryrefslogtreecommitdiff
path: root/testcase/hptest.c
blob: 34ce6bd38342756356aca55e07e9e570e0c067aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "../source/uapi/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,
        .is_unsigned = 0,
        .above_threshold = 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;
}