aboutsummaryrefslogtreecommitdiffstats
path: root/src/load.c
blob: 994ac6d03f860fc7391479f8cb0a24d76447d416 (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
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>

#include "sched_trace.h"
#include "eheap.h"
#include "load.h"

static int map_file(const char* filename, void **addr, size_t *size)
{
	struct stat info;
	int error = 0;
	int fd;

	error = stat(filename, &info);
	if (!error) {
		*size = info.st_size;
		if (info.st_size > 0) {
			fd = open(filename, O_RDONLY);
			if (fd >= 0) {
				*addr = mmap(NULL, *size,
					     PROT_READ | PROT_WRITE,
					     MAP_PRIVATE, fd, 0);
				if (*addr == MAP_FAILED)
					error = -1;
				close(fd);
			} else
				error = fd;
		} else
			*addr = NULL;
	}
	return error;
}

int map_trace(const char *name, void **start, void **end, size_t *size)
{
	int ret;

	ret = map_file(name, start, size);
	if (!ret)
		*end = *start + *size;
	return ret;
}


static struct heap* heap_from_file(char* file, unsigned int* count)
{
	size_t s;
	struct st_event_record *rec, *end;
	if (map_trace(file, (void**) &rec, (void**) &end, &s) == 0) {
		*count = ((unsigned int)((char*) end - (char*) rec))
			/ sizeof(struct st_event_record);
		return heapify_events(rec, *count);
	} else
		fprintf(stderr, "mmap: %m (%s)\n", file);
	return NULL;
}

struct heap* load(char **files, int no_files, unsigned int *count)
{
	int i;
	unsigned int c;
	struct heap *h = NULL, *h2;
	*count = 0;
	for (i = 0; i < no_files; i++) {
		h2 = heap_from_file(files[i], &c);
		if (!h2)
			/* potential memory leak, don't care */
			return NULL;
		if (h)
			heap_union(earlier_event, h, h2);
		else
			h = h2;
		*count += c;
	}
	return h;
}



struct task tasks[MAX_TASKS];
struct evlink *sys_events = NULL;
struct evlink **sys_next  = &sys_events;
u64 time0 = 0;
u32 g_max_task = MAX_TASKS;
u32 g_min_task = 0;

void init_tasks(void)
{
	int i;

	for (i = 0; i < MAX_TASKS; i++) {
		tasks[i] = (struct task) {0, 0, NULL, NULL, NULL, NULL};
		tasks[i].next = &tasks[i].events;
	}
}

void crop_events(struct task* t, double min, double max)
{
	struct evlink **p;
	double time;
	p = &t->events;
	while (*p) {
		time = evtime((*p)->rec);
		if (time < min || time > max)
			*p = (*p)->next;
		else
			p = &(*p)->next;
	}
}

void crop_events_all(double min, double max)
{
	struct task* t;
	for_each_task(t)
		crop_events(t, min, max);
}

struct task* by_pid(int pid)
{
	struct task* t;
	if (!pid)
		return NULL;
	/* slow, don't care for now */
	for (t = tasks; t < tasks + MAX_TASKS; t++) {
		if (!t->pid) /* end, allocate */
			t->pid = pid;
		if (t->pid == pid)
			return t;
	}
	return NULL;
}

u32 count_tasks(void)

{
	struct task* t;
	u32 i = 0;
	for_each_task(t)
		i++;
	return i;
}


void split(struct heap* h, unsigned int count, int find_time0)
{
	struct evlink *lnk = malloc(count * sizeof(struct evlink));
	struct heap_node *hn;
	u64 time;
	struct st_event_record *rec;
	struct task* t;

	if (!lnk) {
		perror("malloc");
		return;
	}

	while ((hn = heap_take(earlier_event, h))) {
		rec = heap_node_value(hn);
		time =  event_time(rec);
		if (find_time0 && !time0 && time)
			time0 = time;
		t = by_pid(rec->hdr.pid);
		switch (rec->hdr.type) {
		case ST_PARAM:
			if (t)
				t->param = rec;
			else
				fprintf(stderr, "Dropped ST_PARAM record "
					"for PID %d.\n", rec->hdr.pid);
			break;
		case ST_NAME:
			if (t)
				t->name = rec;
			else
				fprintf(stderr, "Dropped ST_NAME record "
					"for PID %d.\n", rec->hdr.pid);
			break;
		default:
			lnk->rec = rec;
			lnk->next = NULL;
			if (t) {
				*(t->next) = lnk;
				t->next = &lnk->next;
				t->no_events++;
			} else {
				*(sys_next) = lnk;
				sys_next    = &lnk->next;
			}
			lnk++;
			break;
		}
	}
}

int tsk_cpu(struct task *t)
{
	if (t->param)
		return t->param->data.param.partition;
	else
		return -1;
}

const char* tsk_name(struct task* t)
{
	if (t->name)
		return t->name->data.name.cmd;
	else
		return "<unknown>";
}

u32 per(struct task* t)
{
	if (t->param)
		return t->param->data.param.period;
	else
		return 0;
}

u32 exe(struct task* t)
{
	if (t->param)
		return t->param->data.param.wcet;
	else
		return 0;
}

u32 idx(struct task* t)
{
	return (t - tasks);
}