1 /* Generate graphic from memory profiling data.
2 Copyright (C) 1998, 1999, 2000, 2005, 2006 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 version 2 as
8 published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #define _FILE_OFFSET_BITS 64
33 #include <sys/param.h>
41 /* Default size of the generated image. */
50 /* Definitions of arguments for argp functions. */
51 static const struct argp_option options
[] =
53 { "output", 'o', "FILE", 0, N_("Name output file") },
54 { "string", 's', "STRING", 0, N_("Title string used in output graphic") },
55 { "time", 't', NULL
, 0, N_("Generate output linear to time (default is linear to number of function calls)") },
56 { "total", 'T', NULL
, 0,
57 N_("Also draw graph for total memory consumption") },
58 { "x-size", 'x', "VALUE", 0, N_("make output graphic VALUE pixel wide") },
59 { "y-size", 'y', "VALUE", 0, N_("make output graphic VALUE pixel high") },
60 { NULL
, 0, NULL
, 0, NULL
}
63 /* Short description of program. */
64 static const char doc
[] = N_("Generate graphic from memory profiling data");
66 /* Strings for arguments in help texts. */
67 static const char args_doc
[] = N_("DATAFILE [OUTFILE]");
69 /* Prototype for option handler. */
70 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
72 /* Function to print some extra text in the help message. */
73 static char *more_help (int key
, const char *text
, void *input
);
75 /* Data structure to communicate with argp functions. */
76 static struct argp argp
=
78 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
91 /* Size of the image. */
95 /* Name of the output file. */
98 /* Title string for the graphic. */
99 static const char *string
;
101 /* Nonzero if graph should be generated linear in time. */
102 static int time_based
;
104 /* Nonzero if graph to display total use of memory should be drawn as well. */
105 static int also_total
= 0;
109 main (int argc
, char *argv
[])
114 int grey
, blue
, red
, green
, yellow
, black
;
118 size_t maxsize_stack
;
119 size_t maxsize_total
;
127 struct entry headent
[2];
131 const char *heap_format
, *stack_format
;
132 int heap_scale
, stack_scale
, line
;
139 /* Parse and process arguments. */
140 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
142 if (remaining
>= argc
|| remaining
+ 2 < argc
)
144 argp_help (&argp
, stdout
, ARGP_HELP_SEE
| ARGP_HELP_EXIT_ERR
,
145 program_invocation_short_name
);
149 inname
= argv
[remaining
++];
151 if (remaining
< argc
)
152 outname
= argv
[remaining
];
153 else if (outname
== NULL
)
155 size_t len
= strlen (inname
);
156 outname
= alloca (len
+ 5);
157 stpcpy (stpcpy (outname
, inname
), ".png");
160 /* Open for read/write since we try to repair the file in case the
161 application hasn't terminated cleanly. */
162 fd
= open (inname
, O_RDWR
);
164 error (EXIT_FAILURE
, errno
, "cannot open input file");
165 if (fstat (fd
, &st
) != 0)
168 error (EXIT_FAILURE
, errno
, "cannot get size of input file");
170 /* Test whether the file contains only full records. */
171 if ((st
.st_size
% sizeof (struct entry
)) != 0
172 /* The file must at least contain the two administrative records. */
173 || st
.st_size
< 2 * sizeof (struct entry
))
176 error (EXIT_FAILURE
, 0, "input file as incorrect size");
178 /* Compute number of data entries. */
179 total
= st
.st_size
/ sizeof (struct entry
) - 2;
181 /* Read the administrative information. */
182 read (fd
, headent
, sizeof (headent
));
183 maxsize_heap
= headent
[1].heap
;
184 maxsize_stack
= headent
[1].stack
;
185 maxsize_total
= headent
[0].stack
;
188 /* We use one scale and since we also draw the total amount of
189 memory used we have to adapt the maximum. */
190 maxsize_heap
= maxsize_total
;
191 maxsize_stack
= maxsize_total
;
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
> headent
[1].heap
)
206 headent
[1].heap
= next
.heap
;
207 if (next
.stack
> headent
[1].stack
)
208 headent
[1].stack
= next
.stack
;
211 headent
[1].time_low
= next
.time_low
;
212 headent
[1].time_high
= next
.time_high
;
214 /* Write the computed values in the file. */
215 lseek (fd
, sizeof (struct entry
), SEEK_SET
);
216 write (fd
, &headent
[1], sizeof (struct entry
));
219 start_time
= ((uint64_t) headent
[0].time_high
) << 32 | headent
[0].time_low
;
220 end_time
= ((uint64_t) headent
[1].time_high
) << 32 | headent
[1].time_low
;
221 total_time
= end_time
- start_time
;
228 /* Create output image with the specified size. */
229 im_out
= gdImageCreate (xsize
, ysize
);
231 /* First color allocated is background. */
232 grey
= gdImageColorAllocate (im_out
, 224, 224, 224);
234 /* Set transparent color. */
235 gdImageColorTransparent (im_out
, grey
);
237 /* These are all the other colors we need (in the moment). */
238 red
= gdImageColorAllocate (im_out
, 255, 0, 0);
239 green
= gdImageColorAllocate (im_out
, 0, 130, 0);
240 blue
= gdImageColorAllocate (im_out
, 0, 0, 255);
241 yellow
= gdImageColorAllocate (im_out
, 154, 205, 50);
242 black
= gdImageColorAllocate (im_out
, 0, 0, 0);
244 gdImageRectangle (im_out
, 40, 20, xsize
- 40, ysize
- 20, blue
);
246 if (maxsize_heap
< 1024)
251 else if (maxsize_heap
< 1024 * 1024 * 100)
253 heap_format
= "%Zuk";
258 heap_format
= "%ZuM";
259 heap_scale
= 1024 * 1024;
262 if (maxsize_stack
< 1024)
264 stack_format
= "%Zu";
267 else if (maxsize_stack
< 1024 * 1024 * 100)
269 stack_format
= "%Zuk";
274 stack_format
= "%ZuM";
275 stack_scale
= 1024 * 1024;
278 gdImageString (im_out
, gdFontSmall
, 38, ysize
- 14, (unsigned char *) "0",
280 snprintf (buf
, sizeof (buf
), heap_format
, 0);
281 gdImageString (im_out
, gdFontSmall
, maxsize_heap
< 1024 ? 32 : 26,
282 ysize
- 26, (unsigned char *) buf
, red
);
283 snprintf (buf
, sizeof (buf
), stack_format
, 0);
284 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26,
285 (unsigned char *) buf
, green
);
288 gdImageString (im_out
, gdFontLarge
, (xsize
- strlen (string
) * 8) / 2,
289 2, (unsigned char *) string
, green
);
291 gdImageStringUp (im_out
, gdFontSmall
, 1, ysize
/ 2 - 10,
292 (unsigned char *) "allocated", red
);
293 gdImageStringUp (im_out
, gdFontSmall
, 11, ysize
/ 2 - 10,
294 (unsigned char *) "memory", red
);
296 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 39, ysize
/ 2 - 10,
297 (unsigned char *) "used", green
);
298 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 27, ysize
/ 2 - 10,
299 (unsigned char *) "stack", green
);
301 snprintf (buf
, sizeof (buf
), heap_format
, maxsize_heap
/ heap_scale
);
302 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6, 14,
303 (unsigned char *) buf
, red
);
304 snprintf (buf
, sizeof (buf
), stack_format
, maxsize_stack
/ stack_scale
);
305 gdImageString (im_out
, gdFontSmall
, xsize
- 37, 14,
306 (unsigned char *) buf
, green
);
308 for (line
= 1; line
<= 3; ++line
)
310 cnt
= ((ysize
- 40) * (maxsize_heap
/ 4 * line
/ heap_scale
)) /
311 (maxsize_heap
/ heap_scale
);
312 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt
, xsize
- 40,
313 ysize
- 20 - cnt
, red
);
314 snprintf (buf
, sizeof (buf
), heap_format
, maxsize_heap
/ 4 * line
/
316 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
317 ysize
- 26 - cnt
, (unsigned char *) buf
, red
);
319 cnt2
= ((ysize
- 40) * (maxsize_stack
/ 4 * line
/ stack_scale
)) /
320 (maxsize_stack
/ stack_scale
);
322 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
323 ysize
- 20 - cnt2
, green
);
324 snprintf (buf
, sizeof (buf
), stack_format
, maxsize_stack
/ 4 * line
/
326 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
327 (unsigned char *) buf
, green
);
330 snprintf (buf
, sizeof (buf
), "%llu", (unsigned long long) total
);
331 gdImageString (im_out
, gdFontSmall
, xsize
- 50, ysize
- 14,
332 (unsigned char *) buf
, blue
);
336 uint64_t previously
= start_time
;
338 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 32 * 6 - 80) / 2,
340 (unsigned char *) "# memory handling function calls",
344 last_stack
= last_heap
= last_total
= ysize
- 20;
345 for (cnt
= 1; cnt
<= total
; ++cnt
)
351 read (fd
, &entry
, sizeof (entry
));
353 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
355 if ((((previously
- start_time
) * 100) / total_time
) % 10 < 5)
356 gdImageFilledRectangle (im_out
,
357 40 + ((cnt
- 1) * (xsize
- 80)) / total
,
359 39 + (cnt
* (xsize
- 80)) / total
,
367 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
368 * (entry
.heap
+ entry
.stack
))
370 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
372 40 + ((xsize
- 80) * cnt
) / total
, new3
,
377 // assert (entry.heap <= maxsize_heap);
378 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
379 * entry
.heap
) / maxsize_heap
);
380 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
381 last_heap
, 40 + ((xsize
- 80) * cnt
) / total
, new[0],
385 // assert (entry.stack <= maxsize_stack);
386 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
387 * entry
.stack
) / maxsize_stack
);
388 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
389 last_stack
, 40 + ((xsize
- 80) * cnt
) / total
, new[1],
397 gdImageLine (im_out
, 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 20,
398 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 15, blue
);
399 cnt
+= MAX (1, total
/ 20);
401 gdImageLine (im_out
, xsize
- 40, ysize
- 20, xsize
- 40, ysize
- 15,
406 uint64_t next_tick
= MAX (1, total
/ 20);
407 size_t last_xpos
= 40;
409 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 39 * 6 - 80) / 2,
412 # memory handling function calls / time", blue
);
414 for (cnt
= 0; cnt
< 20; cnt
+= 2)
415 gdImageFilledRectangle (im_out
,
416 40 + (cnt
* (xsize
- 80)) / 20, ysize
- 19,
417 39 + ((cnt
+ 1) * (xsize
- 80)) / 20,
420 last_stack
= last_heap
= last_total
= ysize
- 20;
421 for (cnt
= 1; cnt
<= total
; ++cnt
)
428 read (fd
, &entry
, sizeof (entry
));
430 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
431 xpos
= 40 + ((xsize
- 80) * (now
- start_time
)) / total_time
;
433 if (cnt
== next_tick
)
435 gdImageLine (im_out
, xpos
, ysize
- 20, xpos
, ysize
- 15, blue
);
436 next_tick
+= MAX (1, total
/ 20);
443 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
444 * (entry
.heap
+ entry
.stack
))
446 gdImageLine (im_out
, last_xpos
, last_total
, xpos
, new3
, black
);
450 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
451 * entry
.heap
) / maxsize_heap
);
452 gdImageLine (im_out
, last_xpos
, last_heap
, xpos
, new[0], red
);
455 // assert (entry.stack <= maxsize_stack);
456 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
457 * entry
.stack
) / maxsize_stack
);
458 gdImageLine (im_out
, last_xpos
, last_stack
, xpos
, new[1], green
);
465 /* Write out the result. */
466 outfile
= fopen (outname
, "w");
468 error (EXIT_FAILURE
, errno
, "cannot open output file");
470 gdImagePng (im_out
, outfile
);
474 gdImageDestroy (im_out
);
480 /* Handle program arguments. */
482 parse_opt (int key
, char *arg
, struct argp_state
*state
)
509 return ARGP_ERR_UNKNOWN
;
516 more_help (int key
, const char *text
, void *input
)
523 case ARGP_KEY_HELP_EXTRA
:
524 /* We print some extra information. */
526 For bug reporting instructions, please see:\n\
527 <http://www.gnu.org/software/libc/bugs.html>.\n");
535 return (char *) text
;