File: pipes.cpp

package info (click to toggle)
icecc 1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,520 kB
  • sloc: cpp: 14,058; sh: 3,006; ansic: 767; xml: 744; makefile: 231; asm: 2
file content (62 lines) | stat: -rw-r--r-- 2,285 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 99; -*- */
/* vim: set ts=4 sw=4 et tw=99:  */
/*
 * distcc -- A simple distributed compiler system
 *
 * Copyright (C) 2002, 2003 by Martin Pool <mbp@samba.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include "pipes.h"

#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>

// Create pipe with a larger buffer, this saves context switches and latencies.
int create_large_pipe( int pipefds[ 2 ] )
{
    // On Linux, the situation seems to be as follows:
    // SO_SNDBUFFORCE should allow the largest size, but it requires
    // the CAP_NET_ADMIN privilege and on Linux we normally run without
    // privileges (except for chroot). The F_SETPIPE_SZ defaults to 1MiB
    // max size, so if available, it should be the second one.
    // The maximum for SO_SNDBUF seems to be just 256KiB, so that one goes
    // last.
#ifdef F_SETPIPE_SZ
    if( pipe( pipefds ) == 0 )
    {
        fcntl( pipefds[ 1 ], F_SETPIPE_SZ, 1048576 );
        // Do not bother checking if this succeeded.
        return 0;
    }
#endif
    // We use a socket pair instead of a pipe to get a "slightly" bigger
    // output buffer. This saves context switches and latencies.
    int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefds);
    if( ret < 0)
        return ret;

    int maxsize = 2 * 1024 * 2024;
#ifdef SO_SNDBUFFORCE
    if (setsockopt(pipefds[1], SOL_SOCKET, SO_SNDBUFFORCE, &maxsize, sizeof(maxsize)) < 0)
#endif
    {
        setsockopt(pipefds[1], SOL_SOCKET, SO_SNDBUF, &maxsize, sizeof(maxsize));
    }
    return 0;
}