]> git.itanic.dy.fi Git - scan-pagemap/blob - analyze.c
analyzer: Print also the total numer of processes printed
[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                 list_for_each_entry(ml, &pf->ml->list, list) {
46                         if (ml->map->pid == af->pid)
47                                 goto get_stats;
48                 }
49                 return;
50         }
51
52 get_stats:
53         if (pf->page_present) {
54                 af->pages_present++;
55         } else if (pf->page_swapped) {
56                 af->pages_swapped++;
57         } else {
58                 af->pages_unused++;
59         }
60 }
61
62 /*
63  * print_page_stats - Prints system wide page stats
64  */
65 void print_page_stats(struct pageframe *pf)
66 {
67         struct analyze_frames af;
68         long count;
69         memset(&af, 0, sizeof(af));
70
71         af.ops.callback = count_pages;
72
73         count = bintree_walk(&pf->tree, &af.ops);
74         printf("present pages: %ld, %lld %sB\n"
75                 "Swapped pages: %ld, %lld %sB\n"
76                 "Unused pages: %ld, %lld %sB\n"
77                 "Total %ld physical pages, %lld %sB\n",
78                 af.pages_present,
79                 PAGE_TO_NICE(af.pages_present),
80                 PAGE_TO_NICE_UNIT(af.pages_present),
81                 af.pages_swapped,
82                 PAGE_TO_NICE(af.pages_swapped),
83                 PAGE_TO_NICE_UNIT(af.pages_swapped),
84                 af.pages_unused,
85                 PAGE_TO_NICE(af.pages_unused),
86                 PAGE_TO_NICE_UNIT(af.pages_unused),
87                 count,
88                 PAGE_TO_NICE(count),
89                 PAGE_TO_NICE_UNIT(count));
90 }
91
92 void print_pid_stats(struct pageframe *pf, struct process *process_list)
93 {
94         struct analyze_frames af;
95         struct process *ps;
96         long int swapped, present, total;
97         long int biggest = 0, second_biggest;
98         int count, processes = 0;
99
100         /*
101          * walk through all processes, find the one with most present
102          * pages
103          */
104         list_for_each_entry(ps, &process_list->list, list) {
105                 memset(&af, 0, sizeof(af));
106                 af.ops.callback = count_pages;
107                 af.pid = ps->pid;
108
109                 bintree_walk(&pf->tree, &af.ops);
110                 ps->pages_present = af.pages_present;
111                 ps->pages_swapped = af.pages_swapped;
112                 biggest = MAX(biggest, ps->pages_present + ps->pages_swapped);
113         }
114
115         printf("   in ram   swapped   pid name\n");
116
117 restart:
118         second_biggest = 0;
119         count = 0;
120         list_for_each_entry(ps, &process_list->list, list) {
121
122                 present = ps->pages_present;
123                 swapped = ps->pages_swapped;
124                 total = present + swapped;
125
126                 second_biggest = (total < biggest) &&
127                         (second_biggest < total) ?
128                         total : second_biggest;
129
130                 if (total != biggest)
131                         continue;
132
133                 if (total == 0)
134                         continue;
135
136                 printf("%6lld %sB %6lld %sB %5d %s\n",
137                         PAGE_TO_NICE(present), PAGE_TO_NICE_UNIT(present),
138                         PAGE_TO_NICE(swapped), PAGE_TO_NICE_UNIT(swapped),
139                         ps->pid, ps->name);
140                 count++;
141                 processes++;
142         }
143
144         if (count > 0) {
145                 biggest = second_biggest;
146                 goto restart;
147         }
148
149         printf("Total %d processes\n", processes);
150 }