SlideShare a Scribd company logo
Interoperable PHP
β€œWe are only as strong as we are united, as weak as we are divided.”
Dambledore
Anatol Belski
PHP Specialist
September 2015
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
Interoperable PHP
OSTC
β€’ PHP
β€’ Hyper-V
β€’ Azure
β€’ Samba
β€’ Docker
Interoperable PHP
Interoperable PHP
Interoperability
Hardware
Architecture
OS
Program
Interoperable PHP
A cross-platform program is …
- Hardware
- OS
- Architecture
- …
independent and can run under various
constellations.
Interoperable PHP
Topics
β€’ Filesystem
β€’ Time API
β€’ Performance
β€’ 32- vs 64-bit
β€’ Thread safety
β€’ Beyond
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
Filesystem
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
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
Interoperable PHP
Access time
β€’ ext3, ext4 and others
β€’ noatime,nodiratime in /etc/fstab
β€’ NTFS
β€’ fsutil behavior query DisableLastAccess
β€’ fsutil behavior set DisebleLastAccess 0
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
Interoperable PHP
PHP FS snippet 1
<?php
var_dump(file_exists("/"));
// Windows
bool(true)
//Linux
bool(true)
Interoperable PHP
PHP FS snippet 2
<?php
var_dump(dirname('/windows/system', 2));
// Windows
string(1) "β€œ
//Linux
string(1) β€œ/"
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)
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)
Interoperable PHP
Some PHP FS functions
β€’ readdir()
β€’ glob()
β€’ symlink()
β€’ relapath()
β€’ temp dir
β€’ PHP extension paths
Time APIs
Interoperable PHP
System time APIs
β€’ TSC
β€’ RDTSC
β€’ RDTSCP
β€’ PM clock
β€’ HPET timer
β€’ POSIX clock
β€’ WIN8+
Interoperable PHP
gettimeofday()
β€’ UNIX/Linux
β€’ us resolution + dependency on userspace tools
β€’ Windows
β€’ Win8+ with resolution <1us
Interoperable PHP
Time examples in PHP
β€’ microtime() != microtime()
β€’ uniqid() is based on microtime()
β€’ usleep()
β€’ timeout handling
PHP Performance
Interoperable PHP
PHP opcode caches
β€’ Opcache
β€’ WinCache
β€’ APC
β€’ APCu
Interoperable PHP
Performance PHP way
β€’ QB extension
β€’ Zephir
β€’ Recki-ct
β€’ Memcache, etc.
β€’ Write good PHP 
Interoperable PHP
Performance compiler way
β€’ PGO (VC++)
β€’ LTO/FDO (gcc)
β€’ Assembler
β€’ Compiler intrinsics
Interoperable PHP
PGO
Instrument
β€’ Produce an instrumented build
Train
β€’ Run training scenarios
Optimize
β€’ Produce optimized build using the training data
64-bit
Interoperable PHP
Do you think this works?
$x = str_repeat("x", PHP_INT_MAX);
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
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
Treads vs. Processes
Interoperable PHP
Threads
Thread #3
Thread
#1
Thread
#2
Interoperable PHP
Process/Thread
Process single
threaded
stack
text
data
heap
Process multi
threaded
stack
stack
text
data
heap
Thread 1
Thread 2
Interoperable PHP
Treads vs processes
β€’ Thread locking
β€’ Shared memory
β€’ security
β€’ performance
Interoperable PHP
PHP SAPIs
β€’ Apache (worker, prefork, winnt)
β€’ CGI/FastCGI/FPM (NGINX, IIS, Apache, etc.)
β€’ Embed
β€’ Many unsupported SAPIs removed in PHP7
Interoperable PHP
Apache mpm_prefork
Master
Child 1
Child 2
Child n
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
Interoperable PHP
TLS
● __thread qualifier is not a standard
● __declspec(thread) is not a standard
● thread_local is C++11 standard
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;
}
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
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
Interoperable PHP
Some PHP core functions
β€’ mt_rand()
β€’ fork()
β€’ chroot()
β€’ setlocale()/localeconv()
β€’ symlink()/link()
β€’ fputcsv()
β€’ mail()
β€’ getenv()/putenv()
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 ...
Questions?
Thanks for your
attention!
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

More Related Content

PDF
A Look at Command Line Swift
PDF
Fluentd at HKOScon
PDF
OSDC 2016 - Ingesting Logs with Style by Pere Urbon-Bayes
PPTX
TakeDownCon Rocket City: WebShells by Adrian Crenshaw
PDF
Egress-Assess and Owning Data Exfiltration
PDF
Infrastructure as code might be literally impossible part 2
Β 
PDF
Kernel Recipes 2016 - Would an ABI changes visualization tool be useful to Li...
PDF
Keynote - Fluentd meetup v14
A Look at Command Line Swift
Fluentd at HKOScon
OSDC 2016 - Ingesting Logs with Style by Pere Urbon-Bayes
TakeDownCon Rocket City: WebShells by Adrian Crenshaw
Egress-Assess and Owning Data Exfiltration
Infrastructure as code might be literally impossible part 2
Β 
Kernel Recipes 2016 - Would an ABI changes visualization tool be useful to Li...
Keynote - Fluentd meetup v14

What's hot (18)

PDF
Fluentd Hacking Guide at RubyKaigi 2014
PDF
Python on FreeBSD
PDF
Experimental dtrace
PDF
Fluentd v0.14 Overview
PPT
Php intro
PDF
Package manages and Puppet - PuppetConf 2015
Β 
PPTX
Php core. get rid of bugs and contribute
PDF
Infrastructure as code might be literally impossible
Β 
ODP
about Debian "squeeze" @201002 OSC Tokyospring
PDF
AV Evasion with the Veil Framework
PPTX
Geek Sync | Using PowerShell with Python and SQL Server
PPTX
Your Inner Sysadmin - MidwestPHP 2015
PDF
Veil-Ordnance
PDF
Http2.0 Guide 2013-08-14 #http2study
PDF
OpenZFS Channel programs
PDF
OpenZFS at LinuxCon
PPT
Linux operating system by Quontra Solutions
PPTX
PowerShell - Be A Cool Blue Kid
Fluentd Hacking Guide at RubyKaigi 2014
Python on FreeBSD
Experimental dtrace
Fluentd v0.14 Overview
Php intro
Package manages and Puppet - PuppetConf 2015
Β 
Php core. get rid of bugs and contribute
Infrastructure as code might be literally impossible
Β 
about Debian "squeeze" @201002 OSC Tokyospring
AV Evasion with the Veil Framework
Geek Sync | Using PowerShell with Python and SQL Server
Your Inner Sysadmin - MidwestPHP 2015
Veil-Ordnance
Http2.0 Guide 2013-08-14 #http2study
OpenZFS Channel programs
OpenZFS at LinuxCon
Linux operating system by Quontra Solutions
PowerShell - Be A Cool Blue Kid
Ad

Viewers also liked (6)

PDF
Portable PHP
PDF
OSS at Microsoft
PDF
Understanding PHP objects
PDF
The Case for HTTP/2 - Internetdagarna 2015 - Stockholm
PDF
Behat c'est plus que Γ§a | Behat is more than that
PDF
Symfony tips and tricks
Portable PHP
OSS at Microsoft
Understanding PHP objects
The Case for HTTP/2 - Internetdagarna 2015 - Stockholm
Behat c'est plus que Γ§a | Behat is more than that
Symfony tips and tricks
Ad

Similar to Interoperable PHP (20)

PPTX
Php internal architecture
PDF
Php through the eyes of a hoster confoo
KEY
Let's creating your own PHP (tejimaya version)
PPTX
Cutting Back Processing Time
PPT
phpwebdev.ppt
PDF
PECL Picks - Extensions to make your life better
PDF
Php through the eyes of a hoster phpbnl11
PDF
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
KEY
Anatomy of a PHP Request ( UTOSC 2010 )
PDF
PHP, Cloud And Microsoft Symfony Live 2010
PDF
Php through the eyes of a hoster
PDF
Meet a parallel, asynchronous PHP world
PDF
Last Month in PHP - November 2016
PDF
Pecl Picks
PPTX
Learn PHP Lacture1
PDF
Php and threads ZTS
PDF
extending-php
PDF
extending-php
PDF
extending-php
PDF
extending-php
Php internal architecture
Php through the eyes of a hoster confoo
Let's creating your own PHP (tejimaya version)
Cutting Back Processing Time
phpwebdev.ppt
PECL Picks - Extensions to make your life better
Php through the eyes of a hoster phpbnl11
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
Anatomy of a PHP Request ( UTOSC 2010 )
PHP, Cloud And Microsoft Symfony Live 2010
Php through the eyes of a hoster
Meet a parallel, asynchronous PHP world
Last Month in PHP - November 2016
Pecl Picks
Learn PHP Lacture1
Php and threads ZTS
extending-php
extending-php
extending-php
extending-php

Recently uploaded (20)

PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
Β 
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PPTX
newyork.pptxirantrafgshenepalchinachinane
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
Funds Management Learning Material for Beg
PDF
Sims 4 Historia para lo sims 4 para jugar
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
PPT
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
DOCX
Unit-3 cyber security network security of internet system
PDF
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
PPTX
Mathew Digital SEO Checklist Guidlines 2025
PPTX
Introduction to cybersecurity and digital nettiquette
PPTX
Internet___Basics___Styled_ presentation
PPTX
Digital Literacy And Online Safety on internet
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Power Point - Lesson 3_2.pptx grad school presentation
Β 
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
newyork.pptxirantrafgshenepalchinachinane
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Funds Management Learning Material for Beg
Sims 4 Historia para lo sims 4 para jugar
Design_with_Watersergyerge45hrbgre4top (1).ppt
Unit-1 introduction to cyber security discuss about how to secure a system
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
SASE Traffic Flow - ZTNA Connector-1.pdf
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Unit-3 cyber security network security of internet system
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
Mathew Digital SEO Checklist Guidlines 2025
Introduction to cybersecurity and digital nettiquette
Internet___Basics___Styled_ presentation
Digital Literacy And Online Safety on internet

Interoperable PHP

Editor's Notes

  • #6: Hardware – various hardware existing or needed for a program. Architecture – how is the hardware put together. OS – an exact operating system. Tree above – platform. Program – actual binary running upon the platform. With PHP - the PHP interpreter.
  • #7: A cross-platform program can be handled within the same source tree, or with different sources for different platforms. As well many interpreted programming languages supports multiple platforms. Also there are like a dozen platforms within Unix, Linux, Windows.
  • #12: Access control lists and others vs UNIX permissions. Unmapped UNIX user example – Samba, NFS, etc.
  • #13: Atime is usually off by default off windows. On linux depends on a distribution, but most likely is on.
  • #19: readdir() fileorder can differ not only between platforms, but also depends on filesystems. symlink() with relative paths is not thread safe, threads could have changed the CWD. realpath() might show different results depending on platform/OS. Fe case insensitive on Windows, case sensitive on Linux, optional case sensitivity on OSX.
  • #21: TSC – time stamp counter, a 64-bit register on intel Architectures. The TSC frequency is not assumed to be constant, it is periodically tweaked, that is what the adjtimex system call is for. A user space program/daemon (like NTP and PTP) uses adjtimex to tell Linux how to slew the frequency over time. A periodic procedure calibrates the TSC frequency and the period is given by the RTC (system clock)? So the change of the TSC counter is compared to an elapsed time of the RTC (system clock). Thus the resulting TSC frequency is calibrated to give exactly one million microseconds/second PM clock – power management clock rate, might depend on ACPI – advanced configuration and power interface HPET – high precision event timer POSIX clock – standard for implementing and representing time sources, abstraction layer
  • #23: Usleep() … system call, posix monotonic clock vs performance timer … mention hrtime here Timeout counting on Linux doesn’t include time spent for syscalls, f.e. for sleep(). On Windows, timer is running in parallel with the PHP script execution.
  • #25: Opcache ASLR issue on Windows Various shared memory issues in NTS builds. Under Linux shared memory is accessible between processes, Windows it's not always the case. Especially it's to mention for ACRL cases.
  • #26: Like SQL query cache.
  • #27: PGO – profile guided optimization LTO – link time optimization FDO – feedback directed optimization Windows x64 no inline ASM PHP targets SSE2 on x86 make clean make -j4 prof-gen # run training scripts make prof-clean make -j4 prof-use configure –enable-pgi nmake # run training nmake clean ./configure –enable-pgo nmake
  • #31: Big file uploads count for LFS as well. A combination of 32-bit processes running on 64-bit OS – the processes are still a bit slower that on a native 32-bit OS, but can use their 2GB memory limits.
  • #35: Stack pointer Registers Scheduling properties (such as policy or priority) Set of pending and blocked signals Thread specific data. Shared file descriptors and co.
  • #37: Most of the dead stuff was removed in PHP7, embed can be used in any userspace programs, also used in several servers, fe NaviServer (former AOLserver).
  • #38: Classic model when learning socket programming.
  • #40: A good example for cross platform thread locking is APC. Cross-platform threading is possible with pthreads.
  • #42: And mention globals in php7 are TLS based as well, but actually it’s a bigger topic which can turn into a sleeping pill  In php7 it opens the door for better binary compatibility, because in many cases a thread specific global variable can be used instead of extending the extension globals structres. This can be a big salvation for bug fixes in the middle of some stable release.
  • #43: TS less robust, more sparse servers can be started.
  • #44: mail() isn’t thread safe until PHP7 on windows Locale is not thread safe mt_rand() might work not in the range expected symlink vs. junction Getenv()/putenv() isn’t thread safe. Fe #69636 . This has an alternative threaded API on windows, but no chance on linux.