0

How can I know the total of used processors by user in Linux? For example: User "X" runs 2 (or more) processes that use in total 3 processors. How can I know it? If it impossible, then How can I know how many processors can be used by user? For example: User "X" opened session with 2 processors (of 8 possibles).

I need to write Perl function that gets user name (for example "root") and returns scalar number - one of possibles: 1. Total user Processors used (e.g. 2 from 8) 2. Total user Cores used 3. Processors that available for user In my case users runs one (maybe more, but not much) big process that runs few days and I want to know how the processes (of special user) loads the machine.... Because if he uses to much resources, then I'll need to stop him...

2 Answers 2

3

It is not possible to tell what cores a given program has used. The CPU assigns threads to cores invisibly and automatically based on a large number of factors, and performs a great deal of slight of hand to make it appear that everything is running synchronously, even if there is some time-slicing in how it assigns processing time to each thread.

what it comes down to, is that a session can't be mapped to a set of cores. you can map a session to the use of programs that run threads on one or more cores, but that's strictly as needed based on the current state of all processes.

its not like you assign 2 cores to sally, 2 cores to bob, and 4 cores to tom, causing you to 'run out' of them. sally's, Bob's, and Tom's programs can all use up to 8 cores, and the system will decide how to best assign the threads to a processing facility.

3
  • @user259543: And the answer still answers it. Any process is assigned to any core at the discretion of the system. You can put various other limits to users, but not this one.
    – Jan Hudec
    Oct 3, 2013 at 13:37
  • But when user open some session he can get part of processes.
    – user259543
    Oct 3, 2013 at 13:42
  • I my machine have 8 processors, I can't limit it for user per session?
    – user259543
    Oct 3, 2013 at 13:59
0

You can use sched_getaffinity and sched_setaffinity to look up on what core's a users process is allowed to run. However, that does not mean it will be running on all of them.

You can limit the cores a process can run on by starting it in a cpuset (a particular part of a cgroup)

e.g. See this guide on how to add a process to a cgroup in Red Hat 6 and this guide on how to effectively make a per-group (or thus, per user) division of cpu and memory.

If you are just interested in getting the cpu% a specific user is using you can use this oneliner:

top -b -n 1 -u <username> | awk 'NR>7 { sum += $9; } END { print sum; }'
1
  • I need to write Perl function that gets user name (for example "root") and returns scalar number - one of possibles: 1. Total user Processors used (e.g. 2 from 8) 2. Total user Cores used 3. Processors that available for user In my case users runs one (maybe more, but not much) big process that runs few days and I want to know how the processes (of special user) loads the machine.... Because if he uses to much resources, then I'll need to stop him...
    – user259543
    Oct 3, 2013 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .