"OZV study session LT" "Linux" process and priority to know with nice command

Ozvision in-house study session LT material.


What is Nice

Run the program with the changed scheduling priority man page : https://linuxjm.osdn.jp/html/GNU_coreutils/man1/nice.1.html

nice [OPTION] [COMMAND [ARG]...]  

-n, --adjustment=N
Add an integer value N to the priority(Default: 10)

--help
Show this usage and exit

--version
Display version information and exit

How to use Nice

# -n [niceness value]Option required
#The range of niceness is, 19 ~ -20 * The lower the niceness, the higher the priority.

nice -n /something/default_nicevalue_is_10
nice -n 0 /batch_sh/something.sh #niceness value 0
nice -n 5 zgrep "something text" very_huge_data.log.gz
nice -n 10 mysqldump -u user -p DBNAME TABLE_NAME > OUTPUT_FILE
nice -n -10 /something/this_is_high_priority_execution

Process priority confirmation


Check with ps

# PRI,Check NI

[vagrant@comics ~]$ ps l
F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
0  1000 22121 22120  20   0 124924  4256 -      Ss   pts/0      0:00 -bash
0  1000 22647 22121  20   0 160236  2204 -      R+   pts/0      0:00 ps l

Check on top

# PR,Check with NI

[vagrant@comics ~]$ top
top - 18:36:27 up  2:22,  1 user,  load average: 1.15, 0.77, 0.66
Tasks: 167 total,   3 running,  90 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.4 us,  2.9 sy,  0.0 ni, 94.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1009004 total,   263660 free,   337808 used,   407536 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   508032 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
22255 apache    20   0  523200  30656  11248 R  20.6  3.0   0:00.69 httpd
 3018 mysql     20   0 2050332 121596  19976 S   0.7 12.1   0:11.54 mysqld
    8 root      20   0       0      0      0 R   0.3  0.0   0:03.04 rcu_sched
 3217 root      20   0  236900  22764   4652 S   0.3  2.3   0:02.13 fluentd
22867 vagrant   20   0  171112   4592   3896 R   0.3  0.5   0:02.46 top

PR and NI


What is NI

Process niceness value, priority correction value (user space setting value)

What is PR (PRI)?

Actual process priority to refer to in the kernels scheduler General process PR formula: 20 --NI


Programs and processes


What is a program?

A set of imperative code that a computer can interpret and execute. Generally, it is saved in a file format.

What is a process?

A resource that is generated from the timing when an execution instruction is given to a program.

For the average person, process resources are loaded into memory Under the control of OS process scheduling control Instructions are processed by higher-level memory and arithmetic units.


Relationship between programs and processes

image.png


Need for process priority


Why can the OS execute and process so many programs?

Basically, 1 CPU (1 CORE) can process only one instruction at a time. (1 time = 1hz, 3Ghz = 3 billion times in 1 second can process instructions)

Why do you feel that your programs are being processed at the same time under these constraints?

--Process control of OS "process scheduler" --Process status change (Life Cycle)

There is a concept of, and it is established.


Process scheduling

Most operating systems use various algorithms to handle multiple processes, but basically use the concept of Time Sharing. The time that multiple processes can process in their own order is set according to the priority.

An easy-to-understand example is the round-robin algorithm image.png


Process state change (Life Cycle)

image.png

--creation: Create a new process --dispatch: The OS scheduler allocates CPU calculation time to execute processing. Transition to Running state --timeout: The allocated calculation time has expired. Return resources and move to Ready state --blocking / unblocking: Wait in the block state to wait for IO processing when IO work occurs in the process, controlled by the processor


What if there were no priorities and scheduling?

With multiple processes allocating the same time, the more processes there are, the more the overall processing time is delayed. The problem is that important processes and system processes are also affected and the control command system of the system does not work.


In practice


Be aware that doing something in an OS environment is a process

Executing any command or program on the server means occupying a part of the limited resources of the computer in the form of time.

Unless you specify the priority of general processes, the same ratio of resources can be assigned, so the more processes there are, the more the processing time of other processes will be affected.

nice can specify this time occupancy and limit the processing priority for each process.

#Is it possible?
On a production web server, adding batch processing slowed down the response speed of the entire website.
>>> APACHE(prefork setting)In the case of, one request becomes a process, so it is naturally affected by the process priority.
>>>If batch processing is not time-critical, try lower priority (if not until separate servers)

After manually running some batch on the production batch server
The processing time of other batches, which had to be finished around by all means, was delayed.
>>>Batches that are run manually should be properly prioritized and run unless there is a specific reason.

Example of using nice

As a premise, the need for real time is low, and heavy processing is premised.

Manual batch execution work required on production batch server

nice -n 10 /batch_sh/huge_time_and_low_priority.sh

Dump large amount of data with mysqldump

nice -n 10 mysqldump -u xxxx -p DBNAME > all_db_dump.sql

Production web server, production log server grep

nice -n 10 zgrep -E `^\d+$` 1GB_FILE.log.gz

#Processing zgrep and regular expressions is a CPU-intensive task

Binary execution by systematic call in php

exec('nice -n 10 /something/command', $result, $ret);

#When required in a system call in batch processing or in asynchronous processing
#It is not recommended for using system calls, so please take a look for reference.

Conclusion

--APACHE, batch, Shell Command, if you do anything, it's a process. --The process repeats processing and waiting within a limited time. --As the number of processes increases, the waiting time of the entire process increases and the processing itself is delayed. --The nice command gives you limited control over the amount of time you spend on a process. --Let's use nice considering the priority of the program to be executed and the degree of impact on the server.


Supplement: renice

Change the niceness of an existing process

Recommended Posts

"OZV study session LT" "Linux" process and priority to know with nice command
[Linux] How to display CPU usage, display header, and not display grep process with ps command
[linux] kill command to kill the process
How to pass args or environment variables with Makefile and make command on #Linux
Yum command to access MySQL with Python 3 on Linux
Linux Study Session 1st: Virtual Console and File Manipulation
[Linux] After connecting to DB with teraterm and typing Sqlplus command, until logging in to DB (oracle)