This is the mail archive of the systemtap@sourceware.org mailing list for the systemtap project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RESEND][ PATCH v2 1/3] Signal-based file switching support for relay/ring buffer.


* runtime/staprun/relay.c (switch_outfile): New function for file switching.
  (reader_thread): Don't assign empty_handler to SIGUSR2, and switch output
  file when receiving signal(SIGUSR2) on ppoll.
  (switchfile_handler): Send SIGUSR2 signal to reader threads for file
  switching.
  (init_relayfs): Assign switchfile_handler to SIGUSR2.
---

 NEWS                    |    3 ++
 runtime/staprun/relay.c |   83 +++++++++++++++++++++++++++++++++++------------
 staprun.8.in            |   33 +++++++++++++++++++
 3 files changed, 97 insertions(+), 22 deletions(-)

diff --git a/NEWS b/NEWS
index b31eaaf..4fbf59e 100644
--- a/NEWS
+++ b/NEWS
@@ -44,6 +44,9 @@
        SYSTEMTAP_RUNTIME, SYSTEMTAP_TAPSET, SYSTEMTAP_DEBUGINFO_PATH
   - nss and nss-tools are required to use this feature.
 
+- Support output file switching by SIGUSR2. Users can command running
+  stapio to switch output file by sending SIGUSR2.
+
 - Memory consumption for scripts involving many uprobes has been
   dramatically reduced.
 
diff --git a/runtime/staprun/relay.c b/runtime/staprun/relay.c
index f4aa139..0c00923 100644
--- a/runtime/staprun/relay.c
+++ b/runtime/staprun/relay.c
@@ -15,6 +15,7 @@
 int out_fd[NR_CPUS];
 static pthread_t reader[NR_CPUS];
 static int relay_fd[NR_CPUS];
+static int switch_file[NR_CPUS];
 static int bulkmode = 0;
 static volatile int stop_threads = 0;
 static time_t *time_backlog[NR_CPUS];
@@ -107,11 +108,25 @@ static int open_outfile(int fnum, int cpu, int remove_file)
 	return 0;
 }
 
+static int switch_outfile(int cpu, int *fnum)
+{
+	int remove_file = 0;
+
+	dbug(3, "thread %d switching file\n", cpu);
+	close(out_fd[cpu]);
+	*fnum += 1;
+	if (fnum_max && *fnum >= fnum_max)
+		remove_file = 1;
+	if (open_outfile(*fnum, cpu, remove_file) < 0) {
+		perr("Couldn't open file for cpu %d, exiting.", cpu);
+		return -1;
+	}
+	return 0;
+}
+
 /**
  *	reader_thread - per-cpu channel buffer reader
  */
-static void empty_handler(int __attribute__((unused)) sig) { /* do nothing */  }
-
 static void *reader_thread(void *data)
 {
         char buf[131072];
@@ -119,10 +134,8 @@ static void *reader_thread(void *data)
         struct pollfd pollfd;
 	struct timespec tim = {.tv_sec=0, .tv_nsec=200000000}, *timeout = &tim;
 	sigset_t sigs;
-	struct sigaction sa;
 	off_t wsize = 0;
 	int fnum = 0;
-	int remove_file = 0;
 
 	sigemptyset(&sigs);
 	sigaddset(&sigs,SIGUSR2);
@@ -131,11 +144,6 @@ static void *reader_thread(void *data)
 	sigfillset(&sigs);
 	sigdelset(&sigs,SIGUSR2);
 	
-	sa.sa_handler = empty_handler;
-        sa.sa_flags = 0;
-	sigemptyset(&sa.sa_mask);
-        sigaction(SIGUSR2, &sa, NULL);
-
 	if (bulkmode) {
 		cpu_set_t cpu_mask;
 		CPU_ZERO(&cpu_mask);
@@ -156,33 +164,39 @@ static void *reader_thread(void *data)
 	pollfd.events = POLLIN;
 
         do {
+		dbug(3, "thread %d start ppoll\n", cpu);
                 rc = ppoll(&pollfd, 1, timeout, &sigs);
+		dbug(3, "thread %d end ppoll:%d\n", cpu, rc);
                 if (rc < 0) {
 			dbug(3, "cpu=%d poll=%d errno=%d\n", cpu, rc, errno);
-                        if (errno != EINTR) {
+			if (errno == EINTR) {
+				if (stop_threads)
+					break;
+				if (switch_file[cpu]) {
+					switch_file[cpu] = 0;
+					if (switch_outfile(cpu, &fnum) < 0)
+						goto error_out;
+					wsize = 0;
+				}
+			} else {
 				_perr("poll error");
 				goto error_out;
-                        }
+			}
                 }
+
 		while ((rc = read(relay_fd[cpu], buf, sizeof(buf))) > 0) {
-			wsize += rc;
 			/* Switching file */
-			if (fsize_max && wsize > fsize_max) {
-				close(out_fd[cpu]);
-				fnum++;
-				if (fnum_max && fnum == fnum_max)
-					remove_file = 1;
-				if (open_outfile(fnum, cpu, remove_file) < 0) {
-					perr("Couldn't open file for cpu %d, exiting.", cpu);
+			if (fsize_max && wsize + rc > fsize_max) {
+				if (switch_outfile(cpu, &fnum) < 0)
 					goto error_out;
-				}
-				wsize = rc;
+				wsize = 0;
 			}
 			if (write(out_fd[cpu], buf, rc) != rc) {
 				if (errno != EPIPE)
 					perr("Couldn't write to output %d for cpu %d, exiting.", out_fd[cpu], cpu);
 				goto error_out;
 			}
+			wsize += rc;
 		}
         } while (!stop_threads);
 	dbug(3, "exiting thread for cpu %d\n", cpu);
@@ -195,6 +209,25 @@ error_out:
 	return(NULL);
 }
 
+static void switchfile_handler(int sig)
+{
+	int i;
+	if (stop_threads)
+		return;
+	for (i = 0; i < ncpus; i++)
+		if (reader[i] && switch_file[i]) {
+			dbug(2, "file switching is progressing, signal ignored.\n", sig);
+			return;
+		}
+	for (i = 0; i < ncpus; i++) {
+		if (reader[i]) {
+			switch_file[i] = 1;
+			pthread_kill(reader[i], SIGUSR2);
+		} else
+			break;
+	}
+}
+
 /**
  *	init_relayfs - create files and threads for relayfs processing
  *
@@ -308,6 +341,12 @@ int init_relayfs(void)
 		
 	}
 	if (!load_only) {
+		struct sigaction sa;
+
+		sa.sa_handler = switchfile_handler;
+		sa.sa_flags = 0;
+		sigemptyset(&sa.sa_mask);
+		sigaction(SIGUSR2, &sa, NULL);
 		dbug(2, "starting threads\n");
 		for (i = 0; i < ncpus; i++) {
 			if (pthread_create(&reader[i], NULL, reader_thread,
@@ -327,7 +366,7 @@ void close_relayfs(void)
 	stop_threads = 1;
 	dbug(2, "closing\n");
 	for (i = 0; i < ncpus; i++) {
-		if (reader[i]) 
+		if (reader[i])
 			pthread_kill(reader[i], SIGUSR2);
 		else
 			break;
diff --git a/staprun.8.in b/staprun.8.in
index 5fe2e7f..5bf2ec6 100644
--- a/staprun.8.in
+++ b/staprun.8.in
@@ -138,6 +138,39 @@ To reattach to a kernel module, the
 .I staprun
 .B \-A
 option would be used.
+
+.SH FILE SWITCHING BY SIGNAL
+After the
+.I staprun
+launched the
+.I stapio
+, users can command it to switch output file to next file when it
+outputs to file(s) (running staprun with
+.B \-o
+option) by sending a
+.B SIGUSR2
+signal to the
+.I stapio
+process. When it receives SIGUSR2, it will switch output file to
+new file with suffix 
+.I .N
+where N is the sequential number.
+For example,
+.PP
+\& $ staprun \-o foo ...
+.PP
+outputs trace logs to 
+.I foo
+and if it receives
+.B SIGUSR2
+signal, it switches output to
+.I foo.1
+file. And receiving
+.B SIGUSR2
+again, it switches to 
+.I foo.2
+file.
+
 .SH SAFETY AND SECURITY
 Systemtap is an administrative tool.  It exposes kernel internal data
 structures and potentially private user information.  See the 


-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America), Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]