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
33 #include <sys/param.h>
40 #include "../version.h"
41 #define PACKAGE _libc_intl_domainname
43 /* Default size of the generated image. */
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
97 /* Size of the image. */
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
[])
120 int grey
, blue
, red
, green
, yellow
, black
;
124 size_t maxsize_stack
;
125 size_t maxsize_total
;
133 struct entry headent
[2];
137 const char *heap_format
, *stack_format
;
138 int heap_scale
, stack_scale
, line
;
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
);
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
);
170 error (EXIT_FAILURE
, errno
, "cannot open input file");
171 if (fstat (fd
, &st
) != 0)
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
))
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
202 if (read (fd
, &next
, sizeof (next
)) == 0)
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
));
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
;
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)
263 else if (maxsize_heap
< 1024 * 1024 * 100)
265 heap_format
= "%Zuk";
270 heap_format
= "%ZuM";
271 heap_scale
= 1024 * 1024;
274 if (maxsize_stack
< 1024)
276 stack_format
= "%Zu";
279 else if (maxsize_stack
< 1024 * 1024 * 100)
281 stack_format
= "%Zuk";
286 stack_format
= "%ZuM";
287 stack_scale
= 1024 * 1024;
290 gdImageString (im_out
, gdFontSmall
, 38, ysize
- 14, (unsigned char *) "0",
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
);
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
);
336 if (maxsize_stack
> 0)
337 cnt2
= (((ysize
- 40) * (maxsize_stack
/ 4 * line
/ stack_scale
))
338 / (maxsize_stack
/ stack_scale
));
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
/
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
);
357 uint64_t previously
= start_time
;
359 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 32 * 6 - 80) / 2,
361 (unsigned char *) "# memory handling function calls",
365 last_stack
= last_heap
= last_total
= ysize
- 20;
366 for (cnt
= 1; cnt
<= total
; ++cnt
)
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
,
380 39 + (cnt
* (xsize
- 80)) / total
,
384 if (also_total
&& maxsize_heap
> 0)
388 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
389 * (entry
.heap
+ entry
.stack
))
391 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
393 40 + ((xsize
- 80) * cnt
) / 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
,
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
,
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,
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,
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,
447 last_stack
= last_heap
= last_total
= ysize
- 20;
448 for (cnt
= 1; cnt
<= total
; ++cnt
)
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)
470 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
471 * (entry
.heap
+ entry
.stack
))
473 gdImageLine (im_out
, last_xpos
, last_total
, xpos
, new3
, black
);
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
);
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],
500 /* Write out the result. */
501 outfile
= fopen (outname
, "w");
503 error (EXIT_FAILURE
, errno
, "cannot open output file");
505 gdImagePng (im_out
, outfile
);
509 gdImageDestroy (im_out
);
515 /* Handle program arguments. */
517 parse_opt (int key
, char *arg
, struct argp_state
*state
)
544 return ARGP_ERR_UNKNOWN
;
551 more_help (int key
, const char *text
, void *input
)
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)
567 return (char *) text
;
570 /* Print the version information. */
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\
580 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");