aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft2csv.c
blob: cc6636f61beaefc9e3990dc7843804fdb4a12306 (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
/*    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;

static unsigned int complete   = 0;
static unsigned int incomplete = 0;
static unsigned int filtered   = 0;
static unsigned int skipped    = 0;
static unsigned int non_rt     = 0;
static unsigned int interleaved = 0;

#define CYCLES_PER_US 2128

static unsigned long long threshold = CYCLES_PER_US * 1000; /* 1 ms == 1 full tick */

static struct timestamp* next(struct timestamp* start, struct timestamp* end,
			      int cpu)
{
	struct timestamp* pos;
	for (pos = start; pos != end && pos->cpu != cpu; pos++);
	return pos != end ? pos : 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 void show_csv(struct timestamp* first, struct timestamp *end)
{
	struct timestamp *second;

	second = find_second_ts(first, end);
	if (second) {
		if (second->timestamp - first->timestamp > threshold)
			filtered++;
		else if (first->task_type != TSK_RT &&
			 second->task_type != TSK_RT && !want_best_effort)
			non_rt++;
		else {
			printf("%llu, %llu, %llu\n",
			       (unsigned long long) first->timestamp,
			       (unsigned long long) second->timestamp,
			       (unsigned long long)
			       (second->timestamp - first->timestamp));
			complete++;
		}
	} else
		incomplete++;

}

static inline uint64_t bget(int x, uint64_t quad)

{
	return (((0xffll << 8 * x) & quad) >> 8 * x);
}

static inline uint64_t bput(uint64_t b, int pos)
{
	return (b << 8 * pos);
}

static inline uint64_t ntohx(uint64_t q)
{
	return (bput(bget(0, q), 7) | bput(bget(1, q), 6) |
		bput(bget(2, q), 5) | bput(bget(3, q), 4) |
		bput(bget(4, q), 3) | bput(bget(5, q), 2) |
		bput(bget(6, q), 1) | bput(bget(7, q), 0));
}

static void restore_byte_order(struct timestamp* start, struct timestamp* end)
{
	struct timestamp* pos = start;
	while (pos !=end) {
		pos->timestamp = ntohx(pos->timestamp);
		pos->seq_no    = ntohl(pos->seq_no);
		pos++;
	}
}

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);
}

#define USAGE \
	"Usage: ft2csv [-e] [-i] [-b] <event_name>  <logfile> \n"	\
	"   -e: endianess swap      -- restores byte order \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" \
	""

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

#define OPTS "eib"

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

	while ((opt = getopt(argc, argv, OPTS)) != -1) {
		switch (opt) {
		case 'e':
			swap_byte_order = 1;
			break;
		case 'i':
			want_interleaved = 0;
			break;
		case 'b':
			want_best_effort = 1;
			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 (swap_byte_order)
		restore_byte_order(ts, end);
	show_id(ts, end, id);

	fprintf(stderr,
		"Total       : %10d\n"
		"Skipped     : %10d\n"
		"Complete    : %10d\n"
		"Incomplete  : %10d\n"
		"Filtered    : %10d\n"
		"Non RT      : %10d\n"
		"Interleaved : %10d\n",
		(int) count,
		skipped, complete,
		incomplete, filtered, non_rt,
		interleaved);

	return 0;
}