File: intr_stat.c

package info (click to toggle)
noflushd 2.7.5-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 792 kB
  • ctags: 302
  • sloc: ansic: 2,537; sh: 1,142; makefile: 85
file content (314 lines) | stat: -rw-r--r-- 6,681 bytes parent folder | download | duplicates (4)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * <intr_stat.c>
 *
 * Copyright (C) 2001 Laurent Pelecq
 *
 * 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
 * 
 * $Id: intr_stat.c,v 1.7 2005/05/08 22:17:51 nold Exp $
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "noflushd.h"
#include "bug.h"
#include "util.h"
#include "intr_stat.h"

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

#define UNKNOWN_INTERRUPT	"unknown"

#define LIST_DELIMITERS		",;"

/* FIXME I'd rather use args like "idle"/"active" to a single script to limit
 * footprint on disk caches. Even better something like:
 * /etc/noflushd/notify /dev/hda spinup, or .../notify irq idle.
 */
#define CMD_IF_IDLE		"/etc/noflushd/do_idle"
#define CMD_IF_ACTIVE		"/etc/noflushd/do_active"

typedef struct irq_stat_s_t *irq_stat_t;

struct irq_stat_s_t {
	int nr;				/* Irq line */
	long unsigned int count;	/* Stat counter */
	int active;			/* True if it should be monitored */
	char *name;			/* Name as in /proc/interrupts */
	irq_stat_t next;
};

static struct {
	irq_stat_t irq_head;	/* Linked list of irq line structs */
	int timeout;		/* Idle timeout for inactivity */
	int is_idle;		/* Inactivity flag */
	unsigned int nb_watch;	/* Number of irq lines watched */
	time_t stamp;		/* last modification */
} global;

/* Query for irq line n in irq list. Returns NULL on failure, irq entry
 * on success. */
static irq_stat_t get_irq(irq_stat_t head, int n)
{
	while (head) {
		if (head->nr == n)
			break;
		head = head->next;
	}
	return head;
}

/* Allocate new entry for irq line n. Returns new list head on success,
 * NULL on failure.
 */
static irq_stat_t alloc_irq(irq_stat_t head, int n)
{
	irq_stat_t irq;

	/* Sanity check. Don't do anything if irq already present. */
	irq = get_irq(head, n);
	if (irq) {
		DEBUG("Double alloc of irq #%d", n);
		return NULL;
	}

	irq = malloc(sizeof(*irq));
	if (!irq) {
		ERR("No mem for irq stat");
		return NULL;
	}

	irq->nr = n;
	irq->count = 0;
	irq->active = 0;
	irq->name = NULL;
	irq->next = head;

	return irq;
}
		

static irq_stat_t add_interrupt(irq_stat_t head, int n,
		                unsigned long int count, const char *name)
{
	irq_stat_t irq;
	
	if (!(irq = alloc_irq(head, n)))
		return NULL;
	
	irq->count = count;
	irq->name = strdup(name);
	if (!irq->name)
		irq->name = UNKNOWN_INTERRUPT;

	return irq;
}

static int parse_interrupts(time_t now)
{
	FILE *stat;
	char *line, *entry, *tmp;
	irq_stat_t irq;
	int ret = 1;
	unsigned int lineno = 0;

	stat = fopen("/proc/interrupts", "r");
	if (!stat)
		return 0;

	while ((line=get_line(stat))) {
		int n, nr;
		unsigned long int count;

		lineno++;
		
		entry = get_entry(line, 0);
		if (!entry || !isdigit(*entry)) {
			release_entry(entry);
			goto next_line;
		}

		nr = atoi(entry);

		release_entry(entry);

		n=1;
		count=0;
		
		while ((entry=get_entry(line, n++))) {
			if (!isdigit(*entry)) {
				break;
			}

			count += atol(entry);
			release_entry(entry);
		}

		if (!entry) {
			ERR("Weird line #%u in /proc/interrupts", lineno);
			ret = 0;
			goto err;
		}

		/* Argh!  The layout of /proc/interrupts differs across
		 * architectures.  Some contain the IRQ type in the
		 * next-to-last field, some don't.
		 */
		if ((tmp = get_remains(line, n))) {
			release_entry(entry);
			entry = tmp;
		}

		irq = get_irq(global.irq_head, nr);
		if (irq) {
			if (irq->active && irq->count != count) {
				DEBUG("Updated irq #%d %12lu %s",
						nr, count, entry);
				global.stamp = now;
			}
			irq->count = count;
		} else
			if ((irq = add_interrupt(global.irq_head, nr,
			                         count, entry)))
				 global.irq_head = irq;
		
		release_remains(entry);
next_line:
		release_line(line);
	}
err:
	fclose(stat);
	return ret;
}

/* Initialize interrupts monitoring */
int intr_stat_init(void)
{
	time_t now = time(NULL);
	global.is_idle = 0;
	global.timeout = 0;
	global.nb_watch = 0;
	global.stamp = now;
	global.irq_head = NULL;
	return parse_interrupts(now);
}

void intr_set_timeout(int timeout)
{
	global.timeout = timeout;
}

/* Release allocated resources */
void intr_stat_release(void)
{
	irq_stat_t irq = global.irq_head;
	
	while (irq) {
		irq_stat_t tmp = irq->next;
		if (irq->name && strcmp(irq->name, UNKNOWN_INTERRUPT))
			free(irq->name);
		free(irq);
		irq = tmp;
	}
}

/* Register a given interrupt to monitor it */
int intr_stat_register(int n)
{
	irq_stat_t irq = get_irq(global.irq_head, n);
	if (!irq) {
		ERR("No irq line #%d found", n);
		return 0;
	}
	irq->active = 1;
	global.nb_watch++;
	DEBUG("Monitoring interrupt #%d \"%s\"", n,
	     irq->name ? irq->name : UNKNOWN_INTERRUPT);
	return 1;
}

/* Register interrupts given as a comma separated list of interrupt
   ids */
int intr_stat_register_byids(const char *ids)
{
	char *buffer = strdup(ids);
	char *persist = NULL;
	const char *p = NULL;
	int status = 1;

	if (!buffer)
		return 0;

	for (p = strtok_r(buffer, LIST_DELIMITERS, &persist);
	     p; p = strtok_r(NULL, LIST_DELIMITERS, &persist)) {
		if (isdigit(*p)) {
			status = intr_stat_register(atoi(p));
		} else {
			irq_stat_t irq = global.irq_head;
			while (irq) {
				if (!strcmp(irq->name, p)) {
					status = intr_stat_register(irq->nr);
					break;
				}
				irq = irq->next;
			}
					
		}
	}
	free(buffer);
	return status;
}

/* Return 1 if not idle */
int intr_stat_check_idleness(void)
{
	int is_idle;
	char *cmd;
	time_t now = time(NULL);
	
	/* No irqs watched -> we're always idle */
	if (!global.nb_watch)
		return 1;
	
	if (!parse_interrupts(now))
		BUG("Failed updating irq stats");
		
	is_idle = (now - global.stamp > global.timeout);
	
	if (global.is_idle == is_idle)
		return is_idle;
		
	cmd = (is_idle) ? CMD_IF_IDLE : CMD_IF_ACTIVE;

	DEBUG("state is %s", (is_idle) ? "idle" : "active");
	
	if (access(cmd, X_OK) == 0 && system(cmd) < 0) {
		ERR("cannot execute %s", cmd);
		return global.is_idle;	/* Will retry next time */
	}
	
	global.is_idle = is_idle;
	
	return is_idle;
}