| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <sys/types.h> |
| 22 |
#include <config.h> |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#undef _LARGE_FILES |
| 30 |
|
| 31 |
#include <unistd.h> |
| 32 |
#include <errno.h> |
| 33 |
|
| 34 |
#include "globals.h" |
| 35 |
#include "profile.h" |
| 36 |
#include "hash.h" |
| 37 |
#include "dep.h" |
| 38 |
|
| 39 |
#include "callgrind_format.h" |
| 40 |
#include "json_format.h" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static struct hash_table profile_table; |
| 45 |
|
| 46 |
static unsigned long |
| 47 |
profile_table_entry_hash_1 (const void *key) |
| 48 |
{ |
| 49 |
return_ISTRING_HASH_1 (((profile_entry_t *) key)->name); |
| 50 |
} |
| 51 |
|
| 52 |
static unsigned long |
| 53 |
profile_table_entry_hash_2 (const void *key) |
| 54 |
{ |
| 55 |
return_ISTRING_HASH_2 (((profile_entry_t *) key)->name); |
| 56 |
} |
| 57 |
|
| 58 |
static int |
| 59 |
profile_hash_cmp (const void *x, const void *y) |
| 60 |
{ |
| 61 |
return_ISTRING_COMPARE (((profile_entry_t *) x)->name, |
| 62 |
((profile_entry_t *) y)->name); |
| 63 |
} |
| 64 |
|
| 65 |
static profile_entry_t * |
| 66 |
add_profile_entry (const file_t *target) |
| 67 |
{ |
| 68 |
|
| 69 |
|
| 70 |
profile_entry_t *new; |
| 71 |
profile_entry_t **slot; |
| 72 |
profile_entry_t *profile_entry; |
| 73 |
|
| 74 |
new = xcalloc (sizeof (profile_entry_t)); |
| 75 |
new->name = target->name; |
| 76 |
|
| 77 |
slot = (profile_entry_t **) hash_find_slot (&profile_table, new); |
| 78 |
profile_entry = *slot; |
| 79 |
if (!HASH_VACANT (profile_entry)) { |
| 80 |
free(new); |
| 81 |
return profile_entry; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
new->calls = NULL; |
| 87 |
memcpy(&(new->floc), &(target->floc), sizeof(gmk_floc)); |
| 88 |
|
| 89 |
hash_insert_at (&profile_table, new, slot); |
| 90 |
return new; |
| 91 |
} |
| 92 |
|
| 93 |
static profile_context_t ctx; |
| 94 |
|
| 95 |
extern bool |
| 96 |
profile_init(const char *creator, const char *const *argv, int jobs) { |
| 97 |
|
| 98 |
ctx.jobs = jobs; |
| 99 |
ctx.pid = getpid(); |
| 100 |
ctx.output_dir = profile_directory; |
| 101 |
|
| 102 |
ctx.resolution = 0; |
| 103 |
ctx.start = file_timestamp_now(&ctx.resolution); |
| 104 |
|
| 105 |
#ifdef PROFILER_MIN_RES |
| 106 |
|
| 107 |
if (ctx.resolution > PROFILER_MIN_RES) { |
| 108 |
printf("Resolution is only %d ns. Profiling not supported\n", ctx.resolution); |
| 109 |
return false; |
| 110 |
} |
| 111 |
#endif |
| 112 |
|
| 113 |
switch (profile_flag) { |
| 114 |
case PROFILE_CALLGRIND: |
| 115 |
if (!callgrind_init(&ctx, creator, argv)) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
break; |
| 119 |
case PROFILE_JSON: |
| 120 |
if (!json_init(&ctx, creator, argv)) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
break; |
| 124 |
default: |
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
hash_init(&profile_table, 1000, profile_table_entry_hash_1, |
| 129 |
profile_table_entry_hash_2, profile_hash_cmp); |
| 130 |
|
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
static void add_timestamp(profile_entry_t *t, bool updated, enum cmd_state state) |
| 135 |
{ |
| 136 |
FILE_TIMESTAMP *ts = NULL; |
| 137 |
int resolution = 0; |
| 138 |
if (state == cs_not_started && t->start == 0) { |
| 139 |
|
| 140 |
ts = &t->start; |
| 141 |
} else if (state == cs_deps_running && t->deps == 0) { |
| 142 |
ts = &t->deps; |
| 143 |
} else if (state == cs_running && t->recipe == 0) { |
| 144 |
ts = &t->recipe; |
| 145 |
} else if ((state == cs_finished || updated) && t->end == 0) { |
| 146 |
ts = &t->end; |
| 147 |
} else { |
| 148 |
|
| 149 |
return; |
| 150 |
} |
| 151 |
*ts = file_timestamp_now(&resolution); |
| 152 |
|
| 153 |
if (t->start > 0 && t->end > 0) { |
| 154 |
t->elapsed_total = (t->end - t->start)/1000; |
| 155 |
} |
| 156 |
|
| 157 |
if (t->recipe > 0 && t->end > 0) { |
| 158 |
t->elapsed_recipe = (t->end - t->recipe)/1000; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
add_dependency(profile_entry_t *target, profile_entry_t *parent) |
| 164 |
{ |
| 165 |
profile_call_t *c = CALLOC(profile_call_t, 1); |
| 166 |
c->target = target; |
| 167 |
c->next = parent->calls; |
| 168 |
parent->calls = c; |
| 169 |
} |
| 170 |
|
| 171 |
extern void |
| 172 |
profile_add_dependency(file_t *target, file_t *parent) |
| 173 |
{ |
| 174 |
profile_entry_t *p = NULL; |
| 175 |
profile_entry_t *t = add_profile_entry(target); |
| 176 |
|
| 177 |
add_timestamp(t, (target->updated == 1), target->command_state); |
| 178 |
if (parent) { |
| 179 |
p = add_profile_entry(parent); |
| 180 |
add_dependency(t, p); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
extern void |
| 185 |
profile_update_target(file_t *target) |
| 186 |
{ |
| 187 |
profile_entry_t *t = add_profile_entry(target); |
| 188 |
add_timestamp(t, (target->updated == 1), target->command_state); |
| 189 |
memcpy(&(t->floc), &(target->floc), sizeof(gmk_floc)); |
| 190 |
} |
| 191 |
|
| 192 |
extern void |
| 193 |
profile_add_timestamp(file_t *target) |
| 194 |
{ |
| 195 |
profile_entry_t *t = add_profile_entry(target); |
| 196 |
add_timestamp(t, (target->updated == 1), target->command_state); |
| 197 |
} |
| 198 |
|
| 199 |
#ifdef DEBUG_PROFILE |
| 200 |
|
| 201 |
static void |
| 202 |
print_profile_entry (const void *item) |
| 203 |
{ |
| 204 |
const profile_entry_t *p = item; |
| 205 |
profile_call_t *c; |
| 206 |
printf("name: %s, file: %s, line: %" PRIu64 ", time: %" PRIu64 "\n", |
| 207 |
p->name, p->floc.filenm, p->floc.lineno, p->elapsed_total); |
| 208 |
for (c = p->calls; c; c = c->p_next) { |
| 209 |
printf("calls: %s\n", c->p_target->name); |
| 210 |
} |
| 211 |
} |
| 212 |
#endif |
| 213 |
|
| 214 |
extern void |
| 215 |
profile_dump_entries(profile_dump_entry_fn fn) { |
| 216 |
hash_map(&profile_table, fn); |
| 217 |
} |
| 218 |
|
| 219 |
extern void |
| 220 |
profile_close(const char *program_status, const struct goaldep *goal, bool jobserver) { |
| 221 |
int resolution = 0; |
| 222 |
ctx.end = file_timestamp_now(&resolution); |
| 223 |
|
| 224 |
ctx.elapsed = (ctx.end - ctx.start)/1000; |
| 225 |
ctx.entry = goal; |
| 226 |
ctx.jobserver = jobserver; |
| 227 |
|
| 228 |
#ifdef DEBUG_PROFILE |
| 229 |
profile_dump_entries(print_profile_entry); |
| 230 |
#endif |
| 231 |
|
| 232 |
switch (profile_flag) { |
| 233 |
case PROFILE_CALLGRIND: |
| 234 |
callgrind_close(&ctx, program_status); |
| 235 |
break; |
| 236 |
case PROFILE_JSON: |
| 237 |
json_close(&ctx, program_status); |
| 238 |
break; |
| 239 |
default: |
| 240 |
break; |
| 241 |
} |
| 242 |
} |