Update copyright notices with scripts/update-copyrights.
[glibc.git] / malloc / memusagestat.c
blobf561e0dbbc55caa8005600b4e95a87d7ab971a34
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 cnt = ((ysize - 40) * (maxsize_heap / 4 * line / heap_scale)) /
323 (maxsize_heap / heap_scale);
324 gdImageDashedLine (im_out, 40, ysize - 20 - cnt, xsize - 40,
325 ysize - 20 - cnt, red);
326 snprintf (buf, sizeof (buf), heap_format, maxsize_heap / 4 * line /
327 heap_scale);
328 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6,
329 ysize - 26 - cnt, (unsigned char *) buf, red);
331 cnt2 = ((ysize - 40) * (maxsize_stack / 4 * line / stack_scale)) /
332 (maxsize_stack / stack_scale);
333 if (cnt != cnt2)
334 gdImageDashedLine (im_out, 40, ysize - 20 - cnt2, xsize - 40,
335 ysize - 20 - cnt2, green);
336 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / 4 * line /
337 stack_scale);
338 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26 - cnt2,
339 (unsigned char *) buf, green);
342 snprintf (buf, sizeof (buf), "%llu", (unsigned long long) total);
343 gdImageString (im_out, gdFontSmall, xsize - 50, ysize - 14,
344 (unsigned char *) buf, blue);
346 if (!time_based)
348 uint64_t previously = start_time;
350 gdImageString (im_out, gdFontSmall, 40 + (xsize - 32 * 6 - 80) / 2,
351 ysize - 12,
352 (unsigned char *) "# memory handling function calls",
353 blue);
356 last_stack = last_heap = last_total = ysize - 20;
357 for (cnt = 1; cnt <= total; ++cnt)
359 struct entry entry;
360 size_t new[2];
361 uint64_t now;
363 read (fd, &entry, sizeof (entry));
365 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
367 if ((((previously - start_time) * 100) / total_time) % 10 < 5)
368 gdImageFilledRectangle (im_out,
369 40 + ((cnt - 1) * (xsize - 80)) / total,
370 ysize - 19,
371 39 + (cnt * (xsize - 80)) / total,
372 ysize - 14, yellow);
373 previously = now;
375 if (also_total)
377 size_t new3;
379 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
380 * (entry.heap + entry.stack))
381 / maxsize_heap);
382 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
383 last_total,
384 40 + ((xsize - 80) * cnt) / total, new3,
385 black);
386 last_total = new3;
389 // assert (entry.heap <= maxsize_heap);
390 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
391 * entry.heap) / maxsize_heap);
392 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
393 last_heap, 40 + ((xsize - 80) * cnt) / total, new[0],
394 red);
395 last_heap = new[0];
397 // assert (entry.stack <= maxsize_stack);
398 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
399 * entry.stack) / maxsize_stack);
400 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
401 last_stack, 40 + ((xsize - 80) * cnt) / total, new[1],
402 green);
403 last_stack = new[1];
406 cnt = 0;
407 while (cnt < total)
409 gdImageLine (im_out, 40 + ((xsize - 80) * cnt) / total, ysize - 20,
410 40 + ((xsize - 80) * cnt) / total, ysize - 15, blue);
411 cnt += MAX (1, total / 20);
413 gdImageLine (im_out, xsize - 40, ysize - 20, xsize - 40, ysize - 15,
414 blue);
416 else
418 uint64_t next_tick = MAX (1, total / 20);
419 size_t last_xpos = 40;
421 gdImageString (im_out, gdFontSmall, 40 + (xsize - 39 * 6 - 80) / 2,
422 ysize - 12,
423 (unsigned char *) "\
424 # memory handling function calls / time", blue);
426 for (cnt = 0; cnt < 20; cnt += 2)
427 gdImageFilledRectangle (im_out,
428 40 + (cnt * (xsize - 80)) / 20, ysize - 19,
429 39 + ((cnt + 1) * (xsize - 80)) / 20,
430 ysize - 14, yellow);
432 last_stack = last_heap = last_total = ysize - 20;
433 for (cnt = 1; cnt <= total; ++cnt)
435 struct entry entry;
436 size_t new[2];
437 size_t xpos;
438 uint64_t now;
440 read (fd, &entry, sizeof (entry));
442 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
443 xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
445 if (cnt == next_tick)
447 gdImageLine (im_out, xpos, ysize - 20, xpos, ysize - 15, blue);
448 next_tick += MAX (1, total / 20);
451 if (also_total)
453 size_t new3;
455 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
456 * (entry.heap + entry.stack))
457 / maxsize_heap);
458 gdImageLine (im_out, last_xpos, last_total, xpos, new3, black);
459 last_total = new3;
462 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
463 * entry.heap) / maxsize_heap);
464 gdImageLine (im_out, last_xpos, last_heap, xpos, new[0], red);
465 last_heap = new[0];
467 // assert (entry.stack <= maxsize_stack);
468 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
469 * entry.stack) / maxsize_stack);
470 gdImageLine (im_out, last_xpos, last_stack, xpos, new[1], green);
471 last_stack = new[1];
473 last_xpos = xpos;
477 /* Write out the result. */
478 outfile = fopen (outname, "w");
479 if (outfile == NULL)
480 error (EXIT_FAILURE, errno, "cannot open output file");
482 gdImagePng (im_out, outfile);
484 fclose (outfile);
486 gdImageDestroy (im_out);
488 return 0;
492 /* Handle program arguments. */
493 static error_t
494 parse_opt (int key, char *arg, struct argp_state *state)
496 switch (key)
498 case 'o':
499 outname = arg;
500 break;
501 case 's':
502 string = arg;
503 break;
504 case 't':
505 time_based = 1;
506 break;
507 case 'T':
508 also_total = 1;
509 break;
510 case 'x':
511 xsize = atoi (arg);
512 if (xsize == 0)
513 xsize = XSIZE;
514 break;
515 case 'y':
516 ysize = atoi (arg);
517 if (ysize == 0)
518 ysize = XSIZE;
519 break;
520 default:
521 return ARGP_ERR_UNKNOWN;
523 return 0;
527 static char *
528 more_help (int key, const char *text, void *input)
530 char *tp;
532 switch (key)
534 case ARGP_KEY_HELP_EXTRA:
535 /* We print some extra information. */
536 if (asprintf (&tp, gettext ("\
537 For bug reporting instructions, please see:\n\
538 %s.\n"), REPORT_BUGS_TO) < 0)
539 return NULL;
540 return tp;
541 default:
542 break;
544 return (char *) text;
547 /* Print the version information. */
548 static void
549 print_version (FILE *stream, struct argp_state *state)
551 fprintf (stream, "memusagestat %s%s\n", PKGVERSION, VERSION);
552 fprintf (stream, gettext ("\
553 Copyright (C) %s Free Software Foundation, Inc.\n\
554 This is free software; see the source for copying conditions. There is NO\n\
555 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
556 "), "2013");
557 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");