[转]linux下使用C语言如何获得CPU的主频

来源:互联网 发布:加工中心模拟仿真软件 编辑:程序博客网 时间:2024/06/10 01:25
 
水木社区C程序设计语 → 同主题阅读
同主题阅读:[教]linux下使用C言如何得CPU的主

[上一主] [下一主]【分页: 12下一

 
[本篇全文] [本篇作者:hurbm] [讨论] [返回]
1
发信人: hurbm (独行客), 信区: CProgramming
标 题: [请教]linux下使用C语言如何获得CPU的主
发信站: 水木社区 (Thu May 18 23:48:44 2006), 站内

不知道有没有相
应的库函数一类的,请有经验的朋友指点一下。

--

※ 来源:·水木社区 newsmth.net·[FROM: 210.77.27.*]
 
[本篇全文本篇作者:chice] [进入讨论区] [返回顶部] ] [
2
发信人: chice (surrender myself to ...), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Thu May 18 23:54:07 2006), 站内

读取 /proc/stat  或者 /proc/cpuinfo 之类的文件

这个不属于C语言问题,下次记得在 LinuxDev 版问   :)


【 在 hurbm (独行客) 的大作中提到: 】
: 不知道有没有相函数一的,经验的朋友指点一下。


--

※ 来源:·水木社区 newsmth.net·[FROM: 59.66.156.*]
 
[本篇全文本篇作者:Chicyu] [进入讨论区] [返回顶部] ] [
3
发信人: Chicyu (民工于某), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 00:09:35 2006), 站内

#include<stdio.h>
#include<stdlib.h>
int main(){
        FILE *fp;
        char str[81];
        memset(str,0,81);
        fp=popen("cat /proc/cpuinfo|grep cpu// MHz|sed -e 's/.*:[^0-9]//'","r");
        if(fp<0){
                printf("无法读取CPU主频信息/n");
                exit(1);
        }
        fgets(str,80,fp);
        fclose(fp);
        double spd=atof(str);
        printf("您CPU的主频是%fMHz/n",spd);
        exit(0);
}

呵呵,不要那么凶....
【 在 chice (surrender myself to ...) 的大作中提到: 】
: /proc/stat  或者 /proc/cpuinfo 的文件
: 个不属于C问题,下次得在 LinuxDev   :)


--
我们懂得民主自由,却忘了伦理纲常
我们拥有音乐神童,却不识角徵宫商
我们能建起高楼大厦,却容不下一块公德牌坊
我们穿着西服革履,却没了自己的衣裳......
在哪里,那个礼仪之邦?在哪里,我的汉家儿郎?


※ 来源:·水木社区 newsmth.net·[FROM: 124.42.116.*]
 
[本篇全文本篇作者:chice] [进入讨论区] [返回顶部] ] [
4
发信人: chice (surrender myself to ...), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 00:10:35 2006), 站内


  晕啊,我最后用的是微笑的表情符号哎,我哪里凶了,55555……

【 在 Chicyu (民工于某) 的大作中提到: 】
: #include<stdio.h>
: #include<stdlib.h>
: int main(){
: ...................

--

※ 来源:·水木社区 newsmth.net·[FROM: 59.66.156.*]
 
[本篇全文本篇作者:Chicyu] [进入讨论区] [返回顶部] ] [
5
发信人: Chicyu (民工于某), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 00:11:33 2006), 站内

sigh,恕俺眼拙....:)

【 在 chice (surrender myself to ...) 的大作中提到: 】
:   啊,我最后用的是微笑的表情符号哎,我哪里凶了,55555……


--
我们懂得民主自由,却忘了伦理纲常
们拥有音乐神童,却不识角徵宫商
们能建起高楼大厦,却容不下一块公德牌坊
们穿着西服革履,却没了自己的衣裳......
在哪里,那个礼仪之邦?在哪里,我的汉家儿郎?


※ 来源:·水木社区 newsmth.net·[FROM: 124.42.116.*]
 
[本篇全文本篇作者:Nineveh] [进入讨论区] [返回顶部] ] [
6
发信人: Nineveh (我是爱北京的), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 00:34:00 2006), 站内

#include <stdio.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/time.h>

static int64_t rdtsc(void)
{
    unsigned int i, j;
    asm volatile ("rdtsc" : "=a"(i), "=d"(j) : );
    return ((int64_t)j<<32) + (int64_t)i;
}
int main()
{
    int64_t tsc_start, tsc_end;
    struct timeval tv_start, tv_end;
    int usec_delay;

    tsc_start = rdtsc();
    gettimeofday(&tv_start, NULL);
    usleep(100000);
    tsc_end = rdtsc();
    gettimeofday(&tv_end, NULL);

    usec_delay = 1000000 * (tv_end.tv_sec - tv_start.tv_sec)
        + (tv_end.tv_usec - tv_start.tv_usec);

    printf("cpu MHz/t/t: %.3f/n",
           (double)(tsc_end-tsc_start) / usec_delay);
}

【 在 hurbm (独行客) 的大作中提到: 】
: 不知道有没有相函数一的,经验的朋友指点一下。


--
Flee from Babylon, and go out of the land of the Chaldeans, and be like male
goats leading the flock. For I am going to stir up and bring against Babylon
a company of great nations from the land of the north; and they shall array
themselves against her; from there she shall be taken. Their arrows are like
the arrows of the skilled warrior who does not return empty-handed. Chaldea
shall be plundered; all who plunder her shall be sated, says the LORD.


※ 来源:·水木社区 newsmth.net·[FROM: 166.111.60.*]
 
[本篇全文本篇作者:Chicyu] [进入讨论区] [返回顶部] ] [
7
发信人: Chicyu (民工于某), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 01:35:07 2006), 站内

[root@Chicyu ~]# cat 1.c
#include <stdio.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/time.h>

static int64_t rdtsc(void)
{
    unsigned int i, j;
    asm volatile ("rdtsc" : "=a"(i), "=d"(j) : );
    return ((int64_t)j<<32) + (int64_t)i;
}
int main()
{
    int64_t tsc_start, tsc_end;
    struct timeval tv_start, tv_end;
    int usec_delay;

    tsc_start = rdtsc();
    gettimeofday(&tv_start, NULL);
    usleep(100000);
    tsc_end = rdtsc();
    gettimeofday(&tv_end, NULL);

    usec_delay = 1000000 * (tv_end.tv_sec - tv_start.tv_sec)
        + (tv_end.tv_usec - tv_start.tv_usec);

    printf("cpu MHz/t/t: %.3f/n",
           (double)(tsc_end-tsc_start) / usec_delay);
}

[root@Chicyu ~]# gcc -o 1 1.c
[root@Chicyu ~]# ./1
cpu MHz         : 105.476
[root@Chicyu ~]# ./1
cpu MHz         : 92.281
[root@Chicyu ~]# ./1
cpu MHz         : 86.374
[root@Chicyu ~]# ./1
cpu MHz         : 99.607
[root@Chicyu ~]# ./1
cpu MHz         : 102.738
[root@Chicyu ~]# ./1
cpu MHz         : 96.673
[root@Chicyu ~]# ./1
cpu MHz         : 220.904
[root@Chicyu ~]# ./1
cpu MHz         : 247.623
[root@Chicyu ~]# ./1
cpu MHz         : 724.219
[root@Chicyu ~]# ./1
cpu MHz         : 210.720
[root@Chicyu ~]# ./1
cpu MHz         : 235.210
[root@Chicyu ~]# ./1
cpu MHz         : 241.263
[root@Chicyu ~]# ./1
cpu MHz         : 226.249
[root@Chicyu ~]# ./1
cpu MHz         : 230.154
[root@Chicyu ~]# ./1
cpu MHz         : 244.584
[root@Chicyu ~]# ./1
cpu MHz         : 727.063
[root@Chicyu ~]# ./1
cpu MHz         : 222.506
[root@Chicyu ~]# ./1
cpu MHz         : 231.858
[root@Chicyu ~]# ./1
cpu MHz         : 222.609
[root@Chicyu ~]# ft
bash: ft: command not found
[root@Chicyu ~]# 粪特
bash: 粪特: command not found
[root@Chicyu ~]# cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 11
model name      : Mobile Intel(R) Pentium(R) III CPU - M  1333MHz
stepping        : 4
cpu MHz         : 1332.380
cache size      : 512 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 2
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pat pse36 mmx fxsr sse
bogomips        : 2666.67

[root@Chicyu ~]#

【 在 Nineveh (我是爱北京的) 的大作中提到: 】
: #include <stdio.h>
: #include <inttypes.h>
: #include <unistd.h>
: ...................

--
我们懂得民主自由,却忘了伦理纲常
们拥有音乐神童,却不识角徵宫商
们能建起高楼大厦,却容不下一块公德牌坊
们穿着西服革履,却没了自己的衣裳......
在哪里,那个礼
仪之邦?在哪里,我的汉家儿郎?


※ 来源:·水木社区 newsmth.net·[FROM: 124.42.116.*]
 
[本篇全文] [本篇作者:HaoYu] [讨论] [返回]
8
发信人: HaoYu (夏之幻·闭上双眼), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主
发信站: 水木社区 (Fri May 19 07:33:54 2006), 站内

这个读到的是OC前还是OC后的?

【 在 Chicyu (民工于某) 的大作中提到: 】
: #include<stdio.h>
: #include<stdlib.h>
: int main(){
: ...................

--

透过窗扇来仰望晴空 飞机的烟云有些神秘

                                        http://www.haoyu.net.cn/来源:·水木社区 newsmth.net·[FROM: 211.151.89.*]                  


 
[本篇全文] [本篇作者:abenbit] [] [返回]
9
发信人: abenbit (aben), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 08:35:05 2006), 站内

我没觉得他凶
说的很在理
【 在 Chicyu (民工于某) 的大作中提到: 】
: #include<stdio.h>
: #include<stdlib.h>
: int main(){
: ...................

--

※ 来源:·水木社区 newsmth.net·[FROM: 203.212.6.*]
 
[本篇全文本篇作者:snnn] [进入讨论区] [返回顶部] ] [
10
发信人: snnn (snnn), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 09:12:57 2006), 站内

用cpuid指令可以获取绝大部分信息,好像除了频率

【 在 hurbm (独行客) 的大作中提到: 】
: 不知道有没有相函数一的,经验的朋友指点一下。


--

※ 来源:·水木社区 newsmth.net·[FROM: 211.151.89.*]
 
[本篇全文本篇作者:hurbm] [进入讨论区] [返回顶部] ] [
11
发信人: hurbm (独行客), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 09:20:45 2006), 站内

很管用,谢谢兄台指点。
也想过读取proc/cpuinfo,但没想到可以用的像兄台列出的这么简洁。呵呵
【 在 Chicyu (民工于某) 的大作中提到: 】
: #include<stdio.h>
: #include<stdlib.h>
: int main(){
: ...................

--

※ 来源:·水木社区 newsmth.net·[FROM: 159.226.41.*]
 
[本篇全文本篇作者:ipou] [进入讨论区] [返回顶部] ] [
12
发信人: ipou (美索布达米亚), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 09:55:58 2006), 站内

大虾的随手一挥
就抖落下一堆知识够我这样菜人狠钻滴
pfpf

fp=popen("cat /proc/cpuinfo|grep cpu// MHz|sed -e 's/.*:[^0-9]//'","r");

这句换成
fp=popen("cat /proc/cpuinfo|cut -f 2 -d ":"","r");
可以吗?

【 在 Chicyu (民工于某) 的大作中提到: 】
: #include<stdio.h>
: #include<stdlib.h>
: int main(){
: ...................



--
生存还是死亡?

※ 来源:·水木社区
http://newsmth.net·[FROM: 159.226.219.*]
 
[本篇全文] [本篇作者:eematlab] [讨论] [返回]
13
发信人: eematlab (我的名字叫3z), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 10:34:08 2006), 站内

shell is not in the scope of CProgramming

cat /proc/cpuinf | awk -F: 'BEGIN{printf "cpu frequency is :"}{if($1~/cpu /)print $2}'
【 在 ipou (美索布达米亚) 的大作中提到: 】
: 的随手一
: 就抖落下一堆知识够这样菜人狠
: pfpf
: ...................



--
木受繩則直,金就礪則利,君子博學而日參省乎己,則知明而行無過矣。

※ 修改:·eematlab 于 May 19 10:40:57 修改本文·[FROM: 219.142.125.*]
※ 来源:·水木社区 http://newsmth.net·[FROM: 219.142.125.*]
 
[本篇全文] [本篇作者:yuhuan] [讨论] [返回]
14
发信人: yuhuan (康总御封板砖王), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主
发信站: 水木社区 (Fri May 19 11:09:04 2006), 站内

一点都不好,
这么个简单问题起那么多进程处理
【 在 hurbm (独行客) 的大作中提到: 】
: 很管用,谢谢兄台指点。
: 也想过读取proc/cpuinfo,但没想到可以用的像兄台列出的这么简洁。呵呵


--

※ 来源:·水木社区 newsmth.net·[FROM: 162.105.154.*]
 
[本篇全文] [本篇作者:FlawZero] [讨论] [返回]
15
发信人: FlawZero (零缺点), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主
发信站: 水木社区 (Fri May 19 11:14:23 2006), 站内

ft……
还起个sed
【 在 Chicyu (民工于某) 的大作中提到: 】
: #include<stdio.h>
: #include<stdlib.h>
: int main(){
: ...................

--

※ 来源:·水木社区 newsmth.net·[FROM: 222.130.181.*]
 
[本篇全文本篇作者:dvlt] [进入讨论区] [返回顶部] ] [
16
发信人: dvlt (饿了就要吃!), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 11:24:35 2006), 站内

哈哈,直接fopen就行了是吧...

【 在 yuhuan (康总御封板砖王) 的大作中提到: 】
: 一点都不好,这么简单问题起那


--
我的心,这只野鸟,在你的双眼中找到了天空。


※ 来源:·水木社区 newsmth.net·[FROM: 59.66.199.*]
 
[本篇全文本篇作者:Nineveh] [进入讨论区] [返回顶部] ] [
17
发信人: Nineveh (我是爱北京的), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 12:49:16 2006), 站内

funny

【 在 Chicyu (民工于某) 的大作中提到: 】
: [root@Chicyu ~]# cat 1.c
: #include <stdio.h>
: #include <inttypes.h>
: ...................

--
Flee from Babylon, and go out of the land of the Chaldeans, and be like male
goats leading the flock. For I am going to stir up and bring against Babylon
a company of great nations from the land of the north; and they shall array
themselves against her; from there she shall be taken. Their arrows are like
the arrows of the skilled warrior who does not return empty-handed. Chaldea
shall be plundered; all who plunder her shall be sated, says the LORD.


※ 来源:·水木社区 newsmth.net·[FROM: 166.111.60.*]
 
[本篇全文本篇作者:colois] [进入讨论区] [返回顶部] ] [
18
发信人: colois (work hard for 40 days), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 13:03:27 2006), 站内

母鸡很有闲心呀

【 在 Nineveh (我是爱北京的) 的大作中提到: 】
: #include <stdio.h>
: #include <inttypes.h>
: #include <unistd.h>
: ...................

--
自由


※ 来源:·水木社区 newsmth.net·[FROM: 211.151.89.*]
 
[本篇全文本篇作者:stonestar] [进入讨论区] [返回顶部] ] [
19
发信人: stonestar (:%s/mm/My mm/g), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 13:12:04 2006), 站内

how about this?
cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 15
model           : 2
model name      : Intel(R) Xeon(TM) CPU 3.06GHz
stepping        : 9
cpu MHz         : 3048.179
cache size      : 512 KB
physical id     : 0
siblings        : 2
runqueue        : 0
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 2
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm
bogomips        : 6081.74

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 15
model           : 2
model name      : Intel(R) Xeon(TM) CPU 3.06GHz
stepping        : 9
cpu MHz         : 3048.179
cache size      : 512 KB
physical id     : 3
siblings        : 2
runqueue        : 1
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 2
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm
bogomips        : 6094.84

processor       : 2
vendor_id       : GenuineIntel
cpu family      : 15
model           : 2
model name      : Intel(R) Xeon(TM) CPU 3.06GHz
stepping        : 9
cpu MHz         : 3048.179
cache size      : 512 KB
physical id     : 0
siblings        : 2
runqueue        : 0
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 2
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm
bogomips        : 6094.84

processor       : 3
vendor_id       : GenuineIntel
cpu family      : 15
model           : 2
model name      : Intel(R) Xeon(TM) CPU 3.06GHz
stepping        : 9
cpu MHz         : 3048.179
cache size      : 512 KB
physical id     : 3
siblings        : 2
runqueue        : 1
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 2
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm
bogomips        : 6094.84
【 在 Chicyu (民工于某) 的大作中提到: 】
: #include<stdio.h>
: #include<stdlib.h>
: int main(){
: ...................



--

今天在食堂吃饭时,突然发现食堂的ppmm多了。于是我对师弟说:“最近ppmm突然多了呀啊!”
师弟扭头看了看我说:“不是ppmm多了,是你的审美观降低了!
嗯 低头想想,我的审美观确实降低了,等到我出岛的时候,不知道审美观会降低到什么程度???

※ 来源:·水木社区
http://newsmth.net·[FROM: 210.51.195.*]
 
[本篇全文] [本篇作者:Nineveh] [讨论] [返回]
20
发信人: Nineveh (我是爱北京的), 信区: CProgramming
标 题: Re: [请教]linux下使用C语言如何获得CPU的主频
发信站: 水木社区 (Fri May 19 13:14:42 2006), 站内

xyt

【 在 stonestar (:%s/mm/My mm/g) 的大作中提到: 】
: how about this?
: cat /proc/cpuinfo
: processor       : 0
: ...................

--
Flee from Babylon, and go out of the land of the Chaldeans, and be like male
goats leading the flock. For I am going to stir up and bring against Babylon
a company of great nations from the land of the north; and they shall array
themselves against her; from there she shall be taken. Their arrows are like
the arrows of the skilled warrior who does not return empty-handed. Chaldea
shall be plundered; all who plunder her shall be sated, says the LORD.


※ 来源:·水木社区 newsmth.net·[FROM: 166.111.60.*]
tcon,745,27219,27220,27221,27222,27223,27224,27225,27226,27227,27228,27229,27231,27233,27234,27235,27237,27250,27253,27254,27255
[上一主] [下一主]【分页: 12下一
百宝箱| 退 | C程序设计语讨论 返回

+-R

 
 
原创粉丝点击