Back to index

lshw  02.16
cpufreq.cc
Go to the documentation of this file.
00001 /*
00002  * cpufreq.cc
00003  *
00004  * This module scans /sys for CPUfreq info
00005  *
00006  *
00007  *
00008  */
00009 
00010 #include "version.h"
00011 #include "hw.h"
00012 #include "osutils.h"
00013 #include <string.h>
00014 #include <sys/types.h>
00015 #include <sys/stat.h>
00016 #include <fcntl.h>
00017 #include <stdio.h>
00018 #include <unistd.h>
00019 #include <dirent.h>
00020 
00021 __ID("@(#) $Id: cpufreq.cc 2433 2012-01-10 22:01:30Z lyonel $");
00022 
00023 #define DEVICESCPUFREQ "/sys/devices/system/cpu/cpu%d/cpufreq/"
00024 
00025 static long get_long(const string & path)
00026 {
00027   long result = 0;
00028   FILE * in = fopen(path.c_str(), "r");
00029 
00030   if (in)
00031   {
00032     if(fscanf(in, "%ld", &result) != 1)
00033       result = 0;
00034     fclose(in);
00035   }
00036 
00037   return result;
00038 }
00039 
00040 
00041 static string cpubusinfo(int cpu)
00042 {
00043   char buffer[10];
00044 
00045   snprintf(buffer, sizeof(buffer), "cpu@%d", cpu);
00046 
00047   return string(buffer);
00048 }
00049 
00050 
00051 bool scan_cpufreq(hwNode & node)
00052 {
00053   char buffer[PATH_MAX];
00054   unsigned i =0;
00055 
00056   while(hwNode * cpu = node.findChildByBusInfo(cpubusinfo(i)))
00057   {
00058     snprintf(buffer, sizeof(buffer), DEVICESCPUFREQ, i);
00059     if(exists(buffer))
00060     {
00061       unsigned long long max, min, cur;
00062       pushd(buffer);
00063 
00064                                                   // in Hz
00065       min = 1000*(unsigned long long)get_long("cpuinfo_min_freq");
00066                                                   // in Hz
00067       max = 1000*(unsigned long long)get_long("cpuinfo_max_freq");
00068                                                   // in Hz
00069       cur = 1000*(unsigned long long)get_long("scaling_cur_freq");
00070       cpu->addCapability("cpufreq", "CPU Frequency scaling");
00071       if(cur) cpu->setSize(cur);
00072       if(max>cpu->getCapacity()) cpu->setCapacity(cur);
00073       popd();
00074     }
00075     i++;
00076   }
00077 
00078   return true;
00079 }