Debian Codegraph @

src/callgrind_format.c

raw symbols SCIP
1 /*
2 Copyright (C) 2015 R. Bernstein <[email protected]>
3 This file is part of GNU Make (remake variant).
4
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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 /* + 10 is more than enough since 2**64 ~= 10**9 */
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 /* If we don't have file-location that don't bother dumping */
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 }