Set GL(dl_nns) to 1 for vDSO in libc.a
[glibc.git] / malloc / memusagestat.c
blobaf8f991003fb19363e70e730858b2c6abf57b996
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, see <http://www.gnu.org/licenses/>. */
20 #define _FILE_OFFSET_BITS 64
22 #include <argp.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <error.h>
26 #include <fcntl.h>
27 #include <getopt.h>
28 #include <inttypes.h>
29 #include <libintl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
37 #include <gd.h>
38 #include <gdfontl.h>
39 #include <gdfonts.h>
41 #include "../version.h"
42 #define PACKAGE _libc_intl_domainname
44 /* Default size of the generated image. */
45 #define XSIZE 800
46 #define YSIZE 600
48 #ifndef N_
49 # define N_(Arg) Arg
50 #endif
53 /* Definitions of arguments for argp functions. */
54 static const struct argp_option options[] =
56 { "output", 'o', "FILE", 0, N_("Name output file") },
57 { "string", 's', "STRING", 0, N_("Title string used in output graphic") },
58 { "time", 't', NULL, 0, N_("Generate output linear to time (default is linear to number of function calls)") },
59 { "total", 'T', NULL, 0,
60 N_("Also draw graph for total memory consumption") },
61 { "x-size", 'x', "VALUE", 0, N_("Make output graphic VALUE pixels wide") },
62 { "y-size", 'y', "VALUE", 0, N_("Make output graphic VALUE pixels high") },
63 { NULL, 0, NULL, 0, NULL }
66 /* Short description of program. */
67 static const char doc[] = N_("Generate graphic from memory profiling data");
69 /* Strings for arguments in help texts. */
70 static const char args_doc[] = N_("DATAFILE [OUTFILE]");
72 /* Prototype for option handler. */
73 static error_t parse_opt (int key, char *arg, struct argp_state *state);
75 /* Function to print some extra text in the help message. */
76 static char *more_help (int key, const char *text, void *input);
78 /* Name and version of program. */
79 static void print_version (FILE *stream, struct argp_state *state);
80 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
82 /* Data structure to communicate with argp functions. */
83 static struct argp argp =
85 options, parse_opt, args_doc, doc, NULL, more_help
89 struct entry
91 uint64_t heap;
92 uint64_t stack;
93 uint32_t time_low;
94 uint32_t time_high;
98 /* Size of the image. */
99 static size_t xsize;
100 static size_t ysize;
102 /* Name of the output file. */
103 static char *outname;
105 /* Title string for the graphic. */
106 static const char *string;
108 /* Nonzero if graph should be generated linear in time. */
109 static int time_based;
111 /* Nonzero if graph to display total use of memory should be drawn as well. */
112 static int also_total = 0;
116 main (int argc, char *argv[])
118 int remaining;
119 const char *inname;
120 gdImagePtr im_out;
121 int grey, blue, red, green, yellow, black;
122 int fd;
123 struct stat st;
124 size_t maxsize_heap;
125 size_t maxsize_stack;
126 size_t maxsize_total;
127 uint64_t total;
128 uint64_t cnt, cnt2;
129 FILE *outfile;
130 char buf[30];
131 size_t last_heap;
132 size_t last_stack;
133 size_t last_total;
134 struct entry headent[2];
135 uint64_t start_time;
136 uint64_t end_time;
137 uint64_t total_time;
138 const char *heap_format, *stack_format;
139 int heap_scale, stack_scale, line;
141 outname = NULL;
142 xsize = XSIZE;
143 ysize = YSIZE;
144 string = NULL;
146 /* Parse and process arguments. */
147 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
149 if (remaining >= argc || remaining + 2 < argc)
151 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
152 program_invocation_short_name);
153 exit (1);
156 inname = argv[remaining++];
158 if (remaining < argc)
159 outname = argv[remaining];
160 else if (outname == NULL)
162 size_t len = strlen (inname);
163 outname = alloca (len + 5);
164 stpcpy (stpcpy (outname, inname), ".png");
167 /* Open for read/write since we try to repair the file in case the
168 application hasn't terminated cleanly. */
169 fd = open (inname, O_RDWR);
170 if (fd == -1)
171 error (EXIT_FAILURE, errno, "cannot open input file");
172 if (fstat (fd, &st) != 0)
174 close (fd);
175 error (EXIT_FAILURE, errno, "cannot get size of input file");
177 /* Test whether the file contains only full records. */
178 if ((st.st_size % sizeof (struct entry)) != 0
179 /* The file must at least contain the two administrative records. */
180 || st.st_size < 2 * sizeof (struct entry))
182 close (fd);
183 error (EXIT_FAILURE, 0, "input file has incorrect size");
185 /* Compute number of data entries. */
186 total = st.st_size / sizeof (struct entry) - 2;
188 /* Read the administrative information. */
189 read (fd, headent, sizeof (headent));
190 maxsize_heap = headent[1].heap;
191 maxsize_stack = headent[1].stack;
192 maxsize_total = headent[0].stack;
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 > maxsize_heap)
206 maxsize_heap = next.heap;
207 if (next.stack > maxsize_stack)
208 maxsize_stack = next.stack;
209 if (maxsize_heap + maxsize_stack > maxsize_total)
210 maxsize_total = maxsize_heap + maxsize_stack;
213 headent[0].stack = maxsize_total;
214 headent[1].heap = maxsize_heap;
215 headent[1].stack = maxsize_stack;
216 headent[1].time_low = next.time_low;
217 headent[1].time_high = next.time_high;
219 /* Write the computed values in the file. */
220 lseek (fd, 0, SEEK_SET);
221 write (fd, headent, 2 * sizeof (struct entry));
224 if (also_total)
226 /* We use one scale and since we also draw the total amount of
227 memory used we have to adapt the maximum. */
228 maxsize_heap = maxsize_total;
229 maxsize_stack = maxsize_total;
232 start_time = ((uint64_t) headent[0].time_high) << 32 | headent[0].time_low;
233 end_time = ((uint64_t) headent[1].time_high) << 32 | headent[1].time_low;
234 total_time = end_time - start_time;
236 if (xsize < 100)
237 xsize = 100;
238 if (ysize < 80)
239 ysize = 80;
241 /* Create output image with the specified size. */
242 im_out = gdImageCreate (xsize, ysize);
244 /* First color allocated is background. */
245 grey = gdImageColorAllocate (im_out, 224, 224, 224);
247 /* Set transparent color. */
248 gdImageColorTransparent (im_out, grey);
250 /* These are all the other colors we need (in the moment). */
251 red = gdImageColorAllocate (im_out, 255, 0, 0);
252 green = gdImageColorAllocate (im_out, 0, 130, 0);
253 blue = gdImageColorAllocate (im_out, 0, 0, 255);
254 yellow = gdImageColorAllocate (im_out, 154, 205, 50);
255 black = gdImageColorAllocate (im_out, 0, 0, 0);
257 gdImageRectangle (im_out, 40, 20, xsize - 40, ysize - 20, blue);
259 if (maxsize_heap < 1024)
261 heap_format = "%Zu";
262 heap_scale = 1;
264 else if (maxsize_heap < 1024 * 1024 * 100)
266 heap_format = "%Zuk";
267 heap_scale = 1024;
269 else
271 heap_format = "%ZuM";
272 heap_scale = 1024 * 1024;
275 if (maxsize_stack < 1024)
277 stack_format = "%Zu";
278 stack_scale = 1;
280 else if (maxsize_stack < 1024 * 1024 * 100)
282 stack_format = "%Zuk";
283 stack_scale = 1024;
285 else
287 stack_format = "%ZuM";
288 stack_scale = 1024 * 1024;
291 gdImageString (im_out, gdFontSmall, 38, ysize - 14, (unsigned char *) "0",
292 blue);
293 snprintf (buf, sizeof (buf), heap_format, 0);
294 gdImageString (im_out, gdFontSmall, maxsize_heap < 1024 ? 32 : 26,
295 ysize - 26, (unsigned char *) buf, red);
296 snprintf (buf, sizeof (buf), stack_format, 0);
297 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26,
298 (unsigned char *) buf, green);
300 if (string != NULL)
301 gdImageString (im_out, gdFontLarge, (xsize - strlen (string) * 8) / 2,
302 2, (unsigned char *) string, green);
304 gdImageStringUp (im_out, gdFontSmall, 1, ysize / 2 - 10,
305 (unsigned char *) "allocated", red);
306 gdImageStringUp (im_out, gdFontSmall, 11, ysize / 2 - 10,
307 (unsigned char *) "memory", red);
309 gdImageStringUp (im_out, gdFontSmall, xsize - 39, ysize / 2 - 10,
310 (unsigned char *) "used", green);
311 gdImageStringUp (im_out, gdFontSmall, xsize - 27, ysize / 2 - 10,
312 (unsigned char *) "stack", green);
314 snprintf (buf, sizeof (buf), heap_format, maxsize_heap / heap_scale);
315 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6, 14,
316 (unsigned char *) buf, red);
317 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / stack_scale);
318 gdImageString (im_out, gdFontSmall, xsize - 37, 14,
319 (unsigned char *) buf, green);
321 for (line = 1; line <= 3; ++line)
323 cnt = ((ysize - 40) * (maxsize_heap / 4 * line / heap_scale)) /
324 (maxsize_heap / heap_scale);
325 gdImageDashedLine (im_out, 40, ysize - 20 - cnt, xsize - 40,
326 ysize - 20 - cnt, red);
327 snprintf (buf, sizeof (buf), heap_format, maxsize_heap / 4 * line /
328 heap_scale);
329 gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6,
330 ysize - 26 - cnt, (unsigned char *) buf, red);
332 cnt2 = ((ysize - 40) * (maxsize_stack / 4 * line / stack_scale)) /
333 (maxsize_stack / stack_scale);
334 if (cnt != cnt2)
335 gdImageDashedLine (im_out, 40, ysize - 20 - cnt2, xsize - 40,
336 ysize - 20 - cnt2, green);
337 snprintf (buf, sizeof (buf), stack_format, maxsize_stack / 4 * line /
338 stack_scale);
339 gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26 - cnt2,
340 (unsigned char *) buf, green);
343 snprintf (buf, sizeof (buf), "%llu", (unsigned long long) total);
344 gdImageString (im_out, gdFontSmall, xsize - 50, ysize - 14,
345 (unsigned char *) buf, blue);
347 if (!time_based)
349 uint64_t previously = start_time;
351 gdImageString (im_out, gdFontSmall, 40 + (xsize - 32 * 6 - 80) / 2,
352 ysize - 12,
353 (unsigned char *) "# memory handling function calls",
354 blue);
357 last_stack = last_heap = last_total = ysize - 20;
358 for (cnt = 1; cnt <= total; ++cnt)
360 struct entry entry;
361 size_t new[2];
362 uint64_t now;
364 read (fd, &entry, sizeof (entry));
366 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
368 if ((((previously - start_time) * 100) / total_time) % 10 < 5)
369 gdImageFilledRectangle (im_out,
370 40 + ((cnt - 1) * (xsize - 80)) / total,
371 ysize - 19,
372 39 + (cnt * (xsize - 80)) / total,
373 ysize - 14, yellow);
374 previously = now;
376 if (also_total)
378 size_t new3;
380 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
381 * (entry.heap + entry.stack))
382 / maxsize_heap);
383 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
384 last_total,
385 40 + ((xsize - 80) * cnt) / total, new3,
386 black);
387 last_total = new3;
390 // assert (entry.heap <= maxsize_heap);
391 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
392 * entry.heap) / maxsize_heap);
393 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
394 last_heap, 40 + ((xsize - 80) * cnt) / total, new[0],
395 red);
396 last_heap = new[0];
398 // assert (entry.stack <= maxsize_stack);
399 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
400 * entry.stack) / maxsize_stack);
401 gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
402 last_stack, 40 + ((xsize - 80) * cnt) / total, new[1],
403 green);
404 last_stack = new[1];
407 cnt = 0;
408 while (cnt < total)
410 gdImageLine (im_out, 40 + ((xsize - 80) * cnt) / total, ysize - 20,
411 40 + ((xsize - 80) * cnt) / total, ysize - 15, blue);
412 cnt += MAX (1, total / 20);
414 gdImageLine (im_out, xsize - 40, ysize - 20, xsize - 40, ysize - 15,
415 blue);
417 else
419 uint64_t next_tick = MAX (1, total / 20);
420 size_t last_xpos = 40;
422 gdImageString (im_out, gdFontSmall, 40 + (xsize - 39 * 6 - 80) / 2,
423 ysize - 12,
424 (unsigned char *) "\
425 # memory handling function calls / time", blue);
427 for (cnt = 0; cnt < 20; cnt += 2)
428 gdImageFilledRectangle (im_out,
429 40 + (cnt * (xsize - 80)) / 20, ysize - 19,
430 39 + ((cnt + 1) * (xsize - 80)) / 20,
431 ysize - 14, yellow);
433 last_stack = last_heap = last_total = ysize - 20;
434 for (cnt = 1; cnt <= total; ++cnt)
436 struct entry entry;
437 size_t new[2];
438 size_t xpos;
439 uint64_t now;
441 read (fd, &entry, sizeof (entry));
443 now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
444 xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
446 if (cnt == next_tick)
448 gdImageLine (im_out, xpos, ysize - 20, xpos, ysize - 15, blue);
449 next_tick += MAX (1, total / 20);
452 if (also_total)
454 size_t new3;
456 new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
457 * (entry.heap + entry.stack))
458 / maxsize_heap);
459 gdImageLine (im_out, last_xpos, last_total, xpos, new3, black);
460 last_total = new3;
463 new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
464 * entry.heap) / maxsize_heap);
465 gdImageLine (im_out, last_xpos, last_heap, xpos, new[0], red);
466 last_heap = new[0];
468 // assert (entry.stack <= maxsize_stack);
469 new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
470 * entry.stack) / maxsize_stack);
471 gdImageLine (im_out, last_xpos, last_stack, xpos, new[1], green);
472 last_stack = new[1];
474 last_xpos = xpos;
478 /* Write out the result. */
479 outfile = fopen (outname, "w");
480 if (outfile == NULL)
481 error (EXIT_FAILURE, errno, "cannot open output file");
483 gdImagePng (im_out, outfile);
485 fclose (outfile);
487 gdImageDestroy (im_out);
489 return 0;
493 /* Handle program arguments. */
494 static error_t
495 parse_opt (int key, char *arg, struct argp_state *state)
497 switch (key)
499 case 'o':
500 outname = arg;
501 break;
502 case 's':
503 string = arg;
504 break;
505 case 't':
506 time_based = 1;
507 break;
508 case 'T':
509 also_total = 1;
510 break;
511 case 'x':
512 xsize = atoi (arg);
513 if (xsize == 0)
514 xsize = XSIZE;
515 break;
516 case 'y':
517 ysize = atoi (arg);
518 if (ysize == 0)
519 ysize = XSIZE;
520 break;
521 default:
522 return ARGP_ERR_UNKNOWN;
524 return 0;
528 static char *
529 more_help (int key, const char *text, void *input)
531 char *orig;
532 char *cp;
534 switch (key)
536 case ARGP_KEY_HELP_EXTRA:
537 /* We print some extra information. */
538 orig = gettext ("\
539 For bug reporting instructions, please see:\n\
540 <http://www.gnu.org/software/libc/bugs.html>.\n");
541 cp = strdup (orig);
542 if (cp == NULL)
543 cp = orig;
544 return cp;
545 default:
546 break;
548 return (char *) text;
551 /* Print the version information. */
552 static void
553 print_version (FILE *stream, struct argp_state *state)
555 fprintf (stream, "memusagestat (GNU %s) %s\n", PACKAGE, VERSION);
556 fprintf (stream, gettext ("\
557 Copyright (C) %s Free Software Foundation, Inc.\n\
558 This is free software; see the source for copying conditions. There is NO\n\
559 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
560 "), "2009");
561 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");