/*
* 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
* 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 <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 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 *
{
return (rfd);
}
/*
* Revoke and free the given rfd_t, returning as close does.
*/
static int
{
}
/*
* Revoke the next file descriptor according to the above constraints. Return
* nonzero if there are none to revoke.
*/
static int
{
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)
/*
* Next try psinfo descriptors, leaving at least one open. Revoke the
* second-last psinfo descriptor, if possible.
*/
}
/*
* Otherwise, revoke the last descriptor allocated, taking the same
* care as above that it is not reserved, if the reserved kind is
* sought.
*/
}
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
{
int fd;
if (revoke_ok) {
if (rfd_revoke_next(class) != 0)
return (-1);
} else
break;
}
if (fd != -1) {
/*
* Create rfd_t and link into list.
*/
return (-1);
}
else
}
return (fd);
}
/*
* Close a given file descriptor, and return as close() does.
*/
int
{
#ifdef DEBUG
int freed = 0;
#endif /* DEBUG */
break;
}
#ifdef DEBUG
freed = 1;
#endif /* DEBUG */
break;
}
}
}
/*
* Makes sure at least n descriptors are available. Returns nonzero if
* successful.
*/
int
rfd_reserve(int n)
{
int i;
int fd = 0;
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.
*/
O_RDONLY, 0);
}
if (fd == -1)
debug("couldn't allocate %d descriptors\n", n);
}
return (fd != -1);
}