From patchwork Tue Apr 2 13:42:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Slavomir Kaslev X-Patchwork-Id: 10881755 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3AD3617E0 for ; Tue, 2 Apr 2019 13:43:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 255952887A for ; Tue, 2 Apr 2019 13:43:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 19F1928892; Tue, 2 Apr 2019 13:43:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 89B092887A for ; Tue, 2 Apr 2019 13:43:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731888AbfDBNnE (ORCPT ); Tue, 2 Apr 2019 09:43:04 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:52037 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731703AbfDBNnE (ORCPT ); Tue, 2 Apr 2019 09:43:04 -0400 Received: by mail-wm1-f65.google.com with SMTP id 4so3479387wmf.1 for ; Tue, 02 Apr 2019 06:43:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=u2EwF0qQ8fIwZ8XAhIn8rabEJP9DpcagTnKdKR/HstQ=; b=B5nzeapPtzrUVoyF942zz11CIFoNtiDK7fnwKn3XBT0VpeRclqOJeOYu84CDc+1ZS0 BXF5IOy47eG4rye8gRvl45u2P4TMtsd6JI6OJJZGWHjAAoQnThP4sh4Wdehfvky9xmeN I6oK9Dp96p2mdVbIWaEe/kDjCbMPY/nBfBDRCKmVk1uX6KbC5T+SIe0jFJZAnTnhTfZM b4oNZFnadIKLMXhPi/tYm/WyumG8UOy0lMKb0o9+aulquOlk0OS6QYWt3yMRj7b+5LgD Jd3RyTpFqAmltRe7MVvWRnnfRKXfM/AHL98IYPnvzCVqNVPWHd3zvpzvaiLr9c/iAi6w wdcg== X-Gm-Message-State: APjAAAXAiNVcnT3veEMVU6HUHXC8mxTaOTOEDH/sC3LSTSuz+x1A9/aI HOiqZJLhtRkPnN96LkZsKn7SHpgwyA== X-Google-Smtp-Source: APXvYqxHxZP0aRqegrvFj6jNGvpjzGRY35sGGWp9gF7rp+QwZltQ+/5ZJbDT4ZIMPOUs8jY0ui8AFQ== X-Received: by 2002:a1c:9e96:: with SMTP id h144mr3801048wme.33.1554212582393; Tue, 02 Apr 2019 06:43:02 -0700 (PDT) Received: from box.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id h84sm19207083wmf.15.2019.04.02.06.43.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Apr 2019 06:43:01 -0700 (PDT) From: Slavomir Kaslev To: rostedt@goodmis.org, linux-trace-devel@vger.kernel.org Cc: slavomir.kaslev@gmail.com Subject: [RFC PATCH v8 12/13] trace-cmd: Add splice() recording from FIFO without additional pipe buffer Date: Tue, 2 Apr 2019 16:42:29 +0300 Message-Id: <20190402134230.30988-24-kaslevs@vmware.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190402134230.30988-1-kaslevs@vmware.com> References: <20190402134230.30988-1-kaslevs@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When `trace-cmd record` is reading tracing data over FIFO we can do a direct splice from the FIFO to the output file descriptor instead of doing two through an additional pipe buffer. This patch implements specialized tracecmd_recorder data transfer version for this case. Signed-off-by: Slavomir Kaslev --- include/trace-cmd/trace-cmd.h | 3 +- lib/trace-cmd/trace-recorder.c | 70 ++++++++++++++++++++++++++++------ 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h index 6a21e66..52962e9 100644 --- a/include/trace-cmd/trace-cmd.h +++ b/include/trace-cmd/trace-cmd.h @@ -270,8 +270,9 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd); enum { TRACECMD_RECORD_NOSPLICE = (1 << 0), /* Use read instead of splice */ - TRACECMD_RECORD_SNAPSHOT = (1 << 1), /* extract from snapshot */ + TRACECMD_RECORD_SNAPSHOT = (1 << 1), /* Extract from snapshot */ TRACECMD_RECORD_BLOCK = (1 << 2), /* Block on splice write */ + TRACECMD_RECORD_NOBRASS = (1 << 3), /* Splice directly without a brass pipe */ }; void tracecmd_free_recorder(struct tracecmd_recorder *recorder); diff --git a/lib/trace-cmd/trace-recorder.c b/lib/trace-cmd/trace-recorder.c index d66f112..8058d71 100644 --- a/lib/trace-cmd/trace-recorder.c +++ b/lib/trace-cmd/trace-recorder.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -26,6 +27,8 @@ # define SPLICE_F_GIFT 8 #endif +#define POLL_TIMEOUT_MS 1000 + struct tracecmd_recorder { int fd; int fd1; @@ -40,6 +43,7 @@ struct tracecmd_recorder { int pages; int count; unsigned fd_flags; + unsigned trace_fd_flags; unsigned flags; }; @@ -127,6 +131,8 @@ tracecmd_create_buffer_recorder_fd2(int fd, int fd2, int cpu, unsigned flags, if (!(recorder->flags & TRACECMD_RECORD_BLOCK)) recorder->fd_flags |= SPLICE_F_NONBLOCK; + recorder->trace_fd_flags = SPLICE_F_MOVE; + /* Init to know what to free and release */ recorder->trace_fd = -1; recorder->brass[0] = -1; @@ -171,7 +177,8 @@ tracecmd_create_buffer_recorder_fd2(int fd, int fd2, int cpu, unsigned flags, goto out_free; } - if ((recorder->flags & TRACECMD_RECORD_NOSPLICE) == 0) { + if (!(recorder->flags & (TRACECMD_RECORD_NOSPLICE | + TRACECMD_RECORD_NOBRASS))) { ret = pipe(recorder->brass); if (ret < 0) goto out_free; @@ -372,7 +379,7 @@ static long splice_data(struct tracecmd_recorder *recorder) long ret; read = splice(recorder->trace_fd, NULL, recorder->brass[1], NULL, - recorder->pipe_size, SPLICE_F_MOVE); + recorder->pipe_size, recorder->trace_fd_flags); if (read < 0) { if (errno != EAGAIN && errno != EINTR) { warning("recorder error in splice input"); @@ -399,6 +406,39 @@ static long splice_data(struct tracecmd_recorder *recorder) return total_read; } +/* + * Returns -1 on error. + * or bytes of data read. + */ +static long direct_splice_data(struct tracecmd_recorder *recorder) +{ + struct pollfd pfd = { + .fd = recorder->trace_fd, + .events = POLLIN, + }; + long read; + int ret; + + ret = poll(&pfd, 1, POLL_TIMEOUT_MS); + if (ret < 0) + return -1; + + if (!(pfd.revents | POLLIN)) + return 0; + + read = splice(recorder->trace_fd, NULL, recorder->fd, NULL, + recorder->pipe_size, recorder->trace_fd_flags); + if (read < 0) { + if (errno == EAGAIN || errno == EINTR) + return 0; + + warning("recorder error in splice input"); + return -1; + } + + return read; +} + /* * Returns -1 on error. * or bytes of data read. @@ -433,6 +473,17 @@ static long read_data(struct tracecmd_recorder *recorder) return r; } +static long move_data(struct tracecmd_recorder *recorder) +{ + if (recorder->flags & TRACECMD_RECORD_NOSPLICE) + return read_data(recorder); + + if (recorder->flags & TRACECMD_RECORD_NOBRASS) + return direct_splice_data(recorder); + + return splice_data(recorder); +} + static void set_nonblock(struct tracecmd_recorder *recorder) { long flags; @@ -440,8 +491,11 @@ static void set_nonblock(struct tracecmd_recorder *recorder) /* Do not block on reads for flushing */ flags = fcntl(recorder->trace_fd, F_GETFL); fcntl(recorder->trace_fd, F_SETFL, flags | O_NONBLOCK); + recorder->trace_fd_flags |= SPLICE_F_NONBLOCK; - /* Do not block on streams for write */ + /* Do not block on pipes for write */ + flags = fcntl(recorder->fd, F_GETFL); + fcntl(recorder->fd, F_SETFL, flags | O_NONBLOCK); recorder->fd_flags |= SPLICE_F_NONBLOCK; } @@ -455,10 +509,7 @@ long tracecmd_flush_recording(struct tracecmd_recorder *recorder) set_nonblock(recorder); do { - if (recorder->flags & TRACECMD_RECORD_NOSPLICE) - ret = read_data(recorder); - else - ret = splice_data(recorder); + ret = move_data(recorder); if (ret < 0) return ret; total += ret; @@ -503,10 +554,7 @@ int tracecmd_start_recording(struct tracecmd_recorder *recorder, unsigned long s read = 0; do { - if (recorder->flags & TRACECMD_RECORD_NOSPLICE) - ret = read_data(recorder); - else - ret = splice_data(recorder); + ret = move_data(recorder); if (ret < 0) return ret; read += ret;