Portable PHP
-
Upload
weltling -
Category
Technology
-
view
1.024 -
download
0
Transcript of Portable PHP
![Page 1: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/1.jpg)
Portable PHP“All for one, one for all.”
Alexandre Dumas
Anatol BelskiPHP SpecialistNovember 2015
![Page 2: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/2.jpg)
Interoperable PHP
Anatol Belski
@weltling
#php.pecl@efnet
#winphp-dev@freenode
PHP 7.0 release manager
PHP core developer
OSS guy
![Page 3: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/3.jpg)
Interoperable PHP
OSTC
• PHP
• Hyper-V
• Azure
• Samba
• Docker
![Page 4: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/4.jpg)
![Page 5: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/5.jpg)
Interoperable PHP
Interoperability
Hardware
Architecture
OS
Program
![Page 6: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/6.jpg)
Interoperable PHP
A cross-platform program is …
- Hardware
- OS
- Architecture
- …
independent and can run under various constellations.
![Page 7: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/7.jpg)
Interoperable PHP
Topics
• Filesystem
• Time API
• Performance
• 32- vs 64-bit
• Thread safety
• Beyond
![Page 8: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/8.jpg)
Interoperable PHP
Brief UNIX/Windows diff
UNIX/Linux
• gcc
• many libs
• processes
• C99
• Ext*, reiserFS, HFS, etc.
• UNIX permissions
Windows
• vc++
• core API + OSS libs
• threads
• C99 starting with VC12
• NTFS, FAT
• ACLs
![Page 9: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/9.jpg)
Filesystem
![Page 10: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/10.jpg)
Interoperable PHP
File permissions
UNIX/Linux
• chmod +x file
• chmod -r file
• chmod ugo-r file
• chmod +r file
• no equivalent
• no equivalent
• chmod +t somedir
Windows
• no equivalent
• icacls file /deny nick:(R)
• no equivalent
• icacls file /grant nick:(R)
• icacls file /grant nick:(GR,GW)
• icacls file /inheritance:e
• no equivalent
![Page 11: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/11.jpg)
Interoperable PHP
File/process permissions
• UNIX/Linux• chmod(), chown(), etc.
• process permissions are handled same way as file ones
• Windows• icacls
• impersonation
• Unmapped UNIX users built-in SID mapping
![Page 12: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/12.jpg)
Interoperable PHP
Access time
• ext3, ext4 and others• noatime,nodiratime in /etc/fstab
• NTFS• fsutil behavior query DisableLastAccess
• fsutil behavior set DisebleLastAccess 0
![Page 13: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/13.jpg)
Interoperable PHP
NTFS streams
C:\tmp> echo hello > foo.txt
C:\tmp>echo world > foo.txt:bar.txt
C:\tmp>dir /r foo.txt
Volume in drive C is SYSTEM
Volume Serial Number is AE0A-76BD
Directory of C:\tmp
08/31/2015 11:50 PM 8 foo.txt
8 foo.txt:bar.txt:$DATA
1 File(s) 8 bytes
0 Dir(s) 59,944,050,688 bytes free
![Page 14: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/14.jpg)
Interoperable PHP
PHP FS snippet 1
<?php
var_dump(file_exists("/"));
// Windowsbool(true)
//Linuxbool(true)
![Page 15: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/15.jpg)
Interoperable PHP
PHP FS snippet 2
<?php
var_dump(dirname('/windows/system', 2));
// Windowsstring(1) "\“
//Linuxstring(1) “/"
![Page 16: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/16.jpg)
Interoperable PHP
PHP FS snippet 3
<?php
$f = fopen("file.txt", "w");
var_dump(unlink("file.txt"));
// Windows
Warning: unlink(file.txt): Permission denied in Command line code on line 1
bool(false)
//Linux
bool(true)
![Page 17: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/17.jpg)
Interoperable PHP
PHP FS snippet 4
<?php
var_dump(fopen("some/directory", "r"));
// Windows
Warning: fopen(some/directory): failed to open stream: Permission denied in Command line code on line 1
bool(false)
//Linux
resource(5) of type (stream)
![Page 18: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/18.jpg)
Interoperable PHP
Some PHP FS functions
• readdir()
• glob()
• symlink()
• relapath()
• temp dir
• PHP extension paths
![Page 19: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/19.jpg)
Time APIs
![Page 20: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/20.jpg)
Interoperable PHP
System time APIs
• TSC• RDTSC
• RDTSCP
• PM clock
• HPET timer
• POSIX clock
• WIN8+
![Page 21: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/21.jpg)
Interoperable PHP
gettimeofday()
• UNIX/Linux• us resolution + dependency on userspace tools
• Windows• Win8+ with resolution <1us
![Page 22: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/22.jpg)
Interoperable PHP
Time examples in PHP
• microtime() != microtime()
• uniqid() is based on usleep()
• usleep()
• timeout handling
![Page 23: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/23.jpg)
Interoperable PHP
Timeout handling
<?php
set_time_limit(3);
/* do some work */
$until = microtime(true) + 2;
while($until > microtime(true));
sleep(1);
/* do some work */
$until = microtime(true) + 1;
while($until > microtime(true));
cho “reached here”;
![Page 24: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/24.jpg)
PHP Performance
![Page 25: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/25.jpg)
Interoperable PHP
PHP opcode caches
• Opcache
• WinCache
• APC
• APCu
![Page 26: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/26.jpg)
Interoperable PHP
Performance PHP way
• QB extension
• Zephir
• Recki-ct
• Memcache, etc.
• Write good PHP
![Page 27: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/27.jpg)
Interoperable PHP
Performance compiler way
• PGO (VC++)
• LTO/FDO (gcc)
• Assembler
• Compiler intrinsics
![Page 28: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/28.jpg)
Interoperable PHP
PGO
Instrument
• Produce an instrumented build
Train
• Run training scenarios
Optimize
• Produce optimized build using the training data
![Page 29: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/29.jpg)
Interoperable PHP
PGO commands
• configure –enable-pgi
• nmake
• x64\Release\php.exe run-training-script.php
• nmake clean
• configure –enable-pgo
• nmake
![Page 30: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/30.jpg)
Interoperable PHP
FDO commands
• make clean
• make prof-gen
• sapi/cli/php run-training-script.php
• make prof-clean
• make prof-use
![Page 31: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/31.jpg)
64-bit
![Page 32: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/32.jpg)
Interoperable PHP
Do you think this works?
$x = str_repeat("x", PHP_INT_MAX);
![Page 33: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/33.jpg)
Interoperable PHP
64- vs 32-bit in PHP
64-bit
• In general slower
• 9.22337204 × 109 GB RAM addressable
• 64-bit integers
• LFS
• 64-bit string length
32-bit
• In general faster
• Less than 2 GB RAM addressable
• 32-bit integers
• 32-bit file operations
• 32-bit string length
![Page 34: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/34.jpg)
Interoperable PHP
Types in PHP7
• zend_long for integers
• zend_ulong for numeric hashes
• size_t for string length
• zend_off_t for file offsets
• zend_stat_t for file status information
• See UPGRADING for more info
![Page 35: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/35.jpg)
Interoperable PHP
64-bit affecting some functions
• stat()
• lstat()
• filesize()
• substr()
• ftell()
• fseek()
![Page 36: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/36.jpg)
Treads vs. Processes
![Page 37: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/37.jpg)
Interoperable PHP
Process
Program code
OS
Processor core #n
![Page 38: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/38.jpg)
Interoperable PHP
Threads
Thread #3
Thread #1
Thread #2
![Page 39: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/39.jpg)
Interoperable PHP
Process/Thread
Process single threaded
stack
text
data
heap
Process multi threaded
stack
stack
text
data
heap
Thread 1
Thread 2
![Page 40: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/40.jpg)
Interoperable PHP
Treads vs processes
• Thread locking
• Shared memory
• security
• performance
![Page 41: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/41.jpg)
Thread 1
Thread 2
static __thread int i = 0; static int j = 0; void start_thread(void *dummy) { char hello[6]; char *world; memcpy(hello, "hello", 5); world = malloc(sizeof(char) * 5); memcpy(world, "world", 5); i++; } int main(int argc, char **argv) { pthread_t t0, t1; rc = pthread_create(&t0, NULL, start_thread, NULL); rc = pthread_create(&t1, NULL, start_thread, NULL); return 0; }
![Page 42: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/42.jpg)
Interoperable PHP
PHP SAPIs
• Apache (worker, prefork, winnt)
• CGI/FastCGI/FPM (NGINX, IIS, Apache, etc.)
• Embed
• Many unsupported SAPIs removed in PHP7
![Page 43: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/43.jpg)
Interoperable PHP
Apache mpm_prefork
Master
Child 1
Child 2
Child n
![Page 44: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/44.jpg)
Interoperable PHP
Apache mpm_worker
Master
Child 1
Child 2
Child n
\\\\\\\\\\\v
Thread 1
Thread 2
Thread n
\\\\\\\\\\\v
\\\\\\\\\\\v
Thread 1
Thread 2
Thread n
\\\\\\\\\\\v
\\\\\\\\\\\v
\\\\\\\\\\\v
Thread 1
Thread 2
Thread n
\\\\\\\\\\\v
\\\\\\\\\\\v
\\\\\\\\\\\v
\\\\\\\\\\\v
![Page 45: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/45.jpg)
Interoperable PHP
Apache mpm_winnt
\\\\\\\\\\\v
Master Child
Thread 1
Thread 2
Thread n
\\\\\\\\\\\v
\\\\\\\\\\\v\\\\\\\\\\\v
![Page 46: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/46.jpg)
Interoperable PHP
void ***tsrm_ls in PHP 5
![Page 47: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/47.jpg)
Interoperable PHP
TS layer in PHP 7
• Global thread specific pointer per binary unit
• Optional special setup to cache TS resource
• No additional tsrm_ls arguments in functions
![Page 48: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/48.jpg)
Interoperable PHP
TLS
● __thread qualifier is not a standard
● __declspec(thread) is not a standard
● thread_local is C++11 standard (not in PHP)
● pthread_setspecific, TlsSetValue, etc.
![Page 49: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/49.jpg)
Interoperable PHP
TLS types in PHP 7TSRM/TSRM.h
#ifdef TSRM_WIN32# define TSRM_TLS __declspec(thread)#else# define TSRM_TLS __thread#endif
Zend/zend_types.h
#ifdef ZTS#define ZEND_TLS static TSRM_TLS#define ZEND_EXT_TLS TSRM_TLS#else#define ZEND_TLS static#define ZEND_EXT_TLS#endif
![Page 50: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/50.jpg)
Interoperable PHP
TS or NTS with PHP?
TS
• Less RAM consuming
• Faster startup
• Less library compatibility
• More complexity
• Slower processing
NTS
• More RAM consuming
• Slower startup
• More library compatibility
• Less complexity
• Faster processing
![Page 51: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/51.jpg)
Interoperable PHP
Some PHP core functions
• mt_rand()
• fork()
• chroot()
• setlocale()/localeconv()
• symlink()/link()
• fputcsv()
• mail()
• getenv()/putenv()
![Page 52: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/52.jpg)
Interoperable PHP
C APIs implementations in PHP
snprintf sprintf spprintf strlcat strlcpyglob sscanf strnatcmp strtodgettimeofday usleep nanosleepselect opendir readdir rewinddirflock socketpair syslog openlogcloselog ...
![Page 53: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/53.jpg)
Questions?
![Page 54: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/54.jpg)
Thanks for your attention!
![Page 55: Portable PHP](https://reader034.fdocuments.fr/reader034/viewer/2022050614/588100f41a28abc3368b7463/html5/thumbnails/55.jpg)
Interoperable PHP
Links
• https://msdn.microsoft.com/en-us/library/e7k32f4k.aspx
• http://blogs.msdn.com/b/vcblog/archive/2013/05/06/speeding-up-php-performance-for-your-application-using-profile-guided-optimization-pgo.aspx
• https://msdn.microsoft.com/en-us/library/windows/desktop/aa364404%28v=vs.85%29.aspx
• https://wiki.php.net/rfc/removal_of_dead_sapis
• https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7
• https://wiki.php.net/rfc/native-tls
• https://wiki.php.net/rfc/size_t_and_int64_next