PowerPC: logb/logbf/logbl multilib for PowerPC32
[glibc.git] / malloc / memusagestat.c
blob7bbd009967b4aec8cc72645e6f8cf118ece8311e
1 /* Generate graphic from memory profiling data.
2 Copyright (C) 1998-2013 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 as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>. */
19 #define _FILE_OFFSET_BITS 64
21 #include <argp.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <error.h>
25 #include <fcntl.h>
26 #include <getopt.h>
27 #include <inttypes.h>
28 #include <libintl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/param.h>
34 #include <sys/stat.h>
36 #include <gd.h>
37 #include <gdfontl.h>
38 #include <gdfonts.h>
40 #include "../version.h"
41 #define PACKAGE _libc_intl_domainname
43 /* Default size of the generated image. */
44 #define XSIZE 800
45 #define YSIZE 600
47 #ifndef N_
48 # define N_(Arg) Arg
49 #endif
52 /* Definitions of arguments for argp functions. */
53 static const struct argp_option options[] =
55 { "output", 'o', "FILE", 0, N_("Name output file") },
56 { "string", 's', "STRING", 0, N_("Title string used in output graphic") },
57 { "time", 't', NULL, 0, N_("Generate output linear to time (default is linear to number of function calls)") },
58 { "total", 'T', NULL, 0,
59 N_("Also draw graph for total memory consumption") },
60 { "x-size", 'x', "VALUE", 0, N_("Make output graphic VALUE pixels wide") },
61 { "y-size", 'y', "VALUE", 0, N_("Make output graphic VALUE pixels high") },
62 { NULL, 0, NULL, 0, NULL }
65 /* Short description of program. */
66 static const char doc[] = N_("Generate graphic from memory profiling data");
68 /* Strings for arguments in help texts. */
69 static const char args_doc[] = N_("DATAFILE [OUTFILE]");
71 /* Prototype for option handler. */
72 static error_t parse_opt (int key, char *arg, struct argp_state *state);
74 /* Function to print some extra text in the help message. */
75 static char *more_help (int key, const char *text, void *input);
77 /* Name and version of program. */
78 static void print_version (FILE *stream, struct argp_state *state);
79 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
81 /* Data structure to communicate with argp functions. */
82 static struct argp argp =
84 options, parse_opt, args_doc, doc, NULL, more_help
88 struct entry
90 uint64_t heap;
91 uint64_t stack;
92 uint32_t time_low;
93 uint32_t time_high;
97 /* Size of the image. */
98 static size_t xsize;
99 static size_t ysize;
101 /* Name of the output file. */
102 static char *outname;
104 /* Title string for the graphic. */
105 static const char *string;
107 /* Nonzero if graph should be generated linear in time. */
108 static int time_based;
110 /* Nonzero if graph to display total use of memory should be drawn as well. */
111 static int also_total = 0;
115 main (int argc, char *argv[])
117 int remaining;
118 const char *inname;
119 gdImagePtr im_out;
120 int grey, blue, red, green, yellow, black;
121 int fd;
122 struct stat st;
123 size_t maxsize_heap;
124 size_t maxsize_stack;
125 size_t maxsize_total;
126 uint64_t total;
127 uint64_t cnt, cnt2;
128 FILE *outfile;
129 char buf[30];
130 size_t last_heap;
131 size_t last_stack;
132 size_t last_total;
133 struct entry headent[2];
134 uint64_t start_time;
135 uint64_t end_time;
136 uint64_t total_time;
137 const char *heap_format, *stack_format;
138 int heap_scale, stack_scale, line;
140 outname = NULL;
141 xsize = XSIZE;
142 ysize = YSIZE;
143 string = NULL;
145 /* Parse and process arguments. */
146 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
148 if (remaining >= argc || remaining + 2 < argc)
150 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
151 program_invocation_short_name);
152 exit (1);
155 inname = argv[remaining++];
157 if (remaining < argc)
158 outname = argv[remaining];
159 else if (outname == NULL)
161 size_t len = strlen (inname);
162 outname = alloca (len + 5);
163 stpcpy (stpcpy (outname, inname), ".png");
166 /* Open for read/write since we try to repair the file in case the
167 application hasn't terminated cleanly. */
168 fd = open (inname, O_RDWR);
169 if (fd == -1)
170 error (EXIT_FAILURE, errno, "cannot open input file");
171 if (fstat (fd, &st) != 0)
173 close (fd);
174 error (EXIT_FAILURE, errno, "cannot get size of input file");
176 /* Test whether the file contains only full records. */
177 if ((st.st_size % sizeof (struct entry)) != 0
178 /* The file must at least contain the two administrative records. */
179 || st.st_size < 2 * sizeof (struct entry))
181 close (fd);
182 error (EXIT_FAILURE, 0, "input file has incorrect size");
184 /* Compute number of data entries. */
185 total = st.st_size / sizeof (struct entry) - 2;
187 /* Read the administrative information. */
188 read (fd, headent, sizeof (headent));
189 maxsize_heap = headent[1].heap;
190 maxsize_stack = headent[1].stack;
191 maxsize_total = headent[0].stack;
193 if (maxsize_heap == 0 && maxsize_stack == 0)
195 /* The program aborted before memusage was able to write the
196 information about the maximum heap and stack use. Repair
197 the file now. */
198 struct entry next;
200 while (1)
202 if (read (fd, &next, sizeof (next)) == 0)
203 break;
204 if (next.heap > maxsize_heap)
205 maxsize_heap = next.heap;
206 if (next.stack > maxsize_stack)
207 maxsize_stack = next.stack;
208 if (maxsize_heap + maxsize_stack > maxsize_total)
209 maxsize_total = maxsize_heap + maxsize_stack;
212 headent[0].stack = maxsize_total;
213 headent[1].heap = maxsize_heap;
214 headent[1].stack = maxsize_stack;
215 headent[1].time_low = next.time_low;
216 headent[1].time_high = next.time_high;
218 /* Write the computed values in the file. */
219 lseek (fd, 0, SEEK_SET);
220 write (fd, headent, 2 * sizeof (struct entry));
223 if (also_total)
225 /* We use one scale and since we also draw the total amount of
226 memory used we have to adapt the maximum. */
227 maxsize_heap = maxsize_total;
228 maxsize_stack = maxsize_total;
231 start_time = ((uint64_t) headent[0].time_high) << 32 | headent[0].time_low;
232 end_time = ((uint64_t) headent[1].time_high) << 32 | headent[1].time_low;
233 total_time = end_time - start_time;
235 if (xsize < 100)
236 xsize = 100;
237 if (ysize < 80)
238 ysize = 80;
240 /* Create output image with the specified size. */
241 im_out = gdImageCreate (xsize, ysize);
243 /* First color allocated is background. */
244 grey = gdImageColorAllocate (im_out, 224, 224, 224);
246 /* Set transparent color. */
247 gdImageColorTransparent (im_out, grey);
249 /* These are all the other colors we need (in the moment). */
250 red = gdImageColorAllocate (im_out, 255, 0, 0);
251 green = gdImageColorAllocate (im_out, 0, 130, 0);
252 blue = gdImageColorAllocate (im_out, 0, 0, 255);
253 yellow = gdImageColorAllocate (im_out, 154, 205, 50);
254 black = gdImageColorAllocate (im_out, 0, 0, 0);
256 gdImageRectangle (im_out, 40, 20, xsize - 40, ysize - 20, blue);
258 if (maxsize_heap < 1024)
260 heap_format = "%Zu";
261 heap_scale = 1;
263 else if (maxsize_heap < 1024 * 1024 * 100)
265 heap_format = "%Zuk";
266 heap_scale = 1024;
268 else
270 heap_format = "%ZuM";
271 heap_scale = 1024 * 1024;
274 if (maxsize_stack < 1024)
276 stack_format = "%Zu";
277 stack_scale = 1;
279 else if (maxsize_stack < 1024 * 1024 * 100)
281 stack_format = "%Zuk";
282 stack_scale = 1024;
284 else
286 stack_format = "%ZuM";
287 stack_scale = 1024 * 1024;
290 gdImageString (im_out, gdFontSmall, 38, ysize - 14, (unsigned char *) "0",
291 blue);
292 snprintf (buf, sizeof (buf), heap_format, 0);
293 gdImageString (im_out, gdFontSmall, maxsize_heap < 1024 ? 32 : 26,
294 ysize - 26, (unsigned char *) buf, red);
295 snprintf (buf, sizeof (buf), stack_format, 0);
296 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26,
297 (unsigned char *) buf, green);
299 if (string != NULL)
300 gdImageString (im_out, gdFontLarge, (xsize - strlen (string) * 8) / 2,
301 2, (unsigned char *) string, green);
303 gdImageStringUp (im_out, gdFontSmall, 1, ysize / 2 - 10,
304 (unsigned char *) "allocated", red);
305 gdImageStringUp (im_out, gdFontSmall, 11, ysize / 2 - 10,
306 (unsigned char *) "memory", red);
308 gdImageStringUp (im_out, gdFontSmall, xsize - 39, ysize / 2 - 10,
309 (unsigned char *) "used", green);
310 gdImageStringUp (im_out, gdFontSmall, xsize - 27, ysize / 2 - 10,
311 (unsigned char *) "stack", green);
313 snprintf (buf, sizeof (buf), heap_format, maxsize_heap / heap_scale);
314 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6, 14,
315 (unsigned char *) buf, red);
316 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / stack_scale);
317 gdImageString (im_out, gdFontSmall, xsize - 37, 14,
318 (unsigned char *) buf, green);
320 for (line = 1; line <= 3; ++line)
322 if (maxsize_heap > 0)
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,
329 maxsize_heap / 4 * line / heap_scale);
330 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6,
331 ysize - 26 - cnt, (unsigned char *) buf, red);
333 else
334 cnt = 0;
336 if (maxsize_stack > 0)
337 cnt2 = (((ysize - 40) * (maxsize_stack / 4 * line / stack_scale))
338 / (maxsize_stack / stack_scale));
339 else
340 cnt2 = 0;
342 if (cnt != cnt2)
343 gdImageDashedLine (im_out, 40, ysize - 20 - cnt2, xsize - 40,
344 ysize - 20 - cnt2, green);
345 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / 4 * line /
346 stack_scale);
347 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26 - cnt2,
348 (unsigned char *) buf, green);
351 snprintf (buf, sizeof (buf), "%llu", (unsigned long long) total);
352 gdImageString (im_out, gdFontSmall, xsize - 50, ysize - 14,
353 (unsigned char *) buf, blue);
355 if (!time_based)
357 uint64_t previously = start_time;
359 gdImageString (im_out, gdFontSmall, 40 + (xsize - 32 * 6 - 80) / 2,
360 ysize - 12,
361 (unsigned char *) "# memory handling function calls",
362 blue);
365 last_stack = last_heap = last_total = ysize - 20;
366 for (cnt = 1; cnt <= total; ++cnt)
368 struct entry entry;
369 size_t new[2];
370 uint64_t now;
372 read (fd, &entry, sizeof (entry));
374 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
376 if ((((previously - start_time) * 100) / total_time) % 10 < 5)
377 gdImageFilledRectangle (im_out,
378 40 + ((cnt - 1) * (xsize - 80)) / total,
379 ysize - 19,
380 39 + (cnt * (xsize - 80)) / total,
381 ysize - 14, yellow);
382 previously = now;
384 if (also_total && maxsize_heap > 0)
386 size_t new3;
388 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
389 * (entry.heap + entry.stack))
390 / maxsize_heap);
391 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
392 last_total,
393 40 + ((xsize - 80) * cnt) / total, new3,
394 black);
395 last_total = new3;
398 if (maxsize_heap > 0)
400 new[0] = ((ysize - 20)
401 - ((((unsigned long long int) (ysize - 40))
402 * entry.heap) / maxsize_heap));
403 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
404 last_heap, 40 + ((xsize - 80) * cnt) / total,
405 new[0], red);
406 last_heap = new[0];
409 if (maxsize_stack > 0)
411 new[1] = ((ysize - 20)
412 - ((((unsigned long long int) (ysize - 40))
413 * entry.stack) / maxsize_stack));
414 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
415 last_stack, 40 + ((xsize - 80) * cnt) / total,
416 new[1], green);
417 last_stack = new[1];
421 cnt = 0;
422 while (cnt < total)
424 gdImageLine (im_out, 40 + ((xsize - 80) * cnt) / total, ysize - 20,
425 40 + ((xsize - 80) * cnt) / total, ysize - 15, blue);
426 cnt += MAX (1, total / 20);
428 gdImageLine (im_out, xsize - 40, ysize - 20, xsize - 40, ysize - 15,
429 blue);
431 else
433 uint64_t next_tick = MAX (1, total / 20);
434 size_t last_xpos = 40;
436 gdImageString (im_out, gdFontSmall, 40 + (xsize - 39 * 6 - 80) / 2,
437 ysize - 12,
438 (unsigned char *) "\
439 # memory handling function calls / time", blue);
441 for (cnt = 0; cnt < 20; cnt += 2)
442 gdImageFilledRectangle (im_out,
443 40 + (cnt * (xsize - 80)) / 20, ysize - 19,
444 39 + ((cnt + 1) * (xsize - 80)) / 20,
445 ysize - 14, yellow);
447 last_stack = last_heap = last_total = ysize - 20;
448 for (cnt = 1; cnt <= total; ++cnt)
450 struct entry entry;
451 size_t new[2];
452 size_t xpos;
453 uint64_t now;
455 read (fd, &entry, sizeof (entry));
457 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
458 xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
460 if (cnt == next_tick)
462 gdImageLine (im_out, xpos, ysize - 20, xpos, ysize - 15, blue);
463 next_tick += MAX (1, total / 20);
466 if (also_total && maxsize_heap > 0)
468 size_t new3;
470 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
471 * (entry.heap + entry.stack))
472 / maxsize_heap);
473 gdImageLine (im_out, last_xpos, last_total, xpos, new3, black);
474 last_total = new3;
477 if (maxsize_heap > 0)
479 new[0] = ((ysize - 20)
480 - ((((unsigned long long int) (ysize - 40))
481 * entry.heap) / maxsize_heap));
482 gdImageLine (im_out, last_xpos, last_heap, xpos, new[0], red);
483 last_heap = new[0];
486 if (maxsize_stack > 0)
488 new[1] = ((ysize - 20)
489 - ((((unsigned long long int) (ysize - 40))
490 * entry.stack) / maxsize_stack));
491 gdImageLine (im_out, last_xpos, last_stack, xpos, new[1],
492 green);
493 last_stack = new[1];
496 last_xpos = xpos;
500 /* Write out the result. */
501 outfile = fopen (outname, "w");
502 if (outfile == NULL)
503 error (EXIT_FAILURE, errno, "cannot open output file");
505 gdImagePng (im_out, outfile);
507 fclose (outfile);
509 gdImageDestroy (im_out);
511 return 0;
515 /* Handle program arguments. */
516 static error_t
517 parse_opt (int key, char *arg, struct argp_state *state)
519 switch (key)
521 case 'o':
522 outname = arg;
523 break;
524 case 's':
525 string = arg;
526 break;
527 case 't':
528 time_based = 1;
529 break;
530 case 'T':
531 also_total = 1;
532 break;
533 case 'x':
534 xsize = atoi (arg);
535 if (xsize == 0)
536 xsize = XSIZE;
537 break;
538 case 'y':
539 ysize = atoi (arg);
540 if (ysize == 0)
541 ysize = XSIZE;
542 break;
543 default:
544 return ARGP_ERR_UNKNOWN;
546 return 0;
550 static char *
551 more_help (int key, const char *text, void *input)
553 char *tp;
555 switch (key)
557 case ARGP_KEY_HELP_EXTRA:
558 /* We print some extra information. */
559 if (asprintf (&tp, gettext ("\
560 For bug reporting instructions, please see:\n\
561 %s.\n"), REPORT_BUGS_TO) < 0)
562 return NULL;
563 return tp;
564 default:
565 break;
567 return (char *) text;
570 /* Print the version information. */
571 static void
572 print_version (FILE *stream, struct argp_state *state)
574 fprintf (stream, "memusagestat %s%s\n", PKGVERSION, VERSION);
575 fprintf (stream, gettext ("\
576 Copyright (C) %s Free Software Foundation, Inc.\n\
577 This is free software; see the source for copying conditions. There is NO\n\
578 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
579 "), "2013");
580 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");