ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/statgrab/statgrab.c
(Generate patch)

Comparing projects/libstatgrab/src/statgrab/statgrab.c (file contents):
Revision 1.35 by tdb, Wed Jul 13 13:01:24 2005 UTC vs.
Revision 1.38 by tdb, Sun Oct 3 18:35:59 2010 UTC

# Line 60 | Line 60 | typedef struct {
60          char *name;
61          stat_type type;
62          void *stat;
63 < } stat;
63 > } stat_item;
64 > /* AIX:
65 > statgrab.c:63: error: 'stat' redeclared as different kind of symbol
66 > /usr/include/sys/stat.h:320: error: previous declaration of 'stat' was here
67 > */
68  
69 < stat *stats = NULL;
69 > stat_item *stat_items = NULL;
70   int num_stats = 0;
71   int alloc_stats = 0;
72   #define INCREMENT_STATS 64
# Line 86 | Line 90 | void clear_stats() {
90          int i;
91  
92          for (i = 0; i < num_stats; i++)
93 <                free(stats[i].name);
93 >                free(stat_items[i].name);
94          num_stats = 0;
95   }
96  
# Line 121 | Line 125 | void add_stat(stat_type type, void *stat, ...) {
125                          break;
126                  partlen = strlen(part);
127                  memcpy(p, part, partlen);
128 <                p += partlen;
128 >
129 >                /* Replace spaces and dots with underscores. */
130 >                while (partlen-- > 0) {
131 >                        if (*p == ' ' || *p == '.')
132 >                                *p = '_';
133 >                        p++;
134 >                }
135 >
136                  *p++ = '.';
137          }
138          va_end(ap);
139          *--p = '\0';
140  
130        /* Replace spaces with underscores. */
131        for (p = name; *p != '\0'; p++) {
132                if (*p == ' ')
133                        *p = '_';
134        }
135
141          /* Stretch the stats array if necessary. */
142          if (num_stats >= alloc_stats) {
143                  alloc_stats += INCREMENT_STATS;
144 <                stats = realloc(stats, alloc_stats * sizeof *stats);
145 <                if (stats == NULL)
144 >                stat_items = realloc(stat_items, alloc_stats * sizeof *stat_items);
145 >                if (stat_items == NULL)
146                          die("out of memory");
147          }
148  
149 <        stats[num_stats].name = name;
150 <        stats[num_stats].type = type;
151 <        stats[num_stats].stat = stat;
149 >        stat_items[num_stats].name = name;
150 >        stat_items[num_stats].type = type;
151 >        stat_items[num_stats].stat = stat;
152          ++num_stats;
153   }
154  
155   /* Compare two stats by name for qsort and bsearch. */
156   int stats_compare(const void *a, const void *b) {
157 <        return strcmp(((stat *)a)->name, ((stat *)b)->name);
157 >        return strcmp(((stat_item *)a)->name, ((stat_item *)b)->name);
158   }
159  
160   /* Compare up to the length of the key for bsearch. */
161   int stats_compare_prefix(const void *key, const void *item) {
162 <        const char *kn = ((stat *)key)->name;
163 <        const char *in = ((stat *)item)->name;
162 >        const char *kn = ((stat_item *)key)->name;
163 >        const char *in = ((stat_item *)item)->name;
164  
165          return strncmp(kn, in, strlen(kn));
166   }
# Line 487 | Line 492 | void select_interesting(int argc, char **argv) {
492          }
493   }
494  
495 < /* Clear and rebuild the stats array. */
495 > /* Clear and rebuild the stat_items array. */
496   void get_stats() {
497          toplevel *t;
498  
# Line 498 | Line 503 | void get_stats() {
503                          t->populate();
504          }
505  
506 <        if (stats != NULL)
507 <                qsort(stats, num_stats, sizeof *stats, stats_compare);
506 >        if (stat_items != NULL)
507 >                qsort(stat_items, num_stats, sizeof *stat_items, stats_compare);
508   }
509  
510 < /* Print the value of a stat. */
511 < void print_stat_value(const stat *s) {
510 > /* Print the value of a stat_item. */
511 > void print_stat_value(const stat_item *s) {
512          void *v = s->stat;
513          double fv;
514          long lv;
# Line 511 | Line 516 | void print_stat_value(const stat *s) {
516  
517          switch (s->type) {
518          case LONG_LONG:
519 + #ifdef WIN32 /* Windows printf does not understand %lld, so use %I64d instead */
520 +                printf("%I64d", *(long long *)v);
521 + #else
522                  printf("%lld", *(long long *)v);
523 + #endif
524                  break;
525          case BYTES:
526                  llv = *(long long *)v;
527                  if (bytes_scale_factor != 0) {
528                          llv /= bytes_scale_factor;
529                  }
530 + #ifdef WIN32
531 +                printf("%I64d", llv);
532 + #else
533                  printf("%lld", llv);
534 + #endif
535                  break;
536          case TIME_T:
537                  /* FIXME option for formatted time? */
# Line 565 | Line 578 | void print_stat_value(const stat *s) {
578   }
579  
580   /* Print the name and value of a stat. */
581 < void print_stat(const stat *s) {
581 > void print_stat(const stat_item *s) {
582          switch (display_mode) {
583          case DISPLAY_LINUX:
584                  printf("%s = ", s->name);
# Line 588 | Line 601 | void print_stats(int argc, char **argv) {
601          if (argc == optind) {
602                  /* Print all stats. */
603                  for (i = 0; i < num_stats; i++)
604 <                        print_stat(&stats[i]);
604 >                        print_stat(&stat_items[i]);
605          } else {
606                  /* Print selected stats. */
607                  for (i = optind; i < argc; i++) {
608                          char *name = argv[i];
609 <                        stat key;
610 <                        const stat *s, *end;
609 >                        stat_item key;
610 >                        const stat_item *s, *end;
611                          int (*compare)(const void *, const void *);
612  
613                          key.name = name;
# Line 603 | Line 616 | void print_stats(int argc, char **argv) {
616                          else
617                                  compare = stats_compare;
618  
619 <                        if (stats == NULL) {
619 >                        if (stat_items == NULL) {
620                                  s = NULL;
621                          } else {
622 <                                s = (const stat *)bsearch(&key, stats,
622 >                                s = (const stat_item *)bsearch(&key, stat_items,
623                                                            num_stats,
624 <                                                          sizeof *stats,
624 >                                                          sizeof *stat_items,
625                                                            compare);
626                          }
627  
# Line 618 | Line 631 | void print_stats(int argc, char **argv) {
631                          }
632  
633                          /* Find the range of stats the user wanted. */
634 <                        for (; s >= stats; s--) {
634 >                        for (; s >= stat_items; s--) {
635                                  if (compare(&key, s) != 0)
636                                          break;
637                          }
638                          s++;
639 <                        for (end = s; end < &stats[num_stats]; end++) {
639 >                        for (end = s; end < &stat_items[num_stats]; end++) {
640                                  if (compare(&key, end) != 0)
641                                          break;
642                          }
# Line 734 | Line 747 | int main(int argc, char **argv) {
747          /* We don't care if sg_init fails, because we can just display
748             the statistics that can be read as non-root. */
749          sg_init();
750 +        sg_snapshot();
751          if (sg_drop_privileges() != 0)
752                  die("Failed to drop setuid/setgid privileges");
753  
# Line 745 | Line 759 | int main(int argc, char **argv) {
759          case REPEAT_ONCE:
760                  get_stats();
761                  sleep(repeat_time);
762 +                sg_snapshot();
763                  get_stats();
764                  print_stats(argc, argv);
765                  break;
# Line 754 | Line 769 | int main(int argc, char **argv) {
769                          print_stats(argc, argv);
770                          printf("\n");
771                          sleep(repeat_time);
772 +                        sg_snapshot();
773                  }
774          }
775  
# Line 761 | Line 777 | int main(int argc, char **argv) {
777                  printf("\n");
778                  printf("statgrab\n");
779          }
780 +
781 +        sg_shutdown();
782  
783          return 0;
784   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines