Shared hosting: how do they estimate server CPU usage?

0

I'm using several shared hosting services and wondered many times: how do they calculate those "CPU seconds"?

For example one of those limiting me with 300,000 seconds/month, 10,000 seconds/24h and 2,000 sec/2h. But the seconds can significantly depends on hoster's hardware and the software (both: my applications and hoster's OS).

So I mostly sure that if I run some complicated and probably bad optimized SQL request which lasts 10 seconds I will probably "spend" exactly 10 CPU seconds. No questions.

But if I put a delay in a PHP script (<?php sleep(10); ?>) it will cost me same 10 CPU seconds? Or if I downloading external webpage and it lasts for 3 seconds - will it be the same in this case?

Mainly I interested in PHP's file_get_contents() CPU consumption.

Roman Matveev

Posted 2015-04-15T13:17:41.430

Reputation: 155

Question was closed 2015-04-15T23:42:34.017

Answers

1

This is done by the kernel scheduler, when a process is scheduled to run, the time it is running is added to the total for that process. The scheduler chooses a process to run 100 to 1000 times per second, depending on the OS and the configuration. (This amount of time is the time slice.)

A sleep(10) will use less than a microsecond of CPU time because it causes the process to stop running. The scheduler will then choose another process to begin running (or if there are none that are ready to run, the CPU can go idle). A timer will then be created for the process that expires in 10 seconds. At that time, the scheduler will then choose the process to run on the CPU at the next available time slice.

Operations involving file I/O will also cause the process to stop running while the CPU waits for the disk or network to be ready, so they will not use much CPU time either.

Operations that do take CPU time would be those that read/write variables, do numeric math operations, build and scan string variables, loops and if statements.

There is no simple formula to tell how much CPU time will be used by a given program. It depends on what exactly the program does. The typical web page that is just reading/writing a database will not use very much CPU time.

Kevin Panko

Posted 2015-04-15T13:17:41.430

Reputation: 6 339