rfc:tls
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:tls [2008/08/24 20:01] – typo lbarnaud | rfc:tls [2008/08/26 19:04] – . lbarnaud | ||
---|---|---|---|
Line 20: | 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 34: | 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 | + | 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 |
+ | |||
+ | Native TLS can be enabled | ||
===== Second patch ===== | ===== Second patch ===== | ||
Line 59: | Line 61: | ||
</ | </ | ||
- | This needs a few instructions compared to the original | + | This needs less instructions compared to the current |
<code c> | <code c> | ||
Line 66: | 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; | ||
Line 74: | Line 76: | ||
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-__thread-tls%% | + | Native TLS can be enabled with %%--with-tsrm-native-tls%%. |
- | For the second patch, the switch | + | 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 89: | 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. For instance, on Linux/ | + | 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. For instance, on Linux/ |
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 97: | 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 107: | 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. |
rfc/tls.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1