-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure_pagefault.h
137 lines (108 loc) · 3.26 KB
/
measure_pagefault.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef MEASURE_PAGEFAULT_H
#define MEASURE_PAGEFAULT_H
#include <stddef.h>
#include <linux/perf_event.h>
enum page_fault_measure_flags
{
MEASURE_HARD_FAULTS = 0x1,
MEASURE_SOFT_FAULTS = 0x2,
};
struct page_fault_ctx
{
int soft_fd;
int hard_fd;
int failed;
enum page_fault_measure_flags flags;
struct perf_event_attr soft_attr;
struct perf_event_attr hard_attr;
};
struct page_fault_result
{
size_t soft_fault_count;
size_t hard_fault_count;
};
struct page_fault_ctx start_pagefault_counter();
struct page_fault_result get_pagefault_counter(struct page_fault_ctx *ctx);
struct page_fault_result end_pagefault_counter(struct page_fault_ctx *ctx);
/******************/
/* IMPLEMENTATION */
/******************/
#if defined(MEASURE_PAGEFAULT_IMPLEMENTATION)
#define _GNU_SOURCE
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/ioctl.h>
#include <linux/hw_breakpoint.h> /* Definition of HW_* constants */
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <unistd.h>
#include <string.h>
// The error can be printed with strerror(errno) if the function
// returns -1
struct page_fault_ctx start_pagefault_counter(enum page_fault_measure_flags flags)
{
struct page_fault_ctx ctx;
memset(&ctx, 0, sizeof(struct page_fault_ctx));
ctx.flags = flags;
if (flags & MEASURE_SOFT_FAULTS)
{
ctx.soft_attr.config = PERF_COUNT_SW_PAGE_FAULTS_MIN;
ctx.soft_attr.size = sizeof(ctx.soft_attr);
ctx.soft_attr.type = PERF_TYPE_SOFTWARE;
ctx.soft_attr.disabled = 1;
ctx.soft_attr.exclude_kernel = 1;
ctx.soft_fd = syscall(SYS_perf_event_open, &ctx.soft_attr, 0, -1, -1, 0);
if (ctx.soft_fd == -1) {
ctx.failed = 1;
return ctx;
}
if (ioctl(ctx.soft_fd, PERF_EVENT_IOC_RESET, 0) == -1) {
return ctx;
}
if (ioctl(ctx.soft_fd, PERF_EVENT_IOC_ENABLE, 0) == -1) {
ctx.failed = 1;
return ctx;
}
}
if (flags & MEASURE_HARD_FAULTS)
{
ctx.hard_attr.config = PERF_COUNT_SW_PAGE_FAULTS_MAJ;
ctx.hard_attr.size = sizeof(ctx.hard_attr);
ctx.hard_attr.type = PERF_TYPE_SOFTWARE;
ctx.hard_attr.disabled = 1;
ctx.hard_attr.exclude_kernel = 1;
ctx.hard_fd = syscall(SYS_perf_event_open, &ctx.hard_attr, 0, -1, -1, 0);
if (ctx.hard_fd == -1) {
ctx.failed = 1;
return ctx;
}
ioctl(ctx.soft_fd, PERF_EVENT_IOC_RESET, 0);
ioctl(ctx.hard_fd, PERF_EVENT_IOC_ENABLE, 0);
}
return ctx;
}
struct page_fault_result get_pagefault_counter(struct page_fault_ctx *ctx)
{
struct page_fault_result result = {};
if (ctx->flags & MEASURE_SOFT_FAULTS) {
read(ctx->soft_fd, &result.soft_fault_count, sizeof(result.soft_fault_count));
}
if (ctx->flags & MEASURE_HARD_FAULTS) {
read(ctx->hard_fd, &result.hard_fault_count, sizeof(result.hard_fault_count));
}
return result;
}
struct page_fault_result end_pagefault_counter(struct page_fault_ctx *ctx)
{
struct page_fault_result result = {};
if (ctx->flags & MEASURE_SOFT_FAULTS) {
ioctl(ctx->soft_fd, PERF_EVENT_IOC_DISABLE, 0);
read(ctx->soft_fd, &result.soft_fault_count, sizeof(result.soft_fault_count));
}
if (ctx->flags & MEASURE_HARD_FAULTS) {
ioctl(ctx->hard_fd, PERF_EVENT_IOC_DISABLE, 0);
read(ctx->hard_fd, &result.hard_fault_count, sizeof(result.hard_fault_count));
}
return result;
}
#endif // MESAURE_PAGEFAULT_IMPLEMENTATION
#endif // MEASURE_PAGEFAULT_H