From a55cc53b3fcb049b0cf8ccceaa5577c862cfb37e Mon Sep 17 00:00:00 2001 From: seb Date: Sat, 23 Feb 2013 16:38:10 +0100 Subject: [PATCH] mpstat and sar now stop and displays its average stats when it receives SIGINT (crtl/c). mpstat and sar/sadc now catch SIGINT (ctrl/c) and display their average statistics when received. --- CHANGES | 4 ++++ mpstat.c | 34 +++++++++++++++++++++++++++++++--- pidstat.c | 4 ++-- sadc.c | 35 ++++++++++++++++++++++++++++++++--- sar.c | 31 +++++++++++++++++++++++++++++-- 5 files changed, 98 insertions(+), 10 deletions(-) diff --git a/CHANGES b/CHANGES index 7314e22..fa9aa44 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,10 @@ Changes: xxxx/xx/xx: Version 10.1.4 - Sebastien Godard (sysstat 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 orange.fr) * Changed network counters (used by sar -n {DEV | EDEV }) to diff --git a/mpstat.c b/mpstat.c index cbb4edd..67f9edc 100644 --- a/mpstat.c +++ b/mpstat.c @@ -1,6 +1,6 @@ /* * mpstat: per-processor statistics - * (C) 2000-2012 by Sebastien GODARD (sysstat orange.fr) + * (C) 2000-2013 by Sebastien GODARD (sysstat 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); diff --git a/pidstat.c b/pidstat.c index a5a6f66..8c2fa9c 100644 --- 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 a9ed2ff..5f6674d 100644 --- a/sadc.c +++ b/sadc.c @@ -1,6 +1,6 @@ /* * sadc: system activity data collector - * (C) 1999-2012 by Sebastien GODARD (sysstat orange.fr) + * (C) 1999-2013 by Sebastien GODARD (sysstat 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 425ffd1..4d10bb3 100644 --- a/sar.c +++ b/sar.c @@ -1,6 +1,6 @@ /* * sar: report system activity - * (C) 1999-2012 by Sebastien GODARD (sysstat orange.fr) + * (C) 1999-2013 by Sebastien GODARD (sysstat orange.fr) * *************************************************************************** * This program is free software; you can redistribute it and/or modify it * @@ -25,6 +25,7 @@ #include #include #include +#include #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); -- 2.40.0