]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
cytune: refactor main(), new function query_tty_stats()
authorSami Kerola <kerolasa@iki.fi>
Sun, 25 Sep 2011 20:13:14 +0000 (22:13 +0200)
committerSami Kerola <kerolasa@iki.fi>
Thu, 29 Sep 2011 20:45:27 +0000 (22:45 +0200)
This commit also moves global variables to the beginning of the
file, or to function scope where they belong.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sys-utils/cytune.c

index 9ca79817519ceefb6fb7951d4e3daa57a6175e97..ffdee8746804cf4684ac8fe84f60ab1c4eee4547 100644 (file)
@@ -84,6 +84,10 @@ struct cyclades_control {
 struct cyclades_control *cmon;
 int cmon_index;
 
+static int global_argc, global_optind;
+static char ***global_argv;
+
+
 #define mvtime(tvpto, tvpfrom)  (((tvpto)->tv_sec = (tvpfrom)->tv_sec),(tvpto)->tv_usec = (tvpfrom)->tv_usec)
 
 static void __attribute__ ((__noreturn__)) usage(FILE * out)
@@ -115,9 +119,6 @@ dtime(struct timeval * tvpnew, struct timeval * tvpold) {
   return diff;
 }
 
-static int global_argc, global_optind;
-static char ***global_argv;
-
 static void
 summary(int sig) {
   struct cyclades_control *cc;
@@ -158,41 +159,142 @@ summary(int sig) {
   cc->timeout_value = 0;
 }
 
-static int query   = 0;
-static int interval = 1;
+void query_tty_stats(int argc, char **argv, int interval, int numfiles,
+                    unsigned long *threshold_value,
+                    unsigned long *timeout_value)
+{
+  struct cyclades_monitor cywork;
+  struct timeval lasttime, thistime;
+  struct timezone tz = {0,0};
+  int i;
+  double diff;
+  double xfer_rate;
+#ifdef XMIT
+  double xmit_rate;
+#endif
 
-static int set     = 0;
-static int set_val = -1;
-static int get     = 0;
+  cmon = xmalloc(sizeof(struct cyclades_control) * numfiles);
 
-static int set_def     = 0;
-static int set_def_val = -1;
-static int get_def     = 0;
+  if(signal(SIGINT, summary)||
+     signal(SIGQUIT, summary)||
+     signal(SIGTERM, summary)) {
+    err(EXIT_FAILURE, _("cannot set signal handler"));
+  }
+  if(gettimeofday(&lasttime,&tz)) {
+    err(EXIT_FAILURE, _("gettimeofday failed"));
+  }
+  for(i = optind; i < argc; i ++) {
+    cmon_index = i - optind;
+    cmon[cmon_index].cfile = open(argv[i], O_RDONLY);
+    if(-1 == cmon[cmon_index].cfile) {
+      err(EXIT_FAILURE, _("cannot open %s"),argv[i]);
+    }
+    if(ioctl(cmon[cmon_index].cfile, CYGETMON, &cmon[cmon_index].c)) {
+      err(EXIT_FAILURE, _("cannot issue CYGETMON on %s"),argv[i]);
+    }
+    summary(-1);
+    if(ioctl(cmon[cmon_index].cfile, CYGETTHRESH, &threshold_value)) {
+      err(EXIT_FAILURE, _("cannot get threshold for %s"),argv[i]);
+    }
+    if(ioctl(cmon[cmon_index].cfile, CYGETTIMEOUT, &timeout_value)) {
+      err(EXIT_FAILURE, _("cannot get timeout for %s"),argv[i]);
+    }
+  }
+  while(1) {
+    sleep(interval);
 
-static int set_time = 0;
-static int set_time_val = -1;
+    if(gettimeofday(&thistime,&tz)) {
+      err(EXIT_FAILURE, _("gettimeofday failed"));
+    }
+    diff = dtime(&thistime, &lasttime);
+    mvtime(&lasttime, &thistime);
 
-static int set_def_time = 0;
-static int set_def_time_val = -1;
+    for(i = optind; i < argc; i ++) {
+      cmon_index = i - optind;
+      if(ioctl(cmon[cmon_index].cfile, CYGETMON, &cywork)) {
+        err(EXIT_FAILURE, _("cannot issue CYGETMON on %s"),argv[i]);
+      }
+      if(ioctl(cmon[cmon_index].cfile, CYGETTHRESH, &threshold_value)) {
+        err(EXIT_FAILURE, _("cannot get threshold for %s"),argv[i]);
+      }
+      if(ioctl(cmon[cmon_index].cfile, CYGETTIMEOUT, &timeout_value)) {
+        err(EXIT_FAILURE, _("cannot get timeout for %s"),argv[i]);
+      }
 
+      xfer_rate = cywork.char_count/diff;
+#ifdef XMIT
+      xmit_rate = cywork.send_count/diff;
+#endif
 
-int main(int argc, char *argv[]) {
+      if((*threshold_value) != cmon[cmon_index].threshold_value ||
+        (*timeout_value) != cmon[cmon_index].timeout_value) {
+       summary(-2);
+       /* Note that the summary must come before the setting of */
+       /* threshold_value */
+       cmon[cmon_index].threshold_value = (*threshold_value);
+       cmon[cmon_index].timeout_value = (*timeout_value);
+      } else {
+       /* Don't record this first cycle after change */
+       if(xfer_rate > cmon[cmon_index].maxtran)
+         cmon[cmon_index].maxtran = xfer_rate;
+#ifdef XMIT
+       if(xmit_rate > cmon[cmon_index].maxxmit)
+          cmon[cmon_index].maxxmit = xmit_rate;
+#endif
+       if(cmon[cmon_index].maxmax < 0 ||
+          cywork.char_max > (unsigned long) cmon[cmon_index].maxmax)
+         cmon[cmon_index].maxmax = cywork.char_max;
+      }
 
-  struct timeval lasttime, thistime;
-  struct timezone tz = {0,0};
-  double diff;
+#ifdef XMIT
+      printf(_("%s: %lu ints, %lu/%lu chars; fifo: %lu thresh, %lu tmout, "
+              "%lu max, %lu now\n"),
+            argv[i],
+            cywork.int_count,cywork.char_count,cywork.send_count,
+            threshold_value,timeout_value,
+            cywork.char_max,cywork.char_last);
+      printf(_("   %f int/sec; %f rec, %f send (char/sec)\n"),
+            cywork.int_count/diff,
+            xfer_rate,
+            xmit_rate);
+#else
+      printf(_("%s: %lu ints, %lu chars; fifo: %lu thresh, %lu tmout, "
+              "%lu max, %lu now\n"),
+            argv[i],
+            cywork.int_count,cywork.char_count,
+            threshold_value,timeout_value,
+            cywork.char_max,cywork.char_last);
+      printf(_("   %f int/sec; %f rec (char/sec)\n"),
+            cywork.int_count/diff,
+            xfer_rate);
+#endif
+      memcpy(&cmon[cmon_index].c, &cywork, sizeof (struct cyclades_monitor));
+    }
+  }
+  free(cmon);
+  return;
+}
+
+int main(int argc, char *argv[]) {
+  int query   = 0;
+  int interval = 1;
+  int set     = 0;
+  int set_val = -1;
+  int get     = 0;
+  int set_def     = 0;
+  int set_def_val = -1;
+  int get_def     = 0;
+  int set_time = 0;
+  int set_time_val = -1;
+  int set_def_time = 0;
+  int set_def_time_val = -1;
   int errflg = 0;
   int file;
   int numfiles;
-  struct cyclades_monitor cywork;
   
   int i;
   unsigned long threshold_value;
   unsigned long timeout_value;
-  double xfer_rate;
-#ifdef XMIT
-  double xmit_rate;
-#endif
   
   static const struct option longopts[] = {
     {"set-threshold", required_argument, NULL, 's'},
@@ -338,106 +440,7 @@ int main(int argc, char *argv[]) {
 
   if(!query) return EXIT_SUCCESS;      /* must have been something earlier */
 
-  /* query stuff after this line */
-  
-  cmon = xmalloc(sizeof(struct cyclades_control) * numfiles);
-
-  if(signal(SIGINT, summary)||
-     signal(SIGQUIT, summary)||
-     signal(SIGTERM, summary)) {
-    err(EXIT_FAILURE, _("cannot set signal handler"));
-  }
-  if(gettimeofday(&lasttime,&tz)) {
-    err(EXIT_FAILURE, _("gettimeofday failed"));
-  }
-  for(i = optind; i < argc; i ++) {
-    cmon_index = i - optind;
-    cmon[cmon_index].cfile = open(argv[i], O_RDONLY);
-    if(-1 == cmon[cmon_index].cfile) {
-      err(EXIT_FAILURE, _("cannot open %s"),argv[i]);
-    }
-    if(ioctl(cmon[cmon_index].cfile, CYGETMON, &cmon[cmon_index].c)) {
-      err(EXIT_FAILURE, _("cannot issue CYGETMON on %s"),argv[i]);
-    }
-    summary(-1);
-    if(ioctl(cmon[cmon_index].cfile, CYGETTHRESH, &threshold_value)) {
-      err(EXIT_FAILURE, _("cannot get threshold for %s"),argv[i]);
-    }
-    if(ioctl(cmon[cmon_index].cfile, CYGETTIMEOUT, &timeout_value)) {
-      err(EXIT_FAILURE, _("cannot get timeout for %s"),argv[i]);
-    }
-  }
-  while(1) {
-    sleep(interval);
-    
-    if(gettimeofday(&thistime,&tz)) {
-      err(EXIT_FAILURE, _("gettimeofday failed"));
-    }
-    diff = dtime(&thistime, &lasttime);
-    mvtime(&lasttime, &thistime);
-
-    for(i = optind; i < argc; i ++) {
-      cmon_index = i - optind;
-      if(ioctl(cmon[cmon_index].cfile, CYGETMON, &cywork)) {
-        err(EXIT_FAILURE, _("cannot issue CYGETMON on %s"),argv[i]);
-      }
-      if(ioctl(cmon[cmon_index].cfile, CYGETTHRESH, &threshold_value)) {
-        err(EXIT_FAILURE, _("cannot get threshold for %s"),argv[i]);
-      }
-      if(ioctl(cmon[cmon_index].cfile, CYGETTIMEOUT, &timeout_value)) {
-        err(EXIT_FAILURE, _("cannot get timeout for %s"),argv[i]);
-      }
-
-      xfer_rate = cywork.char_count/diff;
-#ifdef XMIT
-      xmit_rate = cywork.send_count/diff;
-#endif
-
-      if(threshold_value != cmon[cmon_index].threshold_value ||
-        timeout_value != cmon[cmon_index].timeout_value) {
-       summary(-2);
-       /* Note that the summary must come before the setting of */
-       /* threshold_value */
-       cmon[cmon_index].threshold_value = threshold_value;      
-       cmon[cmon_index].timeout_value = timeout_value;  
-      } else {
-       /* Don't record this first cycle after change */
-       if(xfer_rate > cmon[cmon_index].maxtran) 
-         cmon[cmon_index].maxtran = xfer_rate;
-#ifdef XMIT
-       if(xmit_rate > cmon[cmon_index].maxxmit)
-          cmon[cmon_index].maxxmit = xmit_rate;
-#endif
-       if(cmon[cmon_index].maxmax < 0 ||
-          cywork.char_max > (unsigned long) cmon[cmon_index].maxmax)
-         cmon[cmon_index].maxmax = cywork.char_max;
-      }
-
-#ifdef XMIT
-      printf(_("%s: %lu ints, %lu/%lu chars; fifo: %lu thresh, %lu tmout, "
-              "%lu max, %lu now\n"),
-            argv[i],
-            cywork.int_count,cywork.char_count,cywork.send_count,
-            threshold_value,timeout_value,
-            cywork.char_max,cywork.char_last);
-      printf(_("   %f int/sec; %f rec, %f send (char/sec)\n"),
-            cywork.int_count/diff,
-            xfer_rate,
-            xmit_rate);
-#else
-      printf(_("%s: %lu ints, %lu chars; fifo: %lu thresh, %lu tmout, "
-              "%lu max, %lu now\n"),
-            argv[i],
-            cywork.int_count,cywork.char_count,
-            threshold_value,timeout_value,
-            cywork.char_max,cywork.char_last);
-      printf(_("   %f int/sec; %f rec (char/sec)\n"),
-            cywork.int_count/diff,
-            xfer_rate);
-#endif
-      memcpy(&cmon[cmon_index].c, &cywork, sizeof (struct cyclades_monitor));
-    }
-  }
+  query_tty_stats(argc, argv, interval, numfiles, &threshold_value, &timeout_value);
 
   return EXIT_SUCCESS;
 }