Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Interoperable PHP

765 views

Published on

Interoperability with the focus on PHP. Some pure PHP details, internal details are making it interesting for regular PHP developers as well as for extension developers.

Published in: Internet
  • Be the first to comment

  • Be the first to like this

Interoperable PHP

  1. 1. Interoperable PHP “We are only as strong as we are united, as weak as we are divided.” Dambledore Anatol Belski PHP Specialist September 2015
  2. 2. Interoperable PHP Anatol Belski @weltling ab@php.net #php.pecl on efnet #winphp-dev on freenode PHP 7.0 release manager PHP core developer OSS guy
  3. 3. Interoperable PHP OSTC • PHP • Hyper-V • Azure • Samba • Docker
  4. 4. Interoperable PHP Interoperability Hardware Architecture OS Program
  5. 5. Interoperable PHP A cross-platform program is … - Hardware - OS - Architecture - … independent and can run under various constellations.
  6. 6. Interoperable PHP Topics • Filesystem • Time API • Performance • 32- vs 64-bit • Thread safety • Beyond
  7. 7. 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
  8. 8. Filesystem
  9. 9. 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
  10. 10. 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
  11. 11. Interoperable PHP Access time • ext3, ext4 and others • noatime,nodiratime in /etc/fstab • NTFS • fsutil behavior query DisableLastAccess • fsutil behavior set DisebleLastAccess 0
  12. 12. 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
  13. 13. Interoperable PHP PHP FS snippet 1 <?php var_dump(file_exists("/")); // Windows bool(true) //Linux bool(true)
  14. 14. Interoperable PHP PHP FS snippet 2 <?php var_dump(dirname('/windows/system', 2)); // Windows string(1) "“ //Linux string(1) “/"
  15. 15. 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)
  16. 16. 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)
  17. 17. Interoperable PHP Some PHP FS functions • readdir() • glob() • symlink() • relapath() • temp dir • PHP extension paths
  18. 18. Time APIs
  19. 19. Interoperable PHP System time APIs • TSC • RDTSC • RDTSCP • PM clock • HPET timer • POSIX clock • WIN8+
  20. 20. Interoperable PHP gettimeofday() • UNIX/Linux • us resolution + dependency on userspace tools • Windows • Win8+ with resolution <1us
  21. 21. Interoperable PHP Time examples in PHP • microtime() != microtime() • uniqid() is based on microtime() • usleep() • timeout handling
  22. 22. PHP Performance
  23. 23. Interoperable PHP PHP opcode caches • Opcache • WinCache • APC • APCu
  24. 24. Interoperable PHP Performance PHP way • QB extension • Zephir • Recki-ct • Memcache, etc. • Write good PHP 
  25. 25. Interoperable PHP Performance compiler way • PGO (VC++) • LTO/FDO (gcc) • Assembler • Compiler intrinsics
  26. 26. Interoperable PHP PGO Instrument • Produce an instrumented build Train • Run training scenarios Optimize • Produce optimized build using the training data
  27. 27. 64-bit
  28. 28. Interoperable PHP Do you think this works? $x = str_repeat("x", PHP_INT_MAX);
  29. 29. 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
  30. 30. 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 offsets
  31. 31. Treads vs. Processes
  32. 32. Interoperable PHP Threads Thread #3 Thread #1 Thread #2
  33. 33. Interoperable PHP Process/Thread Process single threaded stack text data heap Process multi threaded stack stack text data heap Thread 1 Thread 2
  34. 34. Interoperable PHP Treads vs processes • Thread locking • Shared memory • security • performance
  35. 35. Interoperable PHP PHP SAPIs • Apache (worker, prefork, winnt) • CGI/FastCGI/FPM (NGINX, IIS, Apache, etc.) • Embed • Many unsupported SAPIs removed in PHP7
  36. 36. Interoperable PHP Apache mpm_prefork Master Child 1 Child 2 Child n
  37. 37. 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
  38. 38. Interoperable PHP TLS ● __thread qualifier is not a standard ● __declspec(thread) is not a standard ● thread_local is C++11 standard
  39. 39. 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; }
  40. 40. Interoperable PHP TLS in PHP7 TSRM/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
  41. 41. 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
  42. 42. Interoperable PHP Some PHP core functions • mt_rand() • fork() • chroot() • setlocale()/localeconv() • symlink()/link() • fputcsv() • mail() • getenv()/putenv()
  43. 43. Interoperable PHP C APIs implementations in PHP snprintf sprintf spprintf strlcat strlcpy glob sscanf strnatcmp strtod gettimeofday usleep nanosleep select opendir readdir rewinddir flock socketpair syslog openlog closelog ...
  44. 44. Questions?
  45. 45. Thanks for your attention!
  46. 46. 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

×