Ozvision in-house study session LT material.
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
# -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
# 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
# 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
Process niceness value, priority correction value (user space setting value)
Actual process priority to refer to in the kernels scheduler General process PR formula: 20 --NI
A set of imperative code that a computer can interpret and execute. Generally, it is saved in a file format.
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.
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.
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
--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
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.
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.
As a premise, the need for real time is low, and heavy processing is premised.
nice -n 10 /batch_sh/huge_time_and_low_priority.sh
nice -n 10 mysqldump -u xxxx -p DBNAME > all_db_dump.sql
nice -n 10 zgrep -E `^\d+$` 1GB_FILE.log.gz
#Processing zgrep and regular expressions is a CPU-intensive task
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.
--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.
renice
Change the niceness of an existing process
Recommended Posts