* posix/unistd.h (_POSIX_VERSION, _POSIX2_VERSION, _POSIX2_C_BIND,
[glibc/pb-stable.git] / malloc / memusagestat.c
blobbf331754153cdf72fba9a02d65970d97d6481c74
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 as 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;
194 if (also_total)
196 /* We use one scale and since we also draw the total amount of
197 memory used we have to adapt the maximum. */
198 maxsize_heap = maxsize_total;
199 maxsize_stack = maxsize_total;
202 if (maxsize_heap == 0 && maxsize_stack == 0)
204 /* The program aborted before memusage was able to write the
205 information about the maximum heap and stack use. Repair
206 the file now. */
207 struct entry next;
209 while (1)
211 if (read (fd, &next, sizeof (next)) == 0)
212 break;
213 if (next.heap > headent[1].heap)
214 headent[1].heap = next.heap;
215 if (next.stack > headent[1].stack)
216 headent[1].stack = next.stack;
219 headent[1].time_low = next.time_low;
220 headent[1].time_high = next.time_high;
222 /* Write the computed values in the file. */
223 lseek (fd, sizeof (struct entry), SEEK_SET);
224 write (fd, &headent[1], sizeof (struct entry));
227 start_time = ((uint64_t) headent[0].time_high) << 32 | headent[0].time_low;
228 end_time = ((uint64_t) headent[1].time_high) << 32 | headent[1].time_low;
229 total_time = end_time - start_time;
231 if (xsize < 100)
232 xsize = 100;
233 if (ysize < 80)
234 ysize = 80;
236 /* Create output image with the specified size. */
237 im_out = gdImageCreate (xsize, ysize);
239 /* First color allocated is background. */
240 grey = gdImageColorAllocate (im_out, 224, 224, 224);
242 /* Set transparent color. */
243 gdImageColorTransparent (im_out, grey);
245 /* These are all the other colors we need (in the moment). */
246 red = gdImageColorAllocate (im_out, 255, 0, 0);
247 green = gdImageColorAllocate (im_out, 0, 130, 0);
248 blue = gdImageColorAllocate (im_out, 0, 0, 255);
249 yellow = gdImageColorAllocate (im_out, 154, 205, 50);
250 black = gdImageColorAllocate (im_out, 0, 0, 0);
252 gdImageRectangle (im_out, 40, 20, xsize - 40, ysize - 20, blue);
254 if (maxsize_heap < 1024)
256 heap_format = "%Zu";
257 heap_scale = 1;
259 else if (maxsize_heap < 1024 * 1024 * 100)
261 heap_format = "%Zuk";
262 heap_scale = 1024;
264 else
266 heap_format = "%ZuM";
267 heap_scale = 1024 * 1024;
270 if (maxsize_stack < 1024)
272 stack_format = "%Zu";
273 stack_scale = 1;
275 else if (maxsize_stack < 1024 * 1024 * 100)
277 stack_format = "%Zuk";
278 stack_scale = 1024;
280 else
282 stack_format = "%ZuM";
283 stack_scale = 1024 * 1024;
286 gdImageString (im_out, gdFontSmall, 38, ysize - 14, (unsigned char *) "0",
287 blue);
288 snprintf (buf, sizeof (buf), heap_format, 0);
289 gdImageString (im_out, gdFontSmall, maxsize_heap < 1024 ? 32 : 26,
290 ysize - 26, (unsigned char *) buf, red);
291 snprintf (buf, sizeof (buf), stack_format, 0);
292 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26,
293 (unsigned char *) buf, green);
295 if (string != NULL)
296 gdImageString (im_out, gdFontLarge, (xsize - strlen (string) * 8) / 2,
297 2, (unsigned char *) string, green);
299 gdImageStringUp (im_out, gdFontSmall, 1, ysize / 2 - 10,
300 (unsigned char *) "allocated", red);
301 gdImageStringUp (im_out, gdFontSmall, 11, ysize / 2 - 10,
302 (unsigned char *) "memory", red);
304 gdImageStringUp (im_out, gdFontSmall, xsize - 39, ysize / 2 - 10,
305 (unsigned char *) "used", green);
306 gdImageStringUp (im_out, gdFontSmall, xsize - 27, ysize / 2 - 10,
307 (unsigned char *) "stack", green);
309 snprintf (buf, sizeof (buf), heap_format, maxsize_heap / heap_scale);
310 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6, 14,
311 (unsigned char *) buf, red);
312 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / stack_scale);
313 gdImageString (im_out, gdFontSmall, xsize - 37, 14,
314 (unsigned char *) buf, green);
316 for (line = 1; line <= 3; ++line)
318 cnt = ((ysize - 40) * (maxsize_heap / 4 * line / heap_scale)) /
319 (maxsize_heap / heap_scale);
320 gdImageDashedLine (im_out, 40, ysize - 20 - cnt, xsize - 40,
321 ysize - 20 - cnt, red);
322 snprintf (buf, sizeof (buf), heap_format, maxsize_heap / 4 * line /
323 heap_scale);
324 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6,
325 ysize - 26 - cnt, (unsigned char *) buf, red);
327 cnt2 = ((ysize - 40) * (maxsize_stack / 4 * line / stack_scale)) /
328 (maxsize_stack / stack_scale);
329 if (cnt != cnt2)
330 gdImageDashedLine (im_out, 40, ysize - 20 - cnt2, xsize - 40,
331 ysize - 20 - cnt2, green);
332 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / 4 * line /
333 stack_scale);
334 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26 - cnt2,
335 (unsigned char *) buf, green);
338 snprintf (buf, sizeof (buf), "%llu", (unsigned long long) total);
339 gdImageString (im_out, gdFontSmall, xsize - 50, ysize - 14,
340 (unsigned char *) buf, blue);
342 if (!time_based)
344 uint64_t previously = start_time;
346 gdImageString (im_out, gdFontSmall, 40 + (xsize - 32 * 6 - 80) / 2,
347 ysize - 12,
348 (unsigned char *) "# memory handling function calls",
349 blue);
352 last_stack = last_heap = last_total = ysize - 20;
353 for (cnt = 1; cnt <= total; ++cnt)
355 struct entry entry;
356 size_t new[2];
357 uint64_t now;
359 read (fd, &entry, sizeof (entry));
361 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
363 if ((((previously - start_time) * 100) / total_time) % 10 < 5)
364 gdImageFilledRectangle (im_out,
365 40 + ((cnt - 1) * (xsize - 80)) / total,
366 ysize - 19,
367 39 + (cnt * (xsize - 80)) / total,
368 ysize - 14, yellow);
369 previously = now;
371 if (also_total)
373 size_t new3;
375 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
376 * (entry.heap + entry.stack))
377 / maxsize_heap);
378 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
379 last_total,
380 40 + ((xsize - 80) * cnt) / total, new3,
381 black);
382 last_total = new3;
385 // assert (entry.heap <= maxsize_heap);
386 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
387 * entry.heap) / maxsize_heap);
388 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
389 last_heap, 40 + ((xsize - 80) * cnt) / total, new[0],
390 red);
391 last_heap = new[0];
393 // assert (entry.stack <= maxsize_stack);
394 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
395 * entry.stack) / maxsize_stack);
396 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
397 last_stack, 40 + ((xsize - 80) * cnt) / total, new[1],
398 green);
399 last_stack = new[1];
402 cnt = 0;
403 while (cnt < total)
405 gdImageLine (im_out, 40 + ((xsize - 80) * cnt) / total, ysize - 20,
406 40 + ((xsize - 80) * cnt) / total, ysize - 15, blue);
407 cnt += MAX (1, total / 20);
409 gdImageLine (im_out, xsize - 40, ysize - 20, xsize - 40, ysize - 15,
410 blue);
412 else
414 uint64_t next_tick = MAX (1, total / 20);
415 size_t last_xpos = 40;
417 gdImageString (im_out, gdFontSmall, 40 + (xsize - 39 * 6 - 80) / 2,
418 ysize - 12,
419 (unsigned char *) "\
420 # memory handling function calls / time", blue);
422 for (cnt = 0; cnt < 20; cnt += 2)
423 gdImageFilledRectangle (im_out,
424 40 + (cnt * (xsize - 80)) / 20, ysize - 19,
425 39 + ((cnt + 1) * (xsize - 80)) / 20,
426 ysize - 14, yellow);
428 last_stack = last_heap = last_total = ysize - 20;
429 for (cnt = 1; cnt <= total; ++cnt)
431 struct entry entry;
432 size_t new[2];
433 size_t xpos;
434 uint64_t now;
436 read (fd, &entry, sizeof (entry));
438 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
439 xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
441 if (cnt == next_tick)
443 gdImageLine (im_out, xpos, ysize - 20, xpos, ysize - 15, blue);
444 next_tick += MAX (1, total / 20);
447 if (also_total)
449 size_t new3;
451 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
452 * (entry.heap + entry.stack))
453 / maxsize_heap);
454 gdImageLine (im_out, last_xpos, last_total, xpos, new3, black);
455 last_total = new3;
458 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
459 * entry.heap) / maxsize_heap);
460 gdImageLine (im_out, last_xpos, last_heap, xpos, new[0], red);
461 last_heap = new[0];
463 // assert (entry.stack <= maxsize_stack);
464 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
465 * entry.stack) / maxsize_stack);
466 gdImageLine (im_out, last_xpos, last_stack, xpos, new[1], green);
467 last_stack = new[1];
469 last_xpos = xpos;
473 /* Write out the result. */
474 outfile = fopen (outname, "w");
475 if (outfile == NULL)
476 error (EXIT_FAILURE, errno, "cannot open output file");
478 gdImagePng (im_out, outfile);
480 fclose (outfile);
482 gdImageDestroy (im_out);
484 return 0;
488 /* Handle program arguments. */
489 static error_t
490 parse_opt (int key, char *arg, struct argp_state *state)
492 switch (key)
494 case 'o':
495 outname = arg;
496 break;
497 case 's':
498 string = arg;
499 break;
500 case 't':
501 time_based = 1;
502 break;
503 case 'T':
504 also_total = 1;
505 break;
506 case 'x':
507 xsize = atoi (arg);
508 if (xsize == 0)
509 xsize = XSIZE;
510 break;
511 case 'y':
512 ysize = atoi (arg);
513 if (ysize == 0)
514 ysize = XSIZE;
515 break;
516 default:
517 return ARGP_ERR_UNKNOWN;
519 return 0;
523 static char *
524 more_help (int key, const char *text, void *input)
526 char *orig;
527 char *cp;
529 switch (key)
531 case ARGP_KEY_HELP_EXTRA:
532 /* We print some extra information. */
533 orig = gettext ("\
534 For bug reporting instructions, please see:\n\
535 <http://www.gnu.org/software/libc/bugs.html>.\n");
536 cp = strdup (orig);
537 if (cp == NULL)
538 cp = orig;
539 return cp;
540 default:
541 break;
543 return (char *) text;
546 /* Print the version information. */
547 static void
548 print_version (FILE *stream, struct argp_state *state)
550 fprintf (stream, "memusagestat (GNU %s) %s\n", PACKAGE, VERSION);
551 fprintf (stream, gettext ("\
552 Copyright (C) %s Free Software Foundation, Inc.\n\
553 This is free software; see the source for copying conditions. There is NO\n\
554 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
555 "), "2009");
556 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");