diff options
Diffstat (limited to 'tools/tool.h')
| -rw-r--r-- | tools/tool.h | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/tools/tool.h b/tools/tool.h new file mode 100644 index 0000000..d75342d --- /dev/null +++ b/tools/tool.h | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | #include <time.h> | ||
| 2 | #include <stdarg.h> | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <inttypes.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include <time.h> | ||
| 8 | |||
| 9 | #include "../src/nissy.h" | ||
| 10 | |||
| 11 | static void | ||
| 12 | log_stderr(const char *str, ...) | ||
| 13 | { | ||
| 14 | va_list args; | ||
| 15 | |||
| 16 | va_start(args, str); | ||
| 17 | vfprintf(stderr, str, args); | ||
| 18 | va_end(args); | ||
| 19 | } | ||
| 20 | |||
| 21 | static double | ||
| 22 | timerun(void (*run)(void), char *name) | ||
| 23 | { | ||
| 24 | struct timespec start, end; | ||
| 25 | double tdiff, tdsec, tdnano; | ||
| 26 | |||
| 27 | printf("\n"); | ||
| 28 | fflush(stdout); | ||
| 29 | |||
| 30 | if (run == NULL) { | ||
| 31 | printf("> %s: nothing to run!\n", name); | ||
| 32 | fflush(stdout); | ||
| 33 | return -1.0; | ||
| 34 | } | ||
| 35 | |||
| 36 | printf("Running tool: %s\n", name); | ||
| 37 | printf("==========\n"); | ||
| 38 | fflush(stdout); | ||
| 39 | |||
| 40 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 41 | run(); | ||
| 42 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 43 | |||
| 44 | tdsec = end.tv_sec - start.tv_sec; | ||
| 45 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 46 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 47 | |||
| 48 | printf("==========\n"); | ||
| 49 | printf("\nTotal time: %.4fs\n", tdiff); | ||
| 50 | fflush(stdout); | ||
| 51 | |||
| 52 | return tdiff; | ||
| 53 | } | ||
| 54 | |||
| 55 | static int | ||
| 56 | getdata( | ||
| 57 | const char *solver, | ||
| 58 | const char *options, | ||
| 59 | char **buf, | ||
| 60 | const char *filename | ||
| 61 | ) { | ||
| 62 | int64_t s, size, sizeread; | ||
| 63 | FILE *f; | ||
| 64 | |||
| 65 | if ((size = nissy_datasize(solver, options)) == -1) { | ||
| 66 | printf("Error in datasize\n"); | ||
| 67 | goto getdata_error_nofree; | ||
| 68 | } | ||
| 69 | |||
| 70 | *buf = malloc(size); | ||
| 71 | |||
| 72 | if ((f = fopen(filename, "rb")) == NULL) { | ||
| 73 | fprintf(stderr, "Table file not found, generating them." | ||
| 74 | " This can take a while.\n"); | ||
| 75 | s = nissy_gendata(solver, options, *buf); | ||
| 76 | if (s != size) { | ||
| 77 | fprintf(stderr, "Error generating table"); | ||
| 78 | if (s != -1) | ||
| 79 | fprintf(stderr, " (got %" PRId64 " bytes)", s); | ||
| 80 | fprintf(stderr, "\n"); | ||
| 81 | goto getdata_error; | ||
| 82 | } | ||
| 83 | if ((f = fopen(filename, "wb")) == NULL) { | ||
| 84 | fprintf(stderr, "Could not write tables to file %s" | ||
| 85 | ", will be regenerated next time.\n", filename); | ||
| 86 | } else { | ||
| 87 | fwrite(*buf, size, 1, f); | ||
| 88 | fclose(f); | ||
| 89 | } | ||
| 90 | } else { | ||
| 91 | fprintf(stderr, "Reading tables from file %s\n", filename); | ||
| 92 | sizeread = fread(*buf, size, 1, f); | ||
| 93 | fclose(f); | ||
| 94 | if (sizeread != 1) { | ||
| 95 | fprintf(stderr, "Error reading table, stopping\n"); | ||
| 96 | goto getdata_error; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | return 0; | ||
| 101 | |||
| 102 | getdata_error: | ||
| 103 | free(*buf); | ||
| 104 | getdata_error_nofree: | ||
| 105 | return 1; | ||
| 106 | } | ||
