xref: /illumos-gate/usr/src/cmd/rcap/rcapd/rcapd_rfd.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * File descriptor usage
29*7c478bd9Sstevel@tonic-gate  *
30*7c478bd9Sstevel@tonic-gate  * The number of processes that can be effectively managed is limited to less
31*7c478bd9Sstevel@tonic-gate  * than half the number of descriptors available:  one for each process's
32*7c478bd9Sstevel@tonic-gate  * psinfo, the other its pagedata.  When managing more processes, file
33*7c478bd9Sstevel@tonic-gate  * descriptors are revoked as needed, in such a way as to maximize the
34*7c478bd9Sstevel@tonic-gate  * distribution of descriptors to pagedata which will be useful in meeting a
35*7c478bd9Sstevel@tonic-gate  * cap without paging out the process's working set, while retaining some
36*7c478bd9Sstevel@tonic-gate  * benefit from caching psinfo descriptors, and leaving enough available for
37*7c478bd9Sstevel@tonic-gate  * use by external consumers, such as are needed for project enumeration or
38*7c478bd9Sstevel@tonic-gate  * configuration file reading.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * Revokable file descriptors are opened and associated with a callback
41*7c478bd9Sstevel@tonic-gate  * function which can be invoked to revoke them later.  pagedata and psinfo
42*7c478bd9Sstevel@tonic-gate  * descriptors are differentiated for the purposes of preferring pagedata over
43*7c478bd9Sstevel@tonic-gate  * psinfo, which effectively places the performance of rcapd behind the
44*7c478bd9Sstevel@tonic-gate  * importance of making good page selections.  The one exception is that one
45*7c478bd9Sstevel@tonic-gate  * psinfo descriptor is guaranteed a place at any time, for the benefit of
46*7c478bd9Sstevel@tonic-gate  * psinfo updates of a presently currently-scanned process.  Descriptors are
47*7c478bd9Sstevel@tonic-gate  * otherwise revoked in LIFO order.
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
51*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
52*7c478bd9Sstevel@tonic-gate #include <errno.h>
53*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
54*7c478bd9Sstevel@tonic-gate #include <limits.h>
55*7c478bd9Sstevel@tonic-gate #include <strings.h>
56*7c478bd9Sstevel@tonic-gate #include <unistd.h>
57*7c478bd9Sstevel@tonic-gate #include "rcapd_rfd.h"
58*7c478bd9Sstevel@tonic-gate #include "utils.h"
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate static rfd_t *tail;		/* tail of global list */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static int rfd_revoke_next(rfd_class_t);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * Return the previous rfd_t of the given class, starting at (and including)
66*7c478bd9Sstevel@tonic-gate  * the given rfd_t.
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate static rfd_t *
rfd_find_prev_class(rfd_t * rfd,rfd_class_t class)69*7c478bd9Sstevel@tonic-gate rfd_find_prev_class(rfd_t *rfd, rfd_class_t class)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	while (rfd != NULL && rfd->rfd_class != class)
72*7c478bd9Sstevel@tonic-gate 		rfd = rfd->rfd_prev;
73*7c478bd9Sstevel@tonic-gate 	return (rfd);
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate  * Revoke and free the given rfd_t, returning as close does.
78*7c478bd9Sstevel@tonic-gate  */
79*7c478bd9Sstevel@tonic-gate static int
rfd_revoke_fd(rfd_t * rfd)80*7c478bd9Sstevel@tonic-gate rfd_revoke_fd(rfd_t *rfd)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	if (rfd->rfd_revoke != NULL)
83*7c478bd9Sstevel@tonic-gate 		rfd->rfd_revoke(rfd);
84*7c478bd9Sstevel@tonic-gate 	return (rfd_close(rfd->rfd_fd));
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /*
88*7c478bd9Sstevel@tonic-gate  * Revoke the next file descriptor according to the above constraints.  Return
89*7c478bd9Sstevel@tonic-gate  * nonzero if there are none to revoke.
90*7c478bd9Sstevel@tonic-gate  */
91*7c478bd9Sstevel@tonic-gate static int
rfd_revoke_next(rfd_class_t class)92*7c478bd9Sstevel@tonic-gate rfd_revoke_next(rfd_class_t class)
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	rfd_t *rfd = NULL;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	if (tail == NULL) {
97*7c478bd9Sstevel@tonic-gate 		debug("nothing to revoke\n");
98*7c478bd9Sstevel@tonic-gate 		return (-1);
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	/*
102*7c478bd9Sstevel@tonic-gate 	 * RESERVED-clsas descriptors are all equivalent and may not be revoked
103*7c478bd9Sstevel@tonic-gate 	 * to satisfy another request of the same clsas.  rfd_reserve() uses
104*7c478bd9Sstevel@tonic-gate 	 * this to reserve descriptors by first allocating, then closing, these
105*7c478bd9Sstevel@tonic-gate 	 * descriptors.
106*7c478bd9Sstevel@tonic-gate 	 */
107*7c478bd9Sstevel@tonic-gate 	if (class != RFD_RESERVED)
108*7c478bd9Sstevel@tonic-gate 		rfd = rfd_find_prev_class(tail, RFD_RESERVED);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	/*
111*7c478bd9Sstevel@tonic-gate 	 * Next try psinfo descriptors, leaving at least one open.  Revoke the
112*7c478bd9Sstevel@tonic-gate 	 * second-last psinfo descriptor, if possible.
113*7c478bd9Sstevel@tonic-gate 	 */
114*7c478bd9Sstevel@tonic-gate 	if (rfd == NULL) {
115*7c478bd9Sstevel@tonic-gate 		rfd = rfd_find_prev_class(tail, RFD_PSINFO);
116*7c478bd9Sstevel@tonic-gate 		if (rfd != NULL)
117*7c478bd9Sstevel@tonic-gate 			rfd = rfd->rfd_prev_class;
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	/*
121*7c478bd9Sstevel@tonic-gate 	 * Otherwise, revoke the last descriptor allocated, taking the same
122*7c478bd9Sstevel@tonic-gate 	 * care as above that it is not reserved, if the reserved kind is
123*7c478bd9Sstevel@tonic-gate 	 * sought.
124*7c478bd9Sstevel@tonic-gate 	 */
125*7c478bd9Sstevel@tonic-gate 	if (rfd == NULL) {
126*7c478bd9Sstevel@tonic-gate 		rfd = tail;
127*7c478bd9Sstevel@tonic-gate 		while (rfd != NULL && class == RFD_RESERVED && rfd->rfd_class ==
128*7c478bd9Sstevel@tonic-gate 		    RFD_RESERVED)
129*7c478bd9Sstevel@tonic-gate 			rfd = rfd->rfd_prev;
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if (rfd != NULL)
133*7c478bd9Sstevel@tonic-gate 		return (rfd_revoke_fd(rfd));
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	/*
136*7c478bd9Sstevel@tonic-gate 	 * Nothing but reserved-class descriptors are revocable, while a
137*7c478bd9Sstevel@tonic-gate 	 * reserved- class descriptor was sought.
138*7c478bd9Sstevel@tonic-gate 	 */
139*7c478bd9Sstevel@tonic-gate 	return (-1);
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate /*
143*7c478bd9Sstevel@tonic-gate  * Opens a file of the given class, which can later be revoked with the given
144*7c478bd9Sstevel@tonic-gate  * callback.  Returns as open does.  The callback should reset any state that
145*7c478bd9Sstevel@tonic-gate  * this caller establishes after the open, but should not close the descriptor,
146*7c478bd9Sstevel@tonic-gate  * which will be done when the caller explicitly does so with rfd_close(), or
147*7c478bd9Sstevel@tonic-gate  * the descriptor is revoked with rfd_revoke().
148*7c478bd9Sstevel@tonic-gate  */
149*7c478bd9Sstevel@tonic-gate int
rfd_open(char * name,int revoke_ok,rfd_class_t class,void (* revoke)(struct rfd *),void * data,int oflag,mode_t mode)150*7c478bd9Sstevel@tonic-gate rfd_open(char *name, int revoke_ok, rfd_class_t class,
151*7c478bd9Sstevel@tonic-gate     void(*revoke)(struct rfd *), void *data, int oflag, mode_t mode)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	int fd;
154*7c478bd9Sstevel@tonic-gate 	rfd_t *rfd;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	while ((fd = open(name, oflag, mode)) == -1 && (errno == ENFILE ||
157*7c478bd9Sstevel@tonic-gate 	    errno == EMFILE)) {
158*7c478bd9Sstevel@tonic-gate 		if (revoke_ok) {
159*7c478bd9Sstevel@tonic-gate 			if (rfd_revoke_next(class) != 0)
160*7c478bd9Sstevel@tonic-gate 				return (-1);
161*7c478bd9Sstevel@tonic-gate 		} else
162*7c478bd9Sstevel@tonic-gate 			break;
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	if (fd != -1) {
166*7c478bd9Sstevel@tonic-gate 		/*
167*7c478bd9Sstevel@tonic-gate 		 * Create rfd_t and link into list.
168*7c478bd9Sstevel@tonic-gate 		 */
169*7c478bd9Sstevel@tonic-gate 		rfd = malloc(sizeof (*rfd));
170*7c478bd9Sstevel@tonic-gate 		if (rfd == NULL) {
171*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
172*7c478bd9Sstevel@tonic-gate 			return (-1);
173*7c478bd9Sstevel@tonic-gate 		}
174*7c478bd9Sstevel@tonic-gate 		(void) bzero(rfd, sizeof (*rfd));
175*7c478bd9Sstevel@tonic-gate 		rfd->rfd_fd = fd;
176*7c478bd9Sstevel@tonic-gate 		rfd->rfd_class = class;
177*7c478bd9Sstevel@tonic-gate 		rfd->rfd_revoke = revoke;
178*7c478bd9Sstevel@tonic-gate 		rfd->rfd_data = data;
179*7c478bd9Sstevel@tonic-gate 		if (tail != NULL)
180*7c478bd9Sstevel@tonic-gate 			rfd->rfd_prev_class = rfd_find_prev_class(tail, class);
181*7c478bd9Sstevel@tonic-gate 		else
182*7c478bd9Sstevel@tonic-gate 			rfd->rfd_prev_class = tail;
183*7c478bd9Sstevel@tonic-gate 		rfd->rfd_prev = tail;
184*7c478bd9Sstevel@tonic-gate 		if (tail != NULL)
185*7c478bd9Sstevel@tonic-gate 			tail->rfd_next = rfd;
186*7c478bd9Sstevel@tonic-gate 		tail = rfd;
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	return (fd);
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate /*
193*7c478bd9Sstevel@tonic-gate  * Close a given file descriptor, and return as close() does.
194*7c478bd9Sstevel@tonic-gate  */
195*7c478bd9Sstevel@tonic-gate int
rfd_close(int fd)196*7c478bd9Sstevel@tonic-gate rfd_close(int fd)
197*7c478bd9Sstevel@tonic-gate {
198*7c478bd9Sstevel@tonic-gate 	rfd_t *nextclass;
199*7c478bd9Sstevel@tonic-gate 	rfd_t *rfdprev;
200*7c478bd9Sstevel@tonic-gate 	rfd_t *rfd;
201*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
202*7c478bd9Sstevel@tonic-gate 	int freed = 0;
203*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	rfd = tail;
206*7c478bd9Sstevel@tonic-gate 	while (rfd != NULL) {
207*7c478bd9Sstevel@tonic-gate 		rfdprev = rfd->rfd_prev;
208*7c478bd9Sstevel@tonic-gate 		if (rfd->rfd_fd == fd) {
209*7c478bd9Sstevel@tonic-gate 			if (rfd->rfd_prev != NULL)
210*7c478bd9Sstevel@tonic-gate 				rfd->rfd_prev->rfd_next = rfd->rfd_next;
211*7c478bd9Sstevel@tonic-gate 			if (rfd->rfd_next != NULL)
212*7c478bd9Sstevel@tonic-gate 				rfd->rfd_next->rfd_prev = rfd->rfd_prev;
213*7c478bd9Sstevel@tonic-gate 			if (tail == rfd)
214*7c478bd9Sstevel@tonic-gate 				tail = rfd->rfd_prev;
215*7c478bd9Sstevel@tonic-gate 			for (nextclass = rfd->rfd_next; nextclass != NULL;
216*7c478bd9Sstevel@tonic-gate 			    nextclass = nextclass->rfd_next)
217*7c478bd9Sstevel@tonic-gate 				if (nextclass->rfd_class == rfd->rfd_class) {
218*7c478bd9Sstevel@tonic-gate 					nextclass->rfd_prev_class =
219*7c478bd9Sstevel@tonic-gate 					    rfd->rfd_prev_class;
220*7c478bd9Sstevel@tonic-gate 					break;
221*7c478bd9Sstevel@tonic-gate 				}
222*7c478bd9Sstevel@tonic-gate 			free(rfd);
223*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
224*7c478bd9Sstevel@tonic-gate 			freed = 1;
225*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
226*7c478bd9Sstevel@tonic-gate 			break;
227*7c478bd9Sstevel@tonic-gate 		}
228*7c478bd9Sstevel@tonic-gate 		rfd = rfdprev;
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 	ASSERT(freed == 1);
231*7c478bd9Sstevel@tonic-gate 	return (close(fd));
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate /*
235*7c478bd9Sstevel@tonic-gate  * Makes sure at least n descriptors are available.  Returns nonzero if
236*7c478bd9Sstevel@tonic-gate  * successful.
237*7c478bd9Sstevel@tonic-gate  */
238*7c478bd9Sstevel@tonic-gate int
rfd_reserve(int n)239*7c478bd9Sstevel@tonic-gate rfd_reserve(int n)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	int i;
242*7c478bd9Sstevel@tonic-gate 	int fd = 0;
243*7c478bd9Sstevel@tonic-gate 	rfd_t *otail = NULL;
244*7c478bd9Sstevel@tonic-gate 	rfd_t *rfdnext;
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < n && fd >= 0; i++) {
247*7c478bd9Sstevel@tonic-gate 		/*
248*7c478bd9Sstevel@tonic-gate 		 * rfd_open() will append as many RFD_RESERVED-clsas
249*7c478bd9Sstevel@tonic-gate 		 * descriptors to the current tail as are requested, revoking
250*7c478bd9Sstevel@tonic-gate 		 * non-RFD_RESERVED-class descriptors until nothing else can be
251*7c478bd9Sstevel@tonic-gate 		 * revoked or the reservation is met.
252*7c478bd9Sstevel@tonic-gate 		 */
253*7c478bd9Sstevel@tonic-gate 		fd = rfd_open("/dev/null", 1, RFD_RESERVED, NULL, NULL,
254*7c478bd9Sstevel@tonic-gate 		    O_RDONLY, 0);
255*7c478bd9Sstevel@tonic-gate 		if (otail == NULL)
256*7c478bd9Sstevel@tonic-gate 			otail = tail;
257*7c478bd9Sstevel@tonic-gate 	}
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	if (fd == -1)
260*7c478bd9Sstevel@tonic-gate 		debug("couldn't allocate %d descriptors\n", n);
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	while (otail != NULL) {
263*7c478bd9Sstevel@tonic-gate 		rfdnext = otail->rfd_next;
264*7c478bd9Sstevel@tonic-gate 		(void) rfd_close(otail->rfd_fd);
265*7c478bd9Sstevel@tonic-gate 		otail = rfdnext;
266*7c478bd9Sstevel@tonic-gate 	}
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	return (fd != -1);
269*7c478bd9Sstevel@tonic-gate }
270