rfc:tls
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:tls [2008/08/24 15:06] – . lbarnaud | rfc:tls [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Request for Comments: Native TLS for globals in ZTS ====== | ====== Request for Comments: Native TLS for globals in ZTS ====== | ||
- | * Version: | + | * Version: |
- | * Date: 2008- | + | * Date: 2008-08-24 |
* Author: Arnaud Le Blanc < | * Author: Arnaud Le Blanc < | ||
* Status: Under Discussion | * Status: Under Discussion | ||
- | * First Published at: http://wiki.php.net/rfc/tls | + | * First Published at: http://marc.info/?l=php-internals& |
+ | * Initial patch: http://gist.github.com/659731 | ||
+ | * Current patch: http:// | ||
Currently ZTS builds are slower than non-ZTS builds. This RFC is about avoiding some of the major overhead of ZTS builds by using native thread local storage. | Currently ZTS builds are slower than non-ZTS builds. This RFC is about avoiding some of the major overhead of ZTS builds by using native thread local storage. | ||
Line 12: | Line 14: | ||
Currently the way globals work forces to pass a thread-local-storage pointer across function calls, which involves some overhead. Also, not all functions get the pointer as argument and need to use TSRMLS_FETCH(), | Currently the way globals work forces to pass a thread-local-storage pointer across function calls, which involves some overhead. Also, not all functions get the pointer as argument and need to use TSRMLS_FETCH(), | ||
- | ===== Propositions | + | ===== Initial patch ===== |
The first proposed patch makes each global a native TLS variable so that accessing them is as simple as global_name-> | The first proposed patch makes each global a native TLS variable so that accessing them is as simple as global_name-> | ||
Line 18: | Line 20: | ||
Results for bench.php: | Results for bench.php: | ||
- | ^ non-PIC | + | ^ non-PIC |
- | |non-ZTS | + | |non-ZTS |
- | |ZTS unpatched | + | |ZTS unpatched |
- | |ZTS patched | + | |ZTS patched |
- | |ZTS patched and static globals|3.8s| | + | |ZTS patched and static globals|3.8s| | |
- | ^ PIC | + | ^ PIC |
- | |non-ZTS | + | |non-ZTS |
- | |ZTS | + | |ZTS |
- | |ZTS patched, static globals, dynamic TLS|4.8s| | + | |ZTS patched, static globals, dynamic TLS|4.8s |
So the patch made ZTS builds mostly as fast as non-ZTS builds. | So the patch made ZTS builds mostly as fast as non-ZTS builds. | ||
Line 32: | Line 34: | ||
Unfortunately the TLS model used in these tests was the static model, which is restrictive, | Unfortunately the TLS model used in these tests was the static model, which is restrictive, | ||
- | Even if this patch can still be used with the dynamic model (it actually greatly improves the performance of ZTS-PIC builds), it can not be used as-is when building non-PIC code (the dynamic model cannot be used with non-PIC code). | + | For the PHP module to be able to be loaded at runtime in Apache or an other server with this patch enabled, it has to be built with the dynamic |
- | The second patch is based on some research I made to see how various | + | Native TLS can be enabled with %%--with-tsrm-__thread-tls%% or %%--with-tsrm-full__thread-tls%%. The last declares globals statically instead of making them pointers. |
+ | |||
+ | ===== Second patch ===== | ||
+ | |||
+ | The second patch is based on some research I made on various TLS implementations. | ||
In fact mostly all implementations I tested (Linux, FreeBSD, Solaris) allocate a surplus of TLS memory especially to allow to dlopen() libraries using static TLS. This memory being allocated in addition to any TLS memory needed by libraries loaded before program startup, it is guaranteed that this memory is always available and is reserved for this case. | In fact mostly all implementations I tested (Linux, FreeBSD, Solaris) allocate a surplus of TLS memory especially to allow to dlopen() libraries using static TLS. This memory being allocated in addition to any TLS memory needed by libraries loaded before program startup, it is guaranteed that this memory is always available and is reserved for this case. | ||
Line 55: | Line 61: | ||
</ | </ | ||
- | This needs a few instructions compared to the original | + | This needs less instructions compared to the current |
<code c> | <code c> | ||
Line 62: | Line 68: | ||
</ | </ | ||
- | This change is also enabled when not using native TLS too, but tsrm_ls needs to be a void** instead of a void*: | + | This change is also enabled when not using native TLS, but tsrm_ls needs to be a void%%**%% instead of a void*: |
<code c> | <code c> | ||
void **tsrm_ls; | void **tsrm_ls; | ||
- | (*tsrm_ls_cache | + | (*tsrm_ls |
</ | </ | ||
Results: | Results: | ||
- | ^ Results | + | ^ Results |
- | |ZTS |5.2s| | + | |non-ZTS |
- | |ZTS-patched, | + | |ZTS |5.2s |4.7s |
- | |ZTS-patched, | + | |ZTS-patched, |
+ | |ZTS-patched, | ||
+ | |||
+ | Native TLS can be enabled with %%--with-tsrm-native-tls%%. | ||
+ | |||
+ | So we have two patches: | ||
+ | |||
+ | * The first one will work only with position independent code, and is the faster on targets where this is the default or when comparing only to PIC builds. At least Debian builds PHP --with-pic, and I guess this is the case on other distributions too. | ||
+ | * The second one does not requires to build PIC code, can not fully take profit of TLS, but is the faster at least on IA-32. | ||
+ | |||
+ | ===== Windows ===== | ||
+ | |||
+ | Dynamically loaded DLLs can use TLS starting with Windows Vista and Server 2008. But there is a restriction: | ||
===== TLS internals ===== | ===== TLS internals ===== | ||
Line 81: | Line 99: | ||
==== Static model ==== | ==== Static model ==== | ||
- | Each block is allocated at a fixed (linker-defined) offset from an address specific to each thread. As this address can be accessed very quickly, this allows very quick access to each TLS block. | + | Each block is allocated at a fixed (loader-defined) offset from an address specific to each thread. As this address can be accessed very quickly, this allows very quick access to each TLS block. |
The way the static model works requires that the memory needed by each TLS variable to be allocated before program startup. This means that the static model can not be used in shared libraries loaded at runtime. | The way the static model works requires that the memory needed by each TLS variable to be allocated before program startup. This means that the static model can not be used in shared libraries loaded at runtime. | ||
Line 89: | Line 107: | ||
Linux, Solaris, FreeBSD, Windows. | Linux, Solaris, FreeBSD, Windows. | ||
- | Linux, Solaris and FreeBSD implementations allocate a fixed amount of surplus memory especially to allow dynamically loaded libraries to use the static model. Linux allocates 1664 bytes, FreeBSD 64 and Solaris 512. This amount of memory is always allocated in addition of the memory allocated for TLS before program startup, and is always available (this memory can be used only by dlopen()ed modules using static TLS). | + | Linux, Solaris and FreeBSD implementations allocate a fixed amount of surplus memory especially to allow dynamically loaded libraries to use the static model. Linux allocates 1664 bytes, FreeBSD 64 and Solaris 512. This amount of memory is always allocated in addition of the memory allocated for TLS before program startup, and is always available (this memory can be used only by dlopen()ed modules using static TLS). These behaviors are undocumented (except by comments in Linux and FreeBSD loaders/ |
On GCC this model can be selected by using -ftls-model=initial-exec. On SunStudio: -xthreadvar=no%dynamic. For both, this model is the default one when building non-PIC code. | On GCC this model can be selected by using -ftls-model=initial-exec. On SunStudio: -xthreadvar=no%dynamic. For both, this model is the default one when building non-PIC code. | ||
Line 99: | Line 117: | ||
=== Implementation === | === Implementation === | ||
- | Linux, Solaris, FreeBSD, Windows Vista. | + | Linux, Solaris, FreeBSD, Windows Vista/Server 2008. |
+ | |||
+ | Windows Vista and Server 2008 can use TLS in DLLs loaded using LoadLibrary(), but TLS symbols cannot be exported, which means that only the DLL where a TLS variable is declared can refer to this variable. | ||
On GCC this model can be selected by using -ftls-model=general-dynamic. On SunStudio: -xthreadvar=dynamic. For both, this is the default when building PIC code. | On GCC this model can be selected by using -ftls-model=general-dynamic. On SunStudio: -xthreadvar=dynamic. For both, this is the default when building PIC code. | ||
Line 112: | Line 132: | ||
===== Code changes ===== | ===== Code changes ===== | ||
+ | |||
+ | ==== Declaring globals ==== | ||
The current way of declaring a global is a follows: | The current way of declaring a global is a follows: | ||
Line 117: | Line 139: | ||
extern ts_rsrc_id my_global_id; | extern ts_rsrc_id my_global_id; | ||
ts_rsrc_id my_global_id; | ts_rsrc_id my_global_id; | ||
- | ts_allocate_id(& | + | ts_allocate_id(& |
</ | </ | ||
Line 123: | Line 145: | ||
<code c> | <code c> | ||
TSRMG_DH(type, | TSRMG_DH(type, | ||
- | TSRMG_D(type, | + | TSRMG_D(type, |
- | TSRMG_ALLOCATE(my_global_id, | + | TSRMG_ALLOCATE(my_global_id, |
</ | </ | ||
All this is already done by the patch for code in the Zend Engine and in /php-src. | All this is already done by the patch for code in the Zend Engine and in /php-src. | ||
- | ==== Extension | + | ==== Extensions |
There is no changes needed for extensions as long as they use the extension-specific macros for declaring globals (as this is done by default for extensions created with ext_skel). | There is no changes needed for extensions as long as they use the extension-specific macros for declaring globals (as this is done by default for extensions created with ext_skel). | ||
Line 139: | Line 161: | ||
TSRM does some sort of JIT initialization of thread data, relying on the fact that TSRMLS_FETCH() calls ts_resource_ex, | TSRM does some sort of JIT initialization of thread data, relying on the fact that TSRMLS_FETCH() calls ts_resource_ex, | ||
+ | ==== #ifdef ZTS ==== | ||
+ | As the patch avoids passing tsrm_ls across function calls, #ifdef ZTS is not anymore relevant to check that. | ||
+ | The new PASS_TSRMLS macro is now defined when tsrm_ls needs to be passed across function calls. For instance this is needed by ZEND_ATTRIBUTE_FORMAT and some other places. |
rfc/tls.1219590412.txt.gz · Last modified: 2017/09/22 13:28 (external edit)