| 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 |
#include "callgrind_format.h" |
| 25 |
|
| 26 |
#define CALLGRIND_FILE_PREFIX "callgrind.out." |
| 27 |
#define CALLGRIND_FILE_TEMPLATE "%s/" CALLGRIND_FILE_PREFIX "%d" |
| 28 |
|
| 29 |
|
| 30 |
#define CALLGRIND_FILENAME_LEN (GET_PATH_MAX) |
| 31 |
|
| 32 |
#define CALLGRIND_PREAMBLE_TEMPLATE1 "version: 1\n\ |
| 33 |
creator: %s\n" |
| 34 |
|
| 35 |
#define CALLGRIND_PREAMBLE_TEMPLATE2 "pid: %d\n\ |
| 36 |
\n\ |
| 37 |
desc: Trigger: %s\n\ |
| 38 |
desc: Node: Targets\n\ |
| 39 |
\n\ |
| 40 |
positions: line\n\ |
| 41 |
event: Wt : Wall Time\n\ |
| 42 |
event: Rt : Recipe Time\n\ |
| 43 |
events: Wt Rt\n" |
| 44 |
|
| 45 |
char callgrind_fname[CALLGRIND_FILENAME_LEN]; |
| 46 |
static FILE *callgrind_fd; |
| 47 |
|
| 48 |
extern bool |
| 49 |
callgrind_init(profile_context_t *ctx, const char *creator, const char *const *argv) { |
| 50 |
size_t len; |
| 51 |
unsigned int i; |
| 52 |
|
| 53 |
len = sprintf(callgrind_fname, CALLGRIND_FILE_TEMPLATE, ctx->output_dir, ctx->pid); |
| 54 |
|
| 55 |
if (len >= CALLGRIND_FILENAME_LEN) { |
| 56 |
printf("Error in generating callgrind name\n"); |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
callgrind_fd = fopen(callgrind_fname, "w"); |
| 61 |
if (NULL == callgrind_fd) { |
| 62 |
printf("Error in opening callgrind file %s\n", callgrind_fname); |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
fprintf(callgrind_fd, CALLGRIND_PREAMBLE_TEMPLATE1, |
| 67 |
creator); |
| 68 |
fprintf(callgrind_fd, "cmd:"); |
| 69 |
for (i = 0; argv[i]; i++) { |
| 70 |
fprintf(callgrind_fd, " %s", argv[i]); |
| 71 |
} |
| 72 |
fprintf(callgrind_fd, "\n"); |
| 73 |
|
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
callgrind_profile_entry (const void *item) |
| 79 |
{ |
| 80 |
const profile_entry_t *p = item; |
| 81 |
profile_call_t *c; |
| 82 |
|
| 83 |
if (NULL == p->floc.filenm) { |
| 84 |
|
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
fprintf(callgrind_fd, "fl=%s/%s\n\n", starting_directory, p->floc.filenm); |
| 89 |
fprintf(callgrind_fd, "fn=%s\n", p->name); |
| 90 |
fprintf(callgrind_fd, "%" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
| 91 |
(uint64_t) p->floc.lineno, |
| 92 |
p->elapsed_total, |
| 93 |
p->elapsed_recipe); |
| 94 |
|
| 95 |
for (c = p->calls; c; c = c->next) { |
| 96 |
if (c->target->floc.filenm) { |
| 97 |
fprintf(callgrind_fd, "cfi=%s\n", c->target->floc.filenm); |
| 98 |
} |
| 99 |
|
| 100 |
fprintf(callgrind_fd, "cfn=%s\n", c->target->name); |
| 101 |
fprintf(callgrind_fd, "calls=1 %" PRIu64 "\n", |
| 102 |
(uint64_t) p->floc.lineno); |
| 103 |
fprintf(callgrind_fd, "%" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
| 104 |
(uint64_t) p->floc.lineno, |
| 105 |
c->target->elapsed_total, |
| 106 |
c->target->elapsed_recipe); |
| 107 |
} |
| 108 |
fprintf(callgrind_fd, "\n"); |
| 109 |
} |
| 110 |
|
| 111 |
extern void |
| 112 |
callgrind_close(profile_context_t *ctx, const char *program_status) { |
| 113 |
|
| 114 |
fprintf(callgrind_fd, CALLGRIND_PREAMBLE_TEMPLATE2, ctx->pid, |
| 115 |
program_status); |
| 116 |
|
| 117 |
fprintf(callgrind_fd, "summary: %" PRIu64 "\n\n", ctx->elapsed); |
| 118 |
|
| 119 |
profile_dump_entries(callgrind_profile_entry); |
| 120 |
fclose(callgrind_fd); |
| 121 |
|
| 122 |
printf("Created callgrind profiling data file: %s\n", callgrind_fname); |
| 123 |
} |