]> granicus.if.org Git - sysstat/commitdiff
mpstat and sar now stop and displays its average stats when it receives
authorseb <seb@kluane.home>
Sat, 23 Feb 2013 15:38:10 +0000 (16:38 +0100)
committerseb <seb@kluane.home>
Sat, 23 Feb 2013 15:38:10 +0000 (16:38 +0100)
SIGINT (crtl/c).

mpstat and sar/sadc now catch SIGINT (ctrl/c) and display their average
statistics when received.

CHANGES
mpstat.c
pidstat.c
sadc.c
sar.c

diff --git a/CHANGES b/CHANGES
index 7314e22b30cff26288bc23253d6f2b1a026b981d..fa9aa4494fcbb03caca5c5f061bef9c63364a04d 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -3,6 +3,10 @@ Changes:
 xxxx/xx/xx: Version 10.1.4 - Sebastien Godard (sysstat <at> orange.fr)
        * [Christophe Cerin]: pidstat now stops and displays its average
          stats when it receives SIGINT (crtl/c).
+       * mpstat now stops and displays its average stats when it
+         receives SIGINT (crtl/c).
+       * sar now stops and displays its average stats when it receives
+         SIGINT (crtl/c).
 
 2012/12/23: Version 10.1.3 - Sebastien Godard (sysstat <at> orange.fr)
        * Changed network counters (used by sar -n {DEV | EDEV }) to
index cbb4edd3af438241866d492daa1f5d0b0bd6ae4f..67f9edc862520afc02d93377d1148e4dd3ad57b3 100644 (file)
--- a/mpstat.c
+++ b/mpstat.c
@@ -1,6 +1,6 @@
 /*
  * mpstat: per-processor statistics
- * (C) 2000-2012 by Sebastien GODARD (sysstat <at> orange.fr)
+ * (C) 2000-2013 by Sebastien GODARD (sysstat <at> orange.fr)
  *
  ***************************************************************************
  * This program is free software; you can redistribute it and/or modify it *
@@ -73,6 +73,8 @@ int irqcpu_nr = 0;
 /* Nb of soft interrupts per processor */
 int softirqcpu_nr = 0;
 
+int sigint_caught = 0;
+
 /*
  ***************************************************************************
  * Print usage and exit
@@ -97,7 +99,7 @@ void usage(char *progname)
  * SIGALRM signal handler
  *
  * IN:
- * @sig        Signal number. Set to 0 for the first time, then to SIGALRM.
+ * @sig        Signal number.
  ***************************************************************************
  */
 void alarm_handler(int sig)
@@ -106,6 +108,19 @@ void alarm_handler(int sig)
        alarm(interval);
 }
 
+/*
+ ***************************************************************************
+ * SIGINT signal handler.
+ * 
+ * IN:
+ * @sig        Signal number.
+ ***************************************************************************
+ */
+void int_handler(int sig)
+{
+       sigint_caught = 1;
+}
+
 /*
  ***************************************************************************
  * Allocate stats structures and cpu bitmap.
@@ -795,6 +810,9 @@ void rw_mpstat_loop(int dis_hdr, int rows)
                       STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
        }
 
+       /* Set a handler for SIGINT */
+       signal(SIGINT, int_handler);
+
        pause();
 
        do {
@@ -844,9 +862,19 @@ void rw_mpstat_loop(int dis_hdr, int rows)
                if (count > 0) {
                        count--;
                }
+               
                if (count) {
-                       curr ^= 1;
+                       
                        pause();
+                       
+                       if (sigint_caught) {
+                               /* SIGINT signal caught => Display average stats */
+                               count = 0;
+                               printf("\n");   /* Skip "^C" displayed on screen */
+                       }
+                       else {
+                               curr ^= 1;
+                       }
                }
        }
        while (count);
index a5a6f66056b82e965db81f0312ba4b733497cfd5..8c2fa9c88496b5400d59ea7f14eb26763d37c87a 100644 (file)
--- a/pidstat.c
+++ b/pidstat.c
@@ -90,7 +90,7 @@ void usage(char *progname)
  * SIGALRM signal handler.
  *
  * IN:
- * @sig        Signal number. Set to 0 for the first time, then to SIGALRM.
+ * @sig        Signal number.
  ***************************************************************************
  */
 void alarm_handler(int sig)
@@ -1959,7 +1959,7 @@ void rw_pidstat_loop(int dis_hdr, int rows)
                if (count) {
                        
                        pause();
-                       
+       
                        if (sigint_caught) {
                                /* SIGINT signal caught => Display average stats */
                                count = 0;
diff --git a/sadc.c b/sadc.c
index a9ed2ffa85ff2b206d2f50ea4861d1f9ac62a909..5f6674de340b3931f9d7d3a15ee24e40a723df4e 100644 (file)
--- a/sadc.c
+++ b/sadc.c
@@ -1,6 +1,6 @@
 /*
  * sadc: system activity data collector
- * (C) 1999-2012 by Sebastien GODARD (sysstat <at> orange.fr)
+ * (C) 1999-2013 by Sebastien GODARD (sysstat <at> orange.fr)
  *
  ***************************************************************************
  * This program is free software; you can redistribute it and/or modify it *
@@ -59,6 +59,7 @@ long interval = 0;
 unsigned int flags = 0;
 
 int dis;
+int optz = 0;
 char timestamp[2][TIMESTAMP_LEN];
 
 struct file_header file_hdr;
@@ -204,7 +205,7 @@ void parse_sadc_S_option(char *argv[], int opt)
  * SIGALRM signal handler.
  *
  * IN:
- * @sig        Signal number. Set to 0 for the first time, then to SIGALRM.
+ * @sig        Signal number.
  ***************************************************************************
  */
 void alarm_handler(int sig)
@@ -213,6 +214,31 @@ void alarm_handler(int sig)
        alarm(interval);
 }
 
+/*
+ ***************************************************************************
+ * SIGINT signal handler.
+ * 
+ * IN:
+ * @sig        Signal number.
+ ***************************************************************************
+ */
+void int_handler(int sig)
+{
+       if (!optz) {
+               /* sadc hasn't been called by sar */
+               exit(1);
+       }
+       
+       /*
+        * When starting sar then pressing ctrl/c, SIGINT is received
+        * by sadc, not sar. So send SIGINT to sar so that average stats
+        * can be displayed.
+        */
+       if (kill(getppid(), SIGINT) < 0) {
+               exit(1);
+       }
+}
+
 /*
  ***************************************************************************
  * Display an error message.
@@ -878,6 +904,9 @@ void rw_sa_stat_loop(long count, struct tm *rectime, int stdfd, int ofd,
        char new_ofile[MAX_FILE_LEN];
 
        new_ofile[0] = '\0';
+       
+       /* Set a handler for SIGINT */
+       signal(SIGINT, int_handler);
 
        /* Main loop */
        do {
@@ -992,7 +1021,7 @@ void rw_sa_stat_loop(long count, struct tm *rectime, int stdfd, int ofd,
  */
 int main(int argc, char **argv)
 {
-       int opt = 0, optz = 0;
+       int opt = 0;
        char ofile[MAX_FILE_LEN];
        struct tm rectime;
        int stdfd = 0, ofd = -1;
diff --git a/sar.c b/sar.c
index 425ffd1ea389f185e9fc30a09de79cc327f91d27..4d10bb39160936190838f8f2b13aa6dca9206f68 100644 (file)
--- a/sar.c
+++ b/sar.c
@@ -1,6 +1,6 @@
 /*
  * sar: report system activity
- * (C) 1999-2012 by Sebastien GODARD (sysstat <at> orange.fr)
+ * (C) 1999-2013 by Sebastien GODARD (sysstat <at> orange.fr)
  *
  ***************************************************************************
  * This program is free software; you can redistribute it and/or modify it *
@@ -25,6 +25,7 @@
 #include <unistd.h>
 #include <time.h>
 #include <errno.h>
+#include <signal.h>
 
 #include "version.h"
 #include "sa.h"
@@ -77,6 +78,8 @@ char *args[MAX_ARGV_NR];
 
 extern struct activity *act[];
 
+int sigint_caught = 0;
+
 /*
  ***************************************************************************
  * Print usage title message.
@@ -174,6 +177,21 @@ void display_help(char *progname)
        exit(0);
 }
 
+/*
+ ***************************************************************************
+ * SIGINT signal handler.
+ * 
+ * IN:
+ * @sig        Signal number.
+ ***************************************************************************
+ */
+void int_handler(int sig)
+{
+       sigint_caught = 1;
+       printf("\n");   /* Skip "^C" displayed on screen */
+
+}
+
 /*
  ***************************************************************************
  * Init some structures.
@@ -1038,6 +1056,9 @@ void read_stats(void)
        /* Save the first stats collected. Will be used to compute the average */
        copy_structures(act, id_seq, record_hdr, 2, 0);
 
+       /* Set a handler for SIGINT */
+       signal(SIGINT, int_handler);
+
        /* Main loop */
        do {
 
@@ -1065,7 +1086,13 @@ void read_stats(void)
                        count--;
                }
                if (count) {
-                       curr ^= 1;
+                       if (sigint_caught) {
+                               /* SIGINT signal caught => Display average stats */
+                               count = 0;
+                       }
+                       else {
+                               curr ^= 1;
+                       }
                }
        }
        while (count);