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