~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/ksysguardd/Linux/loadavg.c

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org>
 
5
 
 
6
    This program is free software; you can redistribute it and/or
 
7
    modify it under the terms of version 2 of the GNU General Public
 
8
    License as published by the Free Software Foundation.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 
 
19
*/
 
20
 
 
21
#include <sys/types.h>
 
22
#include <sys/stat.h>
 
23
#include <fcntl.h>
 
24
#include <unistd.h>
 
25
#include <stdio.h>
 
26
 
 
27
#include "ksysguardd.h"
 
28
#include "Command.h"
 
29
 
 
30
#include "loadavg.h"
 
31
 
 
32
static int LoadAvgOK = 0;
 
33
static double LoadAvg1, LoadAvg5, LoadAvg15;
 
34
 
 
35
#define LOADAVGBUFSIZE 128
 
36
static char LoadAvgBuf[ LOADAVGBUFSIZE ];
 
37
static int Dirty = 0;
 
38
 
 
39
static void processLoadAvg( void )
 
40
{
 
41
  sscanf( LoadAvgBuf, "%lf %lf %lf", &LoadAvg1, &LoadAvg5, &LoadAvg15 );
 
42
  Dirty = 0;
 
43
}
 
44
 
 
45
/*
 
46
================================ public part =================================
 
47
*/
 
48
 
 
49
void initLoadAvg( struct SensorModul* sm )
 
50
{
 
51
  if ( updateLoadAvg() < 0 ) {
 
52
    LoadAvgOK = -1;
 
53
    return;
 
54
  } else
 
55
    LoadAvgOK = 1;
 
56
 
 
57
  registerMonitor( "cpu/system/loadavg1", "float", printLoadAvg1, printLoadAvg1Info, sm );
 
58
  registerMonitor( "cpu/system/loadavg5", "float", printLoadAvg5, printLoadAvg5Info, sm );
 
59
  registerMonitor( "cpu/system/loadavg15", "float", printLoadAvg15, printLoadAvg15Info, sm );
 
60
 
 
61
  /* Register some legacy monitors. These will be hidden from the module list. */
 
62
  registerLegacyMonitor( "cpu/loadavg1", "float", printLoadAvg1, printLoadAvg1Info, sm );
 
63
  registerLegacyMonitor( "cpu/loadavg5", "float", printLoadAvg5, printLoadAvg5Info, sm );
 
64
  registerLegacyMonitor( "cpu/loadavg15", "float", printLoadAvg15, printLoadAvg15Info, sm );
 
65
}
 
66
 
 
67
void exitLoadAvg( void )
 
68
{
 
69
  LoadAvgOK = -1;
 
70
}
 
71
 
 
72
int updateLoadAvg( void )
 
73
{
 
74
  size_t n;
 
75
  int fd;
 
76
 
 
77
  if ( LoadAvgOK < 0 )
 
78
    return -1;
 
79
 
 
80
  if ( ( fd = open( "/proc/loadavg", O_RDONLY ) ) < 0 ) {
 
81
    if ( LoadAvgOK != 0 )
 
82
      print_error( "Cannot open file \'/proc/loadavg\'!\n"
 
83
                   "The kernel needs to be compiled with support\n"
 
84
                   "for /proc file system enabled!\n" );
 
85
    return -1;
 
86
  }
 
87
 
 
88
  n = read( fd, LoadAvgBuf, LOADAVGBUFSIZE - 1 );
 
89
  if ( n == LOADAVGBUFSIZE - 1 || n <= 0 ) {
 
90
    log_error( "Internal buffer too small to read \'/proc/loadavg\'" );
 
91
 
 
92
    close( fd );
 
93
    return -1;
 
94
  }
 
95
 
 
96
  close( fd );
 
97
  LoadAvgBuf[ n ] = '\0';
 
98
  Dirty = 1;
 
99
 
 
100
  return 0;
 
101
}
 
102
 
 
103
void printLoadAvg1( const char* cmd )
 
104
{
 
105
  (void)cmd;
 
106
 
 
107
  if ( Dirty )
 
108
    processLoadAvg();
 
109
 
 
110
  output( "%f\n", LoadAvg1 );
 
111
}
 
112
 
 
113
void printLoadAvg1Info( const char* cmd )
 
114
{
 
115
  (void)cmd;
 
116
  output( "Load average 1 min\t0\t0\t\n" );
 
117
}
 
118
 
 
119
void printLoadAvg5( const char* cmd )
 
120
{
 
121
  (void)cmd;
 
122
 
 
123
  if ( Dirty )
 
124
    processLoadAvg();
 
125
 
 
126
  output( "%f\n", LoadAvg5 );
 
127
}
 
128
 
 
129
void printLoadAvg5Info( const char* cmd )
 
130
{
 
131
  (void)cmd;
 
132
  output( "Load average 5 min\t0\t0\t\n" );
 
133
}
 
134
 
 
135
void printLoadAvg15( const char* cmd )
 
136
{
 
137
  (void)cmd;
 
138
 
 
139
  if ( Dirty )
 
140
    processLoadAvg();
 
141
 
 
142
  output( "%f\n", LoadAvg15 );
 
143
}
 
144
 
 
145
void printLoadAvg15Info( const char* cmd )
 
146
{
 
147
  (void)cmd;
 
148
  output( "Load average 15 min\t0\t0\t\n" );
 
149
}