(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / malloc / memusagestat.c
blobb1cad9b25118603adfed1dac95e1b113fea13bc1
1 /* Generate graphic from memory profiling data.
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 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>
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 pixel wide") },
61 { "y-size", 'y', "VALUE", 0, N_("make output graphic VALUE pixel 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 /* Data structure to communicate with argp functions. */
78 static struct argp argp =
80 options, parse_opt, args_doc, doc, NULL, more_help
84 struct entry
86 size_t heap;
87 size_t stack;
88 uint32_t time_low;
89 uint32_t time_high;
93 /* Size of the image. */
94 static size_t xsize;
95 static size_t ysize;
97 /* Name of the output file. */
98 static char *outname;
100 /* Title string for the graphic. */
101 static const char *string;
103 /* Nonzero if graph should be generated linear in time. */
104 static int time_based;
106 /* Nonzero if graph to display total use of memory should be drawn as well. */
107 static int also_total = 0;
111 main (int argc, char *argv[])
113 int remaining;
114 const char *inname;
115 gdImagePtr im_out;
116 int grey, blue, red, green, yellow, black;
117 int fd;
118 struct stat st;
119 size_t maxsize_heap;
120 size_t maxsize_stack;
121 size_t maxsize_total;
122 uint64_t total;
123 uint64_t cnt, cnt2;
124 FILE *outfile;
125 char buf[30];
126 size_t last_heap;
127 size_t last_stack;
128 size_t last_total;
129 struct entry headent[2];
130 uint64_t start_time;
131 uint64_t end_time;
132 uint64_t total_time;
133 const char *heap_format, *stack_format;
134 int heap_scale, stack_scale, line;
136 outname = NULL;
137 xsize = XSIZE;
138 ysize = YSIZE;
139 string = NULL;
141 /* Parse and process arguments. */
142 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
144 if (remaining >= argc || remaining + 2 < argc)
146 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
147 program_invocation_short_name);
148 exit (1);
151 inname = argv[remaining++];
153 if (remaining < argc)
154 outname = argv[remaining];
155 else if (outname == NULL)
157 size_t len = strlen (inname);
158 outname = alloca (len + 5);
159 stpcpy (stpcpy (outname, inname), ".png");
162 /* Open for read/write since we try to repair the file in case the
163 application hasn't terminated cleanly. */
164 fd = open (inname, O_RDWR);
165 if (fd == -1)
166 error (EXIT_FAILURE, errno, "cannot open input file");
167 if (fstat (fd, &st) != 0)
169 close (fd);
170 error (EXIT_FAILURE, errno, "cannot get size of input file");
172 /* Test whether the file contains only full records. */
173 if ((st.st_size % sizeof (struct entry)) != 0
174 /* The file must at least contain the two administrative records. */
175 || st.st_size < 2 * sizeof (struct entry))
177 close (fd);
178 error (EXIT_FAILURE, 0, "input file as incorrect size");
180 /* Compute number of data entries. */
181 total = st.st_size / sizeof (struct entry) - 2;
183 /* Read the administrative information. */
184 read (fd, headent, sizeof (headent));
185 maxsize_heap = headent[1].heap;
186 maxsize_stack = headent[1].stack;
187 maxsize_total = headent[0].stack;
188 if (also_total)
190 /* We use one scale and since we also draw the total amount of
191 memory used we have to adapt the maximum. */
192 maxsize_heap = maxsize_total;
193 maxsize_stack = maxsize_total;
196 if (maxsize_heap == 0 && maxsize_stack == 0)
198 /* The program aborted before memusage was able to write the
199 information about the maximum heap and stack use. Repair
200 the file now. */
201 struct entry next;
203 while (1)
205 if (read (fd, &next, sizeof (next)) == 0)
206 break;
207 if (next.heap > headent[1].heap)
208 headent[1].heap = next.heap;
209 if (next.stack > headent[1].stack)
210 headent[1].stack = next.stack;
213 headent[1].time_low = next.time_low;
214 headent[1].time_high = next.time_high;
216 /* Write the computed values in the file. */
217 lseek (fd, sizeof (struct entry), SEEK_SET);
218 write (fd, &headent[1], sizeof (struct entry));
221 start_time = ((uint64_t) headent[0].time_high) << 32 | headent[0].time_low;
222 end_time = ((uint64_t) headent[1].time_high) << 32 | headent[1].time_low;
223 total_time = end_time - start_time;
225 if (xsize < 100)
226 xsize = 100;
227 if (ysize < 80)
228 ysize = 80;
230 /* Create output image with the specified size. */
231 im_out = gdImageCreate (xsize, ysize);
233 /* First color allocated is background. */
234 grey = gdImageColorAllocate (im_out, 224, 224, 224);
236 /* Set transparent color. */
237 gdImageColorTransparent (im_out, grey);
239 /* These are all the other colors we need (in the moment). */
240 red = gdImageColorAllocate (im_out, 255, 0, 0);
241 green = gdImageColorAllocate (im_out, 0, 130, 0);
242 blue = gdImageColorAllocate (im_out, 0, 0, 255);
243 yellow = gdImageColorAllocate (im_out, 154, 205, 50);
244 black = gdImageColorAllocate (im_out, 0, 0, 0);
246 gdImageRectangle (im_out, 40, 20, xsize - 40, ysize - 20, blue);
248 if (maxsize_heap < 1024)
250 heap_format = "%Zu";
251 heap_scale = 1;
253 else if (maxsize_heap < 1024 * 1024 * 100)
255 heap_format = "%Zuk";
256 heap_scale = 1024;
258 else
260 heap_format = "%ZuM";
261 heap_scale = 1024 * 1024;
264 if (maxsize_stack < 1024)
266 stack_format = "%Zu";
267 stack_scale = 1;
269 else if (maxsize_stack < 1024 * 1024 * 100)
271 stack_format = "%Zuk";
272 stack_scale = 1024;
274 else
276 stack_format = "%ZuM";
277 stack_scale = 1024 * 1024;
280 gdImageString (im_out, gdFontSmall, 38, ysize - 14, (unsigned char *) "0",
281 blue);
282 snprintf(buf, sizeof (buf), heap_format, 0);
283 gdImageString (im_out, gdFontSmall, maxsize_heap < 1024 ? 32 : 26,
284 ysize - 26, buf, red);
285 snprintf(buf, sizeof (buf), stack_format, 0);
286 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26,
287 buf, green);
289 if (string != NULL)
290 gdImageString (im_out, gdFontLarge, (xsize - strlen (string) * 8) / 2,
291 2, (char *) string, green);
293 gdImageStringUp (im_out, gdFontSmall, 1, ysize / 2 - 10,
294 (unsigned char *) "allocated", red);
295 gdImageStringUp (im_out, gdFontSmall, 11, ysize / 2 - 10,
296 (unsigned char *) "memory", red);
298 gdImageStringUp (im_out, gdFontSmall, xsize - 39, ysize / 2 - 10,
299 (unsigned char *) "used", green);
300 gdImageStringUp (im_out, gdFontSmall, xsize - 27, ysize / 2 - 10,
301 (unsigned char *) "stack", green);
303 snprintf (buf, sizeof (buf), heap_format, maxsize_heap / heap_scale);
304 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6, 14, buf, red);
305 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / stack_scale);
306 gdImageString (im_out, gdFontSmall, xsize - 37, 14, 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, 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 buf, green);
330 snprintf (buf, sizeof (buf), "%llu", (unsigned long long) total);
331 gdImageString (im_out, gdFontSmall, xsize - 50, ysize - 14, buf, blue);
333 if (!time_based)
335 uint64_t previously = start_time;
337 gdImageString (im_out, gdFontSmall, 40 + (xsize - 32 * 6 - 80) / 2,
338 ysize - 12,
339 (unsigned char *) "# memory handling function calls",
340 blue);
343 last_stack = last_heap = last_total = ysize - 20;
344 for (cnt = 1; cnt <= total; ++cnt)
346 struct entry entry;
347 size_t new[2];
348 uint64_t now;
350 read (fd, &entry, sizeof (entry));
352 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
354 if ((((previously - start_time) * 100) / total_time) % 10 < 5)
355 gdImageFilledRectangle (im_out,
356 40 + ((cnt - 1) * (xsize - 80)) / total,
357 ysize - 19,
358 39 + (cnt * (xsize - 80)) / total,
359 ysize - 14, yellow);
360 previously = now;
362 if (also_total)
364 size_t new3;
366 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
367 * (entry.heap + entry.stack))
368 / maxsize_heap);
369 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
370 last_total,
371 40 + ((xsize - 80) * cnt) / total, new3,
372 black);
373 last_total = new3;
376 // assert (entry.heap <= maxsize_heap);
377 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
378 * entry.heap) / maxsize_heap);
379 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
380 last_heap, 40 + ((xsize - 80) * cnt) / total, new[0],
381 red);
382 last_heap = new[0];
384 // assert (entry.stack <= maxsize_stack);
385 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
386 * entry.stack) / maxsize_stack);
387 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
388 last_stack, 40 + ((xsize - 80) * cnt) / total, new[1],
389 green);
390 last_stack = new[1];
393 cnt = 0;
394 while (cnt < total)
396 gdImageLine (im_out, 40 + ((xsize - 80) * cnt) / total, ysize - 20,
397 40 + ((xsize - 80) * cnt) / total, ysize - 15, blue);
398 cnt += MAX (1, total / 20);
400 gdImageLine (im_out, xsize - 40, ysize - 20, xsize - 40, ysize - 15,
401 blue);
403 else
405 uint64_t next_tick = MAX (1, total / 20);
406 size_t last_xpos = 40;
408 gdImageString (im_out, gdFontSmall, 40 + (xsize - 39 * 6 - 80) / 2,
409 ysize - 12,
410 (unsigned char *) "\
411 # memory handling function calls / time", blue);
413 for (cnt = 0; cnt < 20; cnt += 2)
414 gdImageFilledRectangle (im_out,
415 40 + (cnt * (xsize - 80)) / 20, ysize - 19,
416 39 + ((cnt + 1) * (xsize - 80)) / 20,
417 ysize - 14, yellow);
419 last_stack = last_heap = last_total = ysize - 20;
420 for (cnt = 1; cnt <= total; ++cnt)
422 struct entry entry;
423 size_t new[2];
424 size_t xpos;
425 uint64_t now;
427 read (fd, &entry, sizeof (entry));
429 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
430 xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
432 if (cnt == next_tick)
434 gdImageLine (im_out, xpos, ysize - 20, xpos, ysize - 15, blue);
435 next_tick += MAX (1, total / 20);
438 if (also_total)
440 size_t new3;
442 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
443 * (entry.heap + entry.stack))
444 / maxsize_heap);
445 gdImageLine (im_out, last_xpos, last_total, xpos, new3, black);
446 last_total = new3;
449 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
450 * entry.heap) / maxsize_heap);
451 gdImageLine (im_out, last_xpos, last_heap, xpos, new[0], red);
452 last_heap = new[0];
454 // assert (entry.stack <= maxsize_stack);
455 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
456 * entry.stack) / maxsize_stack);
457 gdImageLine (im_out, last_xpos, last_stack, xpos, new[1], green);
458 last_stack = new[1];
460 last_xpos = xpos;
464 /* Write out the result. */
465 outfile = fopen (outname, "w");
466 if (outfile == NULL)
467 error (EXIT_FAILURE, errno, "cannot open output file");
469 gdImagePng (im_out, outfile);
471 fclose (outfile);
473 gdImageDestroy (im_out);
475 return 0;
479 /* Handle program arguments. */
480 static error_t
481 parse_opt (int key, char *arg, struct argp_state *state)
483 switch (key)
485 case 'o':
486 outname = arg;
487 break;
488 case 's':
489 string = arg;
490 break;
491 case 't':
492 time_based = 1;
493 break;
494 case 'T':
495 also_total = 1;
496 break;
497 case 'x':
498 xsize = atoi (arg);
499 if (xsize == 0)
500 xsize = XSIZE;
501 break;
502 case 'y':
503 ysize = atoi (arg);
504 if (ysize == 0)
505 ysize = XSIZE;
506 break;
507 default:
508 return ARGP_ERR_UNKNOWN;
510 return 0;
514 static char *
515 more_help (int key, const char *text, void *input)
517 char *orig;
518 char *cp;
520 switch (key)
522 case ARGP_KEY_HELP_EXTRA:
523 /* We print some extra information. */
524 orig = gettext ("\
525 For bug reporting instructions, please see:\n\
526 <http://www.gnu.org/software/libc/bugs.html>.\n");
527 cp = strdup (orig);
528 if (cp == NULL)
529 cp = orig;
530 return cp;
531 default:
532 break;
534 return (char *) text;