Merge remote branch 'origin/master' into fedora/master
[glibc.git] / malloc / memusagestat.c
bloba5a315056bfd30e42b999ea9b487a8a9a60e5c3a
1 /* Generate graphic from memory profiling data.
2 Copyright (C) 1998, 1999, 2000, 2005, 2006,
3 2009 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published
9 by the Free Software Foundation; version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #define _FILE_OFFSET_BITS 64
23 #include <argp.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <error.h>
27 #include <fcntl.h>
28 #include <getopt.h>
29 #include <inttypes.h>
30 #include <libintl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
38 #include <gd.h>
39 #include <gdfontl.h>
40 #include <gdfonts.h>
42 #include "../version.h"
43 #define PACKAGE _libc_intl_domainname
45 /* Default size of the generated image. */
46 #define XSIZE 800
47 #define YSIZE 600
49 #ifndef N_
50 # define N_(Arg) Arg
51 #endif
54 /* Definitions of arguments for argp functions. */
55 static const struct argp_option options[] =
57 { "output", 'o', "FILE", 0, N_("Name output file") },
58 { "string", 's', "STRING", 0, N_("Title string used in output graphic") },
59 { "time", 't', NULL, 0, N_("Generate output linear to time (default is linear to number of function calls)") },
60 { "total", 'T', NULL, 0,
61 N_("Also draw graph for total memory consumption") },
62 { "x-size", 'x', "VALUE", 0, N_("Make output graphic VALUE pixels wide") },
63 { "y-size", 'y', "VALUE", 0, N_("Make output graphic VALUE pixels high") },
64 { NULL, 0, NULL, 0, NULL }
67 /* Short description of program. */
68 static const char doc[] = N_("Generate graphic from memory profiling data");
70 /* Strings for arguments in help texts. */
71 static const char args_doc[] = N_("DATAFILE [OUTFILE]");
73 /* Prototype for option handler. */
74 static error_t parse_opt (int key, char *arg, struct argp_state *state);
76 /* Function to print some extra text in the help message. */
77 static char *more_help (int key, const char *text, void *input);
79 /* Name and version of program. */
80 static void print_version (FILE *stream, struct argp_state *state);
81 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
83 /* Data structure to communicate with argp functions. */
84 static struct argp argp =
86 options, parse_opt, args_doc, doc, NULL, more_help
90 struct entry
92 uint64_t heap;
93 uint64_t stack;
94 uint32_t time_low;
95 uint32_t time_high;
99 /* Size of the image. */
100 static size_t xsize;
101 static size_t ysize;
103 /* Name of the output file. */
104 static char *outname;
106 /* Title string for the graphic. */
107 static const char *string;
109 /* Nonzero if graph should be generated linear in time. */
110 static int time_based;
112 /* Nonzero if graph to display total use of memory should be drawn as well. */
113 static int also_total = 0;
117 main (int argc, char *argv[])
119 int remaining;
120 const char *inname;
121 gdImagePtr im_out;
122 int grey, blue, red, green, yellow, black;
123 int fd;
124 struct stat st;
125 size_t maxsize_heap;
126 size_t maxsize_stack;
127 size_t maxsize_total;
128 uint64_t total;
129 uint64_t cnt, cnt2;
130 FILE *outfile;
131 char buf[30];
132 size_t last_heap;
133 size_t last_stack;
134 size_t last_total;
135 struct entry headent[2];
136 uint64_t start_time;
137 uint64_t end_time;
138 uint64_t total_time;
139 const char *heap_format, *stack_format;
140 int heap_scale, stack_scale, line;
142 outname = NULL;
143 xsize = XSIZE;
144 ysize = YSIZE;
145 string = NULL;
147 /* Parse and process arguments. */
148 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
150 if (remaining >= argc || remaining + 2 < argc)
152 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
153 program_invocation_short_name);
154 exit (1);
157 inname = argv[remaining++];
159 if (remaining < argc)
160 outname = argv[remaining];
161 else if (outname == NULL)
163 size_t len = strlen (inname);
164 outname = alloca (len + 5);
165 stpcpy (stpcpy (outname, inname), ".png");
168 /* Open for read/write since we try to repair the file in case the
169 application hasn't terminated cleanly. */
170 fd = open (inname, O_RDWR);
171 if (fd == -1)
172 error (EXIT_FAILURE, errno, "cannot open input file");
173 if (fstat (fd, &st) != 0)
175 close (fd);
176 error (EXIT_FAILURE, errno, "cannot get size of input file");
178 /* Test whether the file contains only full records. */
179 if ((st.st_size % sizeof (struct entry)) != 0
180 /* The file must at least contain the two administrative records. */
181 || st.st_size < 2 * sizeof (struct entry))
183 close (fd);
184 error (EXIT_FAILURE, 0, "input file has incorrect size");
186 /* Compute number of data entries. */
187 total = st.st_size / sizeof (struct entry) - 2;
189 /* Read the administrative information. */
190 read (fd, headent, sizeof (headent));
191 maxsize_heap = headent[1].heap;
192 maxsize_stack = headent[1].stack;
193 maxsize_total = headent[0].stack;
195 if (maxsize_heap == 0 && maxsize_stack == 0)
197 /* The program aborted before memusage was able to write the
198 information about the maximum heap and stack use. Repair
199 the file now. */
200 struct entry next;
202 while (1)
204 if (read (fd, &next, sizeof (next)) == 0)
205 break;
206 if (next.heap > maxsize_heap)
207 maxsize_heap = next.heap;
208 if (next.stack > maxsize_stack)
209 maxsize_stack = next.stack;
210 if (maxsize_heap + maxsize_stack > maxsize_total)
211 maxsize_total = maxsize_heap + maxsize_stack;
214 headent[0].stack = maxsize_total;
215 headent[1].heap = maxsize_heap;
216 headent[1].stack = maxsize_stack;
217 headent[1].time_low = next.time_low;
218 headent[1].time_high = next.time_high;
220 /* Write the computed values in the file. */
221 lseek (fd, 0, SEEK_SET);
222 write (fd, headent, 2 * sizeof (struct entry));
225 if (also_total)
227 /* We use one scale and since we also draw the total amount of
228 memory used we have to adapt the maximum. */
229 maxsize_heap = maxsize_total;
230 maxsize_stack = maxsize_total;
233 start_time = ((uint64_t) headent[0].time_high) << 32 | headent[0].time_low;
234 end_time = ((uint64_t) headent[1].time_high) << 32 | headent[1].time_low;
235 total_time = end_time - start_time;
237 if (xsize < 100)
238 xsize = 100;
239 if (ysize < 80)
240 ysize = 80;
242 /* Create output image with the specified size. */
243 im_out = gdImageCreate (xsize, ysize);
245 /* First color allocated is background. */
246 grey = gdImageColorAllocate (im_out, 224, 224, 224);
248 /* Set transparent color. */
249 gdImageColorTransparent (im_out, grey);
251 /* These are all the other colors we need (in the moment). */
252 red = gdImageColorAllocate (im_out, 255, 0, 0);
253 green = gdImageColorAllocate (im_out, 0, 130, 0);
254 blue = gdImageColorAllocate (im_out, 0, 0, 255);
255 yellow = gdImageColorAllocate (im_out, 154, 205, 50);
256 black = gdImageColorAllocate (im_out, 0, 0, 0);
258 gdImageRectangle (im_out, 40, 20, xsize - 40, ysize - 20, blue);
260 if (maxsize_heap < 1024)
262 heap_format = "%Zu";
263 heap_scale = 1;
265 else if (maxsize_heap < 1024 * 1024 * 100)
267 heap_format = "%Zuk";
268 heap_scale = 1024;
270 else
272 heap_format = "%ZuM";
273 heap_scale = 1024 * 1024;
276 if (maxsize_stack < 1024)
278 stack_format = "%Zu";
279 stack_scale = 1;
281 else if (maxsize_stack < 1024 * 1024 * 100)
283 stack_format = "%Zuk";
284 stack_scale = 1024;
286 else
288 stack_format = "%ZuM";
289 stack_scale = 1024 * 1024;
292 gdImageString (im_out, gdFontSmall, 38, ysize - 14, (unsigned char *) "0",
293 blue);
294 snprintf (buf, sizeof (buf), heap_format, 0);
295 gdImageString (im_out, gdFontSmall, maxsize_heap < 1024 ? 32 : 26,
296 ysize - 26, (unsigned char *) buf, red);
297 snprintf (buf, sizeof (buf), stack_format, 0);
298 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26,
299 (unsigned char *) buf, green);
301 if (string != NULL)
302 gdImageString (im_out, gdFontLarge, (xsize - strlen (string) * 8) / 2,
303 2, (unsigned char *) string, green);
305 gdImageStringUp (im_out, gdFontSmall, 1, ysize / 2 - 10,
306 (unsigned char *) "allocated", red);
307 gdImageStringUp (im_out, gdFontSmall, 11, ysize / 2 - 10,
308 (unsigned char *) "memory", red);
310 gdImageStringUp (im_out, gdFontSmall, xsize - 39, ysize / 2 - 10,
311 (unsigned char *) "used", green);
312 gdImageStringUp (im_out, gdFontSmall, xsize - 27, ysize / 2 - 10,
313 (unsigned char *) "stack", green);
315 snprintf (buf, sizeof (buf), heap_format, maxsize_heap / heap_scale);
316 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6, 14,
317 (unsigned char *) buf, red);
318 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / stack_scale);
319 gdImageString (im_out, gdFontSmall, xsize - 37, 14,
320 (unsigned char *) buf, green);
322 for (line = 1; line <= 3; ++line)
324 cnt = ((ysize - 40) * (maxsize_heap / 4 * line / heap_scale)) /
325 (maxsize_heap / heap_scale);
326 gdImageDashedLine (im_out, 40, ysize - 20 - cnt, xsize - 40,
327 ysize - 20 - cnt, red);
328 snprintf (buf, sizeof (buf), heap_format, maxsize_heap / 4 * line /
329 heap_scale);
330 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6,
331 ysize - 26 - cnt, (unsigned char *) buf, red);
333 cnt2 = ((ysize - 40) * (maxsize_stack / 4 * line / stack_scale)) /
334 (maxsize_stack / stack_scale);
335 if (cnt != cnt2)
336 gdImageDashedLine (im_out, 40, ysize - 20 - cnt2, xsize - 40,
337 ysize - 20 - cnt2, green);
338 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / 4 * line /
339 stack_scale);
340 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26 - cnt2,
341 (unsigned char *) buf, green);
344 snprintf (buf, sizeof (buf), "%llu", (unsigned long long) total);
345 gdImageString (im_out, gdFontSmall, xsize - 50, ysize - 14,
346 (unsigned char *) buf, blue);
348 if (!time_based)
350 uint64_t previously = start_time;
352 gdImageString (im_out, gdFontSmall, 40 + (xsize - 32 * 6 - 80) / 2,
353 ysize - 12,
354 (unsigned char *) "# memory handling function calls",
355 blue);
358 last_stack = last_heap = last_total = ysize - 20;
359 for (cnt = 1; cnt <= total; ++cnt)
361 struct entry entry;
362 size_t new[2];
363 uint64_t now;
365 read (fd, &entry, sizeof (entry));
367 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
369 if ((((previously - start_time) * 100) / total_time) % 10 < 5)
370 gdImageFilledRectangle (im_out,
371 40 + ((cnt - 1) * (xsize - 80)) / total,
372 ysize - 19,
373 39 + (cnt * (xsize - 80)) / total,
374 ysize - 14, yellow);
375 previously = now;
377 if (also_total)
379 size_t new3;
381 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
382 * (entry.heap + entry.stack))
383 / maxsize_heap);
384 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
385 last_total,
386 40 + ((xsize - 80) * cnt) / total, new3,
387 black);
388 last_total = new3;
391 // assert (entry.heap <= maxsize_heap);
392 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
393 * entry.heap) / maxsize_heap);
394 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
395 last_heap, 40 + ((xsize - 80) * cnt) / total, new[0],
396 red);
397 last_heap = new[0];
399 // assert (entry.stack <= maxsize_stack);
400 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
401 * entry.stack) / maxsize_stack);
402 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
403 last_stack, 40 + ((xsize - 80) * cnt) / total, new[1],
404 green);
405 last_stack = new[1];
408 cnt = 0;
409 while (cnt < total)
411 gdImageLine (im_out, 40 + ((xsize - 80) * cnt) / total, ysize - 20,
412 40 + ((xsize - 80) * cnt) / total, ysize - 15, blue);
413 cnt += MAX (1, total / 20);
415 gdImageLine (im_out, xsize - 40, ysize - 20, xsize - 40, ysize - 15,
416 blue);
418 else
420 uint64_t next_tick = MAX (1, total / 20);
421 size_t last_xpos = 40;
423 gdImageString (im_out, gdFontSmall, 40 + (xsize - 39 * 6 - 80) / 2,
424 ysize - 12,
425 (unsigned char *) "\
426 # memory handling function calls / time", blue);
428 for (cnt = 0; cnt < 20; cnt += 2)
429 gdImageFilledRectangle (im_out,
430 40 + (cnt * (xsize - 80)) / 20, ysize - 19,
431 39 + ((cnt + 1) * (xsize - 80)) / 20,
432 ysize - 14, yellow);
434 last_stack = last_heap = last_total = ysize - 20;
435 for (cnt = 1; cnt <= total; ++cnt)
437 struct entry entry;
438 size_t new[2];
439 size_t xpos;
440 uint64_t now;
442 read (fd, &entry, sizeof (entry));
444 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
445 xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
447 if (cnt == next_tick)
449 gdImageLine (im_out, xpos, ysize - 20, xpos, ysize - 15, blue);
450 next_tick += MAX (1, total / 20);
453 if (also_total)
455 size_t new3;
457 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
458 * (entry.heap + entry.stack))
459 / maxsize_heap);
460 gdImageLine (im_out, last_xpos, last_total, xpos, new3, black);
461 last_total = new3;
464 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
465 * entry.heap) / maxsize_heap);
466 gdImageLine (im_out, last_xpos, last_heap, xpos, new[0], red);
467 last_heap = new[0];
469 // assert (entry.stack <= maxsize_stack);
470 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
471 * entry.stack) / maxsize_stack);
472 gdImageLine (im_out, last_xpos, last_stack, xpos, new[1], green);
473 last_stack = new[1];
475 last_xpos = xpos;
479 /* Write out the result. */
480 outfile = fopen (outname, "w");
481 if (outfile == NULL)
482 error (EXIT_FAILURE, errno, "cannot open output file");
484 gdImagePng (im_out, outfile);
486 fclose (outfile);
488 gdImageDestroy (im_out);
490 return 0;
494 /* Handle program arguments. */
495 static error_t
496 parse_opt (int key, char *arg, struct argp_state *state)
498 switch (key)
500 case 'o':
501 outname = arg;
502 break;
503 case 's':
504 string = arg;
505 break;
506 case 't':
507 time_based = 1;
508 break;
509 case 'T':
510 also_total = 1;
511 break;
512 case 'x':
513 xsize = atoi (arg);
514 if (xsize == 0)
515 xsize = XSIZE;
516 break;
517 case 'y':
518 ysize = atoi (arg);
519 if (ysize == 0)
520 ysize = XSIZE;
521 break;
522 default:
523 return ARGP_ERR_UNKNOWN;
525 return 0;
529 static char *
530 more_help (int key, const char *text, void *input)
532 char *orig;
533 char *cp;
535 switch (key)
537 case ARGP_KEY_HELP_EXTRA:
538 /* We print some extra information. */
539 orig = gettext ("\
540 For bug reporting instructions, please see:\n\
541 <http://www.gnu.org/software/libc/bugs.html>.\n");
542 cp = strdup (orig);
543 if (cp == NULL)
544 cp = orig;
545 return cp;
546 default:
547 break;
549 return (char *) text;
552 /* Print the version information. */
553 static void
554 print_version (FILE *stream, struct argp_state *state)
556 fprintf (stream, "memusagestat (GNU %s) %s\n", PACKAGE, VERSION);
557 fprintf (stream, gettext ("\
558 Copyright (C) %s Free Software Foundation, Inc.\n\
559 This is free software; see the source for copying conditions. There is NO\n\
560 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
561 "), "2009");
562 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");