~ubuntu-branches/ubuntu/dapper/cpufreqd/dapper

« back to all changes in this revision

Viewing changes to libsys.c

  • Committer: Bazaar Package Importer
  • Author(s): Mattia Dongili
  • Date: 2005-11-27 18:47:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127184742-9h26euwetr6kh1e6
Tags: 2.0.0-1

* New upstream release.
* cpufreqd.init: exit succesfully in case a stop is issued and
  cpufreqd is found running as requested by LSB thus making it
  possible to remove cpufreqd when cpufreqd itsef is stopped
  (closes: #340133)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (C) 2002,2003,2004  Mattia Dongili<dongili@supereva.it>
3
 
 *                                George Staikos <staikos@0wned.org>
4
 
 *
5
 
 *  2004.08.19
6
 
 *  - fixed /proc/stat reads in 2.6 kernels by Holger Ruckdeschel
7
 
 *  - detect kernel version (usefull??) by Mattia Dongili
8
 
 * 
9
 
 *  2003.16.08
10
 
 *  - added support for cpu monitoring, base code by Dietz Proepper and minor
11
 
 *    fixes by Mattia Dongili
12
 
 *    
13
 
 *  This program is free software; you can redistribute it and/or modify
14
 
 *  it under the terms of the GNU General Public License as published by
15
 
 *  the Free Software Foundation; either version 2 of the License, or
16
 
 *  (at your option) any later version.
17
 
 *
18
 
 *  This program is distributed in the hope that it will be useful,
19
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 
 *  GNU General Public License for more details.
22
 
 *
23
 
 *  You should have received a copy of the GNU General Public License
24
 
 *  along with this program; if not, write to the Free Software
25
 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 
 */
27
 
#include "libsys.h"
28
 
 
29
 
void libsys_preinit(void (*in_log) (const int prio, const char *fmt, ...), general *config);
30
 
 
31
 
static unsigned int old_weighted_activity = 0,
32
 
                    old_time = 0;
33
 
 
34
 
static int get_kversion(void) {
35
 
  FILE *fp;
36
 
  char kver[256];
37
 
  int f = 0;
38
 
  
39
 
  fp = fopen ("/proc/version", "r");
40
 
  if (!fp) {
41
 
    cp_log(LOG_ERR, "get_kversion(): %s: %s\n", "/proc/version", strerror(errno));
42
 
    return -1;
43
 
  }
44
 
  do {
45
 
    f = fscanf (fp, "Linux version %s", kver);
46
 
  } while (f != 1);
47
 
  fclose(fp);
48
 
  kver[255] = '\0';
49
 
 
50
 
  cp_log(LOG_INFO, "get_kversion(): read kernel version %s.\n", kver);
51
 
  
52
 
  if (strstr(kver, "2.6") == kver) {
53
 
    cp_log(LOG_INFO, "get_kversion(): kernel version is 2.6.\n");
54
 
    return KVER_26;
55
 
  } else if (strstr(kver, "2.4") == kver) {
56
 
    cp_log(LOG_INFO, "get_kversion(): kernel version is 2.4.\n");
57
 
    return KVER_24;
58
 
  } else {
59
 
    cp_log(LOG_WARNING, "Unknown kernel version let's try to continue assuming a 2.6 kernel.\n");
60
 
    return KVER_26;
61
 
  }
62
 
 
63
 
}
64
 
 
65
 
void libsys_preinit ( void (*in_log) (const int prio, const char *fmt, ...), general *config ) {
66
 
  cp_log = in_log;
67
 
  configuration = config;
68
 
  config->kver = get_kversion();
69
 
  cp_log(LOG_INFO, "libsys_preinit(): pre-initialization done.\n");
70
 
}
71
 
 
72
 
/*
73
 
int get_cpu(sys_info *si) {
74
 
*/
75
 
int get_cpu(void) {
76
 
  FILE* fp;
77
 
  int f;
78
 
  unsigned int c_user, c_nice, c_sys, c_time, delta_time, delta_activity, weighted_activity, perc;
79
 
  unsigned long int c_idle;
80
 
  unsigned long int c_iowait=0, c_irq=0, c_softirq=0; /* for linux 2.6 only */
81
 
 
82
 
  /* read raw jiffies... */
83
 
  fp = fopen ("/proc/stat", "r");
84
 
  if (!fp) {
85
 
    cp_log(LOG_ERR, "get_cpu(): %s: %s\n", "/proc/stat", strerror(errno));
86
 
    return -1;
87
 
  }
88
 
  do {
89
 
    f = fscanf (fp,
90
 
                "cpu  %u %u %u %lu %lu %lu %lu",
91
 
                &c_user, &c_nice, &c_sys, &c_idle, &c_iowait, &c_irq, &c_softirq);
92
 
 
93
 
  } while ((f!=4 && configuration->kver==KVER_24) || (f!=7 && configuration->kver==KVER_26));
94
 
  fclose(fp);
95
 
 
96
 
  cp_log(LOG_DEBUG,
97
 
         "get_cpu(): CPU c_user=%d c_nice=%d c_sys=%d c_idle=%d c_iowait=%d c_irq=%d c_softirq=%d.\n",
98
 
         c_user, c_nice, c_sys, c_idle, c_iowait, c_irq, c_softirq);
99
 
  /* calculate total jiffies, weight them and save */
100
 
  c_sys += c_irq + c_softirq;
101
 
  c_idle += c_iowait;
102
 
  c_time = c_user + c_nice + c_sys + c_idle;
103
 
  delta_time = c_time - old_time;
104
 
  old_time = c_time;
105
 
 
106
 
  /*
107
 
  si->cur_sys_activity = c_user + c_sys;
108
 
  si->cur_nice_activity = c_nice;
109
 
  */
110
 
 
111
 
  weighted_activity = c_user + c_nice / 3 + c_sys;
112
 
  delta_activity = weighted_activity - old_weighted_activity;
113
 
  old_weighted_activity = weighted_activity;
114
 
 
115
 
  cp_log(LOG_DEBUG,
116
 
         "get_cpu(): CPU delta_activity=%d delta_time=%d weighted_activity=%d.\n",
117
 
         delta_activity, delta_time, weighted_activity);
118
 
 
119
 
  if ( delta_activity > delta_time || delta_time <= 0) {
120
 
    perc = 100;
121
 
  } else {
122
 
    perc = delta_activity * 100 / delta_time;
123
 
  }
124
 
  cp_log(LOG_INFO, "get_cpu(): CPU usage = %d.\n", perc);
125
 
 
126
 
  return perc;
127
 
}