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 cnt
= ((ysize
- 40) * (maxsize_heap
/ 4 * line
/ heap_scale
)) /
323 (maxsize_heap
/ heap_scale
);
324 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt
, xsize
- 40,
325 ysize
- 20 - cnt
, red
);
326 snprintf (buf
, sizeof (buf
), heap_format
, maxsize_heap
/ 4 * line
/
328 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
329 ysize
- 26 - cnt
, (unsigned char *) buf
, red
);
331 cnt2
= ((ysize
- 40) * (maxsize_stack
/ 4 * line
/ stack_scale
)) /
332 (maxsize_stack
/ stack_scale
);
334 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
335 ysize
- 20 - cnt2
, green
);
336 snprintf (buf
, sizeof (buf
), stack_format
, maxsize_stack
/ 4 * line
/
338 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
339 (unsigned char *) buf
, green
);
342 snprintf (buf
, sizeof (buf
), "%llu", (unsigned long long) total
);
343 gdImageString (im_out
, gdFontSmall
, xsize
- 50, ysize
- 14,
344 (unsigned char *) buf
, blue
);
348 uint64_t previously
= start_time
;
350 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 32 * 6 - 80) / 2,
352 (unsigned char *) "# memory handling function calls",
356 last_stack
= last_heap
= last_total
= ysize
- 20;
357 for (cnt
= 1; cnt
<= total
; ++cnt
)
363 read (fd
, &entry
, sizeof (entry
));
365 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
367 if ((((previously
- start_time
) * 100) / total_time
) % 10 < 5)
368 gdImageFilledRectangle (im_out
,
369 40 + ((cnt
- 1) * (xsize
- 80)) / total
,
371 39 + (cnt
* (xsize
- 80)) / total
,
379 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
380 * (entry
.heap
+ entry
.stack
))
382 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
384 40 + ((xsize
- 80) * cnt
) / total
, new3
,
389 // assert (entry.heap <= maxsize_heap);
390 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
391 * entry
.heap
) / maxsize_heap
);
392 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
393 last_heap
, 40 + ((xsize
- 80) * cnt
) / total
, new[0],
397 // assert (entry.stack <= maxsize_stack);
398 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
399 * entry
.stack
) / maxsize_stack
);
400 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
401 last_stack
, 40 + ((xsize
- 80) * cnt
) / total
, new[1],
409 gdImageLine (im_out
, 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 20,
410 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 15, blue
);
411 cnt
+= MAX (1, total
/ 20);
413 gdImageLine (im_out
, xsize
- 40, ysize
- 20, xsize
- 40, ysize
- 15,
418 uint64_t next_tick
= MAX (1, total
/ 20);
419 size_t last_xpos
= 40;
421 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 39 * 6 - 80) / 2,
424 # memory handling function calls / time", blue
);
426 for (cnt
= 0; cnt
< 20; cnt
+= 2)
427 gdImageFilledRectangle (im_out
,
428 40 + (cnt
* (xsize
- 80)) / 20, ysize
- 19,
429 39 + ((cnt
+ 1) * (xsize
- 80)) / 20,
432 last_stack
= last_heap
= last_total
= ysize
- 20;
433 for (cnt
= 1; cnt
<= total
; ++cnt
)
440 read (fd
, &entry
, sizeof (entry
));
442 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
443 xpos
= 40 + ((xsize
- 80) * (now
- start_time
)) / total_time
;
445 if (cnt
== next_tick
)
447 gdImageLine (im_out
, xpos
, ysize
- 20, xpos
, ysize
- 15, blue
);
448 next_tick
+= MAX (1, total
/ 20);
455 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
456 * (entry
.heap
+ entry
.stack
))
458 gdImageLine (im_out
, last_xpos
, last_total
, xpos
, new3
, black
);
462 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
463 * entry
.heap
) / maxsize_heap
);
464 gdImageLine (im_out
, last_xpos
, last_heap
, xpos
, new[0], red
);
467 // assert (entry.stack <= maxsize_stack);
468 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
469 * entry
.stack
) / maxsize_stack
);
470 gdImageLine (im_out
, last_xpos
, last_stack
, xpos
, new[1], green
);
477 /* Write out the result. */
478 outfile
= fopen (outname
, "w");
480 error (EXIT_FAILURE
, errno
, "cannot open output file");
482 gdImagePng (im_out
, outfile
);
486 gdImageDestroy (im_out
);
492 /* Handle program arguments. */
494 parse_opt (int key
, char *arg
, struct argp_state
*state
)
521 return ARGP_ERR_UNKNOWN
;
528 more_help (int key
, const char *text
, void *input
)
534 case ARGP_KEY_HELP_EXTRA
:
535 /* We print some extra information. */
536 if (asprintf (&tp
, gettext ("\
537 For bug reporting instructions, please see:\n\
538 %s.\n"), REPORT_BUGS_TO
) < 0)
544 return (char *) text
;
547 /* Print the version information. */
549 print_version (FILE *stream
, struct argp_state
*state
)
551 fprintf (stream
, "memusagestat %s%s\n", PKGVERSION
, VERSION
);
552 fprintf (stream
, gettext ("\
553 Copyright (C) %s Free Software Foundation, Inc.\n\
554 This is free software; see the source for copying conditions. There is NO\n\
555 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
557 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");