summaryrefslogtreecommitdiffstats
path: root/ksysguard/ksysguardd/Linux/cpuinfo.c
blob: de5deb80f5bb1935098f026fd9b33e9ce4705386 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
    KSysGuard, the KDE System Guard

    Copyright (c) 2000-2001 Chris Schlaeger <cs@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include "Command.h"
#include "ksysguardd.h"

#include "cpuinfo.h"

static int CpuInfoOK = 0;
static float* Clocks = 0;
static int CPUs = 0;

#define CPUINFOBUFSIZE (32 * 1024)
static char CpuInfoBuf[ CPUINFOBUFSIZE ];
static int Dirty = 0;
static struct SensorModul *CpuInfoSM;

static void processCpuInfo( void )
{
  char format[ 32 ];
  char tag[ 32 ];
  char value[ 256 ];
  char* cibp = CpuInfoBuf;
  int cpuId = 0;

  if ( !CpuInfoOK )
    return;

  sprintf( format, "%%%d[^:]: %%%d[^\n]\n", (int)sizeof( tag ) - 1,
           (int)sizeof( value ) - 1 );

  while ( sscanf( cibp, format, tag, value ) == 2 ) {
    char* p;
    tag[ sizeof( tag ) - 1 ] = '\0';
    value[ sizeof( value ) - 1 ] = '\0';
    /* remove trailing whitespaces */
    p = tag + strlen( tag ) - 1;
    /* remove trailing whitespaces */
    while ( ( *p == ' ' || *p == '\t' ) && p > tag )
      *p-- = '\0';

    if ( strcmp( tag, "processor" ) == 0 ) {
      if ( sscanf( value, "%d", &cpuId ) == 1 ) {
        if ( cpuId >= CPUs ) {
          char cmdName[ 24 ];
          if ( Clocks )
            free( Clocks );
          CPUs = cpuId + 1;
          Clocks = malloc( CPUs * sizeof( float ) );
          snprintf( cmdName, sizeof( cmdName ) - 1, "cpu%d/clock", cpuId );
          registerMonitor( cmdName, "float", printCPUxClock, printCPUxClockInfo,
                           CpuInfoSM );
        }
      }
    } else if ( strcmp( tag, "cpu MHz" ) == 0 )
      sscanf( value, "%f", &Clocks[ cpuId ] );

    /* Move cibp to begining of next line, if there is one. */
    cibp = strchr( cibp, '\n' );
    if ( cibp )
      cibp++;
    else
      cibp = CpuInfoBuf + strlen( CpuInfoBuf );
  }

  Dirty = 0;
}

/*
================================ public part =================================
*/

void initCpuInfo( struct SensorModul* sm )
{
  CpuInfoSM = sm;

  if ( updateCpuInfo() < 0 )
    return;

  processCpuInfo();
}

void exitCpuInfo( void )
{
  CpuInfoOK = -1;

  free( Clocks );
}

int updateCpuInfo( void )
{
  size_t n;
  int fd;

  if ( CpuInfoOK < 0 )
    return -1;

  if ( ( fd = open( "/proc/cpuinfo", O_RDONLY ) ) < 0 ) {
    if ( CpuInfoOK != 0 )
      print_error( "Cannot open file \'/proc/cpuinfo\'!\n"
                   "The kernel needs to be compiled with support\n"
                   "for /proc filesystem enabled!\n" );
    CpuInfoOK = -1;
    return -1;
  }

  n = 0;
  for(;;) {
    ssize_t len = read( fd, CpuInfoBuf + n, CPUINFOBUFSIZE - 1 - n );
    if( len < 0 ) {
      print_error( "Failed to read file \'/proc/cpuinfo\'!\n" );
      CpuInfoOK = -1;
      close( fd );
      return -1;
    }
    n += len;
    if( len == 0 ) /* reading finished */
      break;
    if( n == CPUINFOBUFSIZE - 1 ) {
      log_error( "Internal buffer too small to read \'/proc/cpuinfo\'" );
      CpuInfoOK = 0;
      close( fd );
      return -1;
    }
  }

  close( fd );
  CpuInfoOK = 1;
  CpuInfoBuf[ n ] = '\0';
  Dirty = 1;

  return 0;
}

void printCPUxClock( const char* cmd )
{
  int id;

  if ( Dirty )
    processCpuInfo();

  sscanf( cmd + 3, "%d", &id );
  fprintf( CurrentClient, "%f\n", Clocks[ id ] );
}

void printCPUxClockInfo( const char* cmd )
{
  int id;

  sscanf( cmd + 3, "%d", &id );
  fprintf( CurrentClient, "CPU%d Clock Frequency\t0\t0\tMHz\n", id );
}