diff mbox series

[rt-tests,v1,10/12] sigwaittest: Move statictic output into print_stat()

Message ID 20201118190642.16006-11-dwagner@suse.de
State New
Headers show
Series Add quiet command line option | expand

Commit Message

Daniel Wagner Nov. 18, 2020, 7:06 p.m. UTC
Prepare the code to introduce the quiet command line option by moving
the statistic output code into print_stat(). We follow here the
pattern from cyclictest.

While at it replace the rather sophisticated error printing code with
a fatal(). Just fail if something is not working.

Also reorder the include headers so that project local headers follow
the system include headers.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
---
 src/sigwaittest/sigwaittest.c | 97 ++++++++++++++++-------------------
 1 file changed, 43 insertions(+), 54 deletions(-)

Comments

John Kacur Dec. 1, 2020, 7:14 a.m. UTC | #1
On Wed, 18 Nov 2020, Daniel Wagner wrote:

> Prepare the code to introduce the quiet command line option by moving

> the statistic output code into print_stat(). We follow here the

> pattern from cyclictest.

> 

> While at it replace the rather sophisticated error printing code with

> a fatal(). Just fail if something is not working.

> 

> Also reorder the include headers so that project local headers follow

> the system include headers.

> 

> Signed-off-by: Daniel Wagner <dwagner@suse.de>

> ---

>  src/sigwaittest/sigwaittest.c | 97 ++++++++++++++++-------------------

>  1 file changed, 43 insertions(+), 54 deletions(-)

> 

> diff --git a/src/sigwaittest/sigwaittest.c b/src/sigwaittest/sigwaittest.c

> index 8a5d73b14fc2..d3be2fd9508c 100644

> --- a/src/sigwaittest/sigwaittest.c

> +++ b/src/sigwaittest/sigwaittest.c

> @@ -38,10 +38,11 @@

>  #include <sys/time.h>

>  #include <linux/unistd.h>

>  #include <utmpx.h>

> +#include <pthread.h>

> +

>  #include "rt-utils.h"

>  #include "rt-get_cpu.h"

> -

> -#include <pthread.h>

> +#include "error.h"

>  

>  enum {

>  	AFFINITY_UNSPECIFIED,

> @@ -69,7 +70,6 @@ struct params {

>  	struct timeval unblocked, received, diff;

>  	pthread_t threadid;

>  	struct params *neighbor;

> -	char error[MAX_PATH * 2];

>  };

>  

>  static int mustfork;

> @@ -184,9 +184,8 @@ void *semathread(void *param)

>  					write(tracing_enabled, "0", 1);

>  					close(tracing_enabled);

>  				} else

> -					snprintf(par->error, sizeof(par->error),

> -					    "Could not access %s\n",

> -					    tracing_enabled_file);

> +					fatal("Could not access %s\n",

> +					      tracing_enabled_file);

>  				par->shutdown = 1;

>  				neighbor->shutdown = 1;

>  			}

> @@ -348,6 +347,39 @@ static void sighand(int sig)

>  	mustshutdown = 1;

>  }

>  

> +static void print_stat(FILE *fp, struct params *receiver, struct params *sender,

> +		       int verbose, int quiet)

> +{

> +	int i;

> +

> +	for (i = 0; i < num_threads; i++) {

> +		int receiver_pid, sender_pid;

> +		if (mustfork) {

> +			receiver_pid = receiver[i].pid;

> +			sender_pid = sender[i].pid;

> +		} else {

> +			receiver_pid = receiver[i].tid;

> +			sender_pid = sender[i].tid;

> +		}

> +		printf("#%1d: ID%d, P%d, CPU%d, I%ld; #%1d: ID%d, P%d, CPU%d, Cycles %d\n",

> +			i*2, receiver_pid, receiver[i].priority,

> +			receiver[i].cpu, receiver[i].delay.tv_nsec /

> +			1000, i*2+1, sender_pid, sender[i].priority,

> +			sender[i].cpu, sender[i].samples);

> +	}

> +

> +	for (i = 0; i < num_threads; i++) {

> +		if (receiver[i].mindiff == -1)

> +			printf("#%d -> #%d (not yet ready)\n", i*2+1, i*2);

> +		else

> +			printf("#%d -> #%d, Min %4d, Cur %4d, Avg %4d, Max %4d\n",

> +				i*2+1, i*2,	receiver[i].mindiff,

> +				(int) receiver[i].diff.tv_usec,

> +				(int) ((receiver[i].sumdiff /

> +					receiver[i].samples) + 0.5),

> +				receiver[i].maxdiff);

> +	}

> +}

>  

>  int main(int argc, char *argv[])

>  {

> @@ -540,56 +572,14 @@ int main(int argc, char *argv[])

>  	}

>  

>  	while (!mustshutdown) {

> -		int printed;

> -		int errorlines = 0;

> -

>  		for (i = 0; i < num_threads; i++)

>  			mustshutdown |= receiver[i].shutdown |

>  			    sender[i].shutdown;

>  

>  		if (receiver[0].samples > oldsamples || mustshutdown) {

> -			for (i = 0; i < num_threads; i++) {

> -				int receiver_pid, sender_pid;

> -				if (mustfork) {

> -					receiver_pid = receiver[i].pid;

> -					sender_pid = sender[i].pid;

> -				} else {

> -					receiver_pid = receiver[i].tid;

> -					sender_pid = sender[i].tid;

> -				}

> -				printf("#%1d: ID%d, P%d, CPU%d, I%ld; #%1d: "

> -				    "ID%d, P%d, CPU%d, Cycles %d\n",

> -				    i*2, receiver_pid, receiver[i].priority,

> -				    receiver[i].cpu, receiver[i].delay.tv_nsec /

> -				    1000, i*2+1, sender_pid, sender[i].priority,

> -				    sender[i].cpu, sender[i].samples);

> -			}

> -			for (i = 0; i < num_threads; i++) {

> -				if (receiver[i].mindiff == -1)

> -					printf("#%d -> #%d (not yet ready)\n",

> -					    i*2+1, i*2);

> -				else

> -					printf("#%d -> #%d, Min %4d, Cur %4d, "

> -					    "Avg %4d, Max %4d\n",

> -					    i*2+1, i*2,	receiver[i].mindiff,

> -					    (int) receiver[i].diff.tv_usec,

> -					    (int) ((receiver[i].sumdiff /

> -					    receiver[i].samples) + 0.5),

> -					    receiver[i].maxdiff);

> -				if (receiver[i].error[0] != '\0') {

> -					printf("%s", receiver[i].error);

> -					receiver[i].error[0] = '\0';

> -					errorlines++;

> -				}

> -				if (sender[i].error[0] != '\0') {

> -					printf("%s", sender[i].error);

> -					sender[i].error[0] = '\0';

> -					errorlines++;

> -				}

> -			}

> -			printed = 1;

> -		} else

> -			printed = 0;

> +			print_stat(stdout, receiver, sender, 0, 0);

> +			printf("\033[%dA", num_threads*2);

> +		}

>  

>  		sigemptyset(&sigset);

>  		sigaddset(&sigset, SIGTERM);

> @@ -601,11 +591,10 @@ int main(int argc, char *argv[])

>  

>  		sigemptyset(&sigset);

>  		pthread_sigmask(SIG_SETMASK, &sigset, NULL);

> -

> -		if (printed && !mustshutdown)

> -			printf("\033[%dA", num_threads*2 + errorlines);

>  	}

>  

> +	printf("\033[%dB", num_threads*2 + 2);

> +

>  	for (i = 0; i < num_threads; i++) {

>  		receiver[i].shutdown = 1;

>  		sender[i].shutdown = 1;

> -- 

> 2.29.2

> 

> 

Signed-off-by: John Kacur <jkacur@redhat.com>
diff mbox series

Patch

diff --git a/src/sigwaittest/sigwaittest.c b/src/sigwaittest/sigwaittest.c
index 8a5d73b14fc2..d3be2fd9508c 100644
--- a/src/sigwaittest/sigwaittest.c
+++ b/src/sigwaittest/sigwaittest.c
@@ -38,10 +38,11 @@ 
 #include <sys/time.h>
 #include <linux/unistd.h>
 #include <utmpx.h>
+#include <pthread.h>
+
 #include "rt-utils.h"
 #include "rt-get_cpu.h"
-
-#include <pthread.h>
+#include "error.h"
 
 enum {
 	AFFINITY_UNSPECIFIED,
@@ -69,7 +70,6 @@  struct params {
 	struct timeval unblocked, received, diff;
 	pthread_t threadid;
 	struct params *neighbor;
-	char error[MAX_PATH * 2];
 };
 
 static int mustfork;
@@ -184,9 +184,8 @@  void *semathread(void *param)
 					write(tracing_enabled, "0", 1);
 					close(tracing_enabled);
 				} else
-					snprintf(par->error, sizeof(par->error),
-					    "Could not access %s\n",
-					    tracing_enabled_file);
+					fatal("Could not access %s\n",
+					      tracing_enabled_file);
 				par->shutdown = 1;
 				neighbor->shutdown = 1;
 			}
@@ -348,6 +347,39 @@  static void sighand(int sig)
 	mustshutdown = 1;
 }
 
+static void print_stat(FILE *fp, struct params *receiver, struct params *sender,
+		       int verbose, int quiet)
+{
+	int i;
+
+	for (i = 0; i < num_threads; i++) {
+		int receiver_pid, sender_pid;
+		if (mustfork) {
+			receiver_pid = receiver[i].pid;
+			sender_pid = sender[i].pid;
+		} else {
+			receiver_pid = receiver[i].tid;
+			sender_pid = sender[i].tid;
+		}
+		printf("#%1d: ID%d, P%d, CPU%d, I%ld; #%1d: ID%d, P%d, CPU%d, Cycles %d\n",
+			i*2, receiver_pid, receiver[i].priority,
+			receiver[i].cpu, receiver[i].delay.tv_nsec /
+			1000, i*2+1, sender_pid, sender[i].priority,
+			sender[i].cpu, sender[i].samples);
+	}
+
+	for (i = 0; i < num_threads; i++) {
+		if (receiver[i].mindiff == -1)
+			printf("#%d -> #%d (not yet ready)\n", i*2+1, i*2);
+		else
+			printf("#%d -> #%d, Min %4d, Cur %4d, Avg %4d, Max %4d\n",
+				i*2+1, i*2,	receiver[i].mindiff,
+				(int) receiver[i].diff.tv_usec,
+				(int) ((receiver[i].sumdiff /
+					receiver[i].samples) + 0.5),
+				receiver[i].maxdiff);
+	}
+}
 
 int main(int argc, char *argv[])
 {
@@ -540,56 +572,14 @@  int main(int argc, char *argv[])
 	}
 
 	while (!mustshutdown) {
-		int printed;
-		int errorlines = 0;
-
 		for (i = 0; i < num_threads; i++)
 			mustshutdown |= receiver[i].shutdown |
 			    sender[i].shutdown;
 
 		if (receiver[0].samples > oldsamples || mustshutdown) {
-			for (i = 0; i < num_threads; i++) {
-				int receiver_pid, sender_pid;
-				if (mustfork) {
-					receiver_pid = receiver[i].pid;
-					sender_pid = sender[i].pid;
-				} else {
-					receiver_pid = receiver[i].tid;
-					sender_pid = sender[i].tid;
-				}
-				printf("#%1d: ID%d, P%d, CPU%d, I%ld; #%1d: "
-				    "ID%d, P%d, CPU%d, Cycles %d\n",
-				    i*2, receiver_pid, receiver[i].priority,
-				    receiver[i].cpu, receiver[i].delay.tv_nsec /
-				    1000, i*2+1, sender_pid, sender[i].priority,
-				    sender[i].cpu, sender[i].samples);
-			}
-			for (i = 0; i < num_threads; i++) {
-				if (receiver[i].mindiff == -1)
-					printf("#%d -> #%d (not yet ready)\n",
-					    i*2+1, i*2);
-				else
-					printf("#%d -> #%d, Min %4d, Cur %4d, "
-					    "Avg %4d, Max %4d\n",
-					    i*2+1, i*2,	receiver[i].mindiff,
-					    (int) receiver[i].diff.tv_usec,
-					    (int) ((receiver[i].sumdiff /
-					    receiver[i].samples) + 0.5),
-					    receiver[i].maxdiff);
-				if (receiver[i].error[0] != '\0') {
-					printf("%s", receiver[i].error);
-					receiver[i].error[0] = '\0';
-					errorlines++;
-				}
-				if (sender[i].error[0] != '\0') {
-					printf("%s", sender[i].error);
-					sender[i].error[0] = '\0';
-					errorlines++;
-				}
-			}
-			printed = 1;
-		} else
-			printed = 0;
+			print_stat(stdout, receiver, sender, 0, 0);
+			printf("\033[%dA", num_threads*2);
+		}
 
 		sigemptyset(&sigset);
 		sigaddset(&sigset, SIGTERM);
@@ -601,11 +591,10 @@  int main(int argc, char *argv[])
 
 		sigemptyset(&sigset);
 		pthread_sigmask(SIG_SETMASK, &sigset, NULL);
-
-		if (printed && !mustshutdown)
-			printf("\033[%dA", num_threads*2 + errorlines);
 	}
 
+	printf("\033[%dB", num_threads*2 + 2);
+
 	for (i = 0; i < num_threads; i++) {
 		receiver[i].shutdown = 1;
 		sender[i].shutdown = 1;