전자책 출간 알림 [마이크로서비스패턴 쉽게 개발하기]

티스토리 뷰

[ocean@bastion ~]$ ps axo pid,comm,nice --sort=nice
  PID COMMAND          NI
    4 kworker/0:0H    -20
   10 lru-add-drain   -20
   16 kworker/1:0H    -20
   21 kworker/2:0H    -20
   ...
    556 auditd           -4
    1 systemd           0
    2 kthreadd          0
    5 kworker/u30:0     0
    6 ksoftirqd/0       0
    7 migration/0       -

tuned 서비스와 nice/renice명령을 이용한 시스템 성능 튜닝 방법에 대해 공부합니다.

 

tuned 서비스

tuned서비스는 기본적으로 enable되어 있습니다. 만약 설치가 안되어 있다면 아래와 같이 설치 및 enable하십시오.

[root@bastion ~]# yum install -y tuned
[root@bastion ~]# systemctl enable --now tuned
[root@bastion ~]# systemctl is-enabled tuned; systemctl is-active tuned
enabled
active

tuned-adm 명령은 tuned 서비스 기능 수행을 위한 CLI입니다.

Openshift는 시스템 성능을 최적화하기 위해 성능튜닝 프로파일을 제공합니다.

[root@bastion ~]# tuned-adm list
Available profiles:
- balanced                    - General non-specialized tuned profile
- desktop                     - Optimize for the desktop use-case
- hpc-compute                 - Optimize for HPC compute workloads
- latency-performance         - Optimize for deterministic performance at the cost of increased power consumption
- network-latency             - Optimize for deterministic performance at the cost of increased power consumption, focused on low latency network performance
- network-throughput          - Optimize for streaming network throughput, generally only necessary on older CPUs or 40G+ networks
- powersave                   - Optimize for low power consumption
- throughput-performance      - Broadly applicable tuning that provides excellent performance across a variety of common server workloads
- virtual-guest               - Optimize for running inside a virtual guest
- virtual-host                - Optimize for running KVM guests

주요 기능은 아래와 같습니다.

  • 현재 프로파일 구하기: tuned-adm active
  • 성능 프로파일 변경: tuned-adm profile <profile명>
  • 추천하는 프로파일 구하기: tuned-adm recommend
  • 성능 프로파일 적용 안함 : tuned-adm off

아래는 사용 예시입니다.

[root@bastion ~]# tuned-adm active
Current active profile: virtual-guest
[root@bastion ~]# tuned-adm profile balanced
[root@bastion ~]# tuned-adm active
Current active profile: balanced
[root@bastion ~]# tuned-adm profile $(tuned-adm recommend)
[root@bastion ~]# tuned-adm active
Current active profile: virtual-guest
[root@bastion ~]# tuned-adm off
[root@bastion ~]# tuned-adm active
No current active profile.
[root@bastion ~]# tuned-adm profile virtual-guest
[root@bastion ~]# tuned-adm active
Current active profile: virtual-guest

 

web console 이용한 tuning profile 변경

web console을 이용하여 성능 프로파일을 변경할 수도 있습니다.

먼저 cockpit.socket 서비스가 enable되어 있는지 확인하고, 안되어 있으면 enable시킵니다.

$ systemctl is-enable cockpit.socket; systemctl is-active cockpit.socket

안되어 있으면 아래와 같이 enable 시킵니다.

Listen Port를 확인하고 https://<host>:<port> 또는 https://<ip>:<port>로 접근합니다.

Profile을 클릭하여 변경합니다.

 

nice/renice

미리 정해진 성능 튜닝 프로파일을 지정하는 방법 외에 시스템 성능을 튜닝하는 다른 방법은 프로세스에 따라 CPU를 다르게 배정하는 방법이 있습니다.

다시말해, 긴급하게 처리될 필요 없는 프로세스에는 CPU 할당을 적게하고 빠르게 처리할 프로세스에는 CPU를 많이 할당하게 하는것입니다.
nice -n <nice값> <command> 는 특정 명령을 실행하면서 nice값을 지정하는 명령이고,

renice -n <PID>는 특정 프로세스의 nice값을 재정의 하는 명령입니다.

참고로, nice라는 이름은 다른 프로세스에 잘 양보하는 멋진 놈이라는 의미에서 지어진것입니다.

nice값은 -20에서 19까지 줄 수 있는데 nice값이 높을 수록 CPU를 덜 차지하는 것입니다.

root user외에 일반 user는 renice로 nice값을 높일 수는 있으나, 낮출 수는 없습니다. 왜냐하면 일반user가 nice값을 너도 나도 낮춰 버리면 CPU점유를 하는 프로세스들이 많아질 수 있고 결과적으로 전체적인 시스템 성능이 떨어질 수 있기 때문입니다.

 

nice 지정하기

nice -n <nice값> <command> 와 같이 nice값을 실행하는 프로세스에 지정합니다.

일반 user는 0~19까지만 지정할 수 있습니다.

[ocean@bastion ~]$ nice -n -1 sha1sum /dev/zero &
[5] 21131

[4]+  Stopped                 sudo nice -n -19 sha1sum /dev/zero
[ocean@bastion ~]$ nice: cannot set niceness: Permission denied

[ocean@bastion ~]$ nice -n 1 sha1sum /dev/zero &
[6] 21151

 

nice값을 보는 방법: top, ps

top명령 이용

NI컬럼이 nice값입니다. 아래와 같이 nice값이 높으면 CPU사용율은 떨어집니다.

PR열은 Priority를 의미하며 nice값에 따라 RT에서 39까지 지정됩니다. 예를 들어 nice값 -20은 priority 0에 해당됩니다.

 

ps 이용

$ ps axo pid,comm,nice --sort=cpu

[ocean@bastion ~]$ ps axo pid,comm,nice --sort=nice
  PID COMMAND          NI
    4 kworker/0:0H    -20
   10 lru-add-drain   -20
   16 kworker/1:0H    -20
   21 kworker/2:0H    -20
   ...
    556 auditd           -4
    1 systemd           0
    2 kthreadd          0
    5 kworker/u30:0     0
    6 ksoftirqd/0       0
    7 migration/0       -

 

renice 방법

nice값 재지정은 CLI와 top명령을 통해 할 수 있습니다.

먼저 테스트를 위한 shell script를 작성합니다.

$ vim control

#!/bin/bash

OUTPUT=./control_output

echo "" > ${OUTPUT}

while true
do
  echo -n "$@ " >> ${OUTPUT}
  sleep 1
done
exit 0

$ chnmod +x control

command : renice -n <nice값> <PID>

[ocean@bastion ~]$ ./control job1 &
[1] 19689
[ocean@bastion ~]$ ps axo pid,comm,nice | grep control
19689 control           0
[ocean@bastion ~]$ renice -n 10 19689
19689 (process ID) old priority 0, new priority 10
[ocean@bastion ~]$ renice -n 0 19689
renice: failed to set priority for 19689 (process ID): Permission denied
[ocean@bastion ~]$ sudo renice -n 0 19689
[sudo] password for ocean:
19689 (process ID) old priority 10, new priority 0

top 이용

현재 사용자의 프로세스만 보기 위해 'u'를 클릭하고 username일 입력합니다.

'r'을 누르고 PID를 입력한 후 nice값을 지정합니다.

 

테스트 하기

5개의 프로세스를 실행합니다.

[ocean@bastion ~]$ for i in {1..5}; do sha1sum /dev/zero & done
[7] 21452
[8] 21453
[9] 21454
[10] 21455
[11] 21456

top으로 보면 5개의 프로세스는 nice값이 0인걸 확인할 수 있습니다. CPU점유율도 대략 비슷하게 나옵니다.

nice값이 10인 프로세스를 추가합니다.

[ocean@bastion ~]$ nice -n 10 sha1sum /dev/zero &
[4] 21483

top으로 보면 추가된 프로세스에 CPU가 적게 할당되는걸 볼 수 있습니다.

강제로 추가한 프로세스의 nice값을 -20으로 변경해 보겠습니다.

$ sudo renice -n -20 <PID> 또는 top에서 'r'을 눌러 변경하십시오.

[ocean@bastion ~]$ sudo renice -n -20 21483
[sudo] password for ocean:
21483 (process ID) old priority 10, new priority -20

아래와 같이 CPU가 100%로 올라가는걸 볼 수 있습니다.

 

마지막으로 테스트한 프로세스를 pkill로 삭제하십시오.

[ocean@bastion ~]$ pkill sha1sum
[8]   Terminated              sha1sum /dev/zero
[9]   Terminated              sha1sum /dev/zero
[10]   Terminated              sha1sum /dev/zero
[12]   Terminated              nice -n 10 sha1sum /dev/zero
[7]   Terminated              sha1sum /dev/zero
[11]   Terminated              sha1sum /dev/zero

 

댓글

전자책 출간 알림 [마이크로서비스패턴 쉽게 개발하기]