My QNAP TS-269L comes with an Intel Atom D2701 which has dual-core with hyper-threading. The File Transfer Performance is better than my other NAS using Marvell single core processor. But does the multithreading architecture really is the key to improve LAMP performance? Well, it’s a long story…
Processor Architecture and Threads
When executing a program, it will be divided into threads which contain instructions. The operating system scheduler will send threads to processor according to core and thread handling architecture.
For a multi-core processor, it sends one threat to each core. For a single-core processor with hyper-threading, it sends more threads to core. For multi-core processor with hyper-threading, it sends more threads to each core.
Confusing? It should be. Let’s take try an real-life example to understand.
When you ask for hot tea, you will be server with one cup of tea and wait for cool before drinking. Like a single-core processor scenario.
If you are a couple, you will be served for two cups of tea. Every one enjoy his/her tea like a multi-core processor.
With hyper-threading, you may ask for two cups of tea at a time. At first, both are hot. But when the left is cool, you drink the left. The right one becomes cool when you are drinking the left. Then you leave the left and begin to drink the right one. It is limited by how fast you may drink. You spend less time waiting for cool.
How LAMP Handles Login Sessions
But if you tick “Remember me next time”, it will save session information in a local cookie on client browser. If the session file is removed on server, PHP will create a new session file with the cookie on client. User doesn’t need to login again.
How Multi-Core Processor with Hyper-Threading Works with Sessions
For most Apache, the default configuration for MPM is prefork. It is non-threaded and isolating each requests. In other words, 1 Apache child process will be assigned with 1 thread serving 1 request only.
When there is a request to a PHP program, the Apache child process will load its PHP module to process them. It prevents from problems when 2 requests asking PHP to access the same session file. It is also call thread-safe.
Although each threads serve 1 request, there might be threads serving the same request which means they need to access the same session file. In this case, if any PHP code or Apache module is not thread safe, you will have problems. PHP has to be thread-safe to be able to play ball correctly with Apache using Worker MPM!
Now, let’s take a closer look at prefork.
When there are 5 login sessions and each of them opens 5 detail contact in different tabs, the processor will serving 4 threads from Apache concurrently.
When only single login session, opening 5 detail contact in different tabs, the processor will serving 1 thread from Apache at a time to prevent conflict.
CPU utilization is more than 300%. 4 threads are serving by processor concurrently while 3 running threads are for Apache.
CPU utilization is less than 100%. 2 threads are served by processor concurrently while only 1 running thread is for Apache.
If you don’t have htop, you may use top and press 1 to switch to view CPU per logical core.
You won’t see all requests from 1 login session running concurrently. They’re all jam in one queue and the next won’t start until the current one finish loading because they are served by 1 thread only in prefork configuration.
Hints for Optimization
For those when Apache is the real bottleneck, try to Optimize Apache first. There are also alternative like thttpd and lighttpd. They may co-exist with Apache but listening to different ports. You may follow this post to test on QNAP TS-269L.
For optimize Apache and PHP both, I recommend Tuning Apache and PHP for Speed on Unix from PHP Everywhere.
You may use PHP benchmark Script to compare hardware performance. To identify it is an hardware or software issue.
If you think both Apache and PHP is not the bottleneck and you are using InnoDB, InnoDB performance optimization basics (redux) from MySQL Performance Blog is worth reading. Clear and conscious.
What Scenarios will be Best for Multi-core Processor with Hyper-threading using Prefork Configuration
But if you are running ZurmoCRM on Atom D2701, 4 concurrent session with 1 request within a session at a time is the best scenarios. Single session with many requests at a time won’t feel much improve by multi-core processor with hyper-threading.
If you are running LAMP without session, eg. WordPress with default configuration, browsing several pages in different tabs from the same browser will be served by different threads. It’s up to the threads a processor may serve concurrently.
Multi-core processor with hyper-threading are charming. But they won’t improve everything, use it in the right scenarios. If you want to do a test, Apache: multi-threaded vs multi-process (pre-forked) from Zurigo is a good guide.
- QNAP TS-269L
- Intel Atom Processor D2700
- Wiki: Multi-core processor
- Wiki: Hyper-threading
- Wiki: Thread (computing)
- Wiki: Multithreading (computer architecture)
- Wiki: Session (computer science)
- QNAP TS-269L File Transfer Performance Report
- Linux Processor Viewer with Thread Support
- Wiki: LAMP (software bundle)
- Test your ZurmoCRM with VirtualBox
- Apache: Multi-Processing Modules (MPMs)
- Apache: Apache MPM prefork
- PHP.net: Installed as an Apache module
- Apache: Apache MPM worker
- modwsgi: Processes And Threading
- StackOverflow: Apache Prefork vs Worker MPM
- Server Fault: Which to install: Apache Worker or Prefork? What are the (dis-)advantages of each?
- Linux Processor Viewer with Thread Support
- StackOverflow: How to dynamically monitor CPU per core usage on Linux?
- StackOverflow: How to measure separate CPU core usage for a process?
- liquidweb: Apache Optimization
- Wiki: thttpd
- Wiki: lighttpd
- PHP Everywhere: Tuning Apache and PHP for Speed on Unix
- PHP benchmark Script
- MySQL Performance Blog: InnoDB performance optimization basics (redux)
- JoStudio: 在QNAP TS-209上安裝 lighttpd, fastcgi, perl …
- Intel: Intel Core i7 processors
- Intel: Intel Atom Processor
- MyGuy Solutions: How To: Enable the Use of Sessions On Your WordPress Blog
- Zerigo: Apache: multi-threaded vs multi-process (pre-forked)
- Wiki: Thread safety
- StackOverflow: What is thread safe or non thread safe in PHP
- StackOverflow: Is PHP thread-safe
- StackOverflow: What does threadsafe mean?