mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jovi Zhangwei <jovi.zhangwei@gmail.com>
To: Ingo Molnar <mingo@redhat.com>, Steven Rostedt <rostedt@goodmis.org>
Cc: linux-kernel@vger.kernel.org,
	Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Andi Kleen <andi@firstfloor.org>,
	Jovi Zhangwei <jovi.zhangwei@gmail.com>
Subject: [PATCH v2 23/29] ktap: add ring buffer reader(tools/ktap/kp_reader.c)
Date: Fri, 28 Mar 2014 10:45:18 -0400	[thread overview]
Message-ID: <1396017924-7754-24-git-send-email-jovi.zhangwei@gmail.com> (raw)
In-Reply-To: <1396017924-7754-1-git-send-email-jovi.zhangwei@gmail.com>

This is ktap ring buffer consumer, a thread poll content from
'/sys/kernel/debug/ktap/trace_pipe_%pid' debugfs file.

Signed-off-by: Jovi Zhangwei <jovi.zhangwei@gmail.com>
---
 tools/ktap/kp_reader.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 106 insertions(+)
 create mode 100644 tools/ktap/kp_reader.c

diff --git a/tools/ktap/kp_reader.c b/tools/ktap/kp_reader.c
new file mode 100644
index 0000000..103940e
--- /dev/null
+++ b/tools/ktap/kp_reader.c
@@ -0,0 +1,106 @@
+/*
+ * reader.c - ring buffer reader in userspace
+ *
+ * This file is part of ktap by Jovi Zhangwei.
+ *
+ * Copyright (C) 2012-2014 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
+ *
+ * ktap is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * ktap is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/poll.h>
+#include <sys/signal.h>
+#include <fcntl.h>
+#include <pthread.h>
+
+#define MAX_BUFLEN  131072
+#define PATH_MAX 128
+
+#define handle_error(str) do { perror(str); exit(-1); } while(0)
+
+void sigfunc(int signo)
+{
+	/* should not not reach here */
+}
+
+static void block_sigint()
+{
+	sigset_t mask;
+
+	sigemptyset(&mask);
+	sigaddset(&mask, SIGINT);
+
+	pthread_sigmask(SIG_BLOCK, &mask, NULL);
+}
+
+static void *reader_thread(void *data)
+{
+	char buf[MAX_BUFLEN];
+	char filename[PATH_MAX];
+	const char *output = data; 
+	int failed = 0, fd, out_fd, len;
+
+	block_sigint();
+
+	if (output) {
+		out_fd = open(output, O_CREAT | O_WRONLY | O_TRUNC,
+					S_IRUSR|S_IWUSR);
+		if (out_fd < 0) {
+			fprintf(stderr, "Cannot open output file %s\n", output);
+			return NULL;
+		}
+	} else
+		out_fd = 1;
+
+	sprintf(filename, "/sys/kernel/debug/ktap/trace_pipe_%d", getpid());
+
+ open_again:
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		usleep(10000);
+
+		if (failed++ == 10) {
+			fprintf(stderr, "Cannot open file %s\n", filename);
+			return NULL;
+		}
+		goto open_again;
+	}
+
+	while ((len = read(fd, buf, sizeof(buf))) > 0)
+		write(out_fd, buf, len);
+
+	close(fd);
+	close(out_fd);
+
+	return NULL;
+}
+
+int kp_create_reader(const char *output)
+{
+	pthread_t reader;
+
+	signal(SIGINT, sigfunc);
+
+	if (pthread_create(&reader, NULL, reader_thread, (void *)output) < 0)
+		handle_error("pthread_create reader_thread failed\n");
+
+	return 0;
+}
+
-- 
1.8.1.4


  parent reply	other threads:[~2014-03-29 15:27 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-28 14:44 [RFC PATCH v2 00/29] ktap: A lightweight dynamic tracing tool for Linux Jovi Zhangwei
2014-03-28 14:44 ` [PATCH v2 01/29] ktap: add tools/ktap/README.md file Jovi Zhangwei
2014-03-28 14:44 ` [PATCH v2 02/29] ktap: add ktap tutorial(tools/ktap/doc/tutorial.md) Jovi Zhangwei
2014-03-28 14:44 ` [PATCH v2 03/29] ktap: add sample scripts(tools/ktap/samples/*) Jovi Zhangwei
2014-03-28 14:44 ` [PATCH v2 04/29] ktap: add basic ktap types definition(include/uapi/ktap/ktap_types.h) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 05/29] ktap: add bytecode definition(include/uapi/ktap/ktap_bc.h) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 06/29] ktap: add ktap_arch.h and error header file(include/uapi/ktap/) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 07/29] ktap: add kernel module main entry(kernel/trace/ktap/ktap.[c|h]) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 08/29] ktap: add bytecode reader(kernel/trace/ktap/kp_bcread.[c|h]) Jovi Zhangwei
2014-03-30  2:47   ` Andi Kleen
2014-03-30  8:02     ` Jovi Zhangwei
2014-03-30 17:17       ` Andi Kleen
2014-03-31  2:05         ` Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 09/29] ktap: add bytecode execution engine(kernel/trace/ktap/kp_vm.[c|h]) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 10/29] ktap: add string handling code(kernel/trace/ktap/kp_[str|mempool].[c|h]) Jovi Zhangwei
2014-03-30  3:50   ` Andi Kleen
2014-03-30  9:12     ` Jovi Zhangwei
2014-03-30 17:19       ` Andi Kleen
2014-03-31  2:35         ` Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 11/29] ktap: add table handling code(kernel/trace/ktap/kp_tab.[c|h]) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 12/29] ktap: add generic object handling code(kernel/trace/ktap/kp_obj.[c|h]) Jovi Zhangwei
2014-03-30  3:56   ` Andi Kleen
2014-03-30  8:14     ` Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 13/29] ktap: add ring buffer handling code(kernel/trace/ktap/kp_transport.[c|h]) Jovi Zhangwei
2014-03-30  3:58   ` Andi Kleen
2014-03-30  7:40     ` Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 14/29] ktap: add events management(kernel/trace/ktap/kp_events.[c|h]) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 15/29] ktap: add built-in functions and library(kernel/trace/ktap/lib_*.c) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 16/29] ktap: add amalgamation build(kernel/trace/ktap/amalg.c) Jovi Zhangwei
2014-03-31  2:17   ` Li Zefan
2014-03-31  3:22     ` Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 17/29] ktap: add Makefile for kernel module(kernel/trace/ktap/Makefile) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 18/29] ktap: add Kconfig(kernel/trace/ktap/Kconfig) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 19/29] ktap: add main file for ktap binary(tools/ktap/kp_main.c) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 20/29] ktap: add compiler(tools/ktap/kp_[lex|parse].[c|h]) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 21/29] ktap: add symbol handling code(tools/ktap/symbol.[c|h]) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 22/29] ktap: add events parse code(tools/ktap/kp_parse_events.c) Jovi Zhangwei
2014-03-28 14:45 ` Jovi Zhangwei [this message]
2014-03-28 14:45 ` [PATCH v2 24/29] ktap: add bytecode writer(tools/ktap/kp_bcwrite.c) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 25/29] ktap: add userspace util(tools/ktap/kp_util.c) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 26/29] ktap: add userspace binary Makefile(tools/ktap/Makefile) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 27/29] ktap: add testsuite and benchmark(tools/ktap/test/*) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 28/29] ktap: add vim syntax file(tools/ktap/vim/*) Jovi Zhangwei
2014-03-28 14:45 ` [PATCH v2 29/29] ktap: add COPYRIGHT file(tools/ktap/COPYRIGHT) Jovi Zhangwei
2014-03-30  1:00 ` [RFC PATCH v2 00/29] ktap: A lightweight dynamic tracing tool for Linux Andi Kleen
2014-03-30  9:18   ` Jovi Zhangwei

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=1396017924-7754-24-git-send-email-jovi.zhangwei@gmail.com \
    --to=jovi.zhangwei@gmail.com \
    --cc=andi@firstfloor.org \
    --cc=fweisbec@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®