获取CPU usage的命令

77 views
Skip to first unread message

杨亚梦

unread,
Sep 9, 2014, 9:31:36 PM9/9/14
to sh...@googlegroups.com
现在搞的项目,需要限制进程的cpu使用率,所以要获取当前的使用率。

网上找到
top -b -n2 | grep "Cpu(s)"|tail -n 1 | awk '{print $2 + $4}'
这样的命令,只能这样吗?有没有其它命令?

XiongJia Le

unread,
Sep 9, 2014, 9:56:22 PM9/9/14
to sh...@googlegroups.com
装 sysstat 用 sar:  http://linux.die.net/man/1/sar
或者 vmstat 的最后激烈也有

--
-- 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/d/optout

杨亚梦

unread,
Sep 9, 2014, 10:03:11 PM9/9/14
to sh...@googlegroups.com
谢谢,对Linux不太懂,感觉这些方法都是一种方式,就是通过命令得到一些数据,然后从中解析。

是不是就没有一个封装好的命令去获取这个数据阿

Shell Xu

unread,
Sep 9, 2014, 10:06:31 PM9/9/14
to shlug
限制CPU使用率?限制可用核数可不可以?
如果可以,你可以直接用cgroups。
彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
blog: http://shell909090.org/blog/

liyaoshi

unread,
Sep 9, 2014, 11:32:14 PM9/9/14
to sh...@googlegroups.com
又是老板拍脑袋想出来的馊主意

如果一个应用程序需要那么多cpu资源,你限制了,他就跑不动了

要么变频要么改代码,限制使用,纯粹文盲提法

XiongJia Le

unread,
Sep 9, 2014, 11:41:21 PM9/9/14
to sh...@googlegroups.com
这些都是从 /proc 取得... sar 的实现也就只是解析 proc 你可以看他们实现 https://github.com/sysstat/sysstat/blob/master/sar.c

你 parse 他们输出没有什么不好...他们统计过一次,省得自己计算...

杨亚梦

unread,
Sep 9, 2014, 11:51:58 PM9/9/14
to sh...@googlegroups.com
也不能这么说。

一台机器上需要同时运行若干个进程,每个进程都是我们自己实现的一些计算函数。

要保证不让某个进程占掉全部CPU资源。

不在乎一个进程跑多久。

杨亚梦

unread,
Sep 10, 2014, 12:02:01 AM9/10/14
to sh...@googlegroups.com
最好还是限制 使用率。

我看了cgroups,还是不知道怎么用。

不过还是很感谢!

在 2014年9月10日 上午10:05,Shell Xu <shell...@gmail.com>写道:

none_nobody

unread,
Sep 10, 2014, 2:39:51 AM9/10/14
to sh...@googlegroups.com
进程啊,你确定不是线程? 如果是线程,自己写个线程池+调度器吧,肯定能控制住。

进程也许能控制住,不过没写过。

On Wednesday, September 10, 2014 11:51:58 AM UTC+8, mz0827 wrote:
也不能这么说。

一台机器上需要同时运行若干个进程,每个进程都是我们自己实现的一些计算函数。

要保证不让某个进程占掉全部CPU资源。

liyaoshi

unread,
Sep 10, 2014, 2:41:20 AM9/10/14
to sh...@googlegroups.com
QNX 有aps 可以干这个活,不过,我觉得很傻的样子
这个LINUX多是平衡调度了,要这玩意干嘛呢

Shell Xu

unread,
Sep 10, 2014, 2:56:04 AM9/10/14
to shlug
因为cgroups干的事情不是限制使用率或者核数,而是亲缘性绑定。就是只能用指定的几个核。

Shell Xu

unread,
Sep 10, 2014, 2:56:35 AM9/10/14
to shlug
我听着这个需求其实比较像nice。

杨亚梦

unread,
Sep 10, 2014, 3:18:18 AM9/10/14
to sh...@googlegroups.com
确实是进程的。

用java的ProcessBuilder创建的进程。

在 2014年9月10日 下午2:39,none_nobody <lyx...@gmail.com>写道:

david pu

unread,
Sep 10, 2014, 4:38:32 AM9/10/14
to sh...@googlegroups.com
cgroup 可以干这事。用起来也简单,几个mount/mkdir/echo的命令即可。
当然省事就可以装cgroup-bin
() ASCII Ribbon Campaign
/\ Keep it simple!

Marco

unread,
Sep 11, 2014, 12:12:50 AM9/11/14
to shlug
nice +1

您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”论坛。

要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/d/optout



--
LinuX
Violin
Canon EOS

杨亚梦

unread,
Sep 11, 2014, 6:02:02 AM9/11/14
to sh...@googlegroups.com
我查了下nice,http://blog.csdn.net/thinkerabc/article/details/656236

这里说  nice是调整优先级

虽然我第一步获取 cpu usage后下一步是要限制这个进程的cpu usage
但是 nice好像没有  cpulimit 更符合我的需求

thanks all

Shell Xu

unread,
Sep 12, 2014, 2:46:30 AM9/12/14
to shlug
除非你打算限制功率,否则空耗CPU计算力没有什么实际的优势。
使用nice限制后,受限代码会利用一切CPU时间进行计算,但是又不会对高优先级上下文形成太大的威胁。

孑影

unread,
Sep 12, 2014, 2:53:04 AM9/12/14
to sh...@googlegroups.com
喵,AWS 不是有一个按照CPU 来计费的吗 ?

喵,你们老板是不是也想拍着做一个,,,
#风起看云涌,叶落品人生#

Shell Xu

unread,
Sep 12, 2014, 3:00:50 AM9/12/14
to shlug
那直接用cgroups,把上下文统统加进去,用cpuacct拿结果。
Reply all
Reply to author
Forward
0 new messages