aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft2csv.c
blob: dc5753bc37e6cfcc0b895fbaf5ecfb15bb20933c (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "mapping.h"

#include "timestamp.h"

static unsigned int incomplete = 0;
static unsigned int filtered   = 0;

static unsigned long long threshold = 2700 * 100;

static struct timestamp* next(struct timestamp** pos, size_t* count, int cpu)
{
	while (*count--)
		if ((++(*pos))->cpu == cpu)
			return *pos;       
	return NULL;
}

static struct timestamp* next_id(struct timestamp** pos, size_t* count, 
				 int cpu, unsigned long id) 
{
	struct timestamp* ret;
	while ((ret = next(pos, count, cpu)) && (*pos)->event != id);
	return ret;
}

static int find_pair(struct timestamp* start, 
		     struct timestamp** end,
		     size_t count)
{
	struct timestamp *pos = start;
	/* convention: the end->event is start->event + 1 */
	*end = next_id(&pos, &count, start->cpu, start->event + 1);
	return *end != NULL;
}

static void show_csv(struct timestamp* ts, size_t count)
{
	struct timestamp* start = ts;
	struct timestamp* stop;

	if (find_pair(start, &stop, count)) {
		if (stop->timestamp - start->timestamp > threshold)
			filtered++;
		else
			printf("%llu, %llu, %llu\n",
			       start->timestamp, stop->timestamp, 
			       stop->timestamp - start->timestamp);
	} else
		incomplete++;
	
}

/*static void show_all_per_cpu(struct timestamp* ts, size_t count)
{
	struct timestamp* _ts = ts;
	size_t _count = count;
	int cpu;
	
	for (cpu = 0; cpu < 4; cpu++) {
		count = _count;
		ts    = _ts;
		while (count--) {
			if (ts->cpu == cpu)
				show(ts, count);	
			ts++;
		}
	}
}*/

static void show_id(struct timestamp* ts, size_t count,  unsigned long id)
{
	while (count--)
		if (ts->event == id)
			show_csv(ts++, count);
		else
			ts++;
}


static void dump(struct timestamp* ts, size_t count,  unsigned long id)
{
	while (count--)
		printf("%lu\n", (ts++)->event);
}


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

int main(int argc, char** argv) 
{
	void* mapped;
	size_t size, count;
	struct timestamp* ts;	
	unsigned long id;

	if (argc != 3)
		die("Usage: ft2csv  <event_name>  <logfile>");
	if (map_file(argv[2], &mapped, &size))
		die("could not map file");

	if (!str2event(argv[1], &id))
		die("Unknown event!");

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

	show_id(ts, count, id);

	fprintf(stderr, 
		"Incomplete: %d\n"
		"Filtered  : %d\n", 
		incomplete, filtered);

	return 0;
}