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
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];
137 /* Parse and process arguments. */
138 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
140 if (remaining
>= argc
|| remaining
+ 2 < argc
)
142 argp_help (&argp
, stdout
, ARGP_HELP_SEE
| ARGP_HELP_EXIT_ERR
,
143 program_invocation_short_name
);
147 inname
= argv
[remaining
++];
149 if (remaining
< argc
)
150 outname
= argv
[remaining
];
151 else if (outname
== NULL
)
153 size_t len
= strlen (inname
);
154 outname
= alloca (len
+ 5);
155 stpcpy (stpcpy (outname
, inname
), ".png");
158 /* Open for read/write since we try to repair the file in case the
159 application hasn't terminated cleanly. */
160 fd
= open (inname
, O_RDWR
);
162 error (EXIT_FAILURE
, errno
, "cannot open input file");
163 if (fstat (fd
, &st
) != 0)
166 error (EXIT_FAILURE
, errno
, "cannot get size of input file");
168 /* Test whether the file contains only full records. */
169 if ((st
.st_size
% sizeof (struct entry
)) != 0
170 /* The file must at least contain the two administrative records. */
171 || st
.st_size
< 2 * sizeof (struct entry
))
174 error (EXIT_FAILURE
, 0, "input file as incorrect size");
176 /* Compute number of data entries. */
177 total
= st
.st_size
/ sizeof (struct entry
) - 2;
179 /* Read the administrative information. */
180 read (fd
, headent
, sizeof (headent
));
181 maxsize_heap
= headent
[1].heap
;
182 maxsize_stack
= headent
[1].stack
;
183 maxsize_total
= headent
[0].stack
;
186 /* We use one scale and since we also draw the total amount of
187 memory used we have to adapt the maximum. */
188 maxsize_heap
= maxsize_total
;
189 maxsize_stack
= maxsize_total
;
192 if (maxsize_heap
== 0 && maxsize_stack
== 0)
194 /* The program aborted before memusage was able to write the
195 information about the maximum heap and stack use. Repair
201 if (read (fd
, &next
, sizeof (next
)) == 0)
203 if (next
.heap
> headent
[1].heap
)
204 headent
[1].heap
= next
.heap
;
205 if (next
.stack
> headent
[1].stack
)
206 headent
[1].stack
= next
.stack
;
209 headent
[1].time_low
= next
.time_low
;
210 headent
[1].time_high
= next
.time_high
;
212 /* Write the computed values in the file. */
213 lseek (fd
, sizeof (struct entry
), SEEK_SET
);
214 write (fd
, &headent
[1], sizeof (struct entry
));
217 start_time
= ((uint64_t) headent
[0].time_high
) << 32 | headent
[0].time_low
;
218 end_time
= ((uint64_t) headent
[1].time_high
) << 32 | headent
[1].time_low
;
219 total_time
= end_time
- start_time
;
226 /* Create output image with the specified size. */
227 im_out
= gdImageCreate (xsize
, ysize
);
229 /* First color allocated is background. */
230 grey
= gdImageColorAllocate (im_out
, 224, 224, 224);
232 /* Set transparent color. */
233 gdImageColorTransparent (im_out
, grey
);
235 /* These are all the other colors we need (in the moment). */
236 red
= gdImageColorAllocate (im_out
, 255, 0, 0);
237 green
= gdImageColorAllocate (im_out
, 0, 130, 0);
238 blue
= gdImageColorAllocate (im_out
, 0, 0, 255);
239 yellow
= gdImageColorAllocate (im_out
, 154, 205, 50);
240 black
= gdImageColorAllocate (im_out
, 0, 0, 0);
242 gdImageRectangle (im_out
, 40, 20, xsize
- 40, ysize
- 20, blue
);
244 gdImageString (im_out
, gdFontSmall
, 38, ysize
- 14, (unsigned char *) "0",
246 gdImageString (im_out
, gdFontSmall
, maxsize_heap
< 1024 ? 32 : 26,
248 (unsigned char *) (maxsize_heap
< 1024 ? "0" : "0k"), red
);
249 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26,
250 (unsigned char *) (maxsize_stack
< 1024 ? "0" : "0k"), green
);
253 gdImageString (im_out
, gdFontLarge
, (xsize
- strlen (string
) * 8) / 2,
254 2, (char *) string
, green
);
256 gdImageStringUp (im_out
, gdFontSmall
, 1, ysize
/ 2 - 10,
257 (unsigned char *) "allocated", red
);
258 gdImageStringUp (im_out
, gdFontSmall
, 11, ysize
/ 2 - 10,
259 (unsigned char *) "memory", red
);
261 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 39, ysize
/ 2 - 10,
262 (unsigned char *) "used", green
);
263 gdImageStringUp (im_out
, gdFontSmall
, xsize
- 27, ysize
/ 2 - 10,
264 (unsigned char *) "stack", green
);
266 if (maxsize_heap
< 1024)
268 snprintf (buf
, sizeof (buf
), "%Zu", maxsize_heap
);
269 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6, 14, buf
, red
);
273 snprintf (buf
, sizeof (buf
), "%Zuk", maxsize_heap
/ 1024);
274 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6, 14, buf
, red
);
276 if (maxsize_stack
< 1024)
278 snprintf (buf
, sizeof (buf
), "%Zu", maxsize_stack
);
279 gdImageString (im_out
, gdFontSmall
, xsize
- 37, 14, buf
, green
);
283 snprintf (buf
, sizeof (buf
), "%Zuk", maxsize_stack
/ 1024);
284 gdImageString (im_out
, gdFontSmall
, xsize
- 37, 14, buf
, green
);
288 if (maxsize_heap
< 1024)
290 cnt
= ((ysize
- 40) * (maxsize_heap
/ 4)) / maxsize_heap
;
291 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt
, xsize
- 40,
292 ysize
- 20 - cnt
, red
);
293 snprintf (buf
, sizeof (buf
), "%Zu", maxsize_heap
/ 4);
294 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
295 ysize
- 26 - cnt
, buf
, red
);
299 cnt
= ((ysize
- 40) * (maxsize_heap
/ 4096)) / (maxsize_heap
/ 1024);
300 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt
, xsize
- 40,
301 ysize
- 20 - cnt
, red
);
302 snprintf (buf
, sizeof (buf
), "%Zuk", maxsize_heap
/ 4096);
303 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
304 ysize
- 26 - cnt
, buf
, red
);
306 if (maxsize_stack
< 1024)
308 cnt2
= ((ysize
- 40) * (maxsize_stack
/ 4)) / maxsize_stack
;
310 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
311 ysize
- 20 - cnt2
, green
);
312 snprintf (buf
, sizeof (buf
), "%Zu", maxsize_stack
/ 4);
313 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
318 cnt2
= ((ysize
- 40) * (maxsize_stack
/ 4096)) / (maxsize_stack
/ 1024);
320 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
321 ysize
- 20 - cnt2
, green
);
322 snprintf (buf
, sizeof (buf
), "%Zuk", maxsize_stack
/ 4096);
323 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
327 if (maxsize_heap
< 1024)
329 cnt
= ((ysize
- 40) * (maxsize_heap
/ 2)) / maxsize_heap
;
330 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt
, xsize
- 40,
331 ysize
- 20 - cnt
, red
);
332 snprintf (buf
, sizeof (buf
), "%Zu", maxsize_heap
/ 2);
333 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
334 ysize
- 26 - cnt
, buf
, red
);
338 cnt
= ((ysize
- 40) * (maxsize_heap
/ 2048)) / (maxsize_heap
/ 1024);
339 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt
, xsize
- 40,
340 ysize
- 20 - cnt
, red
);
341 snprintf (buf
, sizeof (buf
), "%Zuk", maxsize_heap
/ 2048);
342 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
343 ysize
- 26 - cnt
, buf
, red
);
345 if (maxsize_stack
< 1024)
347 cnt2
= ((ysize
- 40) * (maxsize_stack
/ 2)) / maxsize_stack
;
349 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
350 ysize
- 20 - cnt2
, green
);
351 snprintf (buf
, sizeof (buf
), "%Zu", maxsize_stack
/ 2);
352 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
357 cnt2
= ((ysize
- 40) * (maxsize_stack
/ 2048)) / (maxsize_stack
/ 1024);
359 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
360 ysize
- 20 - cnt2
, green
);
361 snprintf (buf
, sizeof (buf
), "%Zuk", maxsize_stack
/ 2048);
362 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
366 if (maxsize_heap
< 1024)
368 cnt
= ((ysize
- 40) * ((3 * maxsize_heap
) / 4)) / maxsize_heap
;
369 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt
, xsize
- 40,
370 ysize
- 20 - cnt
, red
);
371 snprintf (buf
, sizeof (buf
), "%Zu", (3 * maxsize_heap
) / 4);
372 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
373 ysize
- 26 - cnt
, buf
, red
);
377 cnt
= ((ysize
- 40) * ((3 * maxsize_heap
) / 4096)) / (maxsize_heap
379 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt
, xsize
- 40,
380 ysize
- 20 - cnt
, red
);
381 snprintf (buf
, sizeof (buf
), "%Zuk", (3 * maxsize_heap
) / 4096);
382 gdImageString (im_out
, gdFontSmall
, 39 - strlen (buf
) * 6,
383 ysize
- 26 - cnt
, buf
, red
);
385 if (maxsize_stack
< 1024)
387 cnt2
= ((ysize
- 40) * ((3 * maxsize_stack
) / 4)) / maxsize_stack
;
389 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
390 ysize
- 20 - cnt2
, green
);
391 snprintf (buf
, sizeof (buf
), "%Zu", (3 * maxsize_stack
) / 4);
392 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
397 cnt2
= (((ysize
- 40) * ((3 * maxsize_stack
) / 4096))
398 / (maxsize_stack
/ 1024));
400 gdImageDashedLine (im_out
, 40, ysize
- 20 - cnt2
, xsize
- 40,
401 ysize
- 20 - cnt2
, green
);
402 snprintf (buf
, sizeof (buf
), "%Zuk", (3 * maxsize_stack
) / 4096);
403 gdImageString (im_out
, gdFontSmall
, xsize
- 37, ysize
- 26 - cnt2
,
408 snprintf (buf
, sizeof (buf
), "%llu", total
);
409 gdImageString (im_out
, gdFontSmall
, xsize
- 50, ysize
- 14, buf
, blue
);
413 uint64_t previously
= start_time
;
415 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 32 * 6 - 80) / 2,
417 (unsigned char *) "# memory handling function calls",
421 last_stack
= last_heap
= last_total
= ysize
- 20;
422 for (cnt
= 1; cnt
<= total
; ++cnt
)
428 read (fd
, &entry
, sizeof (entry
));
430 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
432 if ((((previously
- start_time
) * 100) / total_time
) % 10 < 5)
433 gdImageFilledRectangle (im_out
,
434 40 + ((cnt
- 1) * (xsize
- 80)) / total
,
436 39 + (cnt
* (xsize
- 80)) / total
,
444 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
445 * (entry
.heap
+ entry
.stack
))
447 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
449 40 + ((xsize
- 80) * cnt
) / total
, new3
,
454 // assert (entry.heap <= maxsize_heap);
455 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
456 * entry
.heap
) / maxsize_heap
);
457 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
458 last_heap
, 40 + ((xsize
- 80) * cnt
) / total
, new[0],
462 // assert (entry.stack <= maxsize_stack);
463 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
464 * entry
.stack
) / maxsize_stack
);
465 gdImageLine (im_out
, 40 + ((xsize
- 80) * (cnt
- 1)) / total
,
466 last_stack
, 40 + ((xsize
- 80) * cnt
) / total
, new[1],
474 gdImageLine (im_out
, 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 20,
475 40 + ((xsize
- 80) * cnt
) / total
, ysize
- 15, blue
);
476 cnt
+= MAX (1, total
/ 20);
478 gdImageLine (im_out
, xsize
- 40, ysize
- 20, xsize
- 40, ysize
- 15,
483 uint64_t next_tick
= MAX (1, total
/ 20);
484 size_t last_xpos
= 40;
486 gdImageString (im_out
, gdFontSmall
, 40 + (xsize
- 39 * 6 - 80) / 2,
489 # memory handling function calls / time", blue
);
491 for (cnt
= 0; cnt
< 20; cnt
+= 2)
492 gdImageFilledRectangle (im_out
,
493 40 + (cnt
* (xsize
- 80)) / 20, ysize
- 19,
494 39 + ((cnt
+ 1) * (xsize
- 80)) / 20,
497 last_stack
= last_heap
= last_total
= ysize
- 20;
498 for (cnt
= 1; cnt
<= total
; ++cnt
)
505 read (fd
, &entry
, sizeof (entry
));
507 now
= ((uint64_t) entry
.time_high
) << 32 | entry
.time_low
;
508 xpos
= 40 + ((xsize
- 80) * (now
- start_time
)) / total_time
;
510 if (cnt
== next_tick
)
512 gdImageLine (im_out
, xpos
, ysize
- 20, xpos
, ysize
- 15, blue
);
513 next_tick
+= MAX (1, total
/ 20);
520 new3
= (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
521 * (entry
.heap
+ entry
.stack
))
523 gdImageLine (im_out
, last_xpos
, last_total
, xpos
, new3
, black
);
527 new[0] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
528 * entry
.heap
) / maxsize_heap
);
529 gdImageLine (im_out
, last_xpos
, last_heap
, xpos
, new[0], red
);
532 // assert (entry.stack <= maxsize_stack);
533 new[1] = (ysize
- 20) - ((((unsigned long long int) (ysize
- 40))
534 * entry
.stack
) / maxsize_stack
);
535 gdImageLine (im_out
, last_xpos
, last_stack
, xpos
, new[1], green
);
542 /* Write out the result. */
543 outfile
= fopen (outname
, "w");
545 error (EXIT_FAILURE
, errno
, "cannot open output file");
547 gdImagePng (im_out
, outfile
);
551 gdImageDestroy (im_out
);
557 /* Handle program arguments. */
559 parse_opt (int key
, char *arg
, struct argp_state
*state
)
586 return ARGP_ERR_UNKNOWN
;
593 more_help (int key
, const char *text
, void *input
)
600 case ARGP_KEY_HELP_EXTRA
:
601 /* We print some extra information. */
603 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n");
611 return (char *) text
;