提交 22acd1e6 编写于 作者: R robottoy

Optimize fwrite in stdio API

Original fwrite call sys_writev to sync user buffer &&
file buffer to disk at same time, but user buffer may samll than file
buffer, that may reduce I/O throughout.
fix freadx internal api in fmemopen/stdin/freopen

Issue:I73VQ3
Test:libc-test, benchmark
Signed-off-by: Nrobottoy <wangyaofeng.wang@huawei.com>
Change-Id: Ic7315e0a5a0f5f18e3c3f3dd8aecbdc9af1290db
上级 dd0adf17
...@@ -2166,7 +2166,11 @@ musl_src_porting_file = [ ...@@ -2166,7 +2166,11 @@ musl_src_porting_file = [
"src/ldso/x86_64/dlvsym.s", "src/ldso/x86_64/dlvsym.s",
"src/stdio/__fdopen.c", "src/stdio/__fdopen.c",
"src/stdio/__stdio_read.c", "src/stdio/__stdio_read.c",
"src/stdio/__stdio_write.c",
"src/stdio/fread.c", "src/stdio/fread.c",
"src/stdio/fmemopen.c",
"src/stdio/freopen.c",
"src/stdio/stdin.c",
"src/internal/stdio_impl.h", "src/internal/stdio_impl.h",
"src/internal/vdso.c", "src/internal/vdso.c",
"src/time/clock_gettime.c", "src/time/clock_gettime.c",
......
...@@ -63,6 +63,8 @@ hidden off_t __stdio_seek(FILE *, off_t, int); ...@@ -63,6 +63,8 @@ hidden off_t __stdio_seek(FILE *, off_t, int);
hidden int __stdio_close(FILE *); hidden int __stdio_close(FILE *);
hidden int __fill_buffer(FILE *f); hidden int __fill_buffer(FILE *f);
hidden ssize_t __flush_buffer(FILE *f);
hidden int __toread(FILE *); hidden int __toread(FILE *);
hidden int __towrite(FILE *); hidden int __towrite(FILE *);
......
#include "stdio_impl.h"
#include <sys/uio.h>
#include <string.h>
ssize_t __flush_buffer(FILE *f)
{
ssize_t cnt = 0;
char *wbase = (char *)f->wbase;
size_t rem = f->wpos - f->wbase;
while (rem > 0) {
cnt = syscall(SYS_write, f->fd, wbase, rem);
if (cnt < 0) {
f->wpos = f->wbase = f->wend = 0;
f->flags |= F_ERR;
return cnt;
}
wbase += cnt;
rem -= cnt;
}
/* reset file buffer */
f->wend = f->buf + f->buf_size;
f->wpos = f->wbase = f->buf;
return cnt;
}
size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
{
size_t rem = len;
unsigned char *wbuf = (unsigned char *)buf;
/* flush buffer first */
ssize_t cnt = __flush_buffer(f);
if (cnt < 0) {
return 0;
}
for (;;) {
if (f->lbf < 0 && rem <= f->wend - f->wpos) {
memcpy(f->wpos, wbuf, rem);
f->wpos += rem;
return len;
}
/* write directly if
* 1. file buffer < rem
* 2. line buffer mode
*/
cnt = syscall(SYS_write, f->fd, wbuf, rem);
if (cnt < 0) {
f->wpos = f->wbase = f->wend = 0;
f->flags |= F_ERR;
return len - rem;
}
rem -= cnt;
wbuf += cnt;
if (rem == 0) {
break;
}
}
return len;
}
#include "stdio_impl.h"
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <inttypes.h>
#include "libc.h"
struct cookie {
size_t pos, len, size;
unsigned char *buf;
int mode;
};
struct mem_FILE {
FILE f;
struct cookie c;
unsigned char buf[UNGET+BUFSIZ], buf2[];
};
static off_t mseek(FILE *f, off_t off, int whence)
{
ssize_t base;
struct cookie *c = f->cookie;
if (whence>2U) {
fail:
errno = EINVAL;
return -1;
}
base = (size_t [3]){0, c->pos, c->len}[whence];
if (off < -base || off > (ssize_t)c->size-base) goto fail;
return c->pos = base+off;
}
static size_t mread(FILE *f, unsigned char *buf, size_t len)
{
struct cookie *c = f->cookie;
size_t rem = c->len - c->pos;
if (c->pos > c->len) rem = 0;
if (len > rem) {
len = rem;
f->flags |= F_EOF;
}
memcpy(buf, c->buf+c->pos, len);
c->pos += len;
rem -= len;
if (rem > f->buf_size) rem = f->buf_size;
f->rpos = f->buf;
f->rend = f->buf + rem;
memcpy(f->rpos, c->buf+c->pos, rem);
c->pos += rem;
return len;
}
static size_t mwrite(FILE *f, const unsigned char *buf, size_t len)
{
struct cookie *c = f->cookie;
size_t rem;
size_t len2 = f->wpos - f->wbase;
if (len2) {
f->wpos = f->wbase;
if (mwrite(f, f->wpos, len2) < len2) return 0;
}
if (c->mode == 'a') c->pos = c->len;
rem = c->size - c->pos;
if (len > rem) len = rem;
memcpy(c->buf+c->pos, buf, len);
c->pos += len;
if (c->pos > c->len) {
c->len = c->pos;
if (c->len < c->size) c->buf[c->len] = 0;
else if ((f->flags&F_NORD) && c->size) c->buf[c->size-1] = 0;
}
return len;
}
static int mclose(FILE *m)
{
return 0;
}
FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
{
struct mem_FILE *f;
int plus = !!strchr(mode, '+');
if (!strchr("rwa", *mode)) {
errno = EINVAL;
return 0;
}
if (!buf && size > PTRDIFF_MAX) {
errno = ENOMEM;
return 0;
}
f = malloc(sizeof *f + (buf?0:size));
if (!f) return 0;
memset(f, 0, offsetof(struct mem_FILE, buf));
f->f.cookie = &f->c;
f->f.fd = -1;
f->f.lbf = EOF;
f->f.buf = f->buf + UNGET;
f->f.buf_size = sizeof f->buf - UNGET;
if (!buf) {
buf = f->buf2;
memset(buf, 0, size);
}
f->c.buf = buf;
f->c.size = size;
f->c.mode = *mode;
if (!plus) f->f.flags = (*mode == 'r') ? F_NOWR : F_NORD;
if (*mode == 'r') f->c.len = size;
else if (*mode == 'a') f->c.len = f->c.pos = strnlen(buf, size);
else if (plus) *f->c.buf = 0;
f->f.read = mread;
f->f.readx = mread;
f->f.write = mwrite;
f->f.seek = mseek;
f->f.close = mclose;
if (!libc.threaded) f->f.lock = -1;
return __ofl_add(&f->f);
}
#include "stdio_impl.h"
#include <fcntl.h>
#include <unistd.h>
/* The basic idea of this implementation is to open a new FILE,
* hack the necessary parts of the new FILE into the old one, then
* close the new FILE. */
/* Locking IS necessary because another thread may provably hold the
* lock, via flockfile or otherwise, when freopen is called, and in that
* case, freopen cannot act until the lock is released. */
FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *restrict f)
{
int fl = __fmodeflags(mode);
FILE *f2;
FLOCK(f);
fflush(f);
if (!filename) {
if (fl&O_CLOEXEC)
__syscall(SYS_fcntl, f->fd, F_SETFD, FD_CLOEXEC);
fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC);
if (syscall(SYS_fcntl, f->fd, F_SETFL, fl) < 0) {
goto fail;
}
} else {
f2 = fopen(filename, mode);
if (!f2) goto fail;
if (f2->fd == f->fd) {
f2->fd = -1; /* avoid closing in fclose */
}
else if (__dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) {
goto fail2;
}
f->flags = (f->flags & F_PERM) | f2->flags;
f->read = f2->read;
f->readx = f2->readx;
f->write = f2->write;
f->seek = f2->seek;
f->close = f2->close;
fclose(f2);
}
FUNLOCK(f);
return f;
fail2:
fclose(f2);
fail:
fclose(f);
return NULL;
}
weak_alias(freopen, freopen64);
#include "stdio_impl.h"
#undef stdin
static unsigned char buf[BUFSIZ+UNGET];
hidden FILE __stdin_FILE = {
.buf = buf+UNGET,
.buf_size = sizeof buf-UNGET,
.fd = 0,
.flags = F_PERM | F_NOWR,
.read = __stdio_read,
.readx = __stdio_readx,
.seek = __stdio_seek,
.close = __stdio_close,
.lock = -1,
};
FILE *const stdin = &__stdin_FILE;
FILE *volatile __stdin_used = &__stdin_FILE;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册