2b16b01ded7dea4c36de263b160f2c98d76ce546
[platform/upstream/linaro-glibc.git] / malloc / memusagestat.c
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.
5
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.
10
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.
15
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
19    02111-1307 USA.  */
20
21 #include <argp.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <error.h>
25 #include <fcntl.h>
26 #include <getopt.h>
27 #include <inttypes.h>
28 #include <libintl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/param.h>
34 #include <sys/stat.h>
35
36 #include <gd.h>
37 #include <gdfontl.h>
38 #include <gdfonts.h>
39
40
41 /* Default size of the generated image.  */
42 #define XSIZE 800
43 #define YSIZE 600
44
45 #ifndef N_
46 # define N_(Arg) Arg
47 #endif
48
49
50 /* Definitions of arguments for argp functions.  */
51 static const struct argp_option options[] =
52 {
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 }
61 };
62
63 /* Short description of program.  */
64 static const char doc[] = N_("Generate graphic from memory profiling data");
65
66 /* Strings for arguments in help texts.  */
67 static const char args_doc[] = N_("DATAFILE [OUTFILE]");
68
69 /* Prototype for option handler.  */
70 static error_t parse_opt (int key, char *arg, struct argp_state *state);
71
72 /* Function to print some extra text in the help message.  */
73 static char *more_help (int key, const char *text, void *input);
74
75 /* Data structure to communicate with argp functions.  */
76 static struct argp argp =
77 {
78   options, parse_opt, args_doc, doc, NULL, more_help
79 };
80
81
82 struct entry
83 {
84   size_t heap;
85   size_t stack;
86   uint32_t time_low;
87   uint32_t time_high;
88 };
89
90
91 /* Size of the image.  */
92 static size_t xsize;
93 static size_t ysize;
94
95 /* Name of the output file.  */
96 static char *outname;
97
98 /* Title string for the graphic.  */
99 static const char *string;
100
101 /* Nonzero if graph should be generated linear in time.  */
102 static int time_based;
103
104 /* Nonzero if graph to display total use of memory should be drawn as well.  */
105 static int also_total = 0;
106
107
108 int
109 main (int argc, char *argv[])
110 {
111   int remaining;
112   const char *inname;
113   gdImagePtr im_out;
114   int grey, blue, red, green, yellow, black;
115   int fd;
116   struct stat st;
117   size_t maxsize_heap;
118   size_t maxsize_stack;
119   size_t maxsize_total;
120   uint64_t total;
121   uint64_t cnt, cnt2;
122   FILE *outfile;
123   char buf[30];
124   size_t last_heap;
125   size_t last_stack;
126   size_t last_total;
127   struct entry headent[2];
128   uint64_t start_time;
129   uint64_t end_time;
130   uint64_t total_time;
131
132   outname = NULL;
133   xsize = XSIZE;
134   ysize = YSIZE;
135   string = NULL;
136
137   /* Parse and process arguments.  */
138   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
139
140   if (remaining >= argc || remaining + 2 < argc)
141     {
142       argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
143                  program_invocation_short_name);
144       exit (1);
145     }
146
147   inname = argv[remaining++];
148
149   if (remaining < argc)
150     outname = argv[remaining];
151   else if (outname == NULL)
152     {
153       size_t len = strlen (inname);
154       outname = alloca (len + 5);
155       stpcpy (stpcpy (outname, inname), ".png");
156     }
157
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);
161   if (fd == -1)
162     error (EXIT_FAILURE, errno, "cannot open input file");
163   if (fstat (fd, &st) != 0)
164     {
165       close (fd);
166       error (EXIT_FAILURE, errno, "cannot get size of input file");
167     }
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))
172     {
173       close (fd);
174       error (EXIT_FAILURE, 0, "input file as incorrect size");
175     }
176   /* Compute number of data entries.  */
177   total = st.st_size / sizeof (struct entry) - 2;
178
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;
184   if (also_total)
185     {
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;
190     }
191
192   if (maxsize_heap == 0 && maxsize_stack == 0)
193     {
194       /* The program aborted before memusage was able to write the
195          information about the maximum heap and stack use.  Repair
196          the file now.  */
197       struct entry next;
198
199       while (1)
200         {
201           if (read (fd, &next, sizeof (next)) == 0)
202             break;
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;
207         }
208
209       headent[1].time_low = next.time_low;
210       headent[1].time_high = next.time_high;
211
212       /* Write the computed values in the file.  */
213       lseek (fd, sizeof (struct entry), SEEK_SET);
214       write (fd, &headent[1], sizeof (struct entry));
215     }
216
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;
220
221   if (xsize < 100)
222     xsize = 100;
223   if (ysize < 80)
224     ysize = 80;
225
226   /* Create output image with the specified size.  */
227   im_out = gdImageCreate (xsize, ysize);
228
229   /* First color allocated is background.  */
230   grey = gdImageColorAllocate (im_out, 224, 224, 224);
231
232   /* Set transparent color. */
233   gdImageColorTransparent (im_out, grey);
234
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);
241
242   gdImageRectangle (im_out, 40, 20, xsize - 40, ysize - 20, blue);
243
244   gdImageString (im_out, gdFontSmall, 38, ysize - 14, (unsigned char *) "0",
245                  blue);
246   gdImageString (im_out, gdFontSmall, maxsize_heap < 1024 ? 32 : 26,
247                  ysize - 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);
251
252   if (string != NULL)
253     gdImageString (im_out, gdFontLarge, (xsize - strlen (string) * 8) / 2,
254                    2, (char *) string, green);
255
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);
260
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);
265
266   if (maxsize_heap < 1024)
267     {
268       snprintf (buf, sizeof (buf), "%Zu", maxsize_heap);
269       gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6, 14, buf, red);
270     }
271   else
272     {
273       snprintf (buf, sizeof (buf), "%Zuk", maxsize_heap / 1024);
274       gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6, 14, buf, red);
275     }
276   if (maxsize_stack < 1024)
277     {
278       snprintf (buf, sizeof (buf), "%Zu", maxsize_stack);
279       gdImageString (im_out, gdFontSmall, xsize - 37, 14, buf, green);
280     }
281   else
282     {
283       snprintf (buf, sizeof (buf), "%Zuk", maxsize_stack / 1024);
284       gdImageString (im_out, gdFontSmall, xsize - 37, 14, buf, green);
285     }
286
287
288   if (maxsize_heap < 1024)
289     {
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);
296     }
297   else
298     {
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);
305     }
306   if (maxsize_stack < 1024)
307     {
308       cnt2 = ((ysize - 40) * (maxsize_stack / 4)) / maxsize_stack;
309       if (cnt != cnt2)
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,
314                      buf, green);
315     }
316   else
317     {
318       cnt2 = ((ysize - 40) * (maxsize_stack / 4096)) / (maxsize_stack / 1024);
319       if (cnt != cnt2)
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,
324                      buf, green);
325     }
326
327   if (maxsize_heap < 1024)
328     {
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);
335     }
336   else
337     {
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);
344     }
345   if (maxsize_stack < 1024)
346     {
347       cnt2 = ((ysize - 40) * (maxsize_stack / 2)) / maxsize_stack;
348       if (cnt != cnt2)
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,
353                      buf, green);
354     }
355   else
356     {
357       cnt2 = ((ysize - 40) * (maxsize_stack / 2048)) / (maxsize_stack / 1024);
358       if (cnt != cnt2)
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,
363                      buf, green);
364     }
365
366   if (maxsize_heap < 1024)
367     {
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);
374     }
375   else
376     {
377       cnt = ((ysize - 40) * ((3 * maxsize_heap) / 4096)) / (maxsize_heap
378                                                             / 1024);
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);
384     }
385   if (maxsize_stack < 1024)
386     {
387       cnt2 = ((ysize - 40) * ((3 * maxsize_stack) / 4)) / maxsize_stack;
388       if (cnt != cnt2)
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,
393                      buf, green);
394     }
395   else
396     {
397       cnt2 = (((ysize - 40) * ((3 * maxsize_stack) / 4096))
398               / (maxsize_stack / 1024));
399       if (cnt != cnt2)
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,
404                      buf, green);
405     }
406
407
408   snprintf (buf, sizeof (buf), "%llu", (unsigned long long) total);
409   gdImageString (im_out, gdFontSmall, xsize - 50, ysize - 14, buf, blue);
410
411   if (!time_based)
412     {
413       uint64_t previously = start_time;
414
415       gdImageString (im_out, gdFontSmall, 40 + (xsize - 32 * 6 - 80) / 2,
416                      ysize - 12,
417                      (unsigned char *) "# memory handling function calls",
418                      blue);
419
420
421       last_stack = last_heap = last_total = ysize - 20;
422       for (cnt = 1; cnt <= total; ++cnt)
423         {
424           struct entry entry;
425           size_t new[2];
426           uint64_t now;
427
428           read (fd, &entry, sizeof (entry));
429
430           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
431
432           if ((((previously - start_time) * 100) / total_time) % 10 < 5)
433             gdImageFilledRectangle (im_out,
434                                     40 + ((cnt - 1) * (xsize - 80)) / total,
435                                     ysize - 19,
436                                     39 + (cnt * (xsize - 80)) / total,
437                                     ysize - 14, yellow);
438           previously = now;
439
440           if (also_total)
441             {
442               size_t new3;
443
444               new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
445                                       * (entry.heap + entry.stack))
446                                      / maxsize_heap);
447               gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
448                            last_total,
449                            40 + ((xsize - 80) * cnt) / total, new3,
450                            black);
451               last_total = new3;
452             }
453
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],
459                        red);
460           last_heap = new[0];
461
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],
467                        green);
468           last_stack = new[1];
469         }
470
471       cnt = 0;
472       while (cnt < total)
473         {
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);
477         }
478       gdImageLine (im_out, xsize - 40, ysize - 20, xsize - 40, ysize - 15,
479                    blue);
480     }
481   else
482     {
483       uint64_t next_tick = MAX (1, total / 20);
484       size_t last_xpos = 40;
485
486       gdImageString (im_out, gdFontSmall, 40 + (xsize - 39 * 6 - 80) / 2,
487                      ysize - 12,
488                      (unsigned char *) "\
489 # memory handling function calls / time", blue);
490
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,
495                                 ysize - 14, yellow);
496
497       last_stack = last_heap = last_total = ysize - 20;
498       for (cnt = 1; cnt <= total; ++cnt)
499         {
500           struct entry entry;
501           size_t new[2];
502           size_t xpos;
503           uint64_t now;
504
505           read (fd, &entry, sizeof (entry));
506
507           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
508           xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
509
510           if (cnt == next_tick)
511             {
512               gdImageLine (im_out, xpos, ysize - 20, xpos, ysize - 15, blue);
513               next_tick += MAX (1, total / 20);
514             }
515
516           if (also_total)
517             {
518               size_t new3;
519
520               new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
521                                       * (entry.heap + entry.stack))
522                                      / maxsize_heap);
523               gdImageLine (im_out, last_xpos, last_total, xpos, new3, black);
524               last_total = new3;
525             }
526
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);
530           last_heap = new[0];
531
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);
536           last_stack = new[1];
537
538           last_xpos = xpos;
539         }
540     }
541
542   /* Write out the result.  */
543   outfile = fopen (outname, "w");
544   if (outfile == NULL)
545     error (EXIT_FAILURE, errno, "cannot open output file");
546
547   gdImagePng (im_out, outfile);
548
549   fclose (outfile);
550
551   gdImageDestroy (im_out);
552
553   return 0;
554 }
555
556
557 /* Handle program arguments.  */
558 static error_t
559 parse_opt (int key, char *arg, struct argp_state *state)
560 {
561   switch (key)
562     {
563     case 'o':
564       outname = arg;
565       break;
566     case 's':
567       string = arg;
568       break;
569     case 't':
570       time_based = 1;
571       break;
572     case 'T':
573       also_total = 1;
574       break;
575     case 'x':
576       xsize = atoi (arg);
577       if (xsize == 0)
578         xsize = XSIZE;
579       break;
580     case 'y':
581       ysize = atoi (arg);
582       if (ysize == 0)
583         ysize = XSIZE;
584       break;
585     default:
586       return ARGP_ERR_UNKNOWN;
587     }
588   return 0;
589 }
590
591
592 static char *
593 more_help (int key, const char *text, void *input)
594 {
595   char *orig;
596   char *cp;
597
598   switch (key)
599     {
600     case ARGP_KEY_HELP_EXTRA:
601       /* We print some extra information.  */
602       orig = gettext ("\
603 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n");
604       cp = strdup (orig);
605       if (cp == NULL)
606         cp = orig;
607       return cp;
608     default:
609       break;
610     }
611   return (char *) text;
612 }