Portal Home > Knowledgebase > Articles Database > Process CPU usage (and php under cPanel)


Process CPU usage (and php under cPanel)




Posted by nkawit, 01-10-2013, 03:27 AM
I'm sure many have had the need to control particular clients PHP usage and maybe things like disk backups CPU usage to ensure that load of shared hosting servers stays low. For PHP this only works if you're using /usr/local/cpanel/cgi-sys/php5 (not DSO). Here is a tip, maybe you want to limit a client to 25% CPU usage if they're being slightly abusive to your system? Its pretty easy to do. You can combine this tool I wrote https://freecode.com/projects/cputool , together with the above php5 script like this ... or maybe conditional depending on the UID? ... You can also use the above to control the CPU usage of a process based on load using the -l parameter. I personally I use this for system rsync's which tend to shoot load up in orders of magnitude. Here is a sample output: Any ideas or suggestions are more than welcome. Currently on my TODO list is the ability to total up all CPU usage for all the clients processes and have possibly a very light weight daemon control the CPU usage to ensure it remains fair.

Posted by madaboutlinux, 01-10-2013, 07:48 AM
Have you tested the script that it can limit a CPU usage for a particular user? As far as I am aware, the cputool will limit CPU usage to say, 25% for PHP processes irrespective of users and I see your script is exactly doing that.

Posted by nkawit, 01-10-2013, 08:20 AM
Look at my second snippet, you can specify users in your php5 file which you wish to limit or even customize it to whatever you want. These are merely examples of how you can use these tools and how people are currently using it

Posted by madaboutlinux, 01-10-2013, 09:29 AM
Yes, I did checked the second snippet itself to come to this conclusion. It isn't restricting CPU usage for a specific user (or one listed in abusers list) but for PHP. Have you tried running CPU intensive scripts on users other than the one listed in abusers list? I guess the cputool won't allow those PHP scripts to use more than 25% of CPU as well.

Posted by nkawit, 01-10-2013, 09:38 AM
Limit EVERYTHING owned by the user is on my todo this. This will just limit php.

Posted by nkawit, 01-14-2013, 08:37 PM
http://freecode.com/projects/cputool#release_351379 I really really appreciate those that gave feedback, if anyone has any feature requests please mention them, lets get discussing.

Posted by TravisT-[SSS], 01-15-2013, 01:22 AM
Very nice project. This could make for a good free option for those who do not or can't run cloudlinux/betterlinux

Posted by iseletsk, 01-15-2013, 10:56 AM
The overhead of such rescheduling had been quite high in our tests. Though we did them back in 2009. Maybe something has changed since then.

Posted by nkawit, 01-15-2013, 11:02 AM
The latest version should have 0.3% CPU cost on a smallish machine. Running cputool at this stage for every single php process is not advisable, I'm thinking about possibly having it daemonize itself so its only 1 process rescheduling, then have cputool just send the pid's to the daemon. The overhead should be much much much less than running multiple cputools. Was your tests done running php under cputool? or something else?

Posted by nkawit, 01-15-2013, 11:05 AM
Oh, basically what I had a client do is take the top utility, order all processes by the amount of CPU time consumed (they using fcgid, so php processes stay active for 5 mins). They then took the top 10 CPU users and limited them to 2 PHP processes and 25% CPU usage. On a machine that was hitting 7 load during most of the day, the load was reduced to 1.6-1.8 on average. This will probably not be the same for anyone else testing, it really depends on the nature of the usage. What about checking how long a process was active for? calculating its average CPU usage, then attaching to that? remember cputool can be run using -p which allows it to attach to almost any process.

Posted by iseletsk, 01-15-2013, 11:06 AM
Is it 0.3% CPU cost per process? We run different software / mostly custom written that helped us track exact CPU usage. We were doing it as part of evaluating different ways to limit CPU.

Posted by nkawit, 01-15-2013, 11:08 AM
Thats what my limited number of tests showed, yep. It should be relatively the same for a process group aswell, although there is a slightly higher overhead having to walk /proc every now and again and having to parse multiple /proc/*/stat files. It would be very useful if you could describe your usage case to me here or even in pvt. I'd very much like to also try pushing multiple PID's to cputool and see the overhead. Having people with interesting usage cases is very very useful for both debugging and performance tuning.

Posted by nkawit, 01-15-2013, 11:19 AM
Its very useful for lower usage cases at the moment, but I'm hoping to improve it based on feedback. I'm wondering if one could even make cputool a shell with jailshell as a argument. Another tool I've been pondering is a utility that uses PTRACE to attach to a process and monitor things like like symlink() or anything with "passwd" in a string parameter ... etc. Making something like this a shell ... similar in functionality to jailshell also peaks my interest. What about faking the results of calls to libc to return generic passwd info and blocking all non homedir access ... hrmmm.

Posted by iseletsk, 01-15-2013, 11:35 AM
Yes, we also thought about ptrace for security. But it is way too expensive. It traps each and every system call -- so the overhead is not 0.3%, but more like 100%. And yes, it should be easy to extend cputool to do jailshell. Just run chroot before -- and you are done.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Free Billing System (Views: 668)

Language: