From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.3 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 8DED51F5B1 for ; Fri, 31 Jul 2020 09:38:56 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1k1RV4-0006bP-Ss; Fri, 31 Jul 2020 09:38:50 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k1RV4-0006ao-2Y for sox-devel@lists.sourceforge.net; Fri, 31 Jul 2020 09:38:50 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=owzAZkAMPo5PPLCGAp02asfPBsZwkdVe/RrW68X74CY=; b=WY5LrKs7EvnzJeoGGSsPZ/92pW 0hYHFS6+rBKuKJdrmsbDgbVde/pyhbJ56WOOlHP7AHcqIhTSNfETsAmUcUTxqa/baNeUj5C6yKRgr SwpUZafbEJMou5OGUjfKaucREzV2tahWlmRhT855MdFMti7a9Kvbn8LSZ94/upqgHUL8=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=owzAZkAMPo5PPLCGAp02asfPBsZwkdVe/RrW68X74CY=; b=W82hR3h0wBpTv1ku3RP8wGM9Zs QieRb5ZMp8KmorR9bjTlKtTrjesQ7V2uDj5JaLzvJCocstxn2vpkRbtf7zG06GRNtwhPdBP+cmqyW Cm7rV5ULq6Y2Y/1w2afUukHwTrrMWjnZ4rGNrXKFyV/EVQ+rjROvpl/5XnlV+L19W68U=; Received: from dcvr.yhbt.net ([64.71.152.64]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1k1RUx-00FjE2-8Q for sox-devel@lists.sourceforge.net; Fri, 31 Jul 2020 09:38:44 +0000 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id E769B1F8DB; Fri, 31 Jul 2020 09:38:01 +0000 (UTC) From: Eric Wong To: =?UTF-8?q?M=C3=A5ns=20Rullg=C3=A5rd?= Date: Fri, 31 Jul 2020 09:37:54 +0000 Message-Id: <20200731093801.23548-3-normalperson@yhbt.net> In-Reply-To: <20200731093801.23548-1-normalperson@yhbt.net> References: <20200731093801.23548-1-normalperson@yhbt.net> MIME-Version: 1.0 X-Headers-End: 1k1RUx-00FjE2-8Q Subject: [PATCH RESEND 2/9] speed up "|program" inputs on Linux 2.6.35+ X-BeenThere: sox-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sox-devel@lists.sourceforge.net Cc: sox-devel@lists.sourceforge.net Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: sox-devel-bounces@lists.sourceforge.net Linux 2.6.35+ allows pipe buffer resizing via fcntl(2). When running multi-threaded SoX invocations with large buffers, the default pipe size (64K) can be too small and become a bottleneck for IPC. Increasing the pipe to the maximum allowed size reduces the amount of stalls in data flow between processes (SoX or otherwise). When using SOX_OPTS="--multi-thread --buffer 131072" on a 4-core system, a command like: sox -M "|sox $< -p $(lft_fx)" "|sox $< -p $(rgt_fx)" $@ ..can run significantly faster (10-80%) depending on the processing chain, file sizes/formats and effects in use. Before this patch, using "--buffer 131072" could be hugely detrimental to performance due to the pipe being only half the size (64K) of the SoX buffer. --- src/formats.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/formats.c b/src/formats.c index f3efe764..1da8489c 100644 --- a/src/formats.c +++ b/src/formats.c @@ -18,6 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#define _GNU_SOURCE #include "sox_i.h" #include @@ -37,6 +38,10 @@ #include #endif +#ifdef HAVE_UNISTD_H +# include +#endif + #define PIPE_AUTO_DETECT_SIZE 256 /* Only as much as we can rewind a pipe */ #define AUTO_DETECT_SIZE 4096 /* For seekable file, so no restriction */ @@ -370,6 +375,50 @@ static int xfclose(FILE * file, lsx_io_type io_type) fclose(file); } +static void incr_pipe_size(FILE *f) +{ +/* + * Linux 2.6.35 and later has the ability to expand the pipe buffer + * Try to get it as big as possible to avoid stalls when SoX itself + * is using big buffers + */ +#if defined(F_GETPIPE_SZ) && defined(F_SETPIPE_SZ) + static long max_pipe_size; + + /* read the maximum size of the pipe the first time this is called */ + if (max_pipe_size == 0) { + const char path[] = "/proc/sys/fs/pipe-max-size"; + int fd = open(path, O_RDONLY); + + max_pipe_size = -1; + if (fd >= 0) { + char buf[80]; + ssize_t r = read(fd, buf, sizeof(buf)); + + if (r > 0) { + buf[r] = 0; + max_pipe_size = strtol(buf, NULL, 10); + + /* guard against obviously wrong values on messed up systems */ + if (max_pipe_size <= PIPE_BUF || max_pipe_size > INT_MAX) + max_pipe_size = -1; + } + close(fd); + } + } + + if (max_pipe_size > PIPE_BUF) { + int fd = fileno(f); + + if (fcntl(fd, F_SETPIPE_SZ, max_pipe_size) >= 0) + lsx_debug("got pipe %ld bytes\n", max_pipe_size); + else + lsx_warn("couldn't set pipe size to %ld bytes: %s\n", + max_pipe_size, strerror(errno)); + } +#endif /* do nothing for platforms without F_{GET,SET}PIPE_SZ */ +} + static FILE * xfopen(char const * identifier, char const * mode, lsx_io_type * io_type) { *io_type = lsx_io_file; @@ -382,6 +431,7 @@ static FILE * xfopen(char const * identifier, char const * mode, lsx_io_type * i #endif f = popen(identifier + 1, POPEN_MODE); *io_type = lsx_io_pipe; + incr_pipe_size(f); #else lsx_fail("this build of SoX cannot open pipes"); #endif @@ -394,6 +444,7 @@ static FILE * xfopen(char const * identifier, char const * mode, lsx_io_type * i char * command = lsx_malloc(strlen(command_format) + strlen(identifier)); sprintf(command, command_format, identifier); f = popen(command, POPEN_MODE); + incr_pipe_size(f); free(command); *io_type = lsx_io_url; #else _______________________________________________ SoX-devel mailing list SoX-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sox-devel