请教问题:libpthread升级后,程序cpu占用过大。

59 views
Skip to first unread message

Jian Qin

unread,
May 16, 2013, 5:04:58 AM5/16/13
to sh...@googlegroups.com
看了下/lib64里面,应该是2.5变成2.12。
rt,代码如下:
 g++ filename.c -pthread

 1#include <pthread.h>
 2#include <list>
 3#include <unistd.h>
 4#include <iostream>
 5using namespace std;
 6pthread_mutex_t _mutex;
 7list<int *> _task_list;
 8void * run(void*);
 9int main()
10{
11        int worker_num  = 4;
12        pthread_t pids[worker_num];
13        pthread_mutex_init(&_mutex, NULL);
14        for (int worker_i = 0; worker_i < worker_num; ++worker_i)
15        {
16                pthread_create(&(pids[worker_i]), NULL, run, NULL);
17        }
18        sleep(14);
19}
20
21
22void *run(void * args)
23{
24        int *recved_info;
25        int f = 1000005;
26        while (f > 0)
27        {
28                f--;
29                pthread_mutex_lock(&_mutex);
30                if (_task_list.empty())
31                {
32                        recved_info = NULL;
33                }
34                else
35                {
36                        recved_info = _task_list.front();
37                        _task_list.pop_front();
38                }
39                pthread_mutex_unlock(&_mutex);
40                if (recved_info == NULL)
41                {
42                        int f = usleep(1);
43                        continue;
44                }
45        }
46}

liyaoshi

unread,
May 16, 2013, 5:23:52 AM5/16/13
to sh...@googlegroups.com
关键是多大


2013/5/16 Jian Qin <love....@gmail.com>

--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 shlug+un...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
 
 

Jian Qin

unread,
May 16, 2013, 5:26:05 AM5/16/13
to sh...@googlegroups.com
主要问题在空闲时,应该会一直sleep,2.5下接近0%,2.12在15%~20%之间。这个就没法接受了。

liyaoshi於 2013年5月16日星期四UTC+8下午5時23分52秒寫道:

none_nobody

unread,
May 16, 2013, 5:32:49 AM5/16/13
to sh...@googlegroups.com
这事吧,应该写成一个thread pool ,  不要一直 pthread_create ,mutex_lock mutex_unlock 的,应该就好很多。

liyaoshi

unread,
May 16, 2013, 5:33:09 AM5/16/13
to sh...@googlegroups.com
你确定是libpthread引起的么?

top - 05:12:06 up  2:11,  5 users,  load average: 0.01, 0.04, 0.05
Tasks:  84 total,   1 running,  83 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    445236k total,   435196k used,    10040k free,    56444k buffers
Swap:  1048572k total,      820k used,  1047752k free,   302904k cached

root@huang-vmware:/lib/i386-linux-gnu# ls -al libpthread*
-rwxr-xr-x 1 root root 121644 Mar  6  2012 libpthread-2.13.so
lrwxrwxrwx 1 root root     18 May 24  2012 libpthread.so.0 -> libpthread-2.13.so

ubuntu 11。04


2013/5/16 Jian Qin <love....@gmail.com>

none_nobody

unread,
May 16, 2013, 5:38:47 AM5/16/13
to sh...@googlegroups.com
还有, if (recved_info == NULL) 在两个版本之间会不会有区别,

如果程序一直没有执行 usleep, 则  CPU 用量确实会很高。



Jian Qin

unread,
May 16, 2013, 5:40:42 AM5/16/13
to sh...@googlegroups.com
这个是接手别人的程序,另外,场景也不需要pool,因为线程数是固定的,只是初始化时创建。

mutex是用来同步消息队列的,有个主线程用来接收数据,往队列里放的。

none_nobody於 2013年5月16日星期四UTC+8下午5時32分49秒寫道:

Jian Qin

unread,
May 16, 2013, 5:45:49 AM5/16/13
to sh...@googlegroups.com
不确定,但是同样的代码分别在两个系统上编译运行的表现是这样。
两个系统一个是centos 6.3一个是centos 5.3
这个简化代码里面除了用标准库,就是pthread库了。也只好怀疑在这点上了。

liyaoshi於 2013年5月16日星期四UTC+8下午5時33分09秒寫道:
你确定是libpthread引起的么?

top - 05:12:06 up  2:11,  5 users,  load average: 0.01, 0.04, 0.05
Tasks:  84 total,   1 running,  83 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    445236k total,   435196k used,    10040k free,    56444k buffers
Swap:  1048572k total,      820k used,  1047752k free,   302904k cached

root@huang-vmware:/lib/i386-linux-gnu# ls -al libpthread*
-rwxr-xr-x 1 root root 121644 Mar  6  2012 libpthread-2.13.so
lrwxrwxrwx 1 root root     18 May 24  2012 libpthread.so.0 -> libpthread-2.13.so

ubuntu 11。04


2013/5/16 Jian Qin <love....@gmail.com>
主要问题在空闲时,应该会一直sleep,2.5下接近0%,2.12在15%~20%之间。这个就没法接受了。

liyaoshi於 2013年5月16日星期四UTC+8下午5�23分52秒�道:

none_nobody

unread,
May 16, 2013, 5:46:51 AM5/16/13
to sh...@googlegroups.com
top - 17:45:18 up 21 days,  2:19,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  98 total,   1 running,  97 sleeping,   0 stopped,   0 zombie
Cpu(s):  2.3%us, 11.2%sy,  0.0%ni, 86.5%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  16467508k total,  6066296k used, 10401212k free,   780660k buffers
Swap:        0k total,        0k used,        0k free,  4159104k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                        
 8457 root      20   0 46856  996  828 S  102  0.0   0:07.53 xth 

root@gluster:/home/stock/software/bfilter/tests#  ldd xth
        linux-vdso.so.1 =>  (0x00007fffc85af000)
        libpthread.so.0 => /lib/libpthread.so.0 (0x00007ffc87781000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007ffc8746d000)
        libm.so.6 => /lib/libm.so.6 (0x00007ffc871e9000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007ffc86fd2000)
        libc.so.6 => /lib/libc.so.6 (0x00007ffc86c50000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ffc879b2000)

root@gluster:/home/stock/software/bfilter/tests# g++ --version
g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3

Chaos Eternal

unread,
May 16, 2013, 5:47:19 AM5/16/13
to sh...@googlegroups.com
profile之

Chaos Eternal

unread,
May 16, 2013, 5:48:13 AM5/16/13
to sh...@googlegroups.com
简单点,strace之也可以

none_nobody

unread,
May 16, 2013, 5:50:05 AM5/16/13
to sh...@googlegroups.com
thread pool 解决的就是这种狂 pthread_create 的应用场景。
Message has been deleted

Jian Qin

unread,
May 16, 2013, 5:54:21 AM5/16/13
to sh...@googlegroups.com
场景没有狂pthread_create,这个程序是简化的演示程序,本身程序很大,是作为服务一直跑的,但是线程创建后就不再变化的。

none_nobody於 2013年5月16日星期四UTC+8下午5時50分05秒寫道:

Chaos Eternal

unread,
May 16, 2013, 5:54:41 AM5/16/13
to sh...@googlegroups.com
我觉得threadpool还不如libev呢。。

2013/5/16 none_nobody <lyx...@gmail.com>:

Jian Qin

unread,
May 16, 2013, 6:05:05 AM5/16/13
to sh...@googlegroups.com
real    0m14.001s
user    0m0.139s
sys     0m1.933s

时间全花在sys里面了...gprof应该是萎了。

Soahc Lanrete於 2013年5月16日星期四UTC+8下午5時47分19秒寫道:

none_nobody

unread,
May 16, 2013, 6:16:04 AM5/16/13
to sh...@googlegroups.com

算了,我再想想。

none_nobody

unread,
May 16, 2013, 6:18:29 AM5/16/13
to sh...@googlegroups.com
有道理啊。

david pu

unread,
May 16, 2013, 6:51:12 AM5/16/13
to sh...@googlegroups.com
上perf
 ()   ASCII Ribbon Campaign
 /\   Keep it simple!

Jian Qin

unread,
May 16, 2013, 7:49:43 AM5/16/13
to sh...@googlegroups.com
我试过树莓派上的arch,mac os,还有xubuntu最新版,都存在CPU过高的问题。看样子,低cpu占用率的才是个案。

liyaoshi於 2013年5月16日星期四UTC+8下午5時33分09秒寫道:
你确定是libpthread引起的么?

top - 05:12:06 up  2:11,  5 users,  load average: 0.01, 0.04, 0.05
Tasks:  84 total,   1 running,  83 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    445236k total,   435196k used,    10040k free,    56444k buffers
Swap:  1048572k total,      820k used,  1047752k free,   302904k cached

root@huang-vmware:/lib/i386-linux-gnu# ls -al libpthread*
-rwxr-xr-x 1 root root 121644 Mar  6  2012 libpthread-2.13.so
lrwxrwxrwx 1 root root     18 May 24  2012 libpthread.so.0 -> libpthread-2.13.so

ubuntu 11。04


2013/5/16 Jian Qin <love....@gmail.com>
主要问题在空闲时,应该会一直sleep,2.5下接近0%,2.12在15%~20%之间。这个就没法接受了。

liyaoshi於 2013年5月16日星期四UTC+8下午5�23分52秒�道:

none_nobody

unread,
May 16, 2013, 10:31:33 PM5/16/13
to sh...@googlegroups.com
你在44-45 之间插入一个 usleep 再试一下。
Reply all
Reply to author
Forward
0 new messages