.
[glibc.git] / malloc / memusagestat.c
blob5d35ee0236e8e25d75917bc799bee823a8fa7aad
1 /* Generate graphic from memory profiling data.
2 Copyright (C) 1998, 1999, 2000, 2005, 2006 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
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>
41 /* Default size of the generated image. */
42 #define XSIZE 800
43 #define YSIZE 600
45 #ifndef N_
46 # define N_(Arg) Arg
47 #endif
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
82 struct entry
84 size_t heap;
85 size_t stack;
86 uint32_t time_low;
87 uint32_t time_high;
91 /* Size of the image. */
92 static size_t xsize;
93 static size_t ysize;
95 /* Name of the output file. */
96 static char *outname;
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[])
111 int remaining;
112 const char *inname;
113 gdImagePtr im_out;
114 int grey, blue, red, green, yellow, black;
115 int fd;
116 struct stat st;
117 size_t maxsize_heap;
118 size_t maxsize_stack;
119 size_t maxsize_total;
120 uint64_t total;
121 uint64_t cnt, cnt2;
122 FILE *outfile;
123 char buf[30];
124 size_t last_heap;
125 size_t last_stack;
126 size_t last_total;
127 struct entry headent[2];
128 uint64_t start_time;
129 uint64_t end_time;
130 uint64_t total_time;
131 const char *heap_format, *stack_format;
132 int heap_scale, stack_scale, line;
134 outname = NULL;
135 xsize = XSIZE;
136 ysize = YSIZE;
137 string = NULL;
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);
146 exit (1);
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);
163 if (fd == -1)
164 error (EXIT_FAILURE, errno, "cannot open input file");
165 if (fstat (fd, &st) != 0)
167 close (fd);
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))
175 close (fd);
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;
186 if (also_total)
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
198 the file now. */
199 struct entry next;
201 while (1)
203 if (read (fd, &next, sizeof (next)) == 0)
204 break;
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;
223 if (xsize < 100)
224 xsize = 100;
225 if (ysize < 80)
226 ysize = 80;
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)
248 heap_format = "%Zu";
249 heap_scale = 1;
251 else if (maxsize_heap < 1024 * 1024 * 100)
253 heap_format = "%Zuk";
254 heap_scale = 1024;
256 else
258 heap_format = "%ZuM";
259 heap_scale = 1024 * 1024;
262 if (maxsize_stack < 1024)
264 stack_format = "%Zu";
265 stack_scale = 1;
267 else if (maxsize_stack < 1024 * 1024 * 100)
269 stack_format = "%Zuk";
270 stack_scale = 1024;
272 else
274 stack_format = "%ZuM";
275 stack_scale = 1024 * 1024;
278 gdImageString (im_out, gdFontSmall, 38, ysize - 14, (unsigned char *) "0",
279 blue);
280 snprintf (buf, sizeof (buf), heap_format, 0);
281 gdImageString (im_out, gdFontSmall, maxsize_heap < 1024 ? 32 : 26,
282 ysize - 26, (unsigned char *) buf, red);
283 snprintf (buf, sizeof (buf), stack_format, 0);
284 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26,
285 (unsigned char *) buf, green);
287 if (string != NULL)
288 gdImageString (im_out, gdFontLarge, (xsize - strlen (string) * 8) / 2,
289 2, (unsigned 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,
303 (unsigned char *) buf, red);
304 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / stack_scale);
305 gdImageString (im_out, gdFontSmall, xsize - 37, 14,
306 (unsigned char *) buf, green);
308 for (line = 1; line <= 3; ++line)
310 cnt = ((ysize - 40) * (maxsize_heap / 4 * line / heap_scale)) /
311 (maxsize_heap / heap_scale);
312 gdImageDashedLine (im_out, 40, ysize - 20 - cnt, xsize - 40,
313 ysize - 20 - cnt, red);
314 snprintf (buf, sizeof (buf), heap_format, maxsize_heap / 4 * line /
315 heap_scale);
316 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6,
317 ysize - 26 - cnt, (unsigned char *) buf, red);
319 cnt2 = ((ysize - 40) * (maxsize_stack / 4 * line / stack_scale)) /
320 (maxsize_stack / stack_scale);
321 if (cnt != cnt2)
322 gdImageDashedLine (im_out, 40, ysize - 20 - cnt2, xsize - 40,
323 ysize - 20 - cnt2, green);
324 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / 4 * line /
325 stack_scale);
326 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26 - cnt2,
327 (unsigned char *) buf, green);
330 snprintf (buf, sizeof (buf), "%llu", (unsigned long long) total);
331 gdImageString (im_out, gdFontSmall, xsize - 50, ysize - 14,
332 (unsigned char *) buf, blue);
334 if (!time_based)
336 uint64_t previously = start_time;
338 gdImageString (im_out, gdFontSmall, 40 + (xsize - 32 * 6 - 80) / 2,
339 ysize - 12,
340 (unsigned char *) "# memory handling function calls",
341 blue);
344 last_stack = last_heap = last_total = ysize - 20;
345 for (cnt = 1; cnt <= total; ++cnt)
347 struct entry entry;
348 size_t new[2];
349 uint64_t now;
351 read (fd, &entry, sizeof (entry));
353 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
355 if ((((previously - start_time) * 100) / total_time) % 10 < 5)
356 gdImageFilledRectangle (im_out,
357 40 + ((cnt - 1) * (xsize - 80)) / total,
358 ysize - 19,
359 39 + (cnt * (xsize - 80)) / total,
360 ysize - 14, yellow);
361 previously = now;
363 if (also_total)
365 size_t new3;
367 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
368 * (entry.heap + entry.stack))
369 / maxsize_heap);
370 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
371 last_total,
372 40 + ((xsize - 80) * cnt) / total, new3,
373 black);
374 last_total = new3;
377 // assert (entry.heap <= maxsize_heap);
378 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
379 * entry.heap) / maxsize_heap);
380 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
381 last_heap, 40 + ((xsize - 80) * cnt) / total, new[0],
382 red);
383 last_heap = new[0];
385 // assert (entry.stack <= maxsize_stack);
386 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
387 * entry.stack) / maxsize_stack);
388 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
389 last_stack, 40 + ((xsize - 80) * cnt) / total, new[1],
390 green);
391 last_stack = new[1];
394 cnt = 0;
395 while (cnt < total)
397 gdImageLine (im_out, 40 + ((xsize - 80) * cnt) / total, ysize - 20,
398 40 + ((xsize - 80) * cnt) / total, ysize - 15, blue);
399 cnt += MAX (1, total / 20);
401 gdImageLine (im_out, xsize - 40, ysize - 20, xsize - 40, ysize - 15,
402 blue);
404 else
406 uint64_t next_tick = MAX (1, total / 20);
407 size_t last_xpos = 40;
409 gdImageString (im_out, gdFontSmall, 40 + (xsize - 39 * 6 - 80) / 2,
410 ysize - 12,
411 (unsigned char *) "\
412 # memory handling function calls / time", blue);
414 for (cnt = 0; cnt < 20; cnt += 2)
415 gdImageFilledRectangle (im_out,
416 40 + (cnt * (xsize - 80)) / 20, ysize - 19,
417 39 + ((cnt + 1) * (xsize - 80)) / 20,
418 ysize - 14, yellow);
420 last_stack = last_heap = last_total = ysize - 20;
421 for (cnt = 1; cnt <= total; ++cnt)
423 struct entry entry;
424 size_t new[2];
425 size_t xpos;
426 uint64_t now;
428 read (fd, &entry, sizeof (entry));
430 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
431 xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
433 if (cnt == next_tick)
435 gdImageLine (im_out, xpos, ysize - 20, xpos, ysize - 15, blue);
436 next_tick += MAX (1, total / 20);
439 if (also_total)
441 size_t new3;
443 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
444 * (entry.heap + entry.stack))
445 / maxsize_heap);
446 gdImageLine (im_out, last_xpos, last_total, xpos, new3, black);
447 last_total = new3;
450 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
451 * entry.heap) / maxsize_heap);
452 gdImageLine (im_out, last_xpos, last_heap, xpos, new[0], red);
453 last_heap = new[0];
455 // assert (entry.stack <= maxsize_stack);
456 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
457 * entry.stack) / maxsize_stack);
458 gdImageLine (im_out, last_xpos, last_stack, xpos, new[1], green);
459 last_stack = new[1];
461 last_xpos = xpos;
465 /* Write out the result. */
466 outfile = fopen (outname, "w");
467 if (outfile == NULL)
468 error (EXIT_FAILURE, errno, "cannot open output file");
470 gdImagePng (im_out, outfile);
472 fclose (outfile);
474 gdImageDestroy (im_out);
476 return 0;
480 /* Handle program arguments. */
481 static error_t
482 parse_opt (int key, char *arg, struct argp_state *state)
484 switch (key)
486 case 'o':
487 outname = arg;
488 break;
489 case 's':
490 string = arg;
491 break;
492 case 't':
493 time_based = 1;
494 break;
495 case 'T':
496 also_total = 1;
497 break;
498 case 'x':
499 xsize = atoi (arg);
500 if (xsize == 0)
501 xsize = XSIZE;
502 break;
503 case 'y':
504 ysize = atoi (arg);
505 if (ysize == 0)
506 ysize = XSIZE;
507 break;
508 default:
509 return ARGP_ERR_UNKNOWN;
511 return 0;
515 static char *
516 more_help (int key, const char *text, void *input)
518 char *orig;
519 char *cp;
521 switch (key)
523 case ARGP_KEY_HELP_EXTRA:
524 /* We print some extra information. */
525 orig = gettext ("\
526 For bug reporting instructions, please see:\n\
527 <http://www.gnu.org/software/libc/bugs.html>.\n");
528 cp = strdup (orig);
529 if (cp == NULL)
530 cp = orig;
531 return cp;
532 default:
533 break;
535 return (char *) text;