fread using 99% CPU in Windows with SSL Connection

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm looking for some information on why fread calls max out CPU usage
on SSL connections in windows.  I've tried upgrading PHP (now at 5.1.2)
and OpenSSL (now 9.8)  but cannot stop fread() from slamming the
processor on long network requests.

Below is a simple way to reproduce the problem with fgets():

Create two files, one (timer.php) has to be accessable from the web
(HTTP and HTTPS) and the other can be ran from the command prompt
(pingtimer.php).  When you run pingtimer.php using SSL, php.exe will
stick at 99% CPU in Task Manager.  When you run it using clean sockets,
its idle while waiting for a response.

timer.php :
    print "<html><body>10 sec. are up!</body></html>";

    //$sock = fsockopen("localhost", 80);
    $sock = fsockopen("ssl://localhost", 443);

    $query = "GET /timer.php HTTP/1.1\r\n";
    $query .= "Host: localhost\r\n";
    $query .= "Connection: Close\r\n\r\n";

    fwrite($sock, $query);
    while (!feof($sock)) {
        echo fgets($sock, 128);

Any info (background, workaround, etc...) would be helpful.

- Joel

Re: fread using 99% CPU in Windows with SSL Connection wrote:
Quoted text here. Click to load it

whatelse is the CPU supposed to do?

let's see:
    do I/O to the network
        encrypt the message
          open the socket - get data from remote
            decrypt the SSL data
                move it somewhere (memory/disk)
                    more processing to display it.

If your CPU is not busy doing other things, it should take as much as it  
needs to get the job done.

Re: fread using 99% CPU in Windows with SSL Connection

If you check to source posted above (timer.php), you'll notice that I'm
using a Sleep() call.  There should be no data to decrypt until that
call returns.  That indicates that the 99% usage is happening while the
socket is being polled for data.  True, the SSL layer is more
intensive, but that should not affect the issue at hand as there is no
data being passed over the socket during the 10 sec. sleep call.

- Joel

Site Timeline