-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlattop.c
293 lines (249 loc) · 6.51 KB
/
lattop.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright 2010 Red Hat Inc.
* Author: Michal Schmidt
* License: GPLv2
*/
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <poll.h>
#include <sched.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "lattop.h"
#include "process_accountant.h"
#include "sym_translator.h"
#include "lat_translator.h"
#include "polled_reader.h"
#include "timer_reader.h"
#include "signal_reader.h"
#include "stap_reader.h"
#include "timespan.h"
#define MAX_READERS 3
int arg_interval = 5;
int arg_count;
enum sort_by arg_sort = SORT_BY_MAX_LATENCY;
bool arg_reverse;
unsigned long long arg_min_delay;
unsigned long long arg_max_interruptible_delay = 5*NSEC_PER_MSEC;
pid_t arg_pid_filter;
static struct polled_reader *readers[MAX_READERS];
static struct pollfd poll_fds[MAX_READERS];
static unsigned num_readers;
static int should_quit;
static int start_reader(unsigned index)
{
int r;
if (!readers[index]) {
fprintf(stderr, "Out of memory.\n");
return -ENOMEM;
}
if (readers[index]->ops->start) {
r = readers[index]->ops->start(readers[index]);
if (r) {
fprintf(stderr, "Failed to start reader #%d: %s\n",
index, strerror(-r));
return r;
}
}
poll_fds[index].fd = readers[index]->ops->get_fd(readers[index]);
poll_fds[index].events = POLLIN;
return 0;
}
void lattop_reader_started(struct polled_reader *r)
{
/* stap reader */
assert(readers[0] == r);
assert(num_readers < MAX_READERS);
readers[num_readers] = timer_reader_new();
start_reader(num_readers);
num_readers++;
fprintf(stderr, "Systemtap probe activated. Reading data...\n");
}
static int main_loop(void)
{
int nready, i;
int r = 0;
while (!should_quit) {
nready = poll(poll_fds, num_readers, -1);
if (nready < 0) {
perror("poll");
return 1;
}
for (i = 0; i < num_readers; i++) {
if (!poll_fds[i].revents)
continue;
r = readers[i]->ops->handle_ready_fd(readers[i]);
if (r) {
should_quit = 1;
if (r > 0)
r = 0;
break;
}
}
}
return r;
}
static void fini(void)
{
int i;
for (i = 0; i < num_readers; i++) {
if (readers[i]->ops->fini)
readers[i]->ops->fini(readers[i]);
free(readers[i]);
}
pa_fini();
sym_translator_fini();
lat_translator_fini();
}
static int init(void)
{
int r, i;
struct sched_param schedp;
r = lat_translator_init();
if (r)
fprintf(stderr, "Warning: Failed to load latencytop translations.\n");
r = sym_translator_init();
if (r) {
fprintf(stderr, "Failed to init the symbol map.\n");
goto err;
}
pa_init();
readers[num_readers++] = stap_reader_new();
readers[num_readers++] = signal_reader_new();
assert(num_readers <= MAX_READERS);
fprintf(stderr, "Initializing Systemtap probe...\n");
for (i = 0; i < num_readers; i++) {
r = start_reader(i);
if (r < 0)
goto err;
}
memset(&schedp, 0, sizeof(schedp));
schedp.sched_priority = 10;
r = sched_setscheduler(0, SCHED_FIFO, &schedp);
if (r < 0)
fprintf(stderr, "Warning: Failed to set real-time scheduling policy: %s\n",
strerror(errno));
return 0;
err:
fini();
return r;
}
static void usage_and_exit(int code)
{
fprintf(stderr,
"Usage: lattop [-i INTERVAL] [-c COUNT] [-s SORT_BY] [-r]\n"
" -i, --interval=INTERVAL time in seconds between printouts (default: 5)\n"
" -c, --count=COUNT stop after COUNT printouts\n"
" -s, --sort=SORT_BY sort the output by one of:\n"
" 'max' maximum latency (default)\n"
" 'total' total latency\n"
" 'pid' pid of the process\n"
" -r, --reverse reverse the sort order\n"
" -m, --min-latency=MIN ignore latencies shorter than MIN microseconds\n"
" -M, --max-interruptible=MAX ignore latencies from interruptible sleeps longer\n"
" than MAX microseconds (default: 5000)\n"
" -p, --pid-filter=PID show only the process with the given PID\n");
exit(code);
}
static void parse_argv(int argc, char *argv[])
{
char *endptr;
int c, i, option_index = 0;
static const struct option long_options[] = {
{ "interval", required_argument, 0, 'i' },
{ "count", required_argument, 0, 'c' },
{ "sort", required_argument, 0, 's' },
{ "reverse", no_argument, 0, 'r' },
{ "min-latency", required_argument, 0, 'm' },
{ "max-interruptible", required_argument, 0, 'M' },
{ "pid-filter", required_argument, 0, 'p' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
static const char *sort_types[_NR_SORT_BY] = {
[SORT_BY_MAX_LATENCY] = "max",
[SORT_BY_TOTAL_LATENCY] = "total",
[SORT_BY_PID] = "pid",
};
for (;;) {
c = getopt_long(argc, argv, "i:c:s:rm:M:p:h", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'i':
arg_interval = atoi(optarg);
if (arg_interval <= 0) {
fprintf(stderr, "Interval must be a positive number.\n");
exit(1);
}
break;
case 'c':
arg_count = atoi(optarg);
if (arg_interval < 0) {
fprintf(stderr, "Count must be positive (or zero for infinite).\n");
exit(1);
}
break;
case 'r':
arg_reverse = true;
break;
case 's':
for (i = 0; i < _NR_SORT_BY; i++) {
if (!strcasecmp(optarg, sort_types[i]))
break;
}
if (i == _NR_SORT_BY) {
fprintf(stderr, "Unknown sort type '%s'. Must be one of: max, total, pid\n", optarg);
exit(1);
}
arg_sort = i;
break;
case 'm':
errno = 0;
arg_min_delay = strtoull(optarg, &endptr, 10);
if (errno || endptr == optarg || *endptr != '\0') {
fprintf(stderr, "Invalid delay specification '%s'\n", optarg);
exit(1);
}
arg_min_delay *= NSEC_PER_USEC;
break;
case 'M':
errno = 0;
arg_max_interruptible_delay = strtoull(optarg, &endptr, 10);
if (errno || endptr == optarg || *endptr != '\0') {
fprintf(stderr, "Invalid delay specification '%s'\n", optarg);
exit(1);
}
arg_max_interruptible_delay *= NSEC_PER_USEC;
break;
case 'p':
errno = 0;
arg_pid_filter = strtoul(optarg, &endptr, 10);
if (errno || endptr == optarg || *endptr != '\0') {
fprintf(stderr, "Invalid PID specification '%s'\n", optarg);
exit(1);
}
break;
case 'h':
usage_and_exit(0);
case '?':
default:
usage_and_exit(1);
}
}
if (optind < argc)
usage_and_exit(1);
}
int main(int argc, char *argv[])
{
int ret;
parse_argv(argc, argv);
if (init())
return 1;
ret = main_loop();
fini();
return ret;
}