1 /* Generate graphic from memory profiling data.
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #define _FILE_OFFSET_BITS 64
35 #include <sys/param.h>
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 pixel wide") },
61 { "y-size", 'y', "VALUE", 0, N_("make output graphic VALUE pixel 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 /* Data structure to communicate with argp functions. */
78 static struct argp argp
=
80 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
93 /* Size of the image. */
97 /* Name of the output file. */
100 /* Title string for the graphic. */
101 static const char *string
;
103 /* Nonzero if graph should be generated linear in time. */
104 static int time_based
;
106 /* Nonzero if graph to display total use of memory should be drawn as well. */
107 static int also_total
= 0;
111 main (int argc
, char *argv
[])
116 int grey
, blue
, red
, green
, yellow
, black
;
120 size_t maxsize_stack
;
121 size_t maxsize_total
;
129 struct entry headent
[2];
133 const char *heap_format
, *stack_format
;
134 int heap_scale
, stack_scale
, line
;
141 /* Parse and process arguments. */
142 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
144 if (remaining
>= argc
|| remaining
+ 2 < argc
)
146 argp_help (&argp
, stdout
, ARGP_HELP_SEE
| ARGP_HELP_EXIT_ERR
,
147 program_invocation_short_name
);
151 inname
= argv
[remaining
++];
153 if (remaining
< argc
)
154 outname
= argv
[remaining
];
155 else if (outname
== NULL
)
157 size_t len
= strlen (inname
);
158 outname
= alloca (len
+ 5);
159 stpcpy (stpcpy (outname
, inname
), ".png");
162 /* Open for read/write since we try to repair the file in case the
163 application hasn't terminated cleanly. */
164 fd
= open (inname
, O_RDWR
);
166 error (EXIT_FAILURE
, errno
, "cannot open input file");
167 if (fstat (fd
, &st
) != 0)
170 error (EXIT_FAILURE
, errno
, "cannot get size of input file");
172 /* Test whether the file contains only full records. */
173 if ((st
.st_size
% sizeof (struct entry
)) != 0
174 /* The file must at least contain the two administrative records. */
175 || st
.st_size
< 2 * sizeof (struct entry
))
178 error (EXIT_FAILURE
, 0, "input file as incorrect size");
180 /* Compute number of data entries. */
181 total
= st
.st_size
/ sizeof (struct entry
) - 2;
183 /* Read the administrative information. */
184 read (fd
, headent
, sizeof (headent
));
185 maxsize_heap
= headent
[1].heap
;
186 maxsize_stack
= headent
[1].stack
;
187 maxsize_total
= headent
[0].stack
;
190 /* We use one scale and since we also draw the total amount of
191 memory used we have to adapt the maximum. */
192 maxsize_heap
= maxsize_total
;
193 maxsize_stack
= maxsize_total
;
196 if (maxsize_heap
== 0 && maxsize_stack
== 0)
198 /* The program aborted before memusage was able to write the
199 information about the maximum heap and stack use. Repair
205 if (read (fd
, &next
, sizeof (next
)) == 0)
207 if (next
.heap
> headent
[1].heap
)
208 headent
[1].heap
= next
.heap
;
209 if (next
.stack
> headent
[1].stack
)
210 headent
[1].stack
= next
.stack
;
213 headent
[1].time_low
= next
.time_low
;
214 headent
[1].time_high
= next
.time_high
;
216 /* Write the computed values in the file. */
217 lseek (fd
, sizeof (struct entry
), SEEK_SET
);
218 write (fd
, &headent
[1], sizeof (struct entry
));
221 start_time
= ((uint64_t) headent
[0].time_high
) << 32 | headent
[0].time_low
;
222 end_time
= ((uint64_t) headent
[1].time_high
) << 32 | headent
[1].time_low
;
223 total_time
= end_time
- start_time
;
230 /* Create output image with the specified size. */
231 im_out
= gdImageCreate (xsize
, ysize
);
233 /* First color allocated is background. */
234 grey
= gdImageColorAllocate (im_out
, 224, 224, 224);
236 /* Set transparent color. */
237 gdImageColorTransparent (im_out
, grey
);
239 /* These are all the other colors we need (in the moment). */
240 red
= gdImageColorAllocate (im_out
, 255, 0, 0);
241 green
= gdImageColorAllocate (im_out
, 0, 130, 0);
242 blue
= gdImageColorAllocate (im_out
, 0, 0, 255);
243 yellow
= gdImageColorAllocate (im_out
, 154, 205, 50);
244 black
= gdImageColorAllocate (im_out
, 0, 0, 0);
246 gdImageRectangle (im_out
, 40, 20, xsize
- 40, ysize
- 20, blue
);
248 if (maxsize_heap
< 1024)
253 else if (maxsize_heap
< 1024 * 1024 * 100)
255 heap_format
= "%Zuk";
260 heap_format
= "%ZuM";
261 heap_scale
= 1024 * 1024;
264 if (maxsize_stack
< 1024)
266 stack_format
= "%Zu";
269 else if (maxsize_stack
< 1024 * 1024 * 100)
271 stack_format
= "%Zuk";
276 stack_format
= "%ZuM";
277 stack_scale
= 1024 * 1024;
280 gdImageString (im_out
, gdFontSmall
, 38, ysize
- 14, (unsigned char *) "0",
282 snprintf(buf
, sizeof (buf
), heap_format
, 0);
283 gdImageString (im_out
, gdFontSmall
, maxsize_heap
< 1024 ? 32 : 26,
284 ysize
- 26, buf
, red
);
285 snprintf(buf
, sizeof (buf
), stack_format
, 0);
286 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26,
290 gdImageString (im_out
, gdFontLarge
, (xsize
- strlen (string
) * 8) / 2,
291 2, (char *) string
, green
);
293 gdImageStringUp (im_out
, gdFontSmall
, 1, ysize
/ 2 - 10,
294 (unsigned char *) "allocated", red
);
295 gdImageStringUp (im_out
, gdFontSmall
, 11, ysize
/ 2 - 10,
296 (unsigned char *) "memory", red
);
298 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 39, ysize
/ 2 - 10,
299 (unsigned char *) "used", green
);
300 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 27, ysize
/ 2 - 10,
301 (unsigned char *) "stack", green
);
303 snprintf (buf
, sizeof (buf
), heap_format
, maxsize_heap
/ heap_scale
);
304 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6, 14, buf
, red
);
305 snprintf (buf
, sizeof (buf
), stack_format
, maxsize_stack
/ stack_scale
);
306 gdImageString (im_out
, gdFontSmall
, xsize
- 37, 14, 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
, 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
,
330 snprintf (buf
, sizeof (buf
), "%llu", (unsigned long long) total
);
331 gdImageString (im_out
, gdFontSmall
, xsize
- 50, ysize
- 14, buf
, blue
);
335 uint64_t previously
= start_time
;
337 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 32 * 6 - 80) / 2,
339 (unsigned char *) "# memory handling function calls",
343 last_stack
= last_heap
= last_total
= ysize
- 20;
344 for (cnt
= 1; cnt
<= total
; ++cnt
)
350 read (fd
, &entry
, sizeof (entry
));
352 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
354 if ((((previously
- start_time
) * 100) / total_time
) % 10 < 5)
355 gdImageFilledRectangle (im_out
,
356 40 + ((cnt
- 1) * (xsize
- 80)) / total
,
358 39 + (cnt
* (xsize
- 80)) / total
,
366 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
367 * (entry
.heap
+ entry
.stack
))
369 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
371 40 + ((xsize
- 80) * cnt
) / total
, new3
,
376 // assert (entry.heap <= maxsize_heap);
377 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
378 * entry
.heap
) / maxsize_heap
);
379 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
380 last_heap
, 40 + ((xsize
- 80) * cnt
) / total
, new[0],
384 // assert (entry.stack <= maxsize_stack);
385 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
386 * entry
.stack
) / maxsize_stack
);
387 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
388 last_stack
, 40 + ((xsize
- 80) * cnt
) / total
, new[1],
396 gdImageLine (im_out
, 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 20,
397 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 15, blue
);
398 cnt
+= MAX (1, total
/ 20);
400 gdImageLine (im_out
, xsize
- 40, ysize
- 20, xsize
- 40, ysize
- 15,
405 uint64_t next_tick
= MAX (1, total
/ 20);
406 size_t last_xpos
= 40;
408 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 39 * 6 - 80) / 2,
411 # memory handling function calls / time", blue
);
413 for (cnt
= 0; cnt
< 20; cnt
+= 2)
414 gdImageFilledRectangle (im_out
,
415 40 + (cnt
* (xsize
- 80)) / 20, ysize
- 19,
416 39 + ((cnt
+ 1) * (xsize
- 80)) / 20,
419 last_stack
= last_heap
= last_total
= ysize
- 20;
420 for (cnt
= 1; cnt
<= total
; ++cnt
)
427 read (fd
, &entry
, sizeof (entry
));
429 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
430 xpos
= 40 + ((xsize
- 80) * (now
- start_time
)) / total_time
;
432 if (cnt
== next_tick
)
434 gdImageLine (im_out
, xpos
, ysize
- 20, xpos
, ysize
- 15, blue
);
435 next_tick
+= MAX (1, total
/ 20);
442 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
443 * (entry
.heap
+ entry
.stack
))
445 gdImageLine (im_out
, last_xpos
, last_total
, xpos
, new3
, black
);
449 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
450 * entry
.heap
) / maxsize_heap
);
451 gdImageLine (im_out
, last_xpos
, last_heap
, xpos
, new[0], red
);
454 // assert (entry.stack <= maxsize_stack);
455 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
456 * entry
.stack
) / maxsize_stack
);
457 gdImageLine (im_out
, last_xpos
, last_stack
, xpos
, new[1], green
);
464 /* Write out the result. */
465 outfile
= fopen (outname
, "w");
467 error (EXIT_FAILURE
, errno
, "cannot open output file");
469 gdImagePng (im_out
, outfile
);
473 gdImageDestroy (im_out
);
479 /* Handle program arguments. */
481 parse_opt (int key
, char *arg
, struct argp_state
*state
)
508 return ARGP_ERR_UNKNOWN
;
515 more_help (int key
, const char *text
, void *input
)
522 case ARGP_KEY_HELP_EXTRA
:
523 /* We print some extra information. */
525 For bug reporting instructions, please see:\n\
526 <http://www.gnu.org/software/libc/bugs.html>.\n");
534 return (char *) text
;