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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* File descriptor usage
*
* The number of processes that can be effectively managed is limited to less
* than half the number of descriptors available: one for each process's
* psinfo, the other its pagedata. When managing more processes, file
* descriptors are revoked as needed, in such a way as to maximize the
* distribution of descriptors to pagedata which will be useful in meeting a
* cap without paging out the process's working set, while retaining some
* benefit from caching psinfo descriptors, and leaving enough available for
* use by external consumers, such as are needed for project enumeration or
* configuration file reading.
*
* Revokable file descriptors are opened and associated with a callback
* function which can be invoked to revoke them later. pagedata and psinfo
* descriptors are differentiated for the purposes of preferring pagedata over
* psinfo, which effectively places the performance of rcapd behind the
* importance of making good page selections. The one exception is that one
* psinfo descriptor is guaranteed a place at any time, for the benefit of
* psinfo updates of a presently currently-scanned process. Descriptors are
* otherwise revoked in LIFO order.
*/
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <strings.h>
#include <unistd.h>
#include "rcapd_rfd.h"
#include "utils.h"
static rfd_t *tail; /* tail of global list */
static int rfd_revoke_next(rfd_class_t);
/*
* Return the previous rfd_t of the given class, starting at (and including)
* the given rfd_t.
*/
static rfd_t *
rfd_find_prev_class(rfd_t *rfd, rfd_class_t class)
{
while (rfd != NULL && rfd->rfd_class != class)
rfd = rfd->rfd_prev;
return (rfd);
}
/*
* Revoke and free the given rfd_t, returning as close does.
*/
static int
rfd_revoke_fd(rfd_t *rfd)
{
if (rfd->rfd_revoke != NULL)
rfd->rfd_revoke(rfd);
return (rfd_close(rfd->rfd_fd));
}
/*
* Revoke the next file descriptor according to the above constraints. Return
* nonzero if there are none to revoke.
*/
static int
rfd_revoke_next(rfd_class_t class)
{
rfd_t *rfd = NULL;
if (tail == NULL) {
debug("nothing to revoke\n");
return (-1);
}
/*
* RESERVED-clsas descriptors are all equivalent and may not be revoked
* to satisfy another request of the same clsas. rfd_reserve() uses
* this to reserve descriptors by first allocating, then closing, these
* descriptors.
*/
if (class != RFD_RESERVED)
rfd = rfd_find_prev_class(tail, RFD_RESERVED);
/*
* Next try psinfo descriptors, leaving at least one open. Revoke the
* second-last psinfo descriptor, if possible.
*/
if (rfd == NULL) {
rfd = rfd_find_prev_class(tail, RFD_PSINFO);
if (rfd != NULL)
rfd = rfd->rfd_prev_class;
}
/*
* Otherwise, revoke the last descriptor allocated, taking the same
* care as above that it is not reserved, if the reserved kind is
* sought.
*/
if (rfd == NULL) {
rfd = tail;
while (rfd != NULL && class == RFD_RESERVED && rfd->rfd_class ==
RFD_RESERVED)
rfd = rfd->rfd_prev;
}
if (rfd != NULL)
return (rfd_revoke_fd(rfd));
/*
* Nothing but reserved-class descriptors are revocable, while a
* reserved- class descriptor was sought.
*/
return (-1);
}
/*
* Opens a file of the given class, which can later be revoked with the given
* callback. Returns as open does. The callback should reset any state that
* this caller establishes after the open, but should not close the descriptor,
* which will be done when the caller explicitly does so with rfd_close(), or
* the descriptor is revoked with rfd_revoke().
*/
int
rfd_open(char *name, int revoke_ok, rfd_class_t class,
void(*revoke)(struct rfd *), void *data, int oflag, mode_t mode)
{
int fd;
rfd_t *rfd;
while ((fd = open(name, oflag, mode)) == -1 && (errno == ENFILE ||
errno == EMFILE)) {
if (revoke_ok) {
if (rfd_revoke_next(class) != 0)
return (-1);
} else
break;
}
if (fd != -1) {
/*
* Create rfd_t and link into list.
*/
rfd = malloc(sizeof (*rfd));
if (rfd == NULL) {
(void) close(fd);
return (-1);
}
(void) bzero(rfd, sizeof (*rfd));
rfd->rfd_fd = fd;
rfd->rfd_class = class;
rfd->rfd_revoke = revoke;
rfd->rfd_data = data;
if (tail != NULL)
rfd->rfd_prev_class = rfd_find_prev_class(tail, class);
else
rfd->rfd_prev_class = tail;
rfd->rfd_prev = tail;
if (tail != NULL)
tail->rfd_next = rfd;
tail = rfd;
}
return (fd);
}
/*
* Close a given file descriptor, and return as close() does.
*/
int
rfd_close(int fd)
{
rfd_t *nextclass;
rfd_t *rfdprev;
rfd_t *rfd;
#ifdef DEBUG
int freed = 0;
#endif /* DEBUG */
rfd = tail;
while (rfd != NULL) {
rfdprev = rfd->rfd_prev;
if (rfd->rfd_fd == fd) {
if (rfd->rfd_prev != NULL)
rfd->rfd_prev->rfd_next = rfd->rfd_next;
if (rfd->rfd_next != NULL)
rfd->rfd_next->rfd_prev = rfd->rfd_prev;
if (tail == rfd)
tail = rfd->rfd_prev;
for (nextclass = rfd->rfd_next; nextclass != NULL;
nextclass = nextclass->rfd_next)
if (nextclass->rfd_class == rfd->rfd_class) {
nextclass->rfd_prev_class =
rfd->rfd_prev_class;
break;
}
free(rfd);
#ifdef DEBUG
freed = 1;
#endif /* DEBUG */
break;
}
rfd = rfdprev;
}
ASSERT(freed == 1);
return (close(fd));
}
/*
* Makes sure at least n descriptors are available. Returns nonzero if
* successful.
*/
int
rfd_reserve(int n)
{
int i;
int fd = 0;
rfd_t *otail = NULL;
rfd_t *rfdnext;
for (i = 0; i < n && fd >= 0; i++) {
/*
* rfd_open() will append as many RFD_RESERVED-clsas
* descriptors to the current tail as are requested, revoking
* non-RFD_RESERVED-class descriptors until nothing else can be
* revoked or the reservation is met.
*/
fd = rfd_open("/dev/null", 1, RFD_RESERVED, NULL, NULL,
O_RDONLY, 0);
if (otail == NULL)
otail = tail;
}
if (fd == -1)
debug("couldn't allocate %d descriptors\n", n);
while (otail != NULL) {
rfdnext = otail->rfd_next;
(void) rfd_close(otail->rfd_fd);
otail = rfdnext;
}
return (fd != -1);
}
|