-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiowaittop.c
190 lines (159 loc) · 4.57 KB
/
iowaittop.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* iowaittop/iowaittop.c
*
* Copyright (c) 2011 Mozilla Foundation
* Patrick Walton <[email protected]>
*/
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "bstrlib.h"
#define DISPLAYED_TASK_COUNT 5
struct task {
pid_t pid;
unsigned iowait_count;
};
struct delta {
pid_t pid;
unsigned iowait_count_delta;
};
int compare_pid_to_task(const void *key, const void *member)
{
const pid_t *pid = key;
const struct task *task = member;
return *pid - task->pid;
}
int compare_deltas(const void *a, const void *b)
{
const struct delta *delta_a = a, *delta_b = b;
return delta_b->iowait_count_delta - delta_a->iowait_count_delta;
}
int compare_tasks(const void *a, const void *b)
{
const struct task *task_a = a, *task_b = b;
return task_a->pid - task_b->pid;
}
bool get_iowait_count(const char *path, unsigned *count)
{
FILE *f;
if (!(f = fopen(path, "r")))
return false;
bstring line;
bool ok = false;
while ((line = bgets((bNgetc)fgetc, f, '\n'))) {
if (sscanf(bdata(line), "se.iowait_count : %u", count)) {
bdestroy(line);
ok = true;
break;
}
bdestroy(line);
}
fclose(f);
return ok;
}
bstring name_of(pid_t pid)
{
bstring path = bformat("/proc/%u/cmdline", (unsigned)pid);
if (!path)
return NULL;
FILE *f = fopen(bdata(path), "r");
bdestroy(path);
if (f) {
bstring result = bgets((bNgetc)fgetc, f, '\n');
fclose(f);
if (result && blength(result))
return result;
}
path = bformat("/proc/%u/status", (unsigned)pid);
if (!path)
return NULL;
f = fopen(bdata(path), "r");
bdestroy(path);
if (!f)
return NULL;
struct tagbstring name_str = bsStatic("Name:\t");
bstring line;
bstring result = NULL;
while ((line = bgets((bNgetc)fgetc, f, '\n'))) {
if (binstr(line, 0, &name_str) != 0) {
bdestroy(line);
continue;
}
result = bmidstr(line, blength(&name_str), blength(line) -
blength(&name_str) - 1);
bdestroy(line);
break;
}
fclose(f);
return result;
}
void display(const_bstring deltas)
{
printf("---\n");
printf("IOWAIT-COUNT PID NAME\n");
int i;
for (i = 0; i < DISPLAYED_TASK_COUNT; i++) {
if (i >= blength(deltas) / sizeof(struct delta))
break;
struct delta *delta = ((struct delta *)bdata(deltas)) + i;
bstring name = name_of(delta->pid);
printf("%12u %6u %s\n", delta->iowait_count_delta,
(unsigned)delta->pid, name ? bdata(name) : "???");
if (name)
bdestroy(name);
}
}
int main()
{
bstring tasks = NULL;
while (1) {
bstring new_tasks = bfromcstr("");
bstring deltas = bfromcstr("");
DIR *dir = opendir("/proc");
if (!dir) {
perror("couldn't open /proc");
return 1;
}
int task_count = blength(tasks) / sizeof(struct task);
struct dirent *ent;
while ((ent = readdir(dir))) {
struct task task;
if (!(task.pid = strtol(ent->d_name, NULL, 0)))
continue;
bstring path = bformat("/proc/%s/sched", ent->d_name);
bool iowait_present = get_iowait_count(bdata(path),
&task.iowait_count);
bdestroy(path);
if (!iowait_present)
continue;
if (tasks) {
struct task *old_task = bsearch(&task.pid, bdata(tasks),
task_count, sizeof(struct task), compare_pid_to_task);
if (old_task) {
struct delta delta;
delta.pid = task.pid;
delta.iowait_count_delta = task.iowait_count -
old_task->iowait_count;
if (bcatblk(deltas, &delta, sizeof(delta)) != BSTR_OK)
return 1;
}
}
if (bcatblk(new_tasks, &task, sizeof(task)) != BSTR_OK)
return 1;
}
closedir(dir);
qsort(bdata(deltas), blength(deltas) / sizeof(struct delta),
sizeof(struct delta), compare_deltas);
display(deltas);
bdestroy(deltas);
qsort(bdata(tasks), blength(tasks) / sizeof(struct task),
sizeof(struct task), compare_tasks);
if (tasks)
bdestroy(tasks);
tasks = new_tasks;
sleep(1);
}
return 0; // not reached
}