summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBjoern B. Brandenburg <bbb@cs.unc.edu>2008-10-29 23:24:23 -0400
committerBjoern B. Brandenburg <bbb@cs.unc.edu>2008-10-29 23:24:23 -0400
commit65ca5c2129f50228ce63f2df5ebe79f963e9b4be (patch)
treea983539ffd7b8433f9167775798fed03ff60e54c /src
parentb1c9e03d3c0a7eaf584b62454b941e06e2c2f5fa (diff)
factor out common code
Diffstat (limited to 'src')
-rw-r--r--src/eheap.c34
-rw-r--r--src/load.c42
-rw-r--r--src/showst.c70
-rw-r--r--src/st2asy.c70
4 files changed, 82 insertions, 134 deletions
diff --git a/src/eheap.c b/src/eheap.c
new file mode 100644
index 0000000..35f986e
--- /dev/null
+++ b/src/eheap.c
@@ -0,0 +1,34 @@
1#include <stdlib.h>
2
3#include "sched_trace.h"
4#include "eheap.h"
5
6int earlier_event(struct heap_node* _a, struct heap_node* _b)
7{
8 struct st_event_record *a, *b;
9 a = heap_node_value(_a);
10 b = heap_node_value(_b);
11 return event_time(a) < event_time(b);
12}
13
14
15struct heap* heapify_events(struct st_event_record *ev, unsigned int count)
16{
17 struct heap_node* hn;
18 struct heap* h;
19 h = malloc(sizeof(struct heap));
20 hn = malloc(sizeof(struct heap_node) * count);
21 if (!hn || !h)
22 return NULL;
23 heap_init(h);
24 while (count) {
25 heap_node_init(hn, ev);
26 heap_insert(earlier_event, h, hn);
27 hn++;
28 ev++;
29 count--;
30 }
31 return h;
32}
33
34
diff --git a/src/load.c b/src/load.c
new file mode 100644
index 0000000..9203fde
--- /dev/null
+++ b/src/load.c
@@ -0,0 +1,42 @@
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <sys/mman.h>
4#include <fcntl.h>
5#include <unistd.h>
6
7
8static int map_file(const char* filename, void **addr, size_t *size)
9{
10 struct stat info;
11 int error = 0;
12 int fd;
13
14 error = stat(filename, &info);
15 if (!error) {
16 *size = info.st_size;
17 if (info.st_size > 0) {
18 fd = open(filename, O_RDONLY);
19 if (fd >= 0) {
20 *addr = mmap(NULL, *size,
21 PROT_READ | PROT_WRITE,
22 MAP_PRIVATE, fd, 0);
23 if (*addr == MAP_FAILED)
24 error = -1;
25 close(fd);
26 } else
27 error = fd;
28 } else
29 *addr = NULL;
30 }
31 return error;
32}
33
34int map_trace(const char *name, void **start, void **end, size_t *size)
35{
36 int ret;
37
38 ret = map_file(name, start, size);
39 if (!ret)
40 *end = *start + *size;
41 return ret;
42}
diff --git a/src/showst.c b/src/showst.c
index ee256ff..2767404 100644
--- a/src/showst.c
+++ b/src/showst.c
@@ -1,68 +1,15 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <sys/mman.h>
6#include <fcntl.h>
7#include <unistd.h>
8 3
4#include "load.h"
9#include "sched_trace.h" 5#include "sched_trace.h"
10 6#include "eheap.h"
11static int map_file(const char* filename, void **addr, size_t *size)
12{
13 struct stat info;
14 int error = 0;
15 int fd;
16
17 error = stat(filename, &info);
18 if (!error) {
19 *size = info.st_size;
20 if (info.st_size > 0) {
21 fd = open(filename, O_RDONLY);
22 if (fd >= 0) {
23 *addr = mmap(NULL, *size,
24 PROT_READ | PROT_WRITE,
25 MAP_PRIVATE, fd, 0);
26 if (*addr == MAP_FAILED)
27 error = -1;
28 close(fd);
29 } else
30 error = fd;
31 } else
32 *addr = NULL;
33 }
34 return error;
35}
36
37static int map_trace(const char *name, void **start, void **end, size_t *size)
38{
39 int ret;
40
41 ret = map_file(name, start, size);
42 if (!ret)
43 *end = *start + *size;
44 return ret;
45}
46
47
48static void show(char* file)
49{
50 size_t s;
51 struct st_event_record *rec, *end;
52 if (map_trace(file, &rec, &end, &s) == 0) {
53 print_all(rec,
54 ((unsigned int)((char*) end - (char*) rec))
55 / sizeof(struct st_event_record));
56 } else
57 perror("mmap");
58}
59
60 7
61static struct heap* file2heap(char* file, unsigned int* count) 8static struct heap* file2heap(char* file, unsigned int* count)
62{ 9{
63 size_t s; 10 size_t s;
64 struct st_event_record *rec, *end; 11 struct st_event_record *rec, *end;
65 if (map_trace(file, &rec, &end, &s) == 0) { 12 if (map_trace(file, (void**) &rec, (void**) &end, &s) == 0) {
66 *count = ((unsigned int)((char*) end - (char*) rec)) 13 *count = ((unsigned int)((char*) end - (char*) rec))
67 / sizeof(struct st_event_record); 14 / sizeof(struct st_event_record);
68 return heapify_events(rec, *count); 15 return heapify_events(rec, *count);
@@ -71,17 +18,6 @@ static struct heap* file2heap(char* file, unsigned int* count)
71 return NULL; 18 return NULL;
72} 19}
73 20
74
75/*
76int main(int argc, char** argv)
77{
78 int i;
79 for (i = 1; i < argc; i++)
80 show(argv[i]);
81 return 0;
82}
83*/
84
85int main(int argc, char** argv) 21int main(int argc, char** argv)
86{ 22{
87 int i; 23 int i;
diff --git a/src/st2asy.c b/src/st2asy.c
index ee256ff..2767404 100644
--- a/src/st2asy.c
+++ b/src/st2asy.c
@@ -1,68 +1,15 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <sys/mman.h>
6#include <fcntl.h>
7#include <unistd.h>
8 3
4#include "load.h"
9#include "sched_trace.h" 5#include "sched_trace.h"
10 6#include "eheap.h"
11static int map_file(const char* filename, void **addr, size_t *size)
12{
13 struct stat info;
14 int error = 0;
15 int fd;
16
17 error = stat(filename, &info);
18 if (!error) {
19 *size = info.st_size;
20 if (info.st_size > 0) {
21 fd = open(filename, O_RDONLY);
22 if (fd >= 0) {
23 *addr = mmap(NULL, *size,
24 PROT_READ | PROT_WRITE,
25 MAP_PRIVATE, fd, 0);
26 if (*addr == MAP_FAILED)
27 error = -1;
28 close(fd);
29 } else
30 error = fd;
31 } else
32 *addr = NULL;
33 }
34 return error;
35}
36
37static int map_trace(const char *name, void **start, void **end, size_t *size)
38{
39 int ret;
40
41 ret = map_file(name, start, size);
42 if (!ret)
43 *end = *start + *size;
44 return ret;
45}
46
47
48static void show(char* file)
49{
50 size_t s;
51 struct st_event_record *rec, *end;
52 if (map_trace(file, &rec, &end, &s) == 0) {
53 print_all(rec,
54 ((unsigned int)((char*) end - (char*) rec))
55 / sizeof(struct st_event_record));
56 } else
57 perror("mmap");
58}
59
60 7
61static struct heap* file2heap(char* file, unsigned int* count) 8static struct heap* file2heap(char* file, unsigned int* count)
62{ 9{
63 size_t s; 10 size_t s;
64 struct st_event_record *rec, *end; 11 struct st_event_record *rec, *end;
65 if (map_trace(file, &rec, &end, &s) == 0) { 12 if (map_trace(file, (void**) &rec, (void**) &end, &s) == 0) {
66 *count = ((unsigned int)((char*) end - (char*) rec)) 13 *count = ((unsigned int)((char*) end - (char*) rec))
67 / sizeof(struct st_event_record); 14 / sizeof(struct st_event_record);
68 return heapify_events(rec, *count); 15 return heapify_events(rec, *count);
@@ -71,17 +18,6 @@ static struct heap* file2heap(char* file, unsigned int* count)
71 return NULL; 18 return NULL;
72} 19}
73 20
74
75/*
76int main(int argc, char** argv)
77{
78 int i;
79 for (i = 1; i < argc; i++)
80 show(argv[i]);
81 return 0;
82}
83*/
84
85int main(int argc, char** argv) 21int main(int argc, char** argv)
86{ 22{
87 int i; 23 int i;