1 /* Generate graphic from memory profiling data.
2 Copyright (C) 1998, 1999, 2000, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 This program 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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #define _FILE_OFFSET_BITS 64
33 #include <sys/param.h>
41 /* Default size of the generated image. */
50 /* Definitions of arguments for argp functions. */
51 static const struct argp_option options
[] =
53 { "output", 'o', "FILE", 0, N_("Name output file") },
54 { "string", 's', "STRING", 0, N_("Title string used in output graphic") },
55 { "time", 't', NULL
, 0, N_("Generate output linear to time (default is linear to number of function calls)") },
56 { "total", 'T', NULL
, 0,
57 N_("Also draw graph for total memory consumption") },
58 { "x-size", 'x', "VALUE", 0, N_("make output graphic VALUE pixel wide") },
59 { "y-size", 'y', "VALUE", 0, N_("make output graphic VALUE pixel high") },
60 { NULL
, 0, NULL
, 0, NULL
}
63 /* Short description of program. */
64 static const char doc
[] = N_("Generate graphic from memory profiling data");
66 /* Strings for arguments in help texts. */
67 static const char args_doc
[] = N_("DATAFILE [OUTFILE]");
69 /* Prototype for option handler. */
70 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
72 /* Function to print some extra text in the help message. */
73 static char *more_help (int key
, const char *text
, void *input
);
75 /* Data structure to communicate with argp functions. */
76 static struct argp argp
=
78 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
91 /* Size of the image. */
95 /* Name of the output file. */
98 /* Title string for the graphic. */
99 static const char *string
;
101 /* Nonzero if graph should be generated linear in time. */
102 static int time_based
;
104 /* Nonzero if graph to display total use of memory should be drawn as well. */
105 static int also_total
= 0;
109 main (int argc
, char *argv
[])
114 int grey
, blue
, red
, green
, yellow
, black
;
118 size_t maxsize_stack
;
119 size_t maxsize_total
;
127 struct entry headent
[2];
131 const char *heap_format
, *stack_format
;
132 int heap_scale
, stack_scale
, line
;
139 /* Parse and process arguments. */
140 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
142 if (remaining
>= argc
|| remaining
+ 2 < argc
)
144 argp_help (&argp
, stdout
, ARGP_HELP_SEE
| ARGP_HELP_EXIT_ERR
,
145 program_invocation_short_name
);
149 inname
= argv
[remaining
++];
151 if (remaining
< argc
)
152 outname
= argv
[remaining
];
153 else if (outname
== NULL
)
155 size_t len
= strlen (inname
);
156 outname
= alloca (len
+ 5);
157 stpcpy (stpcpy (outname
, inname
), ".png");
160 /* Open for read/write since we try to repair the file in case the
161 application hasn't terminated cleanly. */
162 fd
= open (inname
, O_RDWR
);
164 error (EXIT_FAILURE
, errno
, "cannot open input file");
165 if (fstat (fd
, &st
) != 0)
168 error (EXIT_FAILURE
, errno
, "cannot get size of input file");
170 /* Test whether the file contains only full records. */
171 if ((st
.st_size
% sizeof (struct entry
)) != 0
172 /* The file must at least contain the two administrative records. */
173 || st
.st_size
< 2 * sizeof (struct entry
))
176 error (EXIT_FAILURE
, 0, "input file as incorrect size");
178 /* Compute number of data entries. */
179 total
= st
.st_size
/ sizeof (struct entry
) - 2;
181 /* Read the administrative information. */
182 read (fd
, headent
, sizeof (headent
));
183 maxsize_heap
= headent
[1].heap
;
184 maxsize_stack
= headent
[1].stack
;
185 maxsize_total
= headent
[0].stack
;
188 /* We use one scale and since we also draw the total amount of
189 memory used we have to adapt the maximum. */
190 maxsize_heap
= maxsize_total
;
191 maxsize_stack
= maxsize_total
;
194 if (maxsize_heap
== 0 && maxsize_stack
== 0)
196 /* The program aborted before memusage was able to write the
197 information about the maximum heap and stack use. Repair
203 if (read (fd
, &next
, sizeof (next
)) == 0)
205 if (next
.heap
> headent
[1].heap
)
206 headent
[1].heap
= next
.heap
;
207 if (next
.stack
> headent
[1].stack
)
208 headent
[1].stack
= next
.stack
;
211 headent
[1].time_low
= next
.time_low
;
212 headent
[1].time_high
= next
.time_high
;
214 /* Write the computed values in the file. */
215 lseek (fd
, sizeof (struct entry
), SEEK_SET
);
216 write (fd
, &headent
[1], sizeof (struct entry
));
219 start_time
= ((uint64_t) headent
[0].time_high
) << 32 | headent
[0].time_low
;
220 end_time
= ((uint64_t) headent
[1].time_high
) << 32 | headent
[1].time_low
;
221 total_time
= end_time
- start_time
;
228 /* Create output image with the specified size. */
229 im_out
= gdImageCreate (xsize
, ysize
);
231 /* First color allocated is background. */
232 grey
= gdImageColorAllocate (im_out
, 224, 224, 224);
234 /* Set transparent color. */
235 gdImageColorTransparent (im_out
, grey
);
237 /* These are all the other colors we need (in the moment). */
238 red
= gdImageColorAllocate (im_out
, 255, 0, 0);
239 green
= gdImageColorAllocate (im_out
, 0, 130, 0);
240 blue
= gdImageColorAllocate (im_out
, 0, 0, 255);
241 yellow
= gdImageColorAllocate (im_out
, 154, 205, 50);
242 black
= gdImageColorAllocate (im_out
, 0, 0, 0);
244 gdImageRectangle (im_out
, 40, 20, xsize
- 40, ysize
- 20, blue
);
246 if (maxsize_heap
< 1024)
251 else if (maxsize_heap
< 1024 * 1024 * 100)
253 heap_format
= "%Zuk";
258 heap_format
= "%ZuM";
259 heap_scale
= 1024 * 1024;
262 if (maxsize_stack
< 1024)
264 stack_format
= "%Zu";
267 else if (maxsize_stack
< 1024 * 1024 * 100)
269 stack_format
= "%Zuk";
274 stack_format
= "%ZuM";
275 stack_scale
= 1024 * 1024;
278 gdImageString (im_out
, gdFontSmall
, 38, ysize
- 14, (unsigned char *) "0",
280 snprintf(buf
, sizeof (buf
), heap_format
, 0);
281 gdImageString (im_out
, gdFontSmall
, maxsize_heap
< 1024 ? 32 : 26,
282 ysize
- 26, buf
, red
);
283 snprintf(buf
, sizeof (buf
), stack_format
, 0);
284 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26,
288 gdImageString (im_out
, gdFontLarge
, (xsize
- strlen (string
) * 8) / 2,
289 2, (char *) string
, green
);
291 gdImageStringUp (im_out
, gdFontSmall
, 1, ysize
/ 2 - 10,
292 (unsigned char *) "allocated", red
);
293 gdImageStringUp (im_out
, gdFontSmall
, 11, ysize
/ 2 - 10,
294 (unsigned char *) "memory", red
);
296 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 39, ysize
/ 2 - 10,
297 (unsigned char *) "used", green
);
298 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 27, ysize
/ 2 - 10,
299 (unsigned char *) "stack", green
);
301 snprintf (buf
, sizeof (buf
), heap_format
, maxsize_heap
/ heap_scale
);
302 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6, 14, buf
, red
);
303 snprintf (buf
, sizeof (buf
), stack_format
, maxsize_stack
/ stack_scale
);
304 gdImageString (im_out
, gdFontSmall
, xsize
- 37, 14, buf
, green
);
306 for (line
= 1; line
<= 3; ++line
)
308 cnt
= ((ysize
- 40) * (maxsize_heap
/ 4 * line
/ heap_scale
)) /
309 (maxsize_heap
/ heap_scale
);
310 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt
, xsize
- 40,
311 ysize
- 20 - cnt
, red
);
312 snprintf (buf
, sizeof (buf
), heap_format
, maxsize_heap
/ 4 * line
/
314 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
315 ysize
- 26 - cnt
, buf
, red
);
317 cnt2
= ((ysize
- 40) * (maxsize_stack
/ 4 * line
/ stack_scale
)) /
318 (maxsize_stack
/ stack_scale
);
320 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
321 ysize
- 20 - cnt2
, green
);
322 snprintf (buf
, sizeof (buf
), stack_format
, maxsize_stack
/ 4 * line
/
324 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
328 snprintf (buf
, sizeof (buf
), "%llu", (unsigned long long) total
);
329 gdImageString (im_out
, gdFontSmall
, xsize
- 50, ysize
- 14, buf
, blue
);
333 uint64_t previously
= start_time
;
335 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 32 * 6 - 80) / 2,
337 (unsigned char *) "# memory handling function calls",
341 last_stack
= last_heap
= last_total
= ysize
- 20;
342 for (cnt
= 1; cnt
<= total
; ++cnt
)
348 read (fd
, &entry
, sizeof (entry
));
350 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
352 if ((((previously
- start_time
) * 100) / total_time
) % 10 < 5)
353 gdImageFilledRectangle (im_out
,
354 40 + ((cnt
- 1) * (xsize
- 80)) / total
,
356 39 + (cnt
* (xsize
- 80)) / total
,
364 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
365 * (entry
.heap
+ entry
.stack
))
367 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
369 40 + ((xsize
- 80) * cnt
) / total
, new3
,
374 // assert (entry.heap <= maxsize_heap);
375 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
376 * entry
.heap
) / maxsize_heap
);
377 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
378 last_heap
, 40 + ((xsize
- 80) * cnt
) / total
, new[0],
382 // assert (entry.stack <= maxsize_stack);
383 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
384 * entry
.stack
) / maxsize_stack
);
385 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
386 last_stack
, 40 + ((xsize
- 80) * cnt
) / total
, new[1],
394 gdImageLine (im_out
, 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 20,
395 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 15, blue
);
396 cnt
+= MAX (1, total
/ 20);
398 gdImageLine (im_out
, xsize
- 40, ysize
- 20, xsize
- 40, ysize
- 15,
403 uint64_t next_tick
= MAX (1, total
/ 20);
404 size_t last_xpos
= 40;
406 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 39 * 6 - 80) / 2,
409 # memory handling function calls / time", blue
);
411 for (cnt
= 0; cnt
< 20; cnt
+= 2)
412 gdImageFilledRectangle (im_out
,
413 40 + (cnt
* (xsize
- 80)) / 20, ysize
- 19,
414 39 + ((cnt
+ 1) * (xsize
- 80)) / 20,
417 last_stack
= last_heap
= last_total
= ysize
- 20;
418 for (cnt
= 1; cnt
<= total
; ++cnt
)
425 read (fd
, &entry
, sizeof (entry
));
427 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
428 xpos
= 40 + ((xsize
- 80) * (now
- start_time
)) / total_time
;
430 if (cnt
== next_tick
)
432 gdImageLine (im_out
, xpos
, ysize
- 20, xpos
, ysize
- 15, blue
);
433 next_tick
+= MAX (1, total
/ 20);
440 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
441 * (entry
.heap
+ entry
.stack
))
443 gdImageLine (im_out
, last_xpos
, last_total
, xpos
, new3
, black
);
447 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
448 * entry
.heap
) / maxsize_heap
);
449 gdImageLine (im_out
, last_xpos
, last_heap
, xpos
, new[0], red
);
452 // assert (entry.stack <= maxsize_stack);
453 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
454 * entry
.stack
) / maxsize_stack
);
455 gdImageLine (im_out
, last_xpos
, last_stack
, xpos
, new[1], green
);
462 /* Write out the result. */
463 outfile
= fopen (outname
, "w");
465 error (EXIT_FAILURE
, errno
, "cannot open output file");
467 gdImagePng (im_out
, outfile
);
471 gdImageDestroy (im_out
);
477 /* Handle program arguments. */
479 parse_opt (int key
, char *arg
, struct argp_state
*state
)
506 return ARGP_ERR_UNKNOWN
;
513 more_help (int key
, const char *text
, void *input
)
520 case ARGP_KEY_HELP_EXTRA
:
521 /* We print some extra information. */
523 For bug reporting instructions, please see:\n\
524 <http://www.gnu.org/software/libc/bugs.html>.\n");
532 return (char *) text
;