Skip to content

Commit de5a73b

Browse files
committed
MDEV-38507: fadvise64() called on socat pipe by Mariabackup is useless overhead (ESPIPE)
Currently, posix_fadvise(..., 0, 0, POSIX_FADV_DONTNEED) is executed repeatedly after each my_write. In case of pipes, it evaluates to ESPIPE and fails. Because it executes after every write, this results in millions of redundant syscalls, introducing massive overhead. As proposed by Daniel Black, posix_fadvise with a length/size of 0 (which equates to EOF) should be invoked once when the file is opened, rather than repetitively after every write. This change moves the fadvise call to the respective _open functions for ds_local, ds_stdout and ds_tmpfile datasinks. It fails fast on pipes early on and eliminates the redundant syscall overhead for subsequent write operations.
1 parent da52af6 commit de5a73b

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

extra/mariabackup/ds_local.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ local_open(ds_ctxt_t *ctxt, const char *path,
136136
local_file = (ds_local_file_t *) (file + 1);
137137

138138
local_file->fd = fd;
139+
posix_fadvise(local_file->fd, 0, 0, POSIX_FADV_DONTNEED);
139140
local_file->init_ibd_done = 0;
140141
local_file->is_ibd = (path_len > 5) && !strcmp(fullpath + path_len - 5, ".ibd");
141142
local_file->compressed = 0;
@@ -166,9 +167,7 @@ static int write_compressed(File fd, uchar *data, size_t len, size_t pagesize)
166167
size_t n_bytes = MY_MIN(pagesize, len - written);
167168
size_t datasize= trim_binary_zeros(ptr,n_bytes);
168169
if (datasize > 0) {
169-
if (!my_write(fd, ptr, datasize, MYF(MY_WME | MY_NABP)))
170-
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
171-
else
170+
if (my_write(fd, ptr, datasize, MYF(MY_WME | MY_NABP)))
172171
return 1;
173172
}
174173
if (datasize < n_bytes) {
@@ -242,11 +241,10 @@ local_write(ds_file_t *file, const uchar *buf, size_t len)
242241
return write_compressed(fd, b, len, local_file->pagesize);
243242
}
244243

245-
if (!my_write(fd, b , len, MYF(MY_WME | MY_NABP))) {
246-
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
247-
return 0;
244+
if (my_write(fd, b , len, MYF(MY_WME | MY_NABP))) {
245+
return 1;
248246
}
249-
return 1;
247+
return 0;
250248
}
251249

252250
static

extra/mariabackup/ds_stdout.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ stdout_open(ds_ctxt_t *ctxt __attribute__((unused)),
8484
#endif
8585

8686
stdout_file->fd = my_fileno(stdout);
87+
posix_fadvise(stdout_file->fd, 0, 0, POSIX_FADV_DONTNEED);
8788

8889
file->path = (char *) stdout_file + sizeof(ds_stdout_file_t);
8990
memcpy(file->path, fullpath, pathlen);
@@ -99,12 +100,11 @@ stdout_write(ds_file_t *file, const uchar *buf, size_t len)
99100
{
100101
File fd = ((ds_stdout_file_t *) file->ptr)->fd;
101102

102-
if (!my_write(fd, buf, len, MYF(MY_WME | MY_NABP))) {
103-
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
104-
return 0;
103+
if (my_write(fd, buf, len, MYF(MY_WME | MY_NABP))) {
104+
return 1;
105105
}
106106

107-
return 1;
107+
return 0;
108108
}
109109

110110
static

extra/mariabackup/ds_tmpfile.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ tmpfile_open(ds_ctxt_t *ctxt, const char *path,
115115
tmp_file->orig_path = (char *) tmp_file + sizeof(ds_tmp_file_t);
116116

117117
tmp_file->fd = fd;
118+
posix_fadvise(tmp_file->fd, 0, 0, POSIX_FADV_DONTNEED);
118119
memcpy(tmp_file->orig_path, path, path_len);
119120

120121
/* Store the real temporary file name in file->path */
@@ -138,12 +139,11 @@ tmpfile_write(ds_file_t *file, const uchar *buf, size_t len)
138139
{
139140
File fd = ((ds_tmp_file_t *) file->ptr)->fd;
140141

141-
if (!my_write(fd, buf, len, MYF(MY_WME | MY_NABP))) {
142-
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
143-
return 0;
142+
if (my_write(fd, buf, len, MYF(MY_WME | MY_NABP))) {
143+
return 1;
144144
}
145145

146-
return 1;
146+
return 0;
147147
}
148148

149149
static int

0 commit comments

Comments
 (0)