Back to index

glibc  2.9
memusagestat.c
Go to the documentation of this file.
00001 /* Generate graphic from memory profiling data.
00002    Copyright (C) 1998, 1999, 2000, 2005, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published
00008    by the Free Software Foundation; version 2 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software Foundation,
00018    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00019 
00020 #define _FILE_OFFSET_BITS 64
00021 
00022 #include <argp.h>
00023 #include <assert.h>
00024 #include <errno.h>
00025 #include <error.h>
00026 #include <fcntl.h>
00027 #include <getopt.h>
00028 #include <inttypes.h>
00029 #include <libintl.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <unistd.h>
00034 #include <sys/param.h>
00035 #include <sys/stat.h>
00036 
00037 #include <gd.h>
00038 #include <gdfontl.h>
00039 #include <gdfonts.h>
00040 
00041 
00042 /* Default size of the generated image.  */
00043 #define XSIZE 800
00044 #define YSIZE 600
00045 
00046 #ifndef N_
00047 # define N_(Arg) Arg
00048 #endif
00049 
00050 
00051 /* Definitions of arguments for argp functions.  */
00052 static const struct argp_option options[] =
00053 {
00054   { "output", 'o', "FILE", 0, N_("Name output file") },
00055   { "string", 's', "STRING", 0, N_("Title string used in output graphic") },
00056   { "time", 't', NULL, 0, N_("Generate output linear to time (default is linear to number of function calls)") },
00057   { "total", 'T', NULL, 0,
00058     N_("Also draw graph for total memory consumption") },
00059   { "x-size", 'x', "VALUE", 0, N_("Make output graphic VALUE pixels wide") },
00060   { "y-size", 'y', "VALUE", 0, N_("Make output graphic VALUE pixels high") },
00061   { NULL, 0, NULL, 0, NULL }
00062 };
00063 
00064 /* Short description of program.  */
00065 static const char doc[] = N_("Generate graphic from memory profiling data");
00066 
00067 /* Strings for arguments in help texts.  */
00068 static const char args_doc[] = N_("DATAFILE [OUTFILE]");
00069 
00070 /* Prototype for option handler.  */
00071 static error_t parse_opt (int key, char *arg, struct argp_state *state);
00072 
00073 /* Function to print some extra text in the help message.  */
00074 static char *more_help (int key, const char *text, void *input);
00075 
00076 /* Data structure to communicate with argp functions.  */
00077 static struct argp argp =
00078 {
00079   options, parse_opt, args_doc, doc, NULL, more_help
00080 };
00081 
00082 
00083 struct entry
00084 {
00085   uint64_t heap;
00086   uint64_t stack;
00087   uint32_t time_low;
00088   uint32_t time_high;
00089 };
00090 
00091 
00092 /* Size of the image.  */
00093 static size_t xsize;
00094 static size_t ysize;
00095 
00096 /* Name of the output file.  */
00097 static char *outname;
00098 
00099 /* Title string for the graphic.  */
00100 static const char *string;
00101 
00102 /* Nonzero if graph should be generated linear in time.  */
00103 static int time_based;
00104 
00105 /* Nonzero if graph to display total use of memory should be drawn as well.  */
00106 static int also_total = 0;
00107 
00108 
00109 int
00110 main (int argc, char *argv[])
00111 {
00112   int remaining;
00113   const char *inname;
00114   gdImagePtr im_out;
00115   int grey, blue, red, green, yellow, black;
00116   int fd;
00117   struct stat st;
00118   size_t maxsize_heap;
00119   size_t maxsize_stack;
00120   size_t maxsize_total;
00121   uint64_t total;
00122   uint64_t cnt, cnt2;
00123   FILE *outfile;
00124   char buf[30];
00125   size_t last_heap;
00126   size_t last_stack;
00127   size_t last_total;
00128   struct entry headent[2];
00129   uint64_t start_time;
00130   uint64_t end_time;
00131   uint64_t total_time;
00132   const char *heap_format, *stack_format;
00133   int heap_scale, stack_scale, line;
00134 
00135   outname = NULL;
00136   xsize = XSIZE;
00137   ysize = YSIZE;
00138   string = NULL;
00139 
00140   /* Parse and process arguments.  */
00141   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
00142 
00143   if (remaining >= argc || remaining + 2 < argc)
00144     {
00145       argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
00146                program_invocation_short_name);
00147       exit (1);
00148     }
00149 
00150   inname = argv[remaining++];
00151 
00152   if (remaining < argc)
00153     outname = argv[remaining];
00154   else if (outname == NULL)
00155     {
00156       size_t len = strlen (inname);
00157       outname = alloca (len + 5);
00158       stpcpy (stpcpy (outname, inname), ".png");
00159     }
00160 
00161   /* Open for read/write since we try to repair the file in case the
00162      application hasn't terminated cleanly.  */
00163   fd = open (inname, O_RDWR);
00164   if (fd == -1)
00165     error (EXIT_FAILURE, errno, "cannot open input file");
00166   if (fstat (fd, &st) != 0)
00167     {
00168       close (fd);
00169       error (EXIT_FAILURE, errno, "cannot get size of input file");
00170     }
00171   /* Test whether the file contains only full records.  */
00172   if ((st.st_size % sizeof (struct entry)) != 0
00173       /* The file must at least contain the two administrative records.  */
00174       || st.st_size < 2 * sizeof (struct entry))
00175     {
00176       close (fd);
00177       error (EXIT_FAILURE, 0, "input file as incorrect size");
00178     }
00179   /* Compute number of data entries.  */
00180   total = st.st_size / sizeof (struct entry) - 2;
00181 
00182   /* Read the administrative information.  */
00183   read (fd, headent, sizeof (headent));
00184   maxsize_heap = headent[1].heap;
00185   maxsize_stack = headent[1].stack;
00186   maxsize_total = headent[0].stack;
00187   if (also_total)
00188     {
00189       /* We use one scale and since we also draw the total amount of
00190         memory used we have to adapt the maximum.  */
00191       maxsize_heap = maxsize_total;
00192       maxsize_stack = maxsize_total;
00193     }
00194 
00195   if (maxsize_heap == 0 && maxsize_stack == 0)
00196     {
00197       /* The program aborted before memusage was able to write the
00198         information about the maximum heap and stack use.  Repair
00199         the file now.  */
00200       struct entry next;
00201 
00202       while (1)
00203        {
00204          if (read (fd, &next, sizeof (next)) == 0)
00205            break;
00206          if (next.heap > headent[1].heap)
00207            headent[1].heap = next.heap;
00208          if (next.stack > headent[1].stack)
00209            headent[1].stack = next.stack;
00210        }
00211 
00212       headent[1].time_low = next.time_low;
00213       headent[1].time_high = next.time_high;
00214 
00215       /* Write the computed values in the file.  */
00216       lseek (fd, sizeof (struct entry), SEEK_SET);
00217       write (fd, &headent[1], sizeof (struct entry));
00218     }
00219 
00220   start_time = ((uint64_t) headent[0].time_high) << 32 | headent[0].time_low;
00221   end_time = ((uint64_t) headent[1].time_high) << 32 | headent[1].time_low;
00222   total_time = end_time - start_time;
00223 
00224   if (xsize < 100)
00225     xsize = 100;
00226   if (ysize < 80)
00227     ysize = 80;
00228 
00229   /* Create output image with the specified size.  */
00230   im_out = gdImageCreate (xsize, ysize);
00231 
00232   /* First color allocated is background.  */
00233   grey = gdImageColorAllocate (im_out, 224, 224, 224);
00234 
00235   /* Set transparent color. */
00236   gdImageColorTransparent (im_out, grey);
00237 
00238   /* These are all the other colors we need (in the moment).  */
00239   red = gdImageColorAllocate (im_out, 255, 0, 0);
00240   green = gdImageColorAllocate (im_out, 0, 130, 0);
00241   blue = gdImageColorAllocate (im_out, 0, 0, 255);
00242   yellow = gdImageColorAllocate (im_out, 154, 205, 50);
00243   black = gdImageColorAllocate (im_out, 0, 0, 0);
00244 
00245   gdImageRectangle (im_out, 40, 20, xsize - 40, ysize - 20, blue);
00246 
00247   if (maxsize_heap < 1024)
00248     {
00249       heap_format = "%Zu";
00250       heap_scale = 1;
00251     }
00252   else if (maxsize_heap < 1024 * 1024 * 100)
00253     {
00254       heap_format = "%Zuk";
00255       heap_scale = 1024;
00256     }
00257   else
00258     {
00259       heap_format = "%ZuM";
00260       heap_scale = 1024 * 1024;
00261     }
00262 
00263   if (maxsize_stack < 1024)
00264     {
00265       stack_format = "%Zu";
00266       stack_scale = 1;
00267     }
00268   else if (maxsize_stack < 1024 * 1024 * 100)
00269     {
00270       stack_format = "%Zuk";
00271       stack_scale = 1024;
00272     }
00273   else
00274     {
00275       stack_format = "%ZuM";
00276       stack_scale = 1024 * 1024;
00277     }
00278 
00279   gdImageString (im_out, gdFontSmall, 38, ysize - 14, (unsigned char *) "0",
00280                blue);
00281   snprintf (buf, sizeof (buf), heap_format, 0);
00282   gdImageString (im_out, gdFontSmall, maxsize_heap < 1024 ? 32 : 26,
00283                ysize - 26, (unsigned char *) buf, red);
00284   snprintf (buf, sizeof (buf), stack_format, 0);
00285   gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26,
00286                (unsigned char *) buf, green);
00287 
00288   if (string != NULL)
00289     gdImageString (im_out, gdFontLarge, (xsize - strlen (string) * 8) / 2,
00290                  2, (unsigned char *) string, green);
00291 
00292   gdImageStringUp (im_out, gdFontSmall, 1, ysize / 2 - 10,
00293                  (unsigned char *) "allocated", red);
00294   gdImageStringUp (im_out, gdFontSmall, 11, ysize / 2 - 10,
00295                  (unsigned char *) "memory", red);
00296 
00297   gdImageStringUp (im_out, gdFontSmall, xsize - 39, ysize / 2 - 10,
00298                  (unsigned char *) "used", green);
00299   gdImageStringUp (im_out, gdFontSmall, xsize - 27, ysize / 2 - 10,
00300                  (unsigned char *) "stack", green);
00301 
00302   snprintf (buf, sizeof (buf), heap_format, maxsize_heap / heap_scale);
00303   gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6, 14,
00304                (unsigned char *) buf, red);
00305   snprintf (buf, sizeof (buf), stack_format, maxsize_stack / stack_scale);
00306   gdImageString (im_out, gdFontSmall, xsize - 37, 14,
00307                (unsigned char *) buf, green);
00308 
00309   for (line = 1; line <= 3; ++line)
00310     {
00311       cnt = ((ysize - 40) * (maxsize_heap / 4 * line / heap_scale)) /
00312        (maxsize_heap / heap_scale);
00313       gdImageDashedLine (im_out, 40, ysize - 20 - cnt, xsize - 40,
00314                       ysize - 20 - cnt, red);
00315       snprintf (buf, sizeof (buf), heap_format, maxsize_heap / 4 * line /
00316               heap_scale);
00317       gdImageString (im_out, gdFontSmall, 39 - strlen (buf) * 6,
00318                    ysize - 26 - cnt, (unsigned char *) buf, red);
00319 
00320       cnt2 = ((ysize - 40) * (maxsize_stack / 4 * line / stack_scale)) /
00321        (maxsize_stack / stack_scale);
00322       if (cnt != cnt2)
00323        gdImageDashedLine (im_out, 40, ysize - 20 - cnt2, xsize - 40,
00324                         ysize - 20 - cnt2, green);
00325       snprintf (buf, sizeof (buf), stack_format, maxsize_stack / 4 * line /
00326               stack_scale);
00327       gdImageString (im_out, gdFontSmall, xsize - 37, ysize - 26 - cnt2,
00328                    (unsigned char *) buf, green);
00329     }
00330 
00331   snprintf (buf, sizeof (buf), "%llu", (unsigned long long) total);
00332   gdImageString (im_out, gdFontSmall, xsize - 50, ysize - 14,
00333                (unsigned char *) buf, blue);
00334 
00335   if (!time_based)
00336     {
00337       uint64_t previously = start_time;
00338 
00339       gdImageString (im_out, gdFontSmall, 40 + (xsize - 32 * 6 - 80) / 2,
00340                    ysize - 12,
00341                    (unsigned char *) "# memory handling function calls",
00342                    blue);
00343 
00344 
00345       last_stack = last_heap = last_total = ysize - 20;
00346       for (cnt = 1; cnt <= total; ++cnt)
00347        {
00348          struct entry entry;
00349          size_t new[2];
00350          uint64_t now;
00351 
00352          read (fd, &entry, sizeof (entry));
00353 
00354          now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
00355 
00356          if ((((previously - start_time) * 100) / total_time) % 10 < 5)
00357            gdImageFilledRectangle (im_out,
00358                                 40 + ((cnt - 1) * (xsize - 80)) / total,
00359                                 ysize - 19,
00360                                 39 + (cnt * (xsize - 80)) / total,
00361                                 ysize - 14, yellow);
00362          previously = now;
00363 
00364          if (also_total)
00365            {
00366              size_t new3;
00367 
00368              new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
00369                                   * (entry.heap + entry.stack))
00370                                  / maxsize_heap);
00371              gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
00372                         last_total,
00373                         40 + ((xsize - 80) * cnt) / total, new3,
00374                         black);
00375              last_total = new3;
00376            }
00377 
00378          // assert (entry.heap <= maxsize_heap);
00379          new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
00380                                 * entry.heap) / maxsize_heap);
00381          gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
00382                      last_heap, 40 + ((xsize - 80) * cnt) / total, new[0],
00383                      red);
00384          last_heap = new[0];
00385 
00386          // assert (entry.stack <= maxsize_stack);
00387          new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
00388                                 * entry.stack) / maxsize_stack);
00389          gdImageLine (im_out, 40 + ((xsize - 80) * (cnt - 1)) / total,
00390                      last_stack, 40 + ((xsize - 80) * cnt) / total, new[1],
00391                      green);
00392          last_stack = new[1];
00393        }
00394 
00395       cnt = 0;
00396       while (cnt < total)
00397        {
00398          gdImageLine (im_out, 40 + ((xsize - 80) * cnt) / total, ysize - 20,
00399                      40 + ((xsize - 80) * cnt) / total, ysize - 15, blue);
00400          cnt += MAX (1, total / 20);
00401        }
00402       gdImageLine (im_out, xsize - 40, ysize - 20, xsize - 40, ysize - 15,
00403                  blue);
00404     }
00405   else
00406     {
00407       uint64_t next_tick = MAX (1, total / 20);
00408       size_t last_xpos = 40;
00409 
00410       gdImageString (im_out, gdFontSmall, 40 + (xsize - 39 * 6 - 80) / 2,
00411                    ysize - 12,
00412                    (unsigned char *) "\
00413 # memory handling function calls / time", blue);
00414 
00415       for (cnt = 0; cnt < 20; cnt += 2)
00416        gdImageFilledRectangle (im_out,
00417                             40 + (cnt * (xsize - 80)) / 20, ysize - 19,
00418                             39 + ((cnt + 1) * (xsize - 80)) / 20,
00419                             ysize - 14, yellow);
00420 
00421       last_stack = last_heap = last_total = ysize - 20;
00422       for (cnt = 1; cnt <= total; ++cnt)
00423        {
00424          struct entry entry;
00425          size_t new[2];
00426          size_t xpos;
00427          uint64_t now;
00428 
00429          read (fd, &entry, sizeof (entry));
00430 
00431          now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
00432          xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
00433 
00434          if (cnt == next_tick)
00435            {
00436              gdImageLine (im_out, xpos, ysize - 20, xpos, ysize - 15, blue);
00437              next_tick += MAX (1, total / 20);
00438            }
00439 
00440          if (also_total)
00441            {
00442              size_t new3;
00443 
00444              new3 = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
00445                                   * (entry.heap + entry.stack))
00446                                  / maxsize_heap);
00447              gdImageLine (im_out, last_xpos, last_total, xpos, new3, black);
00448              last_total = new3;
00449            }
00450 
00451          new[0] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
00452                                 * entry.heap) / maxsize_heap);
00453          gdImageLine (im_out, last_xpos, last_heap, xpos, new[0], red);
00454          last_heap = new[0];
00455 
00456          // assert (entry.stack <= maxsize_stack);
00457          new[1] = (ysize - 20) - ((((unsigned long long int) (ysize - 40))
00458                                 * entry.stack) / maxsize_stack);
00459          gdImageLine (im_out, last_xpos, last_stack, xpos, new[1], green);
00460          last_stack = new[1];
00461 
00462          last_xpos = xpos;
00463        }
00464     }
00465 
00466   /* Write out the result.  */
00467   outfile = fopen (outname, "w");
00468   if (outfile == NULL)
00469     error (EXIT_FAILURE, errno, "cannot open output file");
00470 
00471   gdImagePng (im_out, outfile);
00472 
00473   fclose (outfile);
00474 
00475   gdImageDestroy (im_out);
00476 
00477   return 0;
00478 }
00479 
00480 
00481 /* Handle program arguments.  */
00482 static error_t
00483 parse_opt (int key, char *arg, struct argp_state *state)
00484 {
00485   switch (key)
00486     {
00487     case 'o':
00488       outname = arg;
00489       break;
00490     case 's':
00491       string = arg;
00492       break;
00493     case 't':
00494       time_based = 1;
00495       break;
00496     case 'T':
00497       also_total = 1;
00498       break;
00499     case 'x':
00500       xsize = atoi (arg);
00501       if (xsize == 0)
00502        xsize = XSIZE;
00503       break;
00504     case 'y':
00505       ysize = atoi (arg);
00506       if (ysize == 0)
00507        ysize = XSIZE;
00508       break;
00509     default:
00510       return ARGP_ERR_UNKNOWN;
00511     }
00512   return 0;
00513 }
00514 
00515 
00516 static char *
00517 more_help (int key, const char *text, void *input)
00518 {
00519   char *orig;
00520   char *cp;
00521 
00522   switch (key)
00523     {
00524     case ARGP_KEY_HELP_EXTRA:
00525       /* We print some extra information.  */
00526       orig = gettext ("\
00527 For bug reporting instructions, please see:\n\
00528 <http://www.gnu.org/software/libc/bugs.html>.\n");
00529       cp = strdup (orig);
00530       if (cp == NULL)
00531        cp = orig;
00532       return cp;
00533     default:
00534       break;
00535     }
00536   return (char *) text;
00537 }