aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft2csv.c
blob: b75b284e2efd8497f7ab47c4d48b1c253960ff89 (plain) (blame)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*    ft2csv -- Convert start and end events from a Feather-Trace
 *              event stream into a CSV file.
 *    Copyright (C) 2007, 2008  B. Brandenburg.
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>

#include "mapping.h"

#include "timestamp.h"

static int want_interleaved    = 1;
static int want_best_effort    = 0;

/* discard samples from a specific CPU */
static int avoid_cpu = -1;
/* only use samples from a specific CPU */
static int only_cpu  = -1;

static unsigned int complete   = 0;
static unsigned int incomplete = 0;
static unsigned int skipped    = 0;
static unsigned int non_rt     = 0;
static unsigned int interleaved = 0;
static unsigned int avoided    = 0;
static unsigned int lvl_a_sched = 0;
static unsigned int lvl_b_sched = 0;
static unsigned int lvl_c_sched = 0;
static unsigned int other_sched = 0;
static unsigned int filtered = 0;

#define CYCLES_PER_US 2128
static unsigned long long threshold = CYCLES_PER_US * 10000; /* 10 ms == 10 full ticks */

static struct timestamp* next(struct timestamp* start, struct timestamp* end,
			      int cpu)
{
	struct timestamp* pos;
	unsigned int last_seqno = 0;

	for (pos = start; pos != end;  pos++) {
		/* check for for holes in the sequence number */
		if (last_seqno && last_seqno + 1 != pos->seq_no) {
			/* stumbled across a hole */
			return NULL;
		}
		last_seqno = pos->seq_no;

		if (pos->cpu == cpu)
			return pos;
	}
	return NULL;
}

static struct timestamp* next_id(struct timestamp* start, struct timestamp* end,
				 int cpu, unsigned long id,
				 unsigned long stop_id)
{
	struct timestamp* pos = start;
	int restarts = 0;

	while ((pos = next(pos, end, cpu))) {
		if (pos->event == id)
			break;
		else if (pos->event == stop_id)
			return NULL;
		pos++;
		restarts++;
		if (!want_interleaved)
			return NULL;
	}
	if (pos)
		interleaved += restarts;
	return pos;
}

static struct timestamp* find_second_ts(struct timestamp* start,
					struct timestamp* end)
{
	/* convention: the end->event is start->event + 1 */
	return next_id(start + 1, end, start->cpu, start->event + 1,
		       start->event);
}

static struct timestamp* next_pid(struct timestamp* start, struct timestamp* end,
				  unsigned int pid, unsigned long id1, unsigned long id2)
{
	struct timestamp* pos;
	unsigned int last_seqno = 0;

	for (pos = start; pos != end;  pos++) {
		/* check for for holes in the sequence number */
		if (last_seqno && last_seqno + 1 != pos->seq_no) {
			/* stumbled across a hole */
			return NULL;
		}
		last_seqno = pos->seq_no;

		/* only care about this PID */
		if (ts_pid_fragment(pos) == pid) {
			/* is  it the right one? */
			if (pos->event == id1 || pos->event == id2)
				return pos;
			else
				/* Don't allow unexptected IDs interleaved.
				 * Tasks are sequential, there shouldn't be
				 * anything else. */
				return NULL;
		}
	}
	return NULL;
}

static struct timestamp* accumulate_exec_time(
	struct timestamp* start, struct timestamp* end,
	unsigned int pid, unsigned long stop_id, uint64_t *sum)
{
	struct timestamp* pos = start;
	uint64_t exec_start;

	*sum = 0;

	while (1) {
		exec_start = pos->timestamp;

		/* find a suspension */
		pos = next_pid(pos + 1, end, pid, TS_LOCK_SUSPEND, stop_id);
		if (!pos)
			/* broken stream */
			return NULL;

		/* account for exec until pos */
		*sum += pos->timestamp - exec_start;

		if (pos->event == stop_id)
			/* no suspension */
			return pos;

		/* find matching resume */
		pos = next_pid(pos + 1, end, pid, TS_LOCK_RESUME, 0);
		if (!pos)
			/* broken stream */
			return NULL;

		/* must be a resume => start over */
	}
}

typedef void (*pair_fmt_t)(struct timestamp* first, struct timestamp* second, uint64_t exec_time);

static void print_pair_csv(struct timestamp* first, struct timestamp* second, uint64_t exec_time)
{
	printf("%llu, %llu, %llu\n",
	       (unsigned long long) first->timestamp,
	       (unsigned long long) second->timestamp,
	       (unsigned long long) exec_time);
}

static void print_pair_bin(struct timestamp* first, struct timestamp* second, uint64_t exec_time)
{
	float delta =  exec_time;
	fwrite(&delta, sizeof(delta), 1, stdout);
}

pair_fmt_t format_pair = print_pair_csv;

static void find_event_by_pid(struct timestamp* first, struct timestamp* end)
{
	struct timestamp *second;
	uint64_t exec_time = 0;

	/* special case: take suspensions into account */
	if (first->event == TS_LOCK_START) {
		second = accumulate_exec_time(first, end,
					      ts_pid_fragment(first),
					      first->event + 3, &exec_time);
	} else {
		second = next_pid(first + 1, end, ts_pid_fragment(first),
				  first->event + 1, 0);
		if (second)
			exec_time = second->timestamp - first->timestamp;
	}
	if (second) {
		if (exec_time > threshold)
			filtered++;
		else
			format_pair(first, second, exec_time);
			complete++;
	} else
		incomplete++;
}

static void find_event_by_eid(struct timestamp *first, struct timestamp* end)
{
	struct timestamp *second;
	uint64_t exec_time;

	second = find_second_ts(first, end);
	if (second) {
		exec_time = second->timestamp - first->timestamp;
		if (exec_time > threshold)
			filtered++;
		else if (first->task_type != TSK_RT &&
			 second->task_type != TSK_RT && !want_best_effort)
		{
			if (TS_LVLA_SCHED_END == second->event &&
			    second->task_type == TSK_LVLA)
			{
				format_pair(first, second, exec_time);
				complete++;
				lvl_a_sched++;
			} else if (TS_LVLB_SCHED_END == second->event &&
				   second->task_type == TSK_LVLB)
			{
				format_pair(first, second, exec_time);
				complete++;
				lvl_b_sched++;
			} else if (TS_LVLC_SCHED_END == second->event &&
				   second->task_type == TSK_LVLC)
			{
				format_pair(first, second, exec_time);
				complete++;
				lvl_c_sched++;

			} else {
				non_rt++;
			}
		} else if ((TS_LVLA_SCHED_END == second->event ||
			    TS_LVLB_SCHED_END == second->event ||
			    TS_LVLC_SCHED_END == second->event) &&
			   TSK_RT == second->task_type)
		{
		        other_sched++;
		} else
		{
			format_pair(first, second, exec_time);
			complete++;
		}
	} else
		incomplete++;
}

static void show_csv(struct timestamp* first, struct timestamp *end)
{


	if (first->cpu == avoid_cpu ||
	    (only_cpu != -1 && first->cpu != only_cpu)) {
		avoided++;
		return;
	}

	if (first->event <= PID_RECORDS_RANGE)
		find_event_by_pid(first, end);
	else
		find_event_by_eid(first, end);
}

typedef void (*single_fmt_t)(struct timestamp* ts);

static void print_single_csv(struct timestamp* ts)
{
	printf("0, 0, %llu\n",
	       (unsigned long long) (ts->timestamp));
}

static void print_single_bin(struct timestamp* ts)
{
	float delta = ts->timestamp;

	fwrite(&delta, sizeof(delta), 1, stdout);
}

single_fmt_t single_fmt = print_single_csv;

static void show_single(struct timestamp* ts)
{
	if (ts->cpu == avoid_cpu ||
	    (only_cpu != -1 && ts->cpu != only_cpu)) {
		avoided++;
	} else if (ts->task_type == TSK_RT) {
		single_fmt(ts);
		complete++;
	} else
		non_rt++;
}

static void show_id(struct timestamp* start, struct timestamp* end,
		    unsigned long id)
{
	while (start !=end && start->event != id + 1) {
		skipped++;
		start++;
	}

	for (; start != end; start++)
		if (start->event == id)
			show_csv(start, end);
}

static void show_single_records(struct timestamp* start, struct timestamp* end,
				unsigned long id)
{
	for (; start != end; start++)
		if (start->event == id)
			show_single(start);
}

#define USAGE								\
	"Usage: ft2csv [-r] [-i] [-b] [-a CPU] [-o CPU] <event_name>  <logfile> \n" \
	"   -i: ignore interleaved  -- ignore samples if start "	\
	"and end are non-consecutive\n"					\
	"   -b: best effort         -- don't skip non-rt time stamps \n" \
	"   -r: raw binary format   -- don't produce .csv output \n"	\
	"   -a: avoid CPU           -- skip samples from a specific CPU\n" \
	"   -o: only CPU            -- skip all samples from other CPUs\n" \
	""

static void die(char* msg)
{
	if (errno)
		perror("error: ");
	fprintf(stderr, "%s\n", msg);
	fprintf(stderr, "%s", USAGE);
	exit(1);
}

#define OPTS "ibra:o:"

int main(int argc, char** argv)
{
	void* mapped;
	size_t size, count;
	struct timestamp *ts, *end;
	cmd_t id;
	int opt;
	char event_name[80];

	while ((opt = getopt(argc, argv, OPTS)) != -1) {
		switch (opt) {
		case 'i':
			want_interleaved = 0;
			fprintf(stderr, "Discarging interleaved samples.\n");
			break;
		case 'b':
			fprintf(stderr,"Not filtering samples from best-effort"
				" tasks.\n");
			want_best_effort = 1;
			break;
		case 'r':
			fprintf(stderr, "Generating binary (raw) output.\n");
			single_fmt  = print_single_bin;
			format_pair = print_pair_bin;
			break;
		case 'a':
			avoid_cpu = atoi(optarg);
			fprintf(stderr, "Disarding all samples from CPU %d.\n",
				avoid_cpu);
			break;
		case 'o':
			only_cpu = atoi(optarg);
			fprintf(stderr, "Using only samples from CPU %d.\n",
				only_cpu);
			break;
		default:
			die("Unknown option.");
			break;
		}
	}

	if (argc - optind != 2)
		die("arguments missing");
	if (map_file(argv[optind + 1], &mapped, &size))
		die("could not map file");

	if (!str2event(argv[optind], &id)) {
		/* see if it is a short name */
		snprintf(event_name, sizeof(event_name), "%s_START",
			  argv[optind]);
		if (!str2event(event_name, &id))
			die("Unknown event!");
	}

	ts    = (struct timestamp*) mapped;
	count = size / sizeof(struct timestamp);
	end   = ts + count;

	if (id >= SINGLE_RECORDS_RANGE)
		show_single_records(ts, end, id);
	else
		show_id(ts, end, id);

	fprintf(stderr,
		"Total               : %10d\n"
		"Skipped             : %10d\n"
		"Avoided             : %10d\n"
		"Complete            : %10d\n"
		"Incomplete          : %10d\n"
		"Filtered            : %10d\n"
		"Non RT              : %10d\n"
		"Interleaved         : %10d\n"
		"Lvl-A Sched         : %10d\n"
		"Lvl-B Sched         : %10d\n"
		"Lvl-C Sched         : %10d\n"
		"Other Sched (non-A) : %10d\n",
		(int) count,
		skipped, avoided, complete,
		incomplete, filtered, non_rt,
		interleaved, lvl_a_sched, lvl_b_sched, lvl_c_sched,
	        other_sched);


	return 0;
}