All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yoshihiro YUNOMAE <yoshihiro.yunomae.ez@hitachi.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: Hidehiro Kawai <hidehiro.kawai.ez@hitachi.com>,
	Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	linux-kernel@vger.kernel.org, yrl.pp-manager.tt@hitachi.com
Subject: [RFC PATCH 06/11] [CLEANUP] trace-cmd: Split out the communication with client from process_client()
Date: Mon, 19 Aug 2013 18:46:34 +0900	[thread overview]
Message-ID: <20130819094634.26597.60441.stgit@yunodevel> (raw)
In-Reply-To: <20130819094620.26597.79499.stgit@yunodevel>

Split out the communication with client from process_client() for avoiding
duplicate codes between listen mode and virt-server mode.

Signed-off-by: Yoshihiro YUNOMAE <yoshihiro.yunomae.ez@hitachi.com>
---
 trace-listen.c |  163 ++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 116 insertions(+), 47 deletions(-)

diff --git a/trace-listen.c b/trace-listen.c
index c741fa4..f29dd35 100644
--- a/trace-listen.c
+++ b/trace-listen.c
@@ -283,22 +283,12 @@ static int open_udp(const char *node, const char *port, int *pid,
 	return num_port;
 }
 
-static void process_client(const char *node, const char *port, int fd)
+static int communicate_with_client(int fd, int *cpus, int *pagesize)
 {
-	char **temp_files;
 	char buf[BUFSIZ];
 	char *option;
-	int *port_array;
-	int *pid_array;
-	int pagesize;
-	int start_port;
-	int udp_port;
 	int options;
 	int size;
-	int cpus;
-	int cpu;
-	int pid;
-	int ofd;
 	int n, s, t, i;
 
 	/* Let the client know what we are */
@@ -308,31 +298,31 @@ static void process_client(const char *node, const char *port, int fd)
 	n = read_string(fd, buf, BUFSIZ);
 	if (n == BUFSIZ)
 		/** ERROR **/
-		return;
+		return -1;
 
-	cpus = atoi(buf);
+	*cpus = atoi(buf);
 
-	plog("cpus=%d\n", cpus);
-	if (cpus < 0)
-		return;
+	plog("cpus=%d\n", *cpus);
+	if (*cpus < 0)
+		return -1;
 
 	/* next read the page size */
 	n = read_string(fd, buf, BUFSIZ);
 	if (n == BUFSIZ)
 		/** ERROR **/
-		return;
+		return -1;
 
-	pagesize = atoi(buf);
+	*pagesize = atoi(buf);
 
-	plog("pagesize=%d\n", pagesize);
-	if (pagesize <= 0)
-		return;
+	plog("pagesize=%d\n", *pagesize);
+	if (*pagesize <= 0)
+		return -1;
 
 	/* Now the number of options */
 	n = read_string(fd, buf, BUFSIZ);
 	if (n == BUFSIZ)
 		/** ERROR **/
-		return;
+		return -1;
 
 	options = atoi(buf);
 
@@ -341,18 +331,18 @@ static void process_client(const char *node, const char *port, int fd)
 		n = read_string(fd, buf, BUFSIZ);
 		if (n == BUFSIZ)
 			/** ERROR **/
-			return;
+			return -1;
 		size = atoi(buf);
 		/* prevent a client from killing us */
 		if (size > MAX_OPTION_SIZE)
-			return;
+			return -1;
 		option = malloc_or_die(size);
 		do {
 			t = size;
 			s = 0;
 			s = read(fd, option+s, t);
 			if (s <= 0)
-				return;
+				return -1;
 			t -= s;
 			s = size - t;
 		} while (t);
@@ -361,18 +351,53 @@ static void process_client(const char *node, const char *port, int fd)
 		free(option);
 		/* do we understand this option? */
 		if (!s)
-			return;
+			return -1;
 	}
 
 	if (use_tcp)
 		plog("Using TCP for live connection\n");
 
-	/* Create the client file */
+	return 0;
+}
+
+static int create_client_file(const char *node, const char *port)
+{
+	char buf[BUFSIZ];
+	int ofd;
+
 	snprintf(buf, BUFSIZ, "%s.%s:%s.dat", output_file, node, port);
 
 	ofd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0644);
 	if (ofd < 0)
 		pdie("Can not create file %s", buf);
+	return ofd;
+}
+
+static void destroy_all_readers(int cpus, int *pid_array, const char *node,
+				const char *port)
+{
+	int cpu;
+
+	for (cpu = 0; cpu < cpus; cpu++) {
+		if (pid_array[cpu] > 0) {
+			kill(pid_array[cpu], SIGKILL);
+			waitpid(pid_array[cpu], NULL, 0);
+			delete_temp_file(node, port, cpu);
+			pid_array[cpu] = 0;
+		}
+	}
+}
+
+static int *create_all_readers(int cpus, const char *node, const char *port,
+			       int pagesize, int fd)
+{
+	char buf[BUFSIZ];
+	int *port_array;
+	int *pid_array;
+	int start_port;
+	int udp_port;
+	int cpu;
+	int pid;
 
 	port_array = malloc_or_die(sizeof(int) * cpus);
 	pid_array = malloc_or_die(sizeof(int) * cpus);
@@ -382,13 +407,17 @@ static void process_client(const char *node, const char *port, int fd)
 
 	/* Now create a UDP port for each CPU */
 	for (cpu = 0; cpu < cpus; cpu++) {
-		udp_port = open_udp(node, port, &pid, cpu, pagesize, start_port);
+		udp_port = open_udp(node, port, &pid, cpu,
+				    pagesize, start_port);
 		if (udp_port < 0)
 			goto out_free;
 		port_array[cpu] = udp_port;
 		pid_array[cpu] = pid;
-		/* due to some bugging finding ports, force search after last port */
-		start_port = udp_port+1;
+		/*
+		 * due to some bugging finding ports,
+		 * force search after last port
+		 */
+		start_port = udp_port + 1;
 	}
 
 	/* send the client a comma deliminated set of port numbers */
@@ -400,9 +429,20 @@ static void process_client(const char *node, const char *port, int fd)
 	/* end with null terminator */
 	write(fd, "\0", 1);
 
-	/* Now we are ready to start reading data from the client */
+	return pid_array;
+
+ out_free:
+	destroy_all_readers(cpus, pid_array, node, port);
+	return NULL;
+}
+
+static void collect_metadata_from_client(int ifd, int ofd)
+{
+	char buf[BUFSIZ];
+	int n, s, t;
+
 	do {
-		n = read(fd, buf, BUFSIZ);
+		n = read(ifd, buf, BUFSIZ);
 		if (n < 0) {
 			if (errno == EINTR)
 				continue;
@@ -411,7 +451,7 @@ static void process_client(const char *node, const char *port, int fd)
 		t = n;
 		s = 0;
 		do {
-			s = write(ofd, buf+s, t);
+			s = write(ofd, buf + s, t);
 			if (s < 0) {
 				if (errno == EINTR)
 					break;
@@ -421,18 +461,23 @@ static void process_client(const char *node, const char *port, int fd)
 			s = n - t;
 		} while (t);
 	} while (n > 0 && !done);
+}
 
-	/* wait a little to let our readers finish reading */
-	sleep(1);
+static void stop_all_readers(int cpus, int *pid_array)
+{
+	int cpu;
 
-	/* stop our readers */
 	for (cpu = 0; cpu < cpus; cpu++) {
 		if (pid_array[cpu] > 0)
 			kill(pid_array[cpu], SIGUSR1);
 	}
+}
 
-	/* wait a little to have the readers clean up */
-	sleep(1);
+static void put_together_file(int cpus, int ofd, const char *node,
+			      const char *port)
+{
+	char **temp_files;
+	int cpu;
 
 	/* Now put together the file */
 	temp_files = malloc_or_die(sizeof(*temp_files) * cpus);
@@ -441,16 +486,40 @@ static void process_client(const char *node, const char *port, int fd)
 		temp_files[cpu] = get_temp_file(node, port, cpu);
 
 	tracecmd_attach_cpu_data_fd(ofd, cpus, temp_files);
+	free(temp_files);
+}
 
- out_free:
-	for (cpu = 0; cpu < cpus; cpu++) {
-		if (pid_array[cpu] > 0) {
-			kill(pid_array[cpu], SIGKILL);
-			waitpid(pid_array[cpu], NULL, 0);
-			delete_temp_file(node, port, cpu);
-			pid_array[cpu] = 0;
-		}
-	}
+static void process_client(const char *node, const char *port, int fd)
+{
+	int *pid_array;
+	int pagesize;
+	int cpus;
+	int ofd;
+
+	if (communicate_with_client(fd, &cpus, &pagesize) < 0)
+		return;
+
+	ofd = create_client_file(node, port);
+
+	pid_array = create_all_readers(cpus, node, port, pagesize, fd);
+	if (!pid_array)
+		return;
+
+	/* Now we are ready to start reading data from the client */
+	collect_metadata_from_client(fd, ofd);
+
+	/* wait a little to let our readers finish reading */
+	sleep(1);
+
+	/* stop our readers */
+	stop_all_readers(cpus, pid_array);
+
+	/* wait a little to have the readers clean up */
+	sleep(1);
+
+	put_together_file(cpus, ofd, node, port);
+
+	destroy_all_readers(cpus, pid_array, node, port);
 }
 
 static int do_fork(int cfd)


  parent reply	other threads:[~2013-08-19  9:44 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-19  9:46 [RFC PATCH 00/11] trace-cmd: Support the feature recording trace data of guests on the host Yoshihiro YUNOMAE
2013-08-19  9:46 ` [RFC PATCH 01/11] [TRIVIAL] trace-cmd: Delete the variable iface in trace-listen Yoshihiro YUNOMAE
2013-08-19  9:46 ` [RFC PATCH 02/11] [BUGFIX] trace-cmd: Add waitpid() when recorders are destoried Yoshihiro YUNOMAE
2013-08-19  9:46 ` [RFC PATCH 03/11] [BUGFIX]trace-cmd: Quit from splice(read) if there are no data Yoshihiro YUNOMAE
2013-08-19  9:46 ` [RFC PATCH 04/11] [CLEANUP] trace-cmd: Split out the communication with listener from setup_network() Yoshihiro YUNOMAE
2013-08-19  9:46 ` [RFC PATCH 05/11] [CLEANUP] trace-cmd: Split out the connect waiting loop from do_listen() Yoshihiro YUNOMAE
2013-08-20 17:15   ` Steven Rostedt
2013-08-20 17:18   ` Steven Rostedt
2013-08-19  9:46 ` Yoshihiro YUNOMAE [this message]
2013-08-20 17:38   ` [RFC PATCH 06/11] [CLEANUP] trace-cmd: Split out the communication with client from process_client() Steven Rostedt
2013-08-19  9:46 ` [RFC PATCH 07/11] [CLEANUP] trace-cmd: Split out binding a port and fork reader from open_udp() Yoshihiro YUNOMAE
2013-08-20 17:49   ` Steven Rostedt
2013-08-26  1:48     ` Yoshihiro YUNOMAE
2013-08-26 14:37       ` Steven Rostedt
2013-08-27  8:08         ` Yoshihiro YUNOMAE
2013-08-19  9:46 ` [RFC PATCH 08/11] trace-cmd: Apply the trace-msg protocol for communication between a server and clients Yoshihiro YUNOMAE
2013-08-20 17:56   ` Steven Rostedt
2013-08-26  1:50     ` Yoshihiro YUNOMAE
2013-08-26 15:11       ` Steven Rostedt
2013-08-27 10:23         ` Yoshihiro YUNOMAE
2013-08-27 13:05           ` Steven Rostedt
2013-08-28 11:30             ` Yoshihiro YUNOMAE
2013-08-28 14:42               ` Steven Rostedt
2013-08-29  1:57                 ` Yoshihiro YUNOMAE
2013-08-19  9:46 ` [RFC PATCH 09/11] trace-cmd: Use poll(2) to wait for a message Yoshihiro YUNOMAE
2013-08-19  9:46 ` [RFC PATCH 10/11] trace-cmd: Add virt-server mode for a virtualization environment Yoshihiro YUNOMAE
2013-08-19  9:46 ` [RFC PATCH 11/11] trace-cmd: Add --virt option for record mode Yoshihiro YUNOMAE
2013-08-20 16:00 ` [RFC PATCH 00/11] trace-cmd: Support the feature recording trace data of guests on the host Steven Rostedt
2013-08-26  1:46   ` Yoshihiro YUNOMAE
2013-08-26 14:22     ` Steven Rostedt
2013-08-27  8:07       ` Yoshihiro YUNOMAE
2013-08-27 13:00         ` Steven Rostedt
2013-08-28 11:31           ` Yoshihiro YUNOMAE

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130819094634.26597.60441.stgit@yunodevel \
    --to=yoshihiro.yunomae.ez@hitachi.com \
    --cc=hidehiro.kawai.ez@hitachi.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=rostedt@goodmis.org \
    --cc=yrl.pp-manager.tt@hitachi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.