]> git.itanic.dy.fi Git - scan-pagemap/blob - pagemap.h
4bb1302d3bc7e563bb3ab24fb960e5600da5ecf6
[scan-pagemap] / pagemap.h
1 #ifndef _PAGEMAP_H
2 #define _PAGEMAP_H
3
4 #include "utils.h"
5 #include "list.h"
6 #include "bintree.h"
7
8 #define PAGE_SHIFT      12
9 #define PAGE_SIZE       (1 << PAGE_SHIFT)
10
11 struct maps;
12
13 struct maps_list {
14         struct maps *map;
15         struct list_head list;
16 };
17 #define list_to_maps_list(list_head)                            \
18         container_of((list_head), struct maps_list, list)
19
20 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
21
22 struct pageframe {
23         struct bintree tree;
24         struct list_head ml;    /* List of mappings which refer to this pfn */
25
26         unsigned long long pf;  /* page frame entry from /proc/pid/pagemap */
27
28         int refcount;
29 };
30
31 #define tree_to_pageframe(tree_struct)                          \
32         container_of((tree_struct), struct pageframe, tree)
33
34 static inline void clear_pageframe(struct pageframe *pf)
35 {
36         memset(pf, 0, sizeof(*pf));
37         INIT_LIST_HEAD(&pf->ml);
38 }
39
40 static inline int page_present(struct pageframe *p)
41 {
42         return !!(BITRANGE(63, 63) & p->pf);
43 }
44
45 static inline int page_swapped(struct pageframe *p)
46 {
47         return !!(BITRANGE(62, 62) & p->pf);
48 }
49
50 static inline int page_shift(struct pageframe *p)
51 {
52         return (BITRANGE(55, 60) & p->pf) >> 55;
53 }
54
55 static inline long int pfn(struct pageframe *p)
56 {
57         return (BITRANGE(0, 54) & p->pf);
58 }
59
60 static inline int swap_type(struct pageframe *p)
61 {
62         return (BITRANGE(0, 4) & p->pf);
63 }
64
65 static inline int swap_offset(struct pageframe *p)
66 {
67         return (BITRANGE(5, 54) & p->pf) >> 5;
68 }
69
70 struct maps {
71         struct list_head list;
72
73         /* Memory segment of a mapping */
74         unsigned long start;
75         unsigned long end;
76         unsigned long size;
77
78         long int pages_present;
79         long int pages_swapped;
80
81         /* Name of the mapping, such as library name or something else */
82         char name[128];
83         int pid; /* Process which address space the mapping belongs to */
84         int tid; /* thread id */
85 };
86
87 #define list_to_maps(list_head)                         \
88         container_of((list_head), struct maps, list)
89
90 struct process {
91         struct maps *maps;
92         struct list_head list;
93         int pid;
94         int tid;
95         char name[256];
96
97         long int pages_present;
98         long int pages_swapped;
99
100         int is_initial_pid;
101 };
102
103 #define PARSE_PID               0x1
104 #define PARSE_MAP_NAME          0x2
105 #define PARSE_DUMP              0x8
106 #define PARSE_NOADD_TREE        0x10
107
108 struct pidlist {
109         struct list_head list;
110
111         int pid;
112 };
113
114 struct parse_opts {
115         struct list_head pidlist;
116
117         int parse_mask;
118         char *name;
119         int with_threads;
120 };
121
122 static inline void init_parse_opts(struct parse_opts *p)
123 {
124         memset(p, 0, sizeof(*p));
125         INIT_LIST_HEAD(&p->pidlist);
126 }
127
128 static inline struct pidlist *alloc_pidlist(void)
129 {
130         struct pidlist *p = malloc(sizeof(*p));
131
132         if (p == NULL)
133                 return p;
134
135         memset(p, 0, sizeof(*p));
136         INIT_LIST_HEAD(&p->list);
137
138         return p;
139 }
140
141 #define is_parse_option(parse_opts, flag) \
142         (!!((parse_opts)->parse_mask & (flag)))
143
144 #endif