-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
114 lines (102 loc) · 2.15 KB
/
test.c
File metadata and controls
114 lines (102 loc) · 2.15 KB
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
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
#include<errno.h>
#define DEVICE_FILENAME "/dev/mchar"
struct memory_stat_report
{
unsigned long vmpeak;
unsigned long vmsize;
unsigned long vmlck;
unsigned long vmpin;
unsigned long vmhwm;
unsigned long vmrss;
unsigned long rssanon;
unsigned long rssfile;
unsigned long rssshmem;
unsigned long vmdata;
unsigned long vmstk;
unsigned long vmexe;
unsigned long vmlib;
unsigned long vmpte;
unsigned long vmswap;
unsigned long hugetlb;
};
int main()
{
int fd;
int ret;
char *p = NULL;
char buff[64];
fd = open(DEVICE_FILENAME, O_RDWR|O_NDELAY);
while (1)
{
//usleep(100);
int pid = getpid();
ret = write(fd, (char*)(&pid), sizeof(pid));
if (ret < 0) {
printf("write error!\n");
perror("The write error");
if (errno == EBUSY)
continue;
ret = errno;
goto out;
}
ret = read(fd, buff, sizeof(size_t));
if (ret < 0) {
printf("read error!\n");
ret = errno;
goto out;
}
printf("mypid is: %d\n", getpid());
printf("read: %ld\n", *((size_t*)(&buff[0])));
if(fd >= 0) {
p = (char*)mmap(0,
4096,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0);
struct memory_stat_report* msr = (struct memory_stat_report*)p;
printf("VmPeak:\t%8lu kB\n" \
"VmSize:\t%8lu kB\n" \
"VmLck:\t%8lu kB\n" \
"VmPin:\t%8lu kB\n" \
"VmHWM:\t%8lu kB\n" \
"VmRSS:\t%8lu kB\n" \
"RssAnon:\t%8lu kB\n" \
"RssFile:\t%8lu kB\n" \
"RssShmem:\t%8lu kB\n" \
"VmData:\t%8lu kB\n" \
"VmStk:\t%8lu kB\n" \
"VmExe:\t%8lu kB\n" \
"VmLib:\t%8lu kB\n" \
"VmPTE:\t%8lu kB\n" \
"VmSwap:\t%8lu kB\n" \
"HugetlbPages:\t%8lu kB\n",
msr->vmpeak,
msr->vmsize,
msr->vmlck,
msr->vmpin,
msr->vmhwm,
msr->vmrss,
msr->rssanon,
msr->rssfile,
msr->rssshmem,
msr->vmdata,
msr->vmstk,
msr->vmexe,
msr->vmlib,
msr->vmpte,
msr->vmswap,
msr->hugetlb);
munmap(p, 4096);
}
}
close(fd);
//fd = open(DEVICE_FILENAME, O_RDWR|O_NDELAY);
out:
return ret;
}