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;
    }


  • 相关阅读:
    Word Embedding理解
    几种简单的主题模型(生成模型)
    BTM学习小记
    LDA学习小记
    word2vec训练好的词向量
    java带字符编码编译
    web安全领域常见的攻击方式
    宝塔webhook布置gitee自动同步服务端
    unity3d学习路线
    缩略图含裁剪文件
  • 原文地址:https://www.cnblogs.com/aquester/p/9891593.html
Copyright © 2011-2022 走看看