File: sys_check.c

package info (click to toggle)
cpufreqd 1.2.3-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,648 kB
  • ctags: 210
  • sloc: sh: 8,665; ansic: 1,648; makefile: 120
file content (123 lines) | stat: -rw-r--r-- 3,613 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 *  Copyright (C) 2002,2003,2004  Mattia Dongili<dongili@supereva.it>
 *                                George Staikos <staikos@0wned.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "cpufreqd.h"
#include "main.h"
#include "sys_check.h"

/* int select_pm_plugin
 * 
 * Check the existance of power interfaces trying
 * to call the check function for each plugin
 */



/* int find_cpufreq_interface(general *configuration)
 *
 * Checks whether we need to use the 2.4 or 2.5/2.6
 * cpufreq interface and sets the has_sysfs bit
 * accordingly.
 *
 * Returns 0 on success, -1 otherwise
 */
int find_cpufreq_interface(general *configuration) {
  struct stat sb;
  int rc;

  /* try first with the 2.6 series */
  rc = stat(CPUFREQ_SYSFS_INTERFACE, &sb);
  if (rc == 0) {
    FILE *fp;
    char buf[256];
    
    cp_log(LOG_DEBUG, "find_cpufreq_interface(): found %s interface.\n", CPUFREQ_SYSFS_INTERFACE);
    configuration->has_sysfs = 1;
    
    /* Read min freq to translate frequencies */
    fp = fopen(CPUFREQ_SYSFS_INTERFACE_CPUMIN, "r");
    if (!fp || !fgets(buf, 255, fp)) {
      cp_log(LOG_ERR, "find_cpufreq_interface(): %s: %s\n",
             CPUFREQ_SYSFS_INTERFACE_CPUMIN, strerror(errno));
      return -1;
    }
    configuration->cpu_min_freq = atoi(buf);
    cp_log(LOG_DEBUG, "find_cpufreq_interface(): minimum available frequency %d\n", configuration->cpu_min_freq);
    fclose(fp);

    /* Read max freq to translate frequencies */
    fp = fopen(CPUFREQ_SYSFS_INTERFACE_CPUMAX, "r");
    if (!fp || !fgets(buf, 255, fp)) {
      cp_log(LOG_ERR, "find_cpufreq_interface(): %s: %s\n",
             CPUFREQ_SYSFS_INTERFACE_CPUMAX, strerror(errno));
      return -1;
    }
    configuration->cpu_max_freq = atoi(buf);
    cp_log(LOG_DEBUG, "find_cpufreq_interface(): maximum available frequency %d\n", configuration->cpu_max_freq);
    fclose(fp);

    return 0;
  }
  
  /* try with the 2.4 series or the deprecated /prco/cpufreq interface also */
  rc = stat(CPUFREQ_PROC_INTERFACE, &sb);
  if (rc == 0) {
    cp_log(LOG_DEBUG, "find_cpufreq_interface(): found %s interface.\n", CPUFREQ_PROC_INTERFACE);
    configuration->has_sysfs = 0;
    return 0;
  }
  
  cp_log(LOG_ERR, "find_cpufreq_interface(): no cpufreq interface found.\n");
  return -1;
    
}


/* int get_cpu_num(general *configuration)
 *
 * Gets the number of installed CPUs from procfs
 * and sets cpu_num appropriately.
 *
 * Returns 0 on success, -1 on error
 */
int get_cpu_num(general *configuration)
{
  FILE *fp;
  int n;
  char line[256];

  fp = fopen(CPUINFO_PROC, "r");
  if(!fp) {
    cp_log(LOG_ERR, "get_cpu_num(): %s: %s\n", CPUINFO_PROC, strerror(errno));
    return -1;
  }
  
  n = 0;
  while(!feof(fp)) {
    fgets(line, 255, fp);
    if(!strncmp(line, "processor", 9))
      n++;
  }
  fclose(fp);
  
  cp_log(LOG_DEBUG, "get_cpu_num(): found %i CPUs\n", n);
  configuration->cpu_num = n;

  return 0;
}