[RFC PATCH] Single threaded stdio optimization
Szabolcs Nagy
szabolcs.nagy@arm.com
Tue May 16 15:42:00 GMT 2017
Locking overhead can be significant in some stdio operations
which are common in single threaded applications so it makes
sense to optimize that case.
there are two basic approaches:
1) avoid the use of expensive atomics in the lowlevellock code
while there is only one thread (x86 already implements this).
2) jump to the _unlocked variant of an stdio function in case
the file needs no locking (the process will be single threaded
until the end of the stdio function).
this patch is an incomplete implementation of 2) which is target
independent and improves stdio performance more (however it does
not affect lock usage in malloc for example).
issues not tackled:
- files with _IO_USER_LOCK flag set could use the same mechanism
which would mean less checks.
- malloc interposition is not handled yet. whenever (non-as-safe)
user code may run between flockfile and funlockfile the optimization
must be disabled in case a thread is created, i just don't know
what's the best way to detect malloc interposition at libc startup.
- i used a new libc symbol (_IO_enable_locks) that pthread_create
can call to enable the stdio locks, there might be a better way.
(abilists are not updated yet).
- stdio has various configurations that i did not test (non-linux
or non-multi-threaded setups).
my question is if this approach is acceptable or if the target
specific lowlevellock optimization (like x86 does it) preferred.
(a further performance improvement is possible if the flag check
is inlined in user code, but then the flag bit becomes abi.)
2017-05-16 Szabolcs Nagy <szabolcs.nagy@arm.com>
* libio/libio.h (_IO_FLAGS2_NEED_LOCK, _IO_need_lock): Define.
* libio/libioP.h (_IO_enable_locks): Declare.
* libio/Versions (_IO_enable_locks): New symbol.
* libio/genops.c (_IO_enable_locks): Define.
(_IO_old_init): Initialize flags2.
* include/libio.h (_IO_flockfile): Avoid locking when possible.
(_IO_funlockfile): Likewise.
* libio/fputc.c (fputc): Likewise.
* libio/putc.c (_IO_putc): Likewise.
* libio/getc.c (_IO_getc): Likewise.
* libio/getchar.c (getchar): Likewise
* nptl/pthread_create.c (__pthread_create_2_1): Enable stdio locks.
* stdio-common/reg-printf.c (__register_printf_specifier): Likewise.
* stdio-common/reg-type.c (__register_printf_type): Likwise.
* libio/iofopncook.c (_IO_fopencookie): Enable locking for the file.
* sysdeps/pthread/flockfile.c (__flockfile): Likewise.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: stdio.diff
Type: text/x-patch
Size: 7466 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20170516/86bc2502/attachment.bin>
More information about the Libc-alpha
mailing list