]> git.itanic.dy.fi Git - scan-pagemap/blob - analyze.c
Add support for parsing all threads, not just thead parents
[scan-pagemap] / analyze.c
1 #include <string.h>
2 #include <stdio.h>
3
4 #include "analyze.h"
5 #include "utils.h"
6 #include "bintree.h"
7
8 #define SI_k    1024ll
9 #define SI_M    (SI_k * SI_k)
10 #define SI_G    (SI_M * SI_k)
11
12 #define PRETTY_THRESH   100
13 #define NICE_DIV(a)                                                     \
14         ((a) < SI_k * PRETTY_THRESH ? (a) :                             \
15                 (a < SI_M * PRETTY_THRESH ? ((a) / SI_k) :              \
16                         (a < SI_G * PRETTY_THRESH ? ((a) / SI_M) :      \
17                                 ((a) / SI_G))))
18
19 #define NICE_UNIT(a)                                                    \
20         ((a) < (SI_k * PRETTY_THRESH) ? " " :                           \
21                 ((a) < (SI_M * PRETTY_THRESH) ? "k" :                   \
22                         ((a) < (SI_G * PRETTY_THRESH) ? "M" : "G")))
23
24 #define PAGE_TO_NICE(a)         NICE_DIV((long long)a * PAGE_SIZE)
25 #define PAGE_TO_NICE_UNIT(a)    NICE_UNIT((long long)a * PAGE_SIZE)
26
27 struct analyze_frames {
28         struct bintree_ops ops;
29         long int pages_present;
30         long int pages_swapped;
31         long int pages_unused;
32         int pid;
33 };
34
35 #define bintree_ops_to_af(bintree_ops)                          \
36         container_of((bintree_ops), struct analyze_frames, ops)
37
38 static void count_pages(struct bintree *b, struct bintree_ops *ops)
39 {
40         struct pageframe *pf = tree_to_pageframe(b);
41         struct analyze_frames *af = bintree_ops_to_af(ops);
42         struct maps_list *ml;
43
44         if (af->pid) {
45                 if (!pf->ml)
46                         return;
47                 list_for_each_entry(ml, &pf->ml->list, list) {
48                         if (ml->map->pid == af->pid)
49                                 goto get_stats;
50                 }
51                 return;
52         }
53
54 get_stats:
55         if (pf->page_present) {
56                 af->pages_present++;
57         } else if (pf->page_swapped) {
58                 af->pages_swapped++;
59         } else {
60                 af->pages_unused++;
61         }
62 }
63
64 /*
65  * print_page_stats - Prints system wide page stats
66  */
67 void print_page_stats(struct pageframe *pf)
68 {
69         struct analyze_frames af;
70         long count;
71         memset(&af, 0, sizeof(af));
72
73         af.ops.callback = count_pages;
74
75         count = bintree_walk(&pf->tree, &af.ops);
76         printf("present pages: %ld, %lld %sB\n"
77                 "Swapped pages: %ld, %lld %sB\n"
78                 "Unused pages: %ld, %lld %sB\n"
79                 "Total %ld physical pages, %lld %sB\n",
80                 af.pages_present,
81                 PAGE_TO_NICE(af.pages_present),
82                 PAGE_TO_NICE_UNIT(af.pages_present),
83                 af.pages_swapped,
84                 PAGE_TO_NICE(af.pages_swapped),
85                 PAGE_TO_NICE_UNIT(af.pages_swapped),
86                 af.pages_unused,
87                 PAGE_TO_NICE(af.pages_unused),
88                 PAGE_TO_NICE_UNIT(af.pages_unused),
89                 count,
90                 PAGE_TO_NICE(count),
91                 PAGE_TO_NICE_UNIT(count));
92 }
93
94 void print_pid_stats(struct pageframe *pf, struct process *process_list)
95 {
96         struct analyze_frames af;
97         struct process *ps;
98         long int swapped, present, total;
99         long int biggest = 0, second_biggest;
100         int count, processes = 0;
101
102         /*
103          * walk through all processes, find the one with most present
104          * pages
105          */
106         list_for_each_entry(ps, &process_list->list, list) {
107                 memset(&af, 0, sizeof(af));
108                 af.ops.callback = count_pages;
109                 af.pid = ps->pid;
110
111                 bintree_walk(&pf->tree, &af.ops);
112                 ps->pages_present = af.pages_present;
113                 ps->pages_swapped = af.pages_swapped;
114                 biggest = MAX(biggest, ps->pages_present + ps->pages_swapped);
115         }
116
117         printf("   in ram   swapped   pid   tid name\n");
118
119 restart:
120         second_biggest = 0;
121         count = 0;
122         list_for_each_entry(ps, &process_list->list, list) {
123
124                 present = ps->pages_present;
125                 swapped = ps->pages_swapped;
126                 total = present + swapped;
127
128                 second_biggest = (total < biggest) &&
129                         (second_biggest < total) ?
130                         total : second_biggest;
131
132                 if (total != biggest)
133                         continue;
134
135                 if (total == 0)
136                         continue;
137
138                 printf("%6lld %sB %6lld %sB %5d %5d %s\n",
139                         PAGE_TO_NICE(present), PAGE_TO_NICE_UNIT(present),
140                         PAGE_TO_NICE(swapped), PAGE_TO_NICE_UNIT(swapped),
141                         ps->pid, ps->tid,ps->name);
142                 count++;
143                 processes++;
144         }
145
146         if (count > 0) {
147                 biggest = second_biggest;
148                 goto restart;
149         }
150
151         printf("Total %d processes\n", processes);
152 }