Alto uso de la CPU del usleep en Cent OS 6.3.

Compilo el siguiente código en cent os 5.3 y cent os 6.3:

#include <pthread.h>
#include <list>
#include <unistd.h>
#include <iostream>
using namespace std;
pthread_mutex_t _mutex;
pthread_spinlock_t spinlock;

list<int *> _task_list;

void * run(void*);
int main()
{
    int worker_num  = 3;
        pthread_t pids[worker_num];
        pthread_mutex_init(&_mutex, NULL);
        for (int worker_i = 0; worker_i < worker_num; ++worker_i)
        {
                pthread_create(&(pids[worker_i]), NULL, run, NULL);
        }
    sleep(14);
}


void *run(void * args)
{
        int *recved_info;
    long long start;
        while (true)
        {
            pthread_mutex_lock(&_mutex);
                if (_task_list.empty())
                {
                        recved_info = 0;
                }
                else
                {
                        recved_info = _task_list.front();
                        _task_list.pop_front();
                }
            pthread_mutex_unlock(&_mutex);

                if (recved_info == 0)
                {
                        int f = usleep(1);
            continue;
        }
    }
}

Mientras se ejecuta en la versión 5.3, ni siquiera puede encontrar el proceso en la parte superior, el uso de la CPU es de alrededor del 0%. Pero en cent os o 6.3, es aproximadamente el 20% con 6 hilos en una CPU de 4 núcleos.

Así que reviso el a.out conhora ystace , los resultados son sobre eso:

En 5.3:

real    0m14.003s
user    0m0.001s
sys     0m0.001s

En 6.3:

real    0m14.002s
user    0m1.484s
sys  0m1.160s

la huelga

en 5.3:

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 91.71    0.002997           0     14965           nanosleep
  8.29    0.000271         271         1           execve
  0.00    0.000000           0         5           read
  0.00    0.000000           0        10         4 open
  0.00    0.000000           0         6           close
  0.00    0.000000           0         4         4 stat
  0.00    0.000000           0         6           fstat
  0.00    0.000000           0        22           mmap
  0.00    0.000000           0        13           mprotect
  0.00    0.000000           0         1           munmap
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         3           rt_sigaction
  0.00    0.000000           0         3           rt_sigprocmask
  0.00    0.000000           0         1         1 access
  0.00    0.000000           0         3           clone
  0.00    0.000000           0         1           uname
  0.00    0.000000           0         1           getrlimit
  0.00    0.000000           0         1           arch_prctl
  0.00    0.000000           0        38         4 futex
  0.00    0.000000           0         1           set_tid_address
  0.00    0.000000           0         4           set_robust_list
------ ----------- ----------- --------- --------- ----------------
100.00    0.003268                 15092        13 total

en 6.3:

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 99.99    1.372813          36     38219           nanosleep
  0.01    0.000104           0       409        43 futex
  0.00    0.000000           0         5           read
  0.00    0.000000           0         6           open
  0.00    0.000000           0         6           close
  0.00    0.000000           0         6           fstat
  0.00    0.000000           0        22           mmap
  0.00    0.000000           0        15           mprotect
  0.00    0.000000           0         1           munmap
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         3           rt_sigaction
  0.00    0.000000           0         3           rt_sigprocmask
  0.00    0.000000           0         7         7 access
  0.00    0.000000           0         3           clone
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         1           getrlimit
  0.00    0.000000           0         1           arch_prctl
  0.00    0.000000           0         1           set_tid_address
  0.00    0.000000           0         4           set_robust_list
------ ----------- ----------- --------- --------- ----------------
100.00    1.372917                 38716        50 total

El tiempo y los resultados de la carrera no son la misma prueba, por lo que los datos son un poco diferentes. Pero creo que puede mostrar algo.

Reviso la configuración del kernel CONFIG_HIGH_RES_TIMERS, CONFIG_HPET y CONFIG_HZ:

En 5.3:

$ cat /boot/config-`uname -r` |grep CONFIG_HIGH_RES_TIMERS
$ cat /boot/config-`uname -r` |grep CONFIG_HPET
CONFIG_HPET_TIMER=y
CONFIG_HPET_EMULATE_RTC=y
CONFIG_HPET=y
# CONFIG_HPET_RTC_IRQ is not set
# CONFIG_HPET_MMAP is not set
$ cat /boot/config-`uname -r` |grep CONFIG_HZ
# CONFIG_HZ_100 is not set
# CONFIG_HZ_250 is not set
CONFIG_HZ_1000=y
CONFIG_HZ=1000

En 6.3:

$ cat /boot/config-`uname -r` |grep CONFIG_HIGH_RES_TIMERS
CONFIG_HIGH_RES_TIMERS=y
$ cat /boot/config-`uname -r` |grep CONFIG_HPET
CONFIG_HPET_TIMER=y
CONFIG_HPET_EMULATE_RTC=y
CONFIG_HPET=y
CONFIG_HPET_MMAP=y
$ cat /boot/config-`uname -r` |grep CONFIG_HZ
# CONFIG_HZ_100 is not set
# CONFIG_HZ_250 is not set
# CONFIG_HZ_300 is not set
CONFIG_HZ_1000=y
CONFIG_HZ=1000

De hecho, también intento el código en arch en ARM y xubuntu13.04-amd64-desktop, lo mismo que en cent os 6.3.

Entonces, ¿qué puedo hacer para averiguar la razón de los diferentes usos de la CPU?

¿Tiene algo con la configuración del kernel?

Respuestas a la pregunta(1)

Su respuesta a la pregunta