diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/sched_trace.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/sched_trace.c b/src/sched_trace.c new file mode 100644 index 0000000..011b13b --- /dev/null +++ b/src/sched_trace.c | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | |||
| 4 | #include <sys/types.h> | ||
| 5 | #include <sys/stat.h> | ||
| 6 | #include <sys/mman.h> | ||
| 7 | #include <fcntl.h> | ||
| 8 | #include <unistd.h> | ||
| 9 | |||
| 10 | #include "litmus.h" | ||
| 11 | #include "adaptive.h" | ||
| 12 | #include "sched_trace.h" | ||
| 13 | |||
| 14 | |||
| 15 | int walk_sched_trace(void* start, void* end, record_callback_t *cb) | ||
| 16 | { | ||
| 17 | void* pos = start; | ||
| 18 | trace_header_t* header; | ||
| 19 | int ret; | ||
| 20 | |||
| 21 | while (pos < end) { | ||
| 22 | header = (trace_header_t*) pos; | ||
| 23 | if (header->trace >= ST_MAX) | ||
| 24 | return INVALID_HEADER; | ||
| 25 | if (cb->handler[header->trace]) | ||
| 26 | ret = cb->handler[header->trace](header); | ||
| 27 | if (ret) | ||
| 28 | return ret; | ||
| 29 | pos += header->size; | ||
| 30 | } | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | static int map_file(const char* filename, void **addr, size_t *size) | ||
| 35 | { | ||
| 36 | struct stat info; | ||
| 37 | int error = 0; | ||
| 38 | int fd; | ||
| 39 | |||
| 40 | error = stat(filename, &info); | ||
| 41 | if (!error) { | ||
| 42 | *size = info.st_size; | ||
| 43 | if (info.st_size > 0) { | ||
| 44 | fd = open(filename, O_RDONLY); | ||
| 45 | if (fd >= 0) { | ||
| 46 | *addr = mmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | ||
| 47 | if (*addr == MAP_FAILED) | ||
| 48 | error = -1; | ||
| 49 | close(fd); | ||
| 50 | } else | ||
| 51 | error = fd; | ||
| 52 | } else | ||
| 53 | *addr = NULL; | ||
| 54 | } | ||
| 55 | return error; | ||
| 56 | } | ||
| 57 | |||
| 58 | int walk_sched_trace_file(const char* name, int keep_mapped, | ||
| 59 | record_callback_t *cb) | ||
| 60 | { | ||
| 61 | int ret; | ||
| 62 | size_t size; | ||
| 63 | void *start, *end; | ||
| 64 | |||
| 65 | ret = map_file(name, &start, &size); | ||
| 66 | if (!ret) { | ||
| 67 | end = start + size; | ||
| 68 | ret = walk_sched_trace(start, end, cb); | ||
| 69 | } | ||
| 70 | if (!keep_mapped) | ||
| 71 | munmap(start, size); | ||
| 72 | return ret; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
