/ src / core / cpufreq.cc
cpufreq.cc
 1  /*
 2   * cpufreq.cc
 3   *
 4   * This module scans /sys for CPUfreq info
 5   *
 6   *
 7   *
 8   */
 9  
10  #include "version.h"
11  #include "hw.h"
12  #include "osutils.h"
13  #include <string.h>
14  #include <sys/types.h>
15  #include <sys/stat.h>
16  #include <fcntl.h>
17  #include <limits.h>
18  #include <stdio.h>
19  #include <unistd.h>
20  #include <dirent.h>
21  
22  __ID("@(#) $Id$");
23  
24  #define DEVICESCPUFREQ "/sys/devices/system/cpu/cpu%d/cpufreq/"
25  
26  static long get_long(const string & path)
27  {
28    long result = 0;
29    FILE * in = fopen(path.c_str(), "r");
30  
31    if (in)
32    {
33      if(fscanf(in, "%ld", &result) != 1)
34        result = 0;
35      fclose(in);
36    }
37  
38    return result;
39  }
40  
41  
42  static string cpubusinfo(int cpu)
43  {
44    char buffer[10];
45  
46    snprintf(buffer, sizeof(buffer), "cpu@%d", cpu);
47  
48    return string(buffer);
49  }
50  
51  
52  bool scan_cpufreq(hwNode & node)
53  {
54    char buffer[PATH_MAX];
55    unsigned i =0;
56  
57    while(hwNode * cpu = node.findChildByBusInfo(cpubusinfo(i)))
58    {
59      snprintf(buffer, sizeof(buffer), DEVICESCPUFREQ, i);
60      if(exists(buffer))
61      {
62        unsigned long long max, cur;
63        pushd(buffer);
64  
65                                                    // in Hz
66        max = 1000*(unsigned long long)get_long("cpuinfo_max_freq");
67                                                    // in Hz
68        cur = 1000*(unsigned long long)get_long("scaling_cur_freq");
69        cpu->addCapability("cpufreq", "CPU Frequency scaling");
70        if(cur) cpu->setSize(cur);
71        if(max>cpu->getCapacity()) cpu->setCapacity(max);
72        popd();
73      }
74      i++;
75    }
76  
77    return true;
78  }