| 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 |
#include <stdio.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <inttypes.h> |
| 31 |
#include <config.h> |
| 32 |
|
| 33 |
#include "json_format.h" |
| 34 |
#include "dep.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#define JSON_FILE_PREFIX "build." |
| 40 |
#define JSON_FILE_EXT ".json" |
| 41 |
#define JSON_FILE_TEMPLATE "%s/" JSON_FILE_PREFIX "%d" JSON_FILE_EXT |
| 42 |
|
| 43 |
#define JSON_FILENAME_LEN (GET_PATH_MAX) |
| 44 |
|
| 45 |
#define JSON_FILE_VER "1.0.0" |
| 46 |
|
| 47 |
#ifdef JSON_COMPRESS |
| 48 |
#define LVL1 "" |
| 49 |
#else |
| 50 |
|
| 51 |
#define LVL1 "\t" |
| 52 |
#endif |
| 53 |
|
| 54 |
#define LVL2 LVL1 LVL1 |
| 55 |
#define LVL3 LVL2 LVL1 |
| 56 |
#define LVL4 LVL3 LVL1 |
| 57 |
#define LVL5 LVL4 LVL1 |
| 58 |
|
| 59 |
static void dump_preamble(FILE *fd, profile_context_t *ctx); |
| 60 |
static void dump_creator(FILE *fd, const char *creator); |
| 61 |
static void dump_argv(FILE *fd, const char *const *argv); |
| 62 |
static void dump_timestamps(FILE *fd, profile_context_t *ctx); |
| 63 |
static void dump_targets(FILE *fd); |
| 64 |
static void dump_target(const void *item); |
| 65 |
static void dump_target_timestamps(FILE *fd, const profile_entry_t *p); |
| 66 |
static void dump_depends(FILE *fd, const profile_call_t *c); |
| 67 |
|
| 68 |
static char json_fname[GET_PATH_MAX]; |
| 69 |
static FILE *json_fd; |
| 70 |
static bool first_entry; |
| 71 |
|
| 72 |
static void |
| 73 |
escape_print(FILE *fd, const char *str) |
| 74 |
{ |
| 75 |
for (const char *c = str; *c; c++) { |
| 76 |
switch (*c) { |
| 77 |
|
| 78 |
case '\b': |
| 79 |
case '\t': |
| 80 |
case '\n': |
| 81 |
case '\r': |
| 82 |
fputc(' ', fd); |
| 83 |
break; |
| 84 |
|
| 85 |
|
| 86 |
case '\"': |
| 87 |
case '\\': |
| 88 |
fputc('\\', fd); |
| 89 |
__attribute__((fallthrough)); |
| 90 |
|
| 91 |
default: |
| 92 |
fputc(*c, fd); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
static void |
| 98 |
format_timestamp(FILE *fd, FILE_TIMESTAMP ts) |
| 99 |
{ |
| 100 |
if (ts == 0) { |
| 101 |
fprintf(fd, "null"); |
| 102 |
} else { |
| 103 |
uintmax_t s = FILE_TIMESTAMP_S(ts); |
| 104 |
uintmax_t ns = FILE_TIMESTAMP_NS(ts); |
| 105 |
fprintf(fd, "%" PRIuMAX ".%09" PRIuMAX, s, ns); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
dump_preamble(FILE *fd, profile_context_t *ctx) |
| 111 |
{ |
| 112 |
fprintf(fd, "{\n"); |
| 113 |
fprintf(fd, LVL1 "\"version\":\"%s\",\n", JSON_FILE_VER); |
| 114 |
fprintf(fd, LVL1 "\"pid\":%d,\n", ctx->pid); |
| 115 |
if (makeparent_pid != 0) { |
| 116 |
fprintf(fd, LVL1 "\"parent\":{\n"); |
| 117 |
fprintf(fd, LVL2 "\"pid\":%d,\n", makeparent_pid); |
| 118 |
fprintf(fd, LVL2 "\"target\":\""); |
| 119 |
escape_print(fd, makeparent_target); |
| 120 |
fprintf(fd, "\"\n"); |
| 121 |
fprintf(fd, LVL1 "},\n"); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
dump_creator(FILE *fd, const char *creator) |
| 127 |
{ |
| 128 |
fprintf(fd, LVL1 "\"creator\":\"%s\",\n", creator); |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
dump_argv(FILE *fd, const char *const *argv) |
| 133 |
{ |
| 134 |
fprintf(fd, LVL1 "\"argv\": ["); |
| 135 |
for (size_t i = 0; argv[i]; i++) { |
| 136 |
if (i != 0) { |
| 137 |
fprintf(fd, ","); |
| 138 |
} |
| 139 |
|
| 140 |
fprintf(fd, "\n" LVL2 "\""); |
| 141 |
escape_print(fd, argv[i]); |
| 142 |
fprintf(fd, "\""); |
| 143 |
} |
| 144 |
fprintf(fd, "\n" LVL1 "],\n"); |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
dump_timestamps(FILE *fd, profile_context_t *ctx) |
| 149 |
{ |
| 150 |
fprintf(fd, LVL1 "\"start\":"); |
| 151 |
format_timestamp(fd, ctx->start); |
| 152 |
fprintf(fd, ",\n"); |
| 153 |
|
| 154 |
fprintf(fd, LVL1 "\"end\":"); |
| 155 |
format_timestamp(fd, ctx->end); |
| 156 |
fprintf(fd, ",\n"); |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
dump_jobserver(FILE *fd, profile_context_t *ctx) |
| 161 |
{ |
| 162 |
fprintf(fd, LVL1 "\"jobs\":%d,\n", ctx->jobs); |
| 163 |
fprintf(fd, LVL1 "\"server\":%s,\n", ctx->jobserver ? "true" : "false"); |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
static void |
| 168 |
dump_status(FILE *fd, const char *status) |
| 169 |
{ |
| 170 |
fprintf(fd, LVL1 "\"status\":\"%s\",\n", status); |
| 171 |
} |
| 172 |
|
| 173 |
static void |
| 174 |
dump_entry(FILE *fd, const struct goaldep *entry) |
| 175 |
{ |
| 176 |
fprintf(fd, LVL1 "\"directory\":\"%s\",\n", starting_directory); |
| 177 |
fprintf(fd, LVL1 "\"entry\":[\n"); |
| 178 |
for (const struct goaldep *e = entry; e; e=e->next) { |
| 179 |
fprintf(fd, LVL2 "\""); |
| 180 |
escape_print(fd, e->file->name); |
| 181 |
if (e->next) { |
| 182 |
fprintf(fd, "\",\n"); |
| 183 |
} else { |
| 184 |
fprintf(fd, "\"\n"); |
| 185 |
} |
| 186 |
} |
| 187 |
fprintf(fd, LVL1 "],\n"); |
| 188 |
} |
| 189 |
|
| 190 |
static void |
| 191 |
dump_targets(FILE *fd) |
| 192 |
{ |
| 193 |
first_entry = true; |
| 194 |
fprintf(fd, LVL1 "\"targets\":["); |
| 195 |
profile_dump_entries(dump_target); |
| 196 |
fprintf(fd, "\n" LVL1 "]\n"); |
| 197 |
} |
| 198 |
|
| 199 |
static void |
| 200 |
dump_target(const void *item) |
| 201 |
{ |
| 202 |
const profile_entry_t *p = item; |
| 203 |
|
| 204 |
if (first_entry) { |
| 205 |
first_entry = false; |
| 206 |
} else { |
| 207 |
fprintf(json_fd, ","); |
| 208 |
} |
| 209 |
fprintf(json_fd, "\n"); |
| 210 |
|
| 211 |
fprintf(json_fd, LVL2 "{\n"); |
| 212 |
fprintf(json_fd, LVL3 "\"name\":\""); |
| 213 |
escape_print(json_fd, p->name); |
| 214 |
fprintf(json_fd, "\",\n"); |
| 215 |
if (p->floc.filenm) { |
| 216 |
fprintf(json_fd, LVL3 "\"file\":\"%s\",\n", p->floc.filenm); |
| 217 |
} else { |
| 218 |
fprintf(json_fd, LVL3 "\"file\":null,\n"); |
| 219 |
} |
| 220 |
fprintf(json_fd, LVL3 "\"line\":%u,\n", (uint32_t)p->floc.lineno); |
| 221 |
dump_target_timestamps(json_fd, p); |
| 222 |
dump_depends(json_fd, p->calls); |
| 223 |
fprintf(json_fd, LVL2 "}"); |
| 224 |
fflush(json_fd); |
| 225 |
} |
| 226 |
|
| 227 |
static void |
| 228 |
dump_target_timestamps(FILE *fd, const profile_entry_t *p) |
| 229 |
{ |
| 230 |
fprintf(fd, LVL3 "\"start\":"); |
| 231 |
format_timestamp(fd, p->start); |
| 232 |
fprintf(fd, ",\n"); |
| 233 |
|
| 234 |
fprintf(fd, LVL3 "\"deps\":"); |
| 235 |
format_timestamp(fd, p->deps); |
| 236 |
fprintf(fd, ",\n"); |
| 237 |
|
| 238 |
fprintf(fd, LVL3 "\"recipe\":"); |
| 239 |
format_timestamp(fd, p->recipe); |
| 240 |
fprintf(fd, ",\n"); |
| 241 |
|
| 242 |
fprintf(fd, LVL3 "\"end\":"); |
| 243 |
format_timestamp(fd, p->end); |
| 244 |
fprintf(fd, ",\n"); |
| 245 |
} |
| 246 |
|
| 247 |
static void |
| 248 |
dump_depends(FILE *fd, const profile_call_t *c) |
| 249 |
{ |
| 250 |
fprintf(fd, LVL3 "\"depends\":[\n"); |
| 251 |
for (; c; c=c->next) { |
| 252 |
fprintf(fd, LVL4 "\""); |
| 253 |
escape_print(fd, c->target->name); |
| 254 |
if (c->next) { |
| 255 |
fprintf(fd, "\",\n"); |
| 256 |
} else { |
| 257 |
fprintf(fd, "\"\n"); |
| 258 |
} |
| 259 |
} |
| 260 |
fprintf(fd, LVL3 "]\n"); |
| 261 |
} |
| 262 |
|
| 263 |
extern bool |
| 264 |
json_init(profile_context_t *ctx, const char *creator, const char *const *argv) |
| 265 |
{ |
| 266 |
size_t len; |
| 267 |
|
| 268 |
len = sprintf(json_fname, JSON_FILE_TEMPLATE, ctx->output_dir, ctx->pid); |
| 269 |
|
| 270 |
if (len >= JSON_FILENAME_LEN) { |
| 271 |
printf("Error in generating json name\n"); |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
json_fd = fopen(json_fname, "w"); |
| 276 |
if (NULL == json_fd) { |
| 277 |
printf("Error in opening json file %s\n", json_fname); |
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
dump_preamble(json_fd, ctx); |
| 282 |
dump_creator(json_fd, creator); |
| 283 |
dump_argv(json_fd, argv); |
| 284 |
return true; |
| 285 |
} |
| 286 |
|
| 287 |
extern void |
| 288 |
json_close(profile_context_t *ctx, const char *program_status) |
| 289 |
{ |
| 290 |
dump_jobserver(json_fd, ctx); |
| 291 |
dump_status(json_fd, program_status); |
| 292 |
dump_timestamps(json_fd, ctx); |
| 293 |
dump_entry(json_fd, ctx->entry); |
| 294 |
dump_targets(json_fd); |
| 295 |
fprintf(json_fd, "}"); |
| 296 |
fclose(json_fd); |
| 297 |
printf("Created json profiling data file: %s\n", json_fname); |
| 298 |
} |