I have ubuntu server 14.04, and I'm wondering if there is any easy way to achieve this task.
For example, I can already do this for single processes with taskset -pac 2 29435
, but is there any way to assign it via username instead of process id?
Thank you in advance.
Best Answer
Do nothing. Really, do nothing.
If your purpose here is to spread your resources as efficiently as possible, the proper thing to do is leave the operating system to move the processes to relevant CPUs on demand and as necessary.
For the case of performance optimization, limiting CPU resources or usage is never a good idea -- you'll only make performance worse. You only want to restrict CPU resources or CPUs that can be used where you are deliberately attempting to cripple the process.
Times where you might want to cripple a process would be:
So - do nothing and let the operating system sort it out. After a while of your instances running (and if they really are CPU heavy) you can run the command
ps -Lo psr,pid,tid $(pgrep <processname>)
and you'll see that each resource is being divvied up correctly on the CPU.If you want to determine if each process is getting its fair share, and how much you really are utilizing in each program you do the following replacing the process name and get the following results:
In this example I've used firefox on my machine but you can change the process name to suit your needs.
Here, I'm requesting each thread that lives in the process. The columns mean the following:
To determine a proceses utilization during its lifetime as a percentage you could perform the following calculation (there I'm using firefox):
Note: The actual process ID (the main starting thread whose tid == pid) acts as a 'container' for the cumalative total of all threads (existing or no longer existing) CPU times, so gives you a reasonably accurate depiction of a processes entire usage.
To explain, if a processes lifetime equals its cpu time it means that for all the time the process has ever lived, its demanded and recieved a CPU to run on. This would equal 100% of cpu utilization.
I'm almost certain in reality you're going to find that your processes are going to be using hardly any CPU.
So - to reiterate, to perform as efficiently as possible do nothing as your kernel knows how to best prioritize CPU resources to best utilize your system. Anything you could possibly add is in most cases is reducing your overall effectiveness.
Unless its your plan to actually cripple the processes in some way (and there are circumstances where you may actually intend to do that) you dont want to use
taskset
,control groups
orLXC/Docker
to get the best performance possible.