Skip to content

Commit 9d4a42f

Browse files
committed
Use the actual pipe size instead of 8,192
The default for linux pipes is typically something much larger than 8,192 bytes. We take advantage of this to fact to avoid multiple rounds of reading from a full pipe. The constant, `STDIO_BUF_SIZE` is removed in favor of `fcntl(F_GETPIPE_SZ)` calls for pipes / fifos, an adding `DEF_STDIO_BUF_SIZE` for the case where `stdout` is not a PIPE (perhaps character special file) using a 64K buffer to consume more data per system when possible. Signed-off-by: Peter Portante <[email protected]>
1 parent 8ae3a3c commit 9d4a42f

File tree

7 files changed

+49
-6
lines changed

7 files changed

+49
-6
lines changed

src/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define CONFIG_H
44

55
#define BUF_SIZE 8192
6-
#define STDIO_BUF_SIZE 8192
6+
#define DEF_STDOUT_BUF_SIZE 65536
77
#define CONN_SOCK_BUF_SIZE 32768
88
#define DEFAULT_SOCKET_PATH "/var/run/crio"
99
#define WIN_RESIZE_EVENT 1

src/conmon.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ int main(int argc, char *argv[])
165165
pexit("Failed to create !terminal stdin pipe");
166166

167167
mainfd_stdin = fds[1];
168+
ret = fcntl(mainfd_stdin, F_GETPIPE_SZ);
169+
if (ret < 0)
170+
pexit("main stdin pipe size determination failed");
171+
mainfd_stdin_size = (size_t)ret;
168172
workerfd_stdin = fds[0];
169173

170174
if (g_unix_set_fd_nonblocking(mainfd_stdin, TRUE, NULL) == FALSE)
@@ -175,6 +179,10 @@ int main(int argc, char *argv[])
175179
pexit("Failed to create !terminal stdout pipe");
176180

177181
mainfd_stdout = fds[0];
182+
ret = fcntl(mainfd_stdout, F_GETPIPE_SZ);
183+
if (ret < 0)
184+
pexit("main stdout pipe size determination failed");
185+
mainfd_stdout_size = (size_t)ret;
178186
workerfd_stdout = fds[1];
179187
}
180188

@@ -194,6 +202,13 @@ int main(int argc, char *argv[])
194202
pexit("Failed to create stderr pipe");
195203

196204
mainfd_stderr = fds[0];
205+
ret = fcntl(mainfd_stderr, F_GETPIPE_SZ);
206+
if (ret < 0)
207+
pexit("main stderr pipe size determination failed");
208+
mainfd_stderr_size = (size_t)ret;
209+
if ((mainfd_stdout >= 0) && (mainfd_stderr_size != mainfd_stdout_size)) {
210+
nwarn("main stderr and stdout pipe sizes don't match");
211+
}
197212
workerfd_stderr = fds[1];
198213

199214
GPtrArray *runtime_argv = configure_runtime_args(csname);

src/ctr_logging.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define _GNU_SOURCE
22
#include "ctr_logging.h"
3+
#include "globals.h"
34
#include "cli.h"
45
#include "config.h"
56
#include <ctype.h>
@@ -355,10 +356,18 @@ static int parse_priority_prefix(const char *buf, size_t buflen, int *priority,
355356
*/
356357
static int write_journald(int pipe, char *buf, size_t buflen)
357358
{
358-
static char stdout_partial_buf[STDIO_BUF_SIZE];
359+
static char *stdout_partial_buf = NULL;
359360
static size_t stdout_partial_buf_len = 0;
360-
static char stderr_partial_buf[STDIO_BUF_SIZE];
361+
static char *stderr_partial_buf = NULL;
361362
static size_t stderr_partial_buf_len = 0;
363+
size_t buf_size = (pipe == STDOUT_PIPE ? mainfd_stdout_size : mainfd_stderr_size);
364+
365+
if (stdout_partial_buf == NULL) {
366+
stdout_partial_buf = g_malloc(mainfd_stdout_size);
367+
}
368+
if (stderr_partial_buf == NULL) {
369+
stderr_partial_buf = g_malloc(mainfd_stderr_size);
370+
}
362371

363372
char *partial_buf;
364373
size_t *partial_buf_len;
@@ -388,7 +397,7 @@ static int write_journald(int pipe, char *buf, size_t buflen)
388397
/* If this is a partial line, and we have capacity to buffer it, buffer it and return.
389398
* The capacity of the partial_buf is one less than its size so that we can always add
390399
* a null terminating char later */
391-
if (buflen && partial && ((unsigned long)line_len < (STDIO_BUF_SIZE - *partial_buf_len))) {
400+
if (buflen && partial && ((unsigned long)line_len < (buf_size - *partial_buf_len))) {
392401
memcpy(partial_buf + *partial_buf_len, buf, line_len);
393402
*partial_buf_len += line_len;
394403
return 0;

src/ctr_stdio.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@ static void drain_log_buffers(stdpipe_t pipe)
112112

113113
static bool read_stdio(int fd, stdpipe_t pipe, gboolean *eof)
114114
{
115-
char buf[STDIO_BUF_SIZE];
115+
size_t buf_size = ((pipe == STDOUT_PIPE) ? mainfd_stdout_size : mainfd_stderr_size);
116+
char *buf = alloca(buf_size);
116117
ssize_t num_read = 0;
117118

118119
if (eof)
119120
*eof = false;
120121

121-
num_read = read(fd, buf, STDIO_BUF_SIZE);
122+
num_read = read(fd, buf, buf_size);
122123
if (num_read == 0) {
123124
if (eof)
124125
*eof = true;

src/ctrl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ gboolean terminal_accept_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC
7272
mainfd_stdout = dup(console.fd);
7373
if (mainfd_stdout < 0)
7474
pexit("Failed to dup console file descriptor");
75+
struct stat stat_s = {0};
76+
int ret = fstat(mainfd_stdout, &stat_s);
77+
if (ret < 0)
78+
pexit("main stdout pipe fstat() failed");
79+
if (S_ISFIFO(stat_s.st_mode)) {
80+
ret = fcntl(mainfd_stdout, F_GETPIPE_SZ);
81+
if (ret < 0)
82+
pexit("main stdout pipe size determination failed");
83+
mainfd_stdout_size = (size_t)ret;
84+
} else {
85+
mainfd_stdout_size = DEF_STDOUT_BUF_SIZE;
86+
}
7587

7688
/* Now that we have a fd to the tty, make sure we handle any pending data
7789
* that was already buffered. */

src/globals.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ int runtime_status = -1;
44
int container_status = -1;
55

66
int mainfd_stdin = -1;
7+
size_t mainfd_stdin_size = 0;
78
int mainfd_stdout = -1;
9+
size_t mainfd_stdout_size = 0;
810
int mainfd_stderr = -1;
11+
size_t mainfd_stderr_size = 0;
912

1013
int attach_socket_fd = -1;
1114
int console_socket_fd = -1;

src/globals.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ extern int runtime_status;
88
extern int container_status;
99

1010
extern int mainfd_stdin;
11+
extern size_t mainfd_stdin_size;
1112
extern int mainfd_stdout;
13+
extern size_t mainfd_stdout_size;
1214
extern int mainfd_stderr;
15+
extern size_t mainfd_stderr_size;
1316

1417
extern int attach_socket_fd;
1518
extern int console_socket_fd;

0 commit comments

Comments
 (0)