viernes, 7 de abril de 2017

LINUX-optimizar la ejecución de procesos: context switching

en un post anterior comenté acerca del uso del comando TOP (tail of processes) para conocer el load average
http://eloraculodelpinguino.blogspot.cl/2014/01/lalal.html

En el siguiente laboratorio, aprendemos a usar diferentes utilitarios  para manejar un proceso en particular y optimizar su ejecución adecuando el context switch.

Concepto de context switch.

hay varios post que se pueden encontrar googleando, sin embargo recomiendo el que (a mi parecer) está mejor explicado:
http://www.linfo.org/context_switch.html (los 3 primeros párrafos son suficientes)



Cómo determinar una sobrecarga por context switch y obtener los pid de procesos que están ocasionándolo.

utilitarios: vmstat, pidtstat
El utilitario vmstat se puede encontrar en 2 packages: procps y pcp

[root@centOS-testing02 ~]# yum whatprovides */vmstat
...

procps-3.2.8-45.el6.x86_64 : System and process monitoring utilities
Repo        : base
Matched from:
Filename    : /usr/bin/vmstat

pcp-3.10.9-9.el6.x86_64 : System-level performance monitoring and performance management
Repo        : base
Matched from:
Filename    : /var/lib/pcp/config/pmlogconf/tools/vmstat

accediendo a la documentación del utilitario, se encuentra lo siguiente:
columns
   System
       in: The number of interrupts per second, including the clock.
       cs: The number of context switches per second.

Por lo que hay que poner atención a las columnas mencionadas. 

información desplegada antes de la sobrecarga
[root@centOS-testing02 ~]# vmstat 
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 729592   9328 181928    0    0    20     9   14   13  0  0 100  0  0
[root@centOS-testing02 ~]# 

toma de muestra con delay de 3 seg
[root@centOS-testing02 ~]# vmstat 3
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 729600   9328 181928    0    0    20     9   14   13  0  0 100  0  0
 0  0      0 729592   9328 181928    0    0     0     0   15   10  0  0 100  0  0
 0  0      0 729592   9328 181928    0    0     0     0   11    7  0  0 100  0  0
 0  0      0 729592   9328 181928    0    0     0     1   14   12  0  0 100  0  0
 0  0      0 729592   9328 181928    0    0     0     0   11   10  0  0 100  0  0
 0  0      0 729468   9336 181936    0    0     0    13   43   35  0  0 100  0  0
^C
[root@centOS-testing02 ~]# 

Acotinuación procedemos a sobrecargar el uso de CPU con el comando dd

[root@centOS-testing02 ~]# dd if=/dev/zero of=/dev/null &
[1] 2192
[root@centOS-testing02 ~]# dd if=/dev/zero of=/dev/null &
[2] 2193
[root@centOS-testing02 ~]# dd if=/dev/zero of=/dev/null &
[3] 2194
[root@centOS-testing02 ~]#

información desplegada durante la sobrecarga
Veamos cómo se despliega la CPU a través del comando vmstat
las columnas IN (interrupciones) CS (context switch) aumentan considerablemente. 
[root@centOS-testing02 ~]# vmstat 3
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 3  0      0 729328   9400 181936    0    0    19     9   16   12  0  0 99  0  0
 3  0      0 729320   9400 181936    0    0     0     0 2106  152 25 75  0  0  0
 3  0      0 729320   9400 181936    0    0     0     0 2066  139 23 77  0  0  0
 3  0      0 729320   9400 181936    0    0     0     0 2113  153 24 76  0  0  0
 3  0      0 729320   9400 181936    0    0     0     0 2072  148 24 76  0  0  0
 3  0      0 729320   9408 181936    0    0     0     4 2267  162 25 75  0  0  0
^C
[root@centOS-testing02 ~]# 


El utilitario pidstat se puede encontrar en el package: sysstat

[root@centOS-testing02 ~]# yum provides */pidstat
sysstat-9.0.4-33.el6.x86_64 : The sar and iostat system monitoring commands
Repo        : base
Matched from:
Filename    : /usr/bin/pidstat

accediendo a la documentación del utilitario, se encuentra lo siguiente:
       -w     Report task switching activity (kernels 2.6.23 and later only).  The following values may be displayed:

              UID
                     The real user identification number of the task being monitored.

              USER
                     The name of the real user owning the task being monitored.

              PID
                     The identification number of the task being monitored.

              cswch/s
                     Total number of voluntary context switches the task made per second.  A voluntary context switch occurs when a task blocks because it requires a resource that is unavailable.

              nvcswch/s
                     Total number of non voluntary context switches the task made per second.  A involuntary context switch takes place when a task executes for the duration of its time  slice  and
                     then is forced to relinquish the processor.

              Command
                     The command name of the task.

información desplegada antes de la sobrecarga
[root@centOS-testing02 ~]# pidstat -w 10 1
Linux 2.6.32-358.el6.x86_64 (centOS-testing02)  07/04/17  _x86_64_ (2 CPU)

10:43:50          PID   cswch/s nvcswch/s  Command
10:44:00            4      0,10      0,00  ksoftirqd/0
10:44:00            9      0,10      0,00  ksoftirqd/1
10:44:00           11      1,60      0,00  events/0
10:44:00           12      1,50      0,00  events/1
10:44:00           18      0,10      0,00  sync_supers
10:44:00           19      0,20      0,00  bdi-default
10:44:00           22      0,50      0,00  kblockd/0
10:44:00           23      0,20      0,00  kblockd/1
10:44:00          460      0,10      0,00  kdmflush
10:44:00          486      0,30      0,00  jbd2/dm-0-8
10:44:00         1127      0,20      0,00  flush-253:0
10:44:00         2191      0,10      0,00  pidstat

Average:          PID   cswch/s nvcswch/s  Command
Average:            4      0,10      0,00  ksoftirqd/0
Average:            9      0,10      0,00  ksoftirqd/1
Average:           11      1,60      0,00  events/0
Average:           12      1,50      0,00  events/1
Average:           18      0,10      0,00  sync_supers
Average:           19      0,20      0,00  bdi-default
Average:           22      0,50      0,00  kblockd/0
Average:           23      0,20      0,00  kblockd/1
Average:          460      0,10      0,00  kdmflush
Average:          486      0,30      0,00  jbd2/dm-0-8
Average:         1127      0,20      0,00  flush-253:0
Average:         2191      0,10      0,00  pidstat
[root@centOS-testing02 ~]# 

información desplegada durante la sobrecarga
Poniendo atención en las columnas PID y nvcswch/s se puede identificar claramente a los procesos por su el aumento de los context switch involuntarios. 

[root@centOS-testing02 ~]# pidstat -w 10 1
Linux 2.6.32-358.el6.x86_64 (centOS-testing02)  07/04/17  _x86_64_ (2 CPU)

10:44:46          PID   cswch/s nvcswch/s  Command
10:44:56            6      0,10      0,00  watchdog/0
10:44:56            9      0,10      0,00  ksoftirqd/1
10:44:56           10      0,10      0,00  watchdog/1
10:44:56           11      1,60      0,00  events/0
10:44:56           12      1,50      0,00  events/1
10:44:56           18      0,20      0,00  sync_supers
10:44:56           19      0,20      0,00  bdi-default
10:44:56           23      0,20      0,00  kblockd/1
10:44:56           37      0,10      0,00  khungtaskd
10:44:56         1127      0,20      0,00  flush-253:0
10:44:56         2192      0,00      2,50  dd
10:44:56         2193      0,00     67,70  dd
10:44:56         2194      0,00     67,80  dd
10:44:56         2197      0,10      0,00  pidstat

Average:          PID   cswch/s nvcswch/s  Command
Average:            6      0,10      0,00  watchdog/0
Average:            9      0,10      0,00  ksoftirqd/1
Average:           10      0,10      0,00  watchdog/1
Average:           11      1,60      0,00  events/0
Average:           12      1,50      0,00  events/1
Average:           18      0,20      0,00  sync_supers
Average:           19      0,20      0,00  bdi-default
Average:           23      0,20      0,00  kblockd/1
Average:           37      0,10      0,00  khungtaskd
Average:         1127      0,20      0,00  flush-253:0
Average:         2192      0,00      2,50  dd
Average:         2193      0,00     67,70  dd
Average:         2194      0,00     67,80  dd
Average:         2197      0,10      0,00  pidstat
[root@centOS-testing02 ~]#

Cómo modificar (Disminuir) el context switch (involuntario) de un proceso.

Antes de modificar, se debe seleccionar la CPU con menos carga. (A través del comando TOP ,1)

top - 14:28:27 up  5:29,  2 users,  load average: 3.00, 3.00, 2.91
Tasks:  95 total,   4 running,  91 sleeping,   0 stopped,   0 zombie
Cpu0  : 24.2%us, 75.4%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.4%hi,  0.0%si,  0.0%st
Cpu1  : 25.6%us, 34.4%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1020380k total,   295508k used,   724872k free,    12484k buffers
Swap:  1048568k total,        0k used,  1048568k free,   182768k cached

En el ejemplo observamos que el server cuenta con 2 CPU (Cpu0,Cpu1) 
Poner atención en los 2 primeros params %us, %sy

accediendo a la documentación del utilitario, se encuentra lo siguiente:
   2c. SUMMARY Area Fields
       The summary area fields describing CPU statistics are abbreviated.  They provide information about times spent in:
           us = user mode
           sy = system mode

%US: user mode representa el porcentaje de uso de CPU que ocupan los procesos de los usuarios (incluído el user root)
%SY: system mode representa el porcentaje de uso de CPU que ocupan los procesos del kernel. 

*tip: lo normal en un server es alto uso de us y bajo uso de sy.

Tomando en cuenta estos antecedentes, concluimos que es mejor utilizar el Cpu1 para fijar la ejecución del proceso.

El utilitario taskset se puede encontrar en el package: util-linux-ng
[root@centOS-testing02 ~]# yum provides taskset
util-linux-ng-2.17.2-12.28.el6.x86_64 : A collection of basic system utilities
Repo        : base
Matched from:
Filename    : /bin/taskset

Con el utilitario taskset se obtiene los numeros de CPU por el cual el proceso hace context switch
[root@centOS-testing02 ~]# taskset -cp 2193
pid 2193's current affinity list: 0,1
[root@centOS-testing02 ~]# 

Para setear una CPU para el pid 2193
[root@centOS-testing02 ~]# taskset -pc 1 2193
pid 2193's current affinity list: 0,1
pid 2193's new affinity list: 1
[root@centOS-testing02 ~]# taskset -cp 2193
pid 2193's current affinity list: 1
[root@centOS-testing02 ~]# 

Al comprobar con el comando pidstat, observamos como la columna nvcswch/s va descendiendo respecto de los otros procesos

[root@centOS-testing02 ~]# pidstat -w -p 2192,2193,2194
Linux 2.6.32-358.el6.x86_64 (centOS-testing02)  07/04/17  _x86_64_ (2 CPU)

11:40:05          PID   cswch/s nvcswch/s  Command
11:40:05         2192      0,00     19,45  dd
11:40:05         2193      0,00     10,23  dd
11:40:05         2194      0,00     23,21  dd
[root@centOS-testing02 ~]# 

también podemos observar a traves de TOP, el uso de CPU también desciende.
[root@centOS-testing02 ~]# top -b | head
top - 11:41:37 up  2:42,  2 users,  load average: 3.00, 3.00, 2.89
Tasks:  96 total,   4 running,  92 sleeping,   0 stopped,   0 zombie
Cpu(s):  7.7%us, 24.3%sy,  0.0%ni, 67.8%id,  0.1%wa,  0.0%hi,  0.1%si,  0.0%st
Mem:   1020380k total,   293268k used,   727112k free,    10556k buffers
Swap:  1048568k total,        0k used,  1048568k free,   182688k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                       
 2193 root      20   0  102m  708  588 R 100.0  0.1  47:12.66 dd                                                                                                                                           
 2192 root      20   0  102m  708  588 R 49.1  0.1  35:53.98 dd                                                                                                                                            
 2194 root      20   0  102m  708  588 R 49.1  0.1  31:32.33 dd                                                                                                                                            
[root@centOS-testing02 ~]# 


Fuentes:

http://unix.stackexchange.com/questions/39342/how-to-see-how-many-context-switches-a-process-makes

No hay comentarios:

Publicar un comentario