zoukankan      html  css  js  c++  java
  • 获取cpu频率的代码

    taskset是linux自带的一个命令,可用来将进程绑定到指定CPU
    相关的函数有: sched_setaffinity, CPU_CLR, CPU_ISSET, CPU_SET, CPU_ZERO


    // cpufreq库可在/usr/lib目录下找到
    // 编译: g++ -g -o x x.cpp -lcpufreq
    // 需要以root用户执行以下代码
    //#include <cpufreq.h>
    #include <stdio.h>
    #include <sys/sysinfo.h> // get_nprocs
    
    // 如果不存在/usr/include/cpufreq.h
    #ifndef _CPUFREQ_H
        extern "C" int cpufreq_cpu_exists(unsigned int cpu);
        extern "C" unsigned long cpufreq_get_freq_kernel(unsigned int cpu);
        extern "C" unsigned long cpufreq_get_freq_hardware(unsigned int cpu);
        extern "C" int cpufreq_get_hardware_limits(unsigned int cpu, unsigned long *min, unsigned long *max);
    #endif
    
    int main()
    {
        // 取得cpu core的个数,proc是processor的意思
        int nprocs = get_nprocs();
        for (int i=0; i<nprocs; ++i)
        {
            if (0 == cpufreq_cpu_exists(i))
            {   
                unsigned long min_freq = 0;
                unsigned long max_freq = 0;
                cpufreq_get_hardware_limits(i, &min_freq, &max_freq);
    
                printf("cpu[%d]:
    ", i); 
                printf("min_freq: %lu, max_freq: %lu
    ", min_freq, max_freq);
                printf("kernel freq: %lu, hardware freq: %lu
    ", cpufreq_get_freq_kernel(i), cpufreq_get_freq_hardware(i));
                printf("
    ");
            }   
        }   
    
        return 0;
    }


  • 相关阅读:
    About “condition variables”
    路由表的读法(zz)
    C++字符串之一(字符表示)
    Audio Codec Summary
    关于telnet
    C++ 之 new
    (转)让ubuntu9.10开机自动挂载NTFS分区
    WorkSpace And Static Library In GarbageXcode4
    mac os x 10.7下配置svn服务器
    ubuntu下设置双显示器扩展桌面
  • 原文地址:https://www.cnblogs.com/aquester/p/9891593.html
Copyright © 2011-2022 走看看