File: libsys.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 (127 lines) | stat: -rw-r--r-- 4,127 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
124
125
126
127
/*
 *  Copyright (C) 2002,2003,2004  Mattia Dongili<dongili@supereva.it>
 *                                George Staikos <staikos@0wned.org>
 *
 *  2004.08.19
 *  - fixed /proc/stat reads in 2.6 kernels by Holger Ruckdeschel
 *  - detect kernel version (usefull??) by Mattia Dongili
 * 
 *  2003.16.08
 *  - added support for cpu monitoring, base code by Dietz Proepper and minor
 *    fixes by Mattia Dongili
 *    
 *  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 "libsys.h"

void libsys_preinit(void (*in_log) (const int prio, const char *fmt, ...), general *config);

static unsigned int old_weighted_activity = 0,
                    old_time = 0;

static int get_kversion(void) {
  FILE *fp;
  char kver[256];
  int f = 0;
  
  fp = fopen ("/proc/version", "r");
  if (!fp) {
    cp_log(LOG_ERR, "get_kversion(): %s: %s\n", "/proc/version", strerror(errno));
    return -1;
  }
  do {
    f = fscanf (fp, "Linux version %s", kver);
  } while (f != 1);
  fclose(fp);
  kver[255] = '\0';

  cp_log(LOG_INFO, "get_kversion(): read kernel version %s.\n", kver);
  
  if (strstr(kver, "2.6") == kver) {
    cp_log(LOG_INFO, "get_kversion(): kernel version is 2.6.\n");
    return KVER_26;
  } else if (strstr(kver, "2.4") == kver) {
    cp_log(LOG_INFO, "get_kversion(): kernel version is 2.4.\n");
    return KVER_24;
  } else {
    cp_log(LOG_WARNING, "Unknown kernel version let's try to continue assuming a 2.6 kernel.\n");
    return KVER_26;
  }

}

void libsys_preinit ( void (*in_log) (const int prio, const char *fmt, ...), general *config ) {
  cp_log = in_log;
  configuration = config;
  config->kver = get_kversion();
  cp_log(LOG_INFO, "libsys_preinit(): pre-initialization done.\n");
}

/*
int get_cpu(sys_info *si) {
*/
int get_cpu(void) {
  FILE* fp;
  int f;
  unsigned int c_user, c_nice, c_sys, c_time, delta_time, delta_activity, weighted_activity, perc;
  unsigned long int c_idle;
  unsigned long int c_iowait=0, c_irq=0, c_softirq=0; /* for linux 2.6 only */

  /* read raw jiffies... */
  fp = fopen ("/proc/stat", "r");
  if (!fp) {
    cp_log(LOG_ERR, "get_cpu(): %s: %s\n", "/proc/stat", strerror(errno));
    return -1;
  }
  do {
    f = fscanf (fp,
                "cpu  %u %u %u %lu %lu %lu %lu",
                &c_user, &c_nice, &c_sys, &c_idle, &c_iowait, &c_irq, &c_softirq);

  } while ((f!=4 && configuration->kver==KVER_24) || (f!=7 && configuration->kver==KVER_26));
  fclose(fp);

  cp_log(LOG_DEBUG,
         "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",
         c_user, c_nice, c_sys, c_idle, c_iowait, c_irq, c_softirq);
  /* calculate total jiffies, weight them and save */
  c_sys += c_irq + c_softirq;
  c_idle += c_iowait;
  c_time = c_user + c_nice + c_sys + c_idle;
  delta_time = c_time - old_time;
  old_time = c_time;

  /*
  si->cur_sys_activity = c_user + c_sys;
  si->cur_nice_activity = c_nice;
  */

  weighted_activity = c_user + c_nice / 3 + c_sys;
  delta_activity = weighted_activity - old_weighted_activity;
  old_weighted_activity = weighted_activity;

  cp_log(LOG_DEBUG,
         "get_cpu(): CPU delta_activity=%d delta_time=%d weighted_activity=%d.\n",
         delta_activity, delta_time, weighted_activity);

  if ( delta_activity > delta_time || delta_time <= 0) {
    perc = 100;
  } else {
    perc = delta_activity * 100 / delta_time;
  }
  cp_log(LOG_INFO, "get_cpu(): CPU usage = %d.\n", perc);

  return perc;
}