-1

to observe the actual working of "nice" utility in linux, I wrote a python script to calculate SHA512 non-stop.

- Preparation -

  • a Dual Core Linux Machine, a Single Core Linux Machine ( actually, they are both cloud instances )

  • a python script to calculate SHA512 non-stop, consumes 100% CPU time when running alone.

- Experiment -

  • on Single Core Machine

    start this script normally, then nice -n 15 python load_generator.py, give the second python process a low priority.

  • on Dual Core Machine

    because only start two processes can not prove the point, I normally start 2 processes, then nice -n 15 python load_generator.py a new process.

- Expected Outcome -

the normally started python process takes far more CPU time/Usage Percentage than nice started process on both machines.

- Actual Result -

On single-core machine, this is tested to be true, one is using 97% and another is using 3% . but on dual-core machine, nice utility does not seem to be working. no CPU usage has been reduced for the process which was created by using nice. each one of them use the same amount of CPU time.

- Appendix : script used -

# load_generator.py
import hashlib, binascii
import random

while True:
    dk = hashlib.pbkdf2_hmac('sha512', 
str(random.random())+str(random.random()), b'salt', 2000000)
    print(binascii.hexlify(dk))

closed as off-topic by Gerald Schneider, ThoriumBR, Ward, Jenny D, Dave M Sep 21 '17 at 17:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on Server Fault must be about managing information technology systems in a business environment. Home and end-user computing questions may be asked on Super User, and questions about development, testing and development tools may be asked on Stack Overflow." – Gerald Schneider, ThoriumBR, Ward, Jenny D, Dave M
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    You have misunderstood how the nice command works. It works in multithreaded environment to tell a kernel process scheduler that a certain process should get lower priority than other ones (or higher - depending on the parameters). When all you have is one process that takes up all the cpu and there are no other processes that need it in the meanwhile, the nice setting will have no real outcome. What you are looking for is the cpulimit command that inserts sleeps into the running process and therefore throttles its performance that way. – bocian85 Sep 19 '17 at 12:49
  • @bocian85 I vote for this to become an answer! :) – Marco Sep 19 '17 at 13:33
  • @Sajuuk from where see 100% CPU?? from top?? – c4f4t0r Sep 19 '17 at 13:38
  • @bocian85 in the second setup I have 2 CPU and 3 processes running, at least two processes would be assigned to the same CPU, then the process which was started by nice should have a lower priority than the one it's sharing CPU with,right? – Sajuuk Sep 19 '17 at 15:07
  • yes that's correct but since they both eat up CPU like biscuits nice will not make huge changes between them in regards of cpu usage, it can lead to a situation that the first process that started will use 97% even if it's the one with higher nice level, you can use it to prioritize, but IMO cpulimit will be much better at it, and since task switching is pretty costly for kernel and system overall you might most likely be better off with one process per core as it should probably run faster in general than overbooking CPU. I mean you'll be able to calculate more SHA hashes in given time – bocian85 Sep 19 '17 at 15:15
1

Following Marco's suggestion I repaste my comment as an answer, and expand it (no idea if there's any better way to do it)

You have misunderstood how the nice command works. It works in multithreaded environment to tell a kernel process scheduler that a certain process should get lower priority than other ones (or higher - depending on the parameters). When all you have is one process that takes up all the cpu, and there are no other processes that need it in the meanwhile - the nice setting will have no real outcome. When you put two CPU intensive processes on one CPU core they will fight for it, and therefore the one with positive nice level will be at a loss, because the scheduler will give it CPU time at a lower rate than the other.

The situation changes when you have enough cores for all the processes, then each of them has the same chance to get CPU time, remembering that there are still plenty of background applications running the operating system and other tasks. In this case the nice level will not have a huge impact on the processes - it will only play a role when some other process will request CPU time to get work done, but if it's not CPU intensive and/or finishes shortly you will not observe it on the system metrics the way you expected it.

You can compare giving a nice priority to a certain process to a pregnant woman in a store. If she has priority before other clients on the checkout, that doesn't make her move any faster it just means, that everyone else will have to wait until she's finished.

You can read more about nice on the wiki
https://en.wikipedia.org/wiki/Nice_(Unix)

What you are looking for is the cpulimit command that inserts sleeps into the running process and therefore throttles its performance that way. When a process is sleeping another process can get it's CPU time and that way you can essentially throttle down a process. Since the nature of this command is to suppress a running process you can only use it to slow down tasks.

  • I have already stated that that script running alone would've taken 100% CPU already, then why would 2 CPU cores be enough for 3 of these python processes? – Sajuuk Sep 20 '17 at 2:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.