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, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #define _FILE_OFFSET_BITS 64
35 #include <sys/param.h>
42 #include "../version.h"
43 #define PACKAGE _libc_intl_domainname
45 /* Default size of the generated image. */
54 /* Definitions of arguments for argp functions. */
55 static const struct argp_option options
[] =
57 { "output", 'o', "FILE", 0, N_("Name output file") },
58 { "string", 's', "STRING", 0, N_("Title string used in output graphic") },
59 { "time", 't', NULL
, 0, N_("Generate output linear to time (default is linear to number of function calls)") },
60 { "total", 'T', NULL
, 0,
61 N_("Also draw graph for total memory consumption") },
62 { "x-size", 'x', "VALUE", 0, N_("Make output graphic VALUE pixels wide") },
63 { "y-size", 'y', "VALUE", 0, N_("Make output graphic VALUE pixels high") },
64 { NULL
, 0, NULL
, 0, NULL
}
67 /* Short description of program. */
68 static const char doc
[] = N_("Generate graphic from memory profiling data");
70 /* Strings for arguments in help texts. */
71 static const char args_doc
[] = N_("DATAFILE [OUTFILE]");
73 /* Prototype for option handler. */
74 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
76 /* Function to print some extra text in the help message. */
77 static char *more_help (int key
, const char *text
, void *input
);
79 /* Name and version of program. */
80 static void print_version (FILE *stream
, struct argp_state
*state
);
81 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
83 /* Data structure to communicate with argp functions. */
84 static struct argp argp
=
86 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
99 /* Size of the image. */
103 /* Name of the output file. */
104 static char *outname
;
106 /* Title string for the graphic. */
107 static const char *string
;
109 /* Nonzero if graph should be generated linear in time. */
110 static int time_based
;
112 /* Nonzero if graph to display total use of memory should be drawn as well. */
113 static int also_total
= 0;
117 main (int argc
, char *argv
[])
122 int grey
, blue
, red
, green
, yellow
, black
;
126 size_t maxsize_stack
;
127 size_t maxsize_total
;
135 struct entry headent
[2];
139 const char *heap_format
, *stack_format
;
140 int heap_scale
, stack_scale
, line
;
147 /* Parse and process arguments. */
148 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
150 if (remaining
>= argc
|| remaining
+ 2 < argc
)
152 argp_help (&argp
, stdout
, ARGP_HELP_SEE
| ARGP_HELP_EXIT_ERR
,
153 program_invocation_short_name
);
157 inname
= argv
[remaining
++];
159 if (remaining
< argc
)
160 outname
= argv
[remaining
];
161 else if (outname
== NULL
)
163 size_t len
= strlen (inname
);
164 outname
= alloca (len
+ 5);
165 stpcpy (stpcpy (outname
, inname
), ".png");
168 /* Open for read/write since we try to repair the file in case the
169 application hasn't terminated cleanly. */
170 fd
= open (inname
, O_RDWR
);
172 error (EXIT_FAILURE
, errno
, "cannot open input file");
173 if (fstat (fd
, &st
) != 0)
176 error (EXIT_FAILURE
, errno
, "cannot get size of input file");
178 /* Test whether the file contains only full records. */
179 if ((st
.st_size
% sizeof (struct entry
)) != 0
180 /* The file must at least contain the two administrative records. */
181 || st
.st_size
< 2 * sizeof (struct entry
))
184 error (EXIT_FAILURE
, 0, "input file has incorrect size");
186 /* Compute number of data entries. */
187 total
= st
.st_size
/ sizeof (struct entry
) - 2;
189 /* Read the administrative information. */
190 read (fd
, headent
, sizeof (headent
));
191 maxsize_heap
= headent
[1].heap
;
192 maxsize_stack
= headent
[1].stack
;
193 maxsize_total
= headent
[0].stack
;
195 if (maxsize_heap
== 0 && maxsize_stack
== 0)
197 /* The program aborted before memusage was able to write the
198 information about the maximum heap and stack use. Repair
204 if (read (fd
, &next
, sizeof (next
)) == 0)
206 if (next
.heap
> maxsize_heap
)
207 maxsize_heap
= next
.heap
;
208 if (next
.stack
> maxsize_stack
)
209 maxsize_stack
= next
.stack
;
210 if (maxsize_heap
+ maxsize_stack
> maxsize_total
)
211 maxsize_total
= maxsize_heap
+ maxsize_stack
;
214 headent
[0].stack
= maxsize_total
;
215 headent
[1].heap
= maxsize_heap
;
216 headent
[1].stack
= maxsize_stack
;
217 headent
[1].time_low
= next
.time_low
;
218 headent
[1].time_high
= next
.time_high
;
220 /* Write the computed values in the file. */
221 lseek (fd
, 0, SEEK_SET
);
222 write (fd
, headent
, 2 * sizeof (struct entry
));
227 /* We use one scale and since we also draw the total amount of
228 memory used we have to adapt the maximum. */
229 maxsize_heap
= maxsize_total
;
230 maxsize_stack
= maxsize_total
;
233 start_time
= ((uint64_t) headent
[0].time_high
) << 32 | headent
[0].time_low
;
234 end_time
= ((uint64_t) headent
[1].time_high
) << 32 | headent
[1].time_low
;
235 total_time
= end_time
- start_time
;
242 /* Create output image with the specified size. */
243 im_out
= gdImageCreate (xsize
, ysize
);
245 /* First color allocated is background. */
246 grey
= gdImageColorAllocate (im_out
, 224, 224, 224);
248 /* Set transparent color. */
249 gdImageColorTransparent (im_out
, grey
);
251 /* These are all the other colors we need (in the moment). */
252 red
= gdImageColorAllocate (im_out
, 255, 0, 0);
253 green
= gdImageColorAllocate (im_out
, 0, 130, 0);
254 blue
= gdImageColorAllocate (im_out
, 0, 0, 255);
255 yellow
= gdImageColorAllocate (im_out
, 154, 205, 50);
256 black
= gdImageColorAllocate (im_out
, 0, 0, 0);
258 gdImageRectangle (im_out
, 40, 20, xsize
- 40, ysize
- 20, blue
);
260 if (maxsize_heap
< 1024)
265 else if (maxsize_heap
< 1024 * 1024 * 100)
267 heap_format
= "%Zuk";
272 heap_format
= "%ZuM";
273 heap_scale
= 1024 * 1024;
276 if (maxsize_stack
< 1024)
278 stack_format
= "%Zu";
281 else if (maxsize_stack
< 1024 * 1024 * 100)
283 stack_format
= "%Zuk";
288 stack_format
= "%ZuM";
289 stack_scale
= 1024 * 1024;
292 gdImageString (im_out
, gdFontSmall
, 38, ysize
- 14, (unsigned char *) "0",
294 snprintf (buf
, sizeof (buf
), heap_format
, 0);
295 gdImageString (im_out
, gdFontSmall
, maxsize_heap
< 1024 ? 32 : 26,
296 ysize
- 26, (unsigned char *) buf
, red
);
297 snprintf (buf
, sizeof (buf
), stack_format
, 0);
298 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26,
299 (unsigned char *) buf
, green
);
302 gdImageString (im_out
, gdFontLarge
, (xsize
- strlen (string
) * 8) / 2,
303 2, (unsigned char *) string
, green
);
305 gdImageStringUp (im_out
, gdFontSmall
, 1, ysize
/ 2 - 10,
306 (unsigned char *) "allocated", red
);
307 gdImageStringUp (im_out
, gdFontSmall
, 11, ysize
/ 2 - 10,
308 (unsigned char *) "memory", red
);
310 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 39, ysize
/ 2 - 10,
311 (unsigned char *) "used", green
);
312 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 27, ysize
/ 2 - 10,
313 (unsigned char *) "stack", green
);
315 snprintf (buf
, sizeof (buf
), heap_format
, maxsize_heap
/ heap_scale
);
316 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6, 14,
317 (unsigned char *) buf
, red
);
318 snprintf (buf
, sizeof (buf
), stack_format
, maxsize_stack
/ stack_scale
);
319 gdImageString (im_out
, gdFontSmall
, xsize
- 37, 14,
320 (unsigned char *) buf
, green
);
322 for (line
= 1; line
<= 3; ++line
)
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
, maxsize_heap
/ 4 * line
/
330 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
331 ysize
- 26 - cnt
, (unsigned char *) buf
, red
);
333 cnt2
= ((ysize
- 40) * (maxsize_stack
/ 4 * line
/ stack_scale
)) /
334 (maxsize_stack
/ stack_scale
);
336 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
337 ysize
- 20 - cnt2
, green
);
338 snprintf (buf
, sizeof (buf
), stack_format
, maxsize_stack
/ 4 * line
/
340 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
341 (unsigned char *) buf
, green
);
344 snprintf (buf
, sizeof (buf
), "%llu", (unsigned long long) total
);
345 gdImageString (im_out
, gdFontSmall
, xsize
- 50, ysize
- 14,
346 (unsigned char *) buf
, blue
);
350 uint64_t previously
= start_time
;
352 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 32 * 6 - 80) / 2,
354 (unsigned char *) "# memory handling function calls",
358 last_stack
= last_heap
= last_total
= ysize
- 20;
359 for (cnt
= 1; cnt
<= total
; ++cnt
)
365 read (fd
, &entry
, sizeof (entry
));
367 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
369 if ((((previously
- start_time
) * 100) / total_time
) % 10 < 5)
370 gdImageFilledRectangle (im_out
,
371 40 + ((cnt
- 1) * (xsize
- 80)) / total
,
373 39 + (cnt
* (xsize
- 80)) / total
,
381 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
382 * (entry
.heap
+ entry
.stack
))
384 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
386 40 + ((xsize
- 80) * cnt
) / total
, new3
,
391 // assert (entry.heap <= maxsize_heap);
392 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
393 * entry
.heap
) / maxsize_heap
);
394 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
395 last_heap
, 40 + ((xsize
- 80) * cnt
) / total
, new[0],
399 // assert (entry.stack <= maxsize_stack);
400 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
401 * entry
.stack
) / maxsize_stack
);
402 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
403 last_stack
, 40 + ((xsize
- 80) * cnt
) / total
, new[1],
411 gdImageLine (im_out
, 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 20,
412 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 15, blue
);
413 cnt
+= MAX (1, total
/ 20);
415 gdImageLine (im_out
, xsize
- 40, ysize
- 20, xsize
- 40, ysize
- 15,
420 uint64_t next_tick
= MAX (1, total
/ 20);
421 size_t last_xpos
= 40;
423 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 39 * 6 - 80) / 2,
426 # memory handling function calls / time", blue
);
428 for (cnt
= 0; cnt
< 20; cnt
+= 2)
429 gdImageFilledRectangle (im_out
,
430 40 + (cnt
* (xsize
- 80)) / 20, ysize
- 19,
431 39 + ((cnt
+ 1) * (xsize
- 80)) / 20,
434 last_stack
= last_heap
= last_total
= ysize
- 20;
435 for (cnt
= 1; cnt
<= total
; ++cnt
)
442 read (fd
, &entry
, sizeof (entry
));
444 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
445 xpos
= 40 + ((xsize
- 80) * (now
- start_time
)) / total_time
;
447 if (cnt
== next_tick
)
449 gdImageLine (im_out
, xpos
, ysize
- 20, xpos
, ysize
- 15, blue
);
450 next_tick
+= MAX (1, total
/ 20);
457 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
458 * (entry
.heap
+ entry
.stack
))
460 gdImageLine (im_out
, last_xpos
, last_total
, xpos
, new3
, black
);
464 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
465 * entry
.heap
) / maxsize_heap
);
466 gdImageLine (im_out
, last_xpos
, last_heap
, xpos
, new[0], red
);
469 // assert (entry.stack <= maxsize_stack);
470 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
471 * entry
.stack
) / maxsize_stack
);
472 gdImageLine (im_out
, last_xpos
, last_stack
, xpos
, new[1], green
);
479 /* Write out the result. */
480 outfile
= fopen (outname
, "w");
482 error (EXIT_FAILURE
, errno
, "cannot open output file");
484 gdImagePng (im_out
, outfile
);
488 gdImageDestroy (im_out
);
494 /* Handle program arguments. */
496 parse_opt (int key
, char *arg
, struct argp_state
*state
)
523 return ARGP_ERR_UNKNOWN
;
530 more_help (int key
, const char *text
, void *input
)
537 case ARGP_KEY_HELP_EXTRA
:
538 /* We print some extra information. */
540 For bug reporting instructions, please see:\n\
541 <http://www.gnu.org/software/libc/bugs.html>.\n");
549 return (char *) text
;
552 /* Print the version information. */
554 print_version (FILE *stream
, struct argp_state
*state
)
556 fprintf (stream
, "memusagestat (GNU %s) %s\n", PACKAGE
, VERSION
);
557 fprintf (stream
, gettext ("\
558 Copyright (C) %s Free Software Foundation, Inc.\n\
559 This is free software; see the source for copying conditions. There is NO\n\
560 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
562 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");