File: sysinfo.c

package info (click to toggle)
xsysinfo 1.6-12.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 176 kB
  • ctags: 510
  • sloc: ansic: 769; makefile: 535; sh: 3
file content (218 lines) | stat: -rw-r--r-- 5,541 bytes parent folder | download | duplicates (3)
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

/*
 * 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;

static char buffer[1024];

/* 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].total   = getentry("MemTotal:", buffer))  < 0 ||
	    (result[0].free    = getentry("MemFree:", buffer))   < 0 ||
	    (result[0].shared  = getentry("MemShared:", 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;
}

static struct load* last_load = NULL;
void get_load(struct load * result, int load_number)
{
	struct load curr_load;
	char *ptr = buffer;
	int i;

	reread( load_fd, "get_load" );
	for ( i = 0; i < load_number; ++i )
		ptr = strchr( ptr, '\n' ) + 1;
	sscanf( ptr, "%*s %lu %lu %lu %lu\n", &curr_load.user,
		&curr_load.nice, &curr_load.system, &curr_load.idle );
	curr_load.total = curr_load.user + curr_load.nice + curr_load.system +
		curr_load.idle;

	result->total  = curr_load.total  - last_load[load_number].total;
	result->user   = curr_load.user   - last_load[load_number].user;
	result->nice   = curr_load.nice   - last_load[load_number].nice;
	result->system = curr_load.system - last_load[load_number].system;
	result->idle   = curr_load.idle   - last_load[load_number].idle;
	last_load[load_number].total  = curr_load.total;
	last_load[load_number].user   = curr_load.user;
	last_load[load_number].nice   = curr_load.nice;
	last_load[load_number].system = curr_load.system;
	last_load[load_number].idle   = curr_load.idle;
}

int get_load_number( void )
{
	int load_number = 0;
	char *ptr = buffer;

	reread( load_fd, "get_load_number" );
	while( strncmp( ptr, "cpu", 3 ) == 0 ) {
		load_number++;
		ptr = strchr( ptr, '\n' ) + 1;
	}
	if ( load_number && last_load == NULL ) {
		last_load = malloc(sizeof(*last_load) * load_number);
		if ( last_load == NULL ) {
			perror("get_load_number");
			exit(1);
		}
		memset(last_load, 0, sizeof(*last_load) * load_number);
	}
	return load_number;
}

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;
}