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 as 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
;
196 /* We use one scale and since we also draw the total amount of
197 memory used we have to adapt the maximum. */
198 maxsize_heap
= maxsize_total
;
199 maxsize_stack
= maxsize_total
;
202 if (maxsize_heap
== 0 && maxsize_stack
== 0)
204 /* The program aborted before memusage was able to write the
205 information about the maximum heap and stack use. Repair
211 if (read (fd
, &next
, sizeof (next
)) == 0)
213 if (next
.heap
> headent
[1].heap
)
214 headent
[1].heap
= next
.heap
;
215 if (next
.stack
> headent
[1].stack
)
216 headent
[1].stack
= next
.stack
;
219 headent
[1].time_low
= next
.time_low
;
220 headent
[1].time_high
= next
.time_high
;
222 /* Write the computed values in the file. */
223 lseek (fd
, sizeof (struct entry
), SEEK_SET
);
224 write (fd
, &headent
[1], sizeof (struct entry
));
227 start_time
= ((uint64_t) headent
[0].time_high
) << 32 | headent
[0].time_low
;
228 end_time
= ((uint64_t) headent
[1].time_high
) << 32 | headent
[1].time_low
;
229 total_time
= end_time
- start_time
;
236 /* Create output image with the specified size. */
237 im_out
= gdImageCreate (xsize
, ysize
);
239 /* First color allocated is background. */
240 grey
= gdImageColorAllocate (im_out
, 224, 224, 224);
242 /* Set transparent color. */
243 gdImageColorTransparent (im_out
, grey
);
245 /* These are all the other colors we need (in the moment). */
246 red
= gdImageColorAllocate (im_out
, 255, 0, 0);
247 green
= gdImageColorAllocate (im_out
, 0, 130, 0);
248 blue
= gdImageColorAllocate (im_out
, 0, 0, 255);
249 yellow
= gdImageColorAllocate (im_out
, 154, 205, 50);
250 black
= gdImageColorAllocate (im_out
, 0, 0, 0);
252 gdImageRectangle (im_out
, 40, 20, xsize
- 40, ysize
- 20, blue
);
254 if (maxsize_heap
< 1024)
259 else if (maxsize_heap
< 1024 * 1024 * 100)
261 heap_format
= "%Zuk";
266 heap_format
= "%ZuM";
267 heap_scale
= 1024 * 1024;
270 if (maxsize_stack
< 1024)
272 stack_format
= "%Zu";
275 else if (maxsize_stack
< 1024 * 1024 * 100)
277 stack_format
= "%Zuk";
282 stack_format
= "%ZuM";
283 stack_scale
= 1024 * 1024;
286 gdImageString (im_out
, gdFontSmall
, 38, ysize
- 14, (unsigned char *) "0",
288 snprintf (buf
, sizeof (buf
), heap_format
, 0);
289 gdImageString (im_out
, gdFontSmall
, maxsize_heap
< 1024 ? 32 : 26,
290 ysize
- 26, (unsigned char *) buf
, red
);
291 snprintf (buf
, sizeof (buf
), stack_format
, 0);
292 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26,
293 (unsigned char *) buf
, green
);
296 gdImageString (im_out
, gdFontLarge
, (xsize
- strlen (string
) * 8) / 2,
297 2, (unsigned char *) string
, green
);
299 gdImageStringUp (im_out
, gdFontSmall
, 1, ysize
/ 2 - 10,
300 (unsigned char *) "allocated", red
);
301 gdImageStringUp (im_out
, gdFontSmall
, 11, ysize
/ 2 - 10,
302 (unsigned char *) "memory", red
);
304 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 39, ysize
/ 2 - 10,
305 (unsigned char *) "used", green
);
306 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 27, ysize
/ 2 - 10,
307 (unsigned char *) "stack", green
);
309 snprintf (buf
, sizeof (buf
), heap_format
, maxsize_heap
/ heap_scale
);
310 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6, 14,
311 (unsigned char *) buf
, red
);
312 snprintf (buf
, sizeof (buf
), stack_format
, maxsize_stack
/ stack_scale
);
313 gdImageString (im_out
, gdFontSmall
, xsize
- 37, 14,
314 (unsigned char *) buf
, green
);
316 for (line
= 1; line
<= 3; ++line
)
318 cnt
= ((ysize
- 40) * (maxsize_heap
/ 4 * line
/ heap_scale
)) /
319 (maxsize_heap
/ heap_scale
);
320 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt
, xsize
- 40,
321 ysize
- 20 - cnt
, red
);
322 snprintf (buf
, sizeof (buf
), heap_format
, maxsize_heap
/ 4 * line
/
324 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
325 ysize
- 26 - cnt
, (unsigned char *) buf
, red
);
327 cnt2
= ((ysize
- 40) * (maxsize_stack
/ 4 * line
/ stack_scale
)) /
328 (maxsize_stack
/ stack_scale
);
330 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
331 ysize
- 20 - cnt2
, green
);
332 snprintf (buf
, sizeof (buf
), stack_format
, maxsize_stack
/ 4 * line
/
334 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
335 (unsigned char *) buf
, green
);
338 snprintf (buf
, sizeof (buf
), "%llu", (unsigned long long) total
);
339 gdImageString (im_out
, gdFontSmall
, xsize
- 50, ysize
- 14,
340 (unsigned char *) buf
, blue
);
344 uint64_t previously
= start_time
;
346 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 32 * 6 - 80) / 2,
348 (unsigned char *) "# memory handling function calls",
352 last_stack
= last_heap
= last_total
= ysize
- 20;
353 for (cnt
= 1; cnt
<= total
; ++cnt
)
359 read (fd
, &entry
, sizeof (entry
));
361 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
363 if ((((previously
- start_time
) * 100) / total_time
) % 10 < 5)
364 gdImageFilledRectangle (im_out
,
365 40 + ((cnt
- 1) * (xsize
- 80)) / total
,
367 39 + (cnt
* (xsize
- 80)) / total
,
375 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
376 * (entry
.heap
+ entry
.stack
))
378 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
380 40 + ((xsize
- 80) * cnt
) / total
, new3
,
385 // assert (entry.heap <= maxsize_heap);
386 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
387 * entry
.heap
) / maxsize_heap
);
388 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
389 last_heap
, 40 + ((xsize
- 80) * cnt
) / total
, new[0],
393 // assert (entry.stack <= maxsize_stack);
394 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
395 * entry
.stack
) / maxsize_stack
);
396 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
397 last_stack
, 40 + ((xsize
- 80) * cnt
) / total
, new[1],
405 gdImageLine (im_out
, 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 20,
406 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 15, blue
);
407 cnt
+= MAX (1, total
/ 20);
409 gdImageLine (im_out
, xsize
- 40, ysize
- 20, xsize
- 40, ysize
- 15,
414 uint64_t next_tick
= MAX (1, total
/ 20);
415 size_t last_xpos
= 40;
417 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 39 * 6 - 80) / 2,
420 # memory handling function calls / time", blue
);
422 for (cnt
= 0; cnt
< 20; cnt
+= 2)
423 gdImageFilledRectangle (im_out
,
424 40 + (cnt
* (xsize
- 80)) / 20, ysize
- 19,
425 39 + ((cnt
+ 1) * (xsize
- 80)) / 20,
428 last_stack
= last_heap
= last_total
= ysize
- 20;
429 for (cnt
= 1; cnt
<= total
; ++cnt
)
436 read (fd
, &entry
, sizeof (entry
));
438 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
439 xpos
= 40 + ((xsize
- 80) * (now
- start_time
)) / total_time
;
441 if (cnt
== next_tick
)
443 gdImageLine (im_out
, xpos
, ysize
- 20, xpos
, ysize
- 15, blue
);
444 next_tick
+= MAX (1, total
/ 20);
451 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
452 * (entry
.heap
+ entry
.stack
))
454 gdImageLine (im_out
, last_xpos
, last_total
, xpos
, new3
, black
);
458 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
459 * entry
.heap
) / maxsize_heap
);
460 gdImageLine (im_out
, last_xpos
, last_heap
, xpos
, new[0], red
);
463 // assert (entry.stack <= maxsize_stack);
464 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
465 * entry
.stack
) / maxsize_stack
);
466 gdImageLine (im_out
, last_xpos
, last_stack
, xpos
, new[1], green
);
473 /* Write out the result. */
474 outfile
= fopen (outname
, "w");
476 error (EXIT_FAILURE
, errno
, "cannot open output file");
478 gdImagePng (im_out
, outfile
);
482 gdImageDestroy (im_out
);
488 /* Handle program arguments. */
490 parse_opt (int key
, char *arg
, struct argp_state
*state
)
517 return ARGP_ERR_UNKNOWN
;
524 more_help (int key
, const char *text
, void *input
)
531 case ARGP_KEY_HELP_EXTRA
:
532 /* We print some extra information. */
534 For bug reporting instructions, please see:\n\
535 <http://www.gnu.org/software/libc/bugs.html>.\n");
543 return (char *) text
;
546 /* Print the version information. */
548 print_version (FILE *stream
, struct argp_state
*state
)
550 fprintf (stream
, "memusagestat (GNU %s) %s\n", PACKAGE
, VERSION
);
551 fprintf (stream
, gettext ("\
552 Copyright (C) %s Free Software Foundation, Inc.\n\
553 This is free software; see the source for copying conditions. There is NO\n\
554 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
556 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");