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
34 #include <sys/param.h>
41 #include "../version.h"
42 #define PACKAGE _libc_intl_domainname
44 /* Default size of the generated image. */
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
98 /* Size of the image. */
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
[])
121 int grey
, blue
, red
, green
, yellow
, black
;
125 size_t maxsize_stack
;
126 size_t maxsize_total
;
134 struct entry headent
[2];
138 const char *heap_format
, *stack_format
;
139 int heap_scale
, stack_scale
, line
;
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
);
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
);
171 error (EXIT_FAILURE
, errno
, "cannot open input file");
172 if (fstat (fd
, &st
) != 0)
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
))
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
203 if (read (fd
, &next
, sizeof (next
)) == 0)
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
));
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
;
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)
264 else if (maxsize_heap
< 1024 * 1024 * 100)
266 heap_format
= "%Zuk";
271 heap_format
= "%ZuM";
272 heap_scale
= 1024 * 1024;
275 if (maxsize_stack
< 1024)
277 stack_format
= "%Zu";
280 else if (maxsize_stack
< 1024 * 1024 * 100)
282 stack_format
= "%Zuk";
287 stack_format
= "%ZuM";
288 stack_scale
= 1024 * 1024;
291 gdImageString (im_out
, gdFontSmall
, 38, ysize
- 14, (unsigned char *) "0",
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
);
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
/
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
);
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
/
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
);
349 uint64_t previously
= start_time
;
351 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 32 * 6 - 80) / 2,
353 (unsigned char *) "# memory handling function calls",
357 last_stack
= last_heap
= last_total
= ysize
- 20;
358 for (cnt
= 1; cnt
<= total
; ++cnt
)
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
,
372 39 + (cnt
* (xsize
- 80)) / total
,
380 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
381 * (entry
.heap
+ entry
.stack
))
383 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
385 40 + ((xsize
- 80) * cnt
) / 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],
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],
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,
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,
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,
433 last_stack
= last_heap
= last_total
= ysize
- 20;
434 for (cnt
= 1; cnt
<= total
; ++cnt
)
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);
456 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
457 * (entry
.heap
+ entry
.stack
))
459 gdImageLine (im_out
, last_xpos
, last_total
, xpos
, new3
, black
);
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
);
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
);
478 /* Write out the result. */
479 outfile
= fopen (outname
, "w");
481 error (EXIT_FAILURE
, errno
, "cannot open output file");
483 gdImagePng (im_out
, outfile
);
487 gdImageDestroy (im_out
);
493 /* Handle program arguments. */
495 parse_opt (int key
, char *arg
, struct argp_state
*state
)
522 return ARGP_ERR_UNKNOWN
;
529 more_help (int key
, const char *text
, void *input
)
536 case ARGP_KEY_HELP_EXTRA
:
537 /* We print some extra information. */
539 For bug reporting instructions, please see:\n\
540 <http://www.gnu.org/software/libc/bugs.html>.\n");
548 return (char *) text
;
551 /* Print the version information. */
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\
561 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");