~ubuntu-branches/ubuntu/saucy/cpufreqd/saucy-updates

« back to all changes in this revision

Viewing changes to src/cpufreqd_tau.c

  • Committer: Bazaar Package Importer
  • Author(s): Mattia Dongili
  • Date: 2006-12-17 17:13:57 UTC
  • mfrom: (3.1.5 feisty)
  • Revision ID: james.westby@ubuntu.com-20061217171357-atrpxy6jqdq846jm
Tags: 2.2.1-2
Provide a more conservative config files, users owning non ondemand
capable CPU have complained. (Closes: #400580)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2006  Victor de la Fuente Castillo <vniebla@gmail.com>
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 *
 
18
 *  Based upon the pmu version:
 
19
 *  Copyright (C) 2003,2004  Rene Rebe <rene@rocklinux.org>
 
20
 *                2005       Mattia Dongili <malattia@linux.it>
 
21
 * 
 
22
 */
 
23
 
 
24
#include <ctype.h>
 
25
#include <errno.h>
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <string.h>
 
29
#include "cpufreqd_plugin.h"
 
30
 
 
31
#  define CPU_INFO_FILE         "/proc/cpuinfo"
 
32
 
 
33
struct temperature_interval {
 
34
        int min, max;
 
35
};
 
36
 
 
37
struct temperature_interval tau_temperature;
 
38
 
 
39
static char tag[255];
 
40
static char val[255];
 
41
 
 
42
static int tokenize (FILE *fp, char *t, char *v) {
 
43
        char str[255];
 
44
        char *s1, *s2;
 
45
 
 
46
        t[0] = v[0] = '\0';
 
47
 
 
48
        if (fgets(str, 255, fp) == NULL)
 
49
                return EOF;
 
50
 
 
51
        if ((s1 = strtok(str, ":")) == NULL)
 
52
                return 0;
 
53
 
 
54
        s2 = s1 + strlen (s1) - 1;
 
55
        /* remove trailing spaces */
 
56
        for ( ; s1 != s2 ; --s2) {
 
57
                if (!isspace(*s2)) {
 
58
                        break;
 
59
                } else {
 
60
                        *s2 = '\0';
 
61
                }
 
62
        }
 
63
 
 
64
        strncpy (t, s1, 255);
 
65
        t[254] = '\0';
 
66
 
 
67
        if ((s1 = strtok(NULL, ":")) == NULL)
 
68
                return 0;
 
69
 
 
70
        for ( ; *s1 != 0 ; ++s1) {
 
71
                if (!isspace(*s1)) {
 
72
                        break;
 
73
                }
 
74
        }
 
75
        s2 = s1 + strlen (s1) - 1;
 
76
        /* remove trailing spaces */
 
77
        for ( ; s1 != s2 ; --s2) {
 
78
                if (!isspace(*s2)) {
 
79
                        break;
 
80
                } else {
 
81
                        *s2 = '\0';
 
82
                }
 
83
        }
 
84
 
 
85
        strncpy (v, s1, 255);
 
86
        v[254] = '\0';
 
87
 
 
88
        return 1;
 
89
}
 
90
 
 
91
static int tau_init(void) {
 
92
        FILE *fp;
 
93
 
 
94
        fp = fopen(CPU_INFO_FILE, "r");
 
95
        if (!fp) {
 
96
                clog(LOG_ERR, "%s: %s\n", CPU_INFO_FILE, strerror(errno));
 
97
                return -1;
 
98
        }
 
99
 
 
100
        fclose(fp);
 
101
 
 
102
        clog(LOG_NOTICE, "/proc/cpuinfo file found\n");
 
103
 
 
104
        return 0;
 
105
}
 
106
 
 
107
static int tau_update(void) {
 
108
 
 
109
        FILE *fp;
 
110
 
 
111
        fp = fopen(CPU_INFO_FILE, "r");
 
112
        if (!fp) {
 
113
                clog(LOG_ERR, "%s: %s\n", CPU_INFO_FILE, strerror(errno));
 
114
                return -1;
 
115
        }
 
116
 
 
117
        char str[255];
 
118
 
 
119
        if (fgets(str, 255, fp) == NULL){
 
120
          clog(LOG_INFO,"%s",str);
 
121
          fclose(fp);
 
122
          return -1;
 
123
        }
 
124
 
 
125
        while (tokenize(fp, tag, val) != EOF) {
 
126
                if (strcmp(tag, "temperature") == 0) {
 
127
                  int readed; 
 
128
                  if (((readed=sscanf(val, "%d-%d", &(tau_temperature.min), &(tau_temperature.max))) < 1) 
 
129
                      || (readed >2)) {
 
130
                    clog(LOG_ERR, "wrong temperature value %s\n", val);
 
131
                    fclose(fp);
 
132
                    return -1;
 
133
                  } else if (readed == 1) {
 
134
                    //Temperature is not an interval
 
135
                    tau_temperature.max = tau_temperature.min;
 
136
                  }
 
137
                  clog(LOG_INFO,"TAU temperature = %d-%d",tau_temperature.min, tau_temperature.max);
 
138
                  break; //Reading more is a waste of time
 
139
                }
 
140
        }
 
141
        fclose(fp);
 
142
 
 
143
        return 0;
 
144
}
 
145
 
 
146
static int tau_parse(const char *ev, void **obj) {
 
147
        struct temperature_interval *tau_temperature_interval = malloc(sizeof(struct temperature_interval));
 
148
        if (tau_temperature_interval == NULL) {
 
149
                clog(LOG_ERR, "couldn't make enough room for tau_temperature_interval (%s)\n",
 
150
                                strerror(errno));
 
151
                return -1;
 
152
        }
 
153
        tau_temperature_interval->min = tau_temperature_interval->max = 0;
 
154
        clog(LOG_DEBUG, "called with %s\n", ev);
 
155
 
 
156
        if (sscanf(ev, "%d-%d", &(tau_temperature_interval->min), &(tau_temperature_interval->max)) != 2) {
 
157
                clog(LOG_ERR, "wrong parameter %s\n", ev);
 
158
                free(tau_temperature_interval);
 
159
                return -1;
 
160
        }
 
161
 
 
162
        clog(LOG_INFO, "parsed %d-%d\n", tau_temperature_interval->min, tau_temperature_interval->max);
 
163
 
 
164
        *obj = tau_temperature_interval;
 
165
        return 0;
 
166
}
 
167
 
 
168
static int tau_evaluate(const void *s) {
 
169
        const struct temperature_interval *ti = (const struct temperature_interval *)s;
 
170
 
 
171
        clog(LOG_DEBUG, "called %d-%d , actual temperature: %d-%d\n", ti->min, ti->max, tau_temperature.min, tau_temperature.max);
 
172
 
 
173
        return ((tau_temperature.min<=ti->min && tau_temperature.max>=ti->min) 
 
174
                || (tau_temperature.min<=ti->max && tau_temperature.max>=ti->max)
 
175
                || (ti->max >= tau_temperature.max && ti->min<=tau_temperature.min))? MATCH : DONT_MATCH;
 
176
}
 
177
 
 
178
static struct cpufreqd_keyword kw[] = {
 
179
        { .word = "tau_temperature",  .parse = &tau_parse, .evaluate = &tau_evaluate },
 
180
        { .word = NULL,       .parse = NULL,           .evaluate = NULL              }
 
181
};
 
182
 
 
183
static struct cpufreqd_plugin tau = {
 
184
        .plugin_name      = "tau_plugin",       /* plugin_name */
 
185
        .keywords         = kw,                 /* config_keywords */
 
186
        .plugin_init      = &tau_init,          /* plugin_init */
 
187
        .plugin_update    = &tau_update         /* plugin_update */
 
188
};
 
189
 
 
190
struct cpufreqd_plugin *create_plugin (void) {
 
191
        return &tau;
 
192
}