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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
/*
* sysinfo.c - get kernel info
*
*/
/*
* Written by Gabor Herr <herr@iti.informatik.th-darmstadt.de>.
*
* Copyright (c) 1992, 1993 by Gabor Herr, all rights reserved.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear in
* supporting documentation, and that may name is not used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. I make no representations
* about the suitability of this software for any purpose. It is
* provided "as is" without express or implied warranty.
*/
/* $Id: sysinfo.c,v 1.3 1993/02/12 11:40:16 gabor Exp $ */
#include "sysinfo.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
static int meminfo_fd;
static int load_fd;
static int loadavg_fd;
static int swaps_fd;
// Sandro Tosi, 2008-01-21
// Size 16k, it must be bigger then any of /proc/{meminfo,loadavg,stat}
static char buffer[16384];
/* reread rewritten by Ernst Kloppenburg <kloppen@isr.uni-stuttgart.de> */
/* Now works with Linux 2.2 */
static void reread( int fd, char *message )
{
int i;
int bytesread;
if ( lseek( fd, 0L, 0 ) == 0 ) {
bytesread = read( fd, buffer, sizeof(buffer) - 1 );
if ( bytesread < (sizeof(buffer) - 1) ) {
buffer[bytesread] = 0x0;
return;
}
}
perror(message);
exit(1);
}
static int getentry(const char *tag, const char *bufptr) {
char *tail;
int retval, len = strlen(tag);
while (bufptr) {
if (*bufptr == '\n') bufptr++;
if (!strncmp(tag, bufptr, len)) {
retval = strtol(bufptr + len, &tail, 10);
if (tail == bufptr + len) {
return -1;
} else {
return retval;
}
}
bufptr = strchr( bufptr, '\n' );
}
return -1;
}
void get_meminfo( int *swapdevs, struct meminfo *result )
{
int i;
char *bufptr;
int res;
int sw_size, sw_used;
reread( meminfo_fd, "get_meminfo");
/* Try new /proc/meminfo format first */
*swapdevs = 1;
if ((result[0].shared = getentry("MemShared:", buffer)) < 0) {
/* Linux 2.4 has MemShared (but it's always zero?), 2.6 doesn't */
result[0].shared = 0;
}
if ((result[0].total = getentry("MemTotal:", buffer)) < 0 ||
(result[0].free = getentry("MemFree:", buffer)) < 0 ||
(result[0].buffers = getentry("Buffers:", buffer)) < 0 ||
(result[0].cache = getentry("Cached:", buffer)) < 0 ||
(result[1].total = getentry("SwapTotal:", buffer)) < 0 ||
(result[1].free = getentry("SwapFree:", buffer)) < 0) {
/* Fall back to old format */
bufptr = strchr( buffer, '\n' ) + 1;
sscanf( bufptr, "Mem: %d %*d %d %d %d",
&result[0].total,
&result[0].free,
&result[0].shared,
&result[0].cache );
result[0].buffers = -1;
for ( i = 1; i < MAX_SWAPFILES; i++ ) {
bufptr = strchr( bufptr, '\n' ) + 1;
if ( *bufptr == '\0' ||
(res = sscanf( bufptr, "Swap: %d %*d %d",
&result[i].total,
&result[i].free )) != 2 )
break;
}
*swapdevs = i - 1;
} else if (swaps_fd > 0) {
reread( swaps_fd, "get_meminfo");
bufptr = buffer;
for ( i = 1; i < MAX_SWAPFILES; i++ ) {
bufptr = strchr( bufptr, '\n' ) + 1;
if ( *bufptr == '\0' ||
(res = sscanf( bufptr, "%*s %*s %d %d",
&sw_size, &sw_used)) != 2 )
break;
result[i].total = sw_size;
result[i].free = sw_size - sw_used;
}
*swapdevs = i - 1;
}
}
double get_loadavg( void )
{
double load;
reread( loadavg_fd, "get_loadavg");
sscanf( buffer, "%lf", &load );
return load;
}
void get_load(struct load * result)
{
static int initialized = 0;
static struct load *last_load;
struct load *loadptr;
struct load curr_load;
int procs, i, index;
char *bufptr;
if(!initialized) {
procs = get_total_procs();
last_load = (struct load *)malloc(procs * sizeof(struct load));
loadptr = last_load;
for( i = 0; i < procs; i++) {
loadptr->total = 0;
loadptr->user = 0;
loadptr->system = 0;
loadptr->nice = 0;
loadptr->idle = 0;
loadptr->cpu = NULL;
loadptr++;
}
initialized = 1;
}
reread( load_fd, "get_load" );
bufptr = buffer;
if(sscanf(result->cpu, "cpu%d", &index) != 1)
loadptr = last_load;
else
loadptr = last_load + index;
bufptr = strstr(bufptr, result->cpu);
#if (1)
{
/* from 2.6 .../Documentation/filesystems/proc.txt */
unsigned long aux_us = 0, /* normal processes executing in user mode */
aux_ni = 0, /* niced processes executing in user mode */
aux_sy = 0, /* processes executing in kernel mode */
aux_id = 0, /* twiddling thumbs */
aux_wa = 0, /* waiting for I/O to complete */
aux_hi = 0, /* servicing hardware interrupts */
aux_si = 0; /* servicing softirqs */
sscanf( bufptr, "%*s %lu %lu %lu %lu %lu %lu %lu\n",
&aux_us, &aux_ni, &aux_sy, &aux_id,
&aux_wa, &aux_hi, &aux_si );
curr_load.user = aux_us;
curr_load.nice = aux_ni;
curr_load.system = aux_sy + aux_hi + aux_si;
curr_load.idle = aux_id + aux_wa;
}
#else
sscanf( bufptr, "%*s %lu %lu %lu %lu\n", &curr_load.user,
&curr_load.nice, &curr_load.system, &curr_load.idle );
#endif
curr_load.total = curr_load.user + curr_load.nice + curr_load.system +
curr_load.idle;
result->total = curr_load.total - loadptr->total;
result->user = curr_load.user - loadptr->user;
result->nice = curr_load.nice - loadptr->nice;
result->system = curr_load.system - loadptr->system;
result->idle = curr_load.idle - loadptr->idle;
loadptr->total = curr_load.total;
loadptr->user = curr_load.user;
loadptr->nice = curr_load.nice;
loadptr->system = curr_load.system;
loadptr->idle = curr_load.idle;
}
int sysinfo_init( void )
{
if ((meminfo_fd = open("/proc/meminfo",O_RDONLY)) < 0) {
perror("/proc/meminfo");
return 1;
}
if ((loadavg_fd = open("/proc/loadavg",O_RDONLY)) < 0) {
perror("/proc/loadavg");
return 1;
}
if ((load_fd = open("/proc/stat",O_RDONLY)) < 0) {
perror("/proc/stat");
return 1;
}
/* Failing here is not critical. We can get the info from meminfo. */
swaps_fd = open("/proc/swaps",O_RDONLY);
return 0;
}
int get_total_procs( void )
{
int count = 1;
char cpu_num[7];
char *bufptr;
reread( load_fd, "get_total_procs" );
while(1)
{
bufptr = buffer;
sprintf(cpu_num,"cpu%d ", count);
if(strstr(bufptr, cpu_num) != NULL)
count++;
else
return count;
}
return -1;
}
|