rcapd_main.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* 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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* rcapd is a long-running daemon enforcing project-based resource caps (see
* rcapd(1M)). Each instance of a process aggregate (project or, generically,
* "collection") may have a memory cap. A single thread monitors the resource
* utilization of capped collections, enforces caps when they are exceeded (and
* other conditions are met), and incorporates changes in configuration or
* caps. Each of these actions occurs not more frequently than the rate
* specified with rcapadm(1M).
*/
#include <sys/priocntl.h>
#include <sys/resource.h>
#include <sys/sysmacros.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <kstat.h>
#include <libintl.h>
#include <limits.h>
#include <locale.h>
#include <priv.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <zone.h>
#include <assert.h>
#include "rcapd.h"
#include "rcapd_mapping.h"
#include "rcapd_rfd.h"
#include "rcapd_stat.h"
#include "utils.h"
#define POSITIVE_MIN(x, y) \
(((x) <= 0) ? (y) : ((y) <= 0) ? (x) : MIN(x, y))
: (hrtime_t)0)
((rcfg.rcfg_stat_file[0] != 0) ? \
typedef struct soft_scan_arg {
static int debug_mode = 0; /* debug mode flag */
/* scanned */
static int enforce_caps = 0; /* cap enforcement flag, dependent on */
/* enforce_soft_caps and */
/* global_scanner_running */
static int enforce_soft_caps = 0; /* soft cap enforcement flag, */
/* depending on memory pressure */
static int memory_pressure = 0; /* physical memory utilization (%) */
static int memory_pressure_sample = 0; /* count of samples */
static int global_scanner_running = 0; /* global scanning flag, to avoid */
/* interference with kernel's page */
/* scanner */
static int termination_signal = 0; /* terminating signal */
/*
* Flags.
*/
static int ever_ran;
int should_run;
static int should_reconfigure;
static int verify_statistics(void);
static int update_statistics(void);
/*
* Checks if a process is marked 'system'. Returns zero only when it is not.
*/
static int
{
PC_KY_NULL) != -1) {
} else {
debug("cannot get class-specific scheduling parameters; "
"assuming system process");
return (-1);
}
}
/*
* fname is the process name, for debugging messages, and unscannable is a flag
* indicating whether the process should be scanned.
*/
static void
{
return;
/*
* If the process is already being tracked, update the unscannable flag,
* as determined by the caller, from the process's psinfo.
*/
debug("process %d: became unscannable\n",
}
return;
}
}
/*
* We've fallen off the list without finding our current process;
* insert it at the list head.
*/
else {
/*
* If the caller didn't flag this process as unscannable
* already, do some more checking.
*/
#ifdef DEBUG
/*
* Verify the sanity of lprocess. It should not contain the
* process we are about to prepend.
*/
debug("The collection %lld already has these members, "
}
abort();
}
#endif /* DEBUG */
}
}
static int
{
debug_high("list_walk_all aborted at lpc %d\n",
(int)pid);
return (1);
}
}
return (0);
}
/*
* Invoke the given callback for each process in each collection. Callbacks
* are allowed to change the linkage of the process on which they act.
*/
static void
{
}
static void
{
} else
debug("revoking psinfo fd for unknown process\n");
}
/*
* Retrieve a process's psinfo via an already-opened or new file descriptor.
* The supplied descriptor will be closed on failure. An optional callback
* will be invoked with the last descriptor tried, and a supplied callback
* argument, as its arguments, such that the new descriptor may be cached, or
* an old one may be invalidated. If the result of the callback is zero, the
* the caller is to assume responsibility for the file descriptor, to close it
* with rfd_close().
*
* On failure, a nonzero value is returned.
*/
int
{
int fd;
int can_try_uncached;
do {
if (cached_fd >= 0) {
can_try_uncached = 1;
debug_high("%d/psinfo, trying cached fd %d\n",
} else {
char pathbuf[PROC_PATH_MAX];
can_try_uncached = 0;
break;
} else
}
break;
else {
}
} while (can_try_uncached == 1);
if (fd >= 0) {
}
return ((fd >= 0) ? 0 : -1);
}
/*
* Retrieve the collection membership of all processes in our zone, and update
* the psinfo of those non-system, non-zombie ones in collections.
*/
static void
{
}
/*
* Cache the process' psinfo fd, taking responsibility for freeing it.
*/
int
{
return (0);
}
/*
* Update the RSS of processes in monitored collections.
*/
/*ARGSUSED*/
static int
{
} else {
else
debug("process %d: cannot read psinfo",
}
return (0);
}
/*
* Sample the collection RSS, updating the collection's statistics with the
* results.
*/
/*ARGSUSED*/
static int
{
/*
* If updating statistics for a new interval, reset the affected
* counters.
*/
if (lcol->lcol_stat_invalidate != 0) {
lcol->lcol_stat_invalidate = 0;
}
if (excess > 0)
return (0);
}
/*
* Open /proc and walk entries.
*/
static void
{
(void) rfd_reserve(1);
continue;
continue;
else
}
}
/*
* Memory update callback.
*/
static int
{
return (0);
}
/*
* Clear unmarked callback.
*/
/*ARGSUSED*/
static int
{
} else {
}
return (0);
}
/*
* Memory clear callback.
*/
/*ARGSUSED*/
static int
{
lcol->lcol_image_size = 0;
return (0);
}
/*
* Print, for debugging purposes, a collection's recently-sampled RSS and
* excess.
*/
/*ARGSUSED*/
static int
{
(unsigned long long)lcol->lcol_rss_cap,
(long long)excess);
return (0);
}
/*
* Scan those collections which have exceeded their caps.
*/
/*ARGSUSED*/
static int
{
}
return (0);
}
/*
* Do a soft scan of those collections which have excesses. A soft scan is one
* in which the cap enforcement pressure is taken into account. The difference
* between the utilized physical memory and the cap enforcement pressure will
* be scanned-for, and each collection will be scanned proportionally by their
* present excesses.
*/
static int
{
soft_scan_arg_t *arg = a;
debug("col %lld excess %lld scan_goal %lld sum_excess %llu, "
(unsigned long long)arg->ssa_sum_excess,
arg->ssa_sum_excess));
arg->ssa_sum_excess));
}
return (0);
}
/*
* When a scan could happen, but caps aren't enforced tick the
* lcols_unenforced_cap counter.
*/
/*ARGSUSED*/
static int
{
return (0);
}
/*
* Update the count of physically installed memory.
*/
static void
update_phys_total(void)
{
/ 1024;
if (phys_total != old_phys_total)
}
/*
* Unlink a process from its collection, updating relevant statistics, and
* freeing its associated memory.
*/
void
{
if (lpc->lpc_psinfo_fd >= 0) {
debug("could not close %d lpc_psinfo_fd %d",
}
if (lpc->lpc_pgdata_fd >= 0) {
debug("could not close %d lpc_pgdata_fd %d",
}
if (lpc->lpc_xmap_fd >= 0) {
debug("could not close %d lpc_xmap_fd %d",
}
}
/*
* Collection clear callback.
*/
/*ARGSUSED*/
static int
{
return (0);
}
/*
* Respond to a terminating signal by setting a termination flag.
*/
/*ARGSUSED*/
static void
terminate_signal(int signal)
{
if (termination_signal == 0)
should_run = 0;
}
/*
* Handle any synchronous or asynchronous signals that would ordinarily cause a
* process to abort.
*/
/*ARGSUSED*/
static void
abort_signal(int signal)
{
/*
* Allow the scanner to make a last-ditch effort to resume any stopped
* processes.
*/
scan_abort();
abort();
}
/*
* Clean up collections which have been removed due to configuration. Unlink
* the collection from lcollection and free it.
*/
/*ARGSUSED*/
static int
{
}
return (0);
}
/*
* Set those variables which depend on the global configuration.
*/
static void
finish_configuration(void)
{
/*
* Warn that any lnode (or non-project) mode specification (by an SRM
* 1.3 configuration file, for example) is ignored.
*/
}
}
/*
* Cause the configuration file to be reread and applied.
*/
static void
{
return;
update_statistics) != 0)
else {
/*
* The configuration file has been read. Remove existing
* collections in case there is a change in collection type.
*/
}
/*
* Make the newly-read configuration the global one, and update
* any variables that depend on it.
*/
}
}
/*
* Reread the configuration filex, then examine changes, additions, and
* deletions to cap definitions.
*/
static void
reconfigure(void)
{
debug("reconfigure...\n");
/*
* Reread the configuration data.
*/
/*
* Walk the lcollection, marking active collections so inactive ones
* can be freed.
*/
}
/*
* Respond to SIGHUP by triggering the rereading the configuration file and cap
* definitions.
*/
/*ARGSUSED*/
static void
{
should_reconfigure = 1;
}
/*
* Print, for debugging purposes, each collection's interval statistics.
*/
/*ARGSUSED*/
static int
{
(unsigned long long)(lcol->lcol_stat_invalidate ? 0 : \
(unsigned long long)(lcol->lcol_stat_invalidate ? 0 : \
"ineffective/scans/unenforced/samplings: %llu/%llu/%llu/%llu, RSS "
(unsigned long long)lcol->lcol_rss_cap,
/ MILLISEC));
return (0);
}
/*
* Record each collection's interval statistics in the statistics file.
*/
static int
{
/*
* Copy the relevant fields to the collection's record.
*/
/*
* Set a flag to indicate that the exported interval snapshot
* values should be reset at the next sample.
*/
} else {
}
return (0);
}
/*
* Determine the count of pages scanned by the global page scanner, obtained
* from the cpu_stat:*::scan kstats. Return zero on success.
*/
static int
{
return (0);
}
scanned += ((cpu_stat_t *)
} else
return (-1);
}
}
return (0);
}
/*
* Update the shared statistics file with each collection's current statistics.
* Return zero on success.
*/
static int
update_statistics(void)
{
/*
* Create a temporary file.
*/
debug("temporary file template size too small\n");
return (-1);
}
(void) rfd_reserve(1);
/*
* Write the header and per-collection statistics.
*/
if (fd >= 0) {
sizeof (rs)) {
/*
* Replace the existing statistics file with this new
* one.
*/
} else
res = -1;
} else
res = -1;
return (res);
}
/*
* Verify the statistics file can be created and written to, and die if an
* existing file may be in use by another rcapd.
*/
static int
verify_statistics(void)
{
/*
* Warn if another instance of rcapd might be active.
*/
(void) rfd_reserve(1);
return (update_statistics());
}
static int
{
lcol->lcol_rss_cap));
return (0);
}
static void
rcapd_usage(void)
{
}
void
check_update_statistics(void)
{
debug("updating statistics...\n");
if (update_statistics() != 0)
debug("couldn't update statistics");
}
}
static void
{
/*
* Ensure the required privileges, suitable for controlling processes,
* are possessed.
*/
/*
*/
}
int
{
int res;
int sig; /* signal iteration */
int old_enforce_caps; /* track changes in enforcement */
/* conditions */
(void) set_message_priority(RCM_INFO);
(void) setprogname("rcapd");
(void) chdir("/");
should_run = 1;
ever_ran = 0;
(void) textdomain(TEXT_DOMAIN);
/*
* Parse command-line options.
*/
switch (res) {
case 'd':
should_fork = 0;
if (debug_mode == 0) {
debug_mode = 1;
(void) set_message_priority(RCM_DEBUG);
} else
(void) set_message_priority(RCM_DEBUG_HIGH);
break;
case 'F':
should_fork = 0;
break;
default:
rcapd_usage();
return (E_USAGE);
/*NOTREACHED*/
}
/*
* If not debugging, fork and continue operating, changing the
* destination of messages to syslog().
*/
if (should_fork == 1) {
debug("forking\n");
if (child == -1)
if (child > 0)
return (0);
else {
(void) set_message_destination(RCD_SYSLOG);
}
/*
* Start a new session and detatch from the controlling tty.
*/
"terminal"));
}
/*
* Read the configuration file.
*/
!= 0)
should_reconfigure = 0;
/*
* Check that required privileges are possessed.
*/
if (rcfg.rcfg_memory_cap_enforcement_pressure == 0) {
/*
* Always enforce caps when strict caps are used.
*/
enforce_caps = 1;
}
/*
* Open the kstat chain.
*/
kctl = kstat_open();
/*
* Set RLIMIT_NOFILE as high as practical, so roughly 10K processes can
* be effectively managed without revoking descriptors (at 3 per
* process).
*/
}
else
debug("fd limit: unknown\n");
/*
* Handle those signals whose (default) exit disposition
* prevents rcapd from finishing scanning before terminating.
*/
/*
* Install a signal handler for reconfiguration processing.
*/
/*
* Determine which process collections to cap.
*/
/*
* Loop forever, monitoring collections' resident set sizes and
* enforcing their caps. Look for changes in caps and process
* membership, as well as responding to requests to reread the
* configuration. Update per-collection statistics periodically.
*/
while (should_run != 0) {
/*
* Announce that rcapd is starting.
*/
if (ever_ran == 0) {
ever_ran = 1;
}
/*
* Update the process list once every proc_walk_interval. The
* condition of global memory pressure is also checked at the
* same frequency, if strict caps are in use.
*/
/*
* Detect configuration and cap changes at every
* reconfiguration_interval, or when SIGHUP has been received.
*/
should_reconfigure == 1) {
reconfigure();
/*
* Reset each event time to the shorter of the
* previous and new intervals.
*/
if (next_report == 0 &&
rcfg.rcfg_report_interval > 0)
next_report = now;
else
if (next_proc_walk == 0 &&
else
if (next_rss_sample == 0 &&
else
should_reconfigure = 0;
continue;
}
debug("scanning process list...\n");
}
/*
* Check for changes to the amount of installed
* physical memory, to compute the current memory
* pressure.
*/
/*
* If soft caps are in use, determine if global memory
* pressure exceeds the configured maximum above which
* soft caps are enforced.
*/
memory_pressure = 100 -
(int)((sysconf(_SC_AVPHYS_PAGES) *
if (rcfg.rcfg_memory_cap_enforcement_pressure > 0) {
if (memory_pressure >
if (enforce_soft_caps == 0) {
debug("memory pressure %d%%\n",
enforce_soft_caps = 1;
}
} else {
if (enforce_soft_caps == 1)
enforce_soft_caps = 0;
}
}
/*
* Determine if the global page scanner is running,
* while which no memory caps should be enforced, to
* prevent interference with the global page scanner.
*/
if (get_globally_scanned_pages(&new_sp) == 0) {
if (old_sp == 0)
/*EMPTY*/
;
if (global_scanner_running == 0) {
debug("global memory pressure "
"detected (%llu pages "
"scanned since last "
"interval)\n",
(unsigned long long)
}
} else if (global_scanner_running == 1) {
debug("global memory pressure "
"relieved\n");
}
} else {
}
/*
* Cap enforcement is determined by the previous two
* conditions.
*/
0 || enforce_soft_caps == 1) &&
if (old_enforce_caps != enforce_caps)
"not " : "");
/*
* Sample collections' member processes' RSSes and
* recompute collections' excess.
*/
if (rcfg.rcfg_memory_cap_enforcement_pressure > 0)
debug("memory pressure %d%%\n",
/*
* If soft caps are in use, determine the size of the
* portion from each collection to scan for.
*/
if (enforce_soft_caps == 1) {
/*
* Compute the sum of the collections'
* excesses, which will be the denominator.
*/
arg.ssa_sum_excess = 0;
/*
* Compute the quantity of memory (in
* kilobytes) above the cap enforcement
* pressure. Set the scan goal to that
* quantity (or at most the excess).
*/
}
/*
* Victimize offending collections.
*/
(enforce_soft_caps == 0)))
if (enforce_soft_caps == 1) {
debug("scan goal is %lldKB\n",
(long long)arg.ssa_scan_goal);
&arg);
} else
else
}
/*
* Update the statistics file, if it's time.
*/
/*
* Sleep for some time before repeating.
*/
}
}
if (termination_signal != 0)
if (ever_ran != 0)
/*
* Unlink the statistics file before exiting.
*/
if (rcfg.rcfg_stat_file[0] != 0)
return (E_SUCCESS);
}