1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * rcapd is a long-running daemon enforcing project-based resource caps (see 31*0Sstevel@tonic-gate * rcapd(1M)). Each instance of a process aggregate (project or, generically, 32*0Sstevel@tonic-gate * "collection") may have a memory cap. A single thread monitors the resource 33*0Sstevel@tonic-gate * utilization of capped collections, enforces caps when they are exceeded (and 34*0Sstevel@tonic-gate * other conditions are met), and incorporates changes in configuration or 35*0Sstevel@tonic-gate * caps. Each of these actions occurs not more frequently than the rate 36*0Sstevel@tonic-gate * specified with rcapadm(1M). 37*0Sstevel@tonic-gate */ 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate #include <sys/priocntl.h> 40*0Sstevel@tonic-gate #include <sys/proc.h> 41*0Sstevel@tonic-gate #include <sys/resource.h> 42*0Sstevel@tonic-gate #include <sys/sysinfo.h> 43*0Sstevel@tonic-gate #include <sys/stat.h> 44*0Sstevel@tonic-gate #include <sys/sysmacros.h> 45*0Sstevel@tonic-gate #include <sys/time.h> 46*0Sstevel@tonic-gate #include <sys/types.h> 47*0Sstevel@tonic-gate #include <dirent.h> 48*0Sstevel@tonic-gate #include <errno.h> 49*0Sstevel@tonic-gate #include <fcntl.h> 50*0Sstevel@tonic-gate #include <kstat.h> 51*0Sstevel@tonic-gate #include <libintl.h> 52*0Sstevel@tonic-gate #include <limits.h> 53*0Sstevel@tonic-gate #include <locale.h> 54*0Sstevel@tonic-gate #include <priv.h> 55*0Sstevel@tonic-gate #include <signal.h> 56*0Sstevel@tonic-gate #include <stdarg.h> 57*0Sstevel@tonic-gate #include <stdio.h> 58*0Sstevel@tonic-gate #include <stdlib.h> 59*0Sstevel@tonic-gate #include <strings.h> 60*0Sstevel@tonic-gate #include <time.h> 61*0Sstevel@tonic-gate #include <unistd.h> 62*0Sstevel@tonic-gate #include <zone.h> 63*0Sstevel@tonic-gate #include <assert.h> 64*0Sstevel@tonic-gate #include "rcapd.h" 65*0Sstevel@tonic-gate #include "rcapd_mapping.h" 66*0Sstevel@tonic-gate #include "rcapd_rfd.h" 67*0Sstevel@tonic-gate #include "rcapd_stat.h" 68*0Sstevel@tonic-gate #include "utils.h" 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate #define POSITIVE_MIN(x, y) \ 71*0Sstevel@tonic-gate (((x) <= 0) ? (y) : ((y) <= 0) ? (x) : MIN(x, y)) 72*0Sstevel@tonic-gate #define NEXT_EVENT_TIME(base, seconds) \ 73*0Sstevel@tonic-gate (((int)seconds > 0) ? (base + (hrtime_t)seconds * (hrtime_t)NANOSEC) \ 74*0Sstevel@tonic-gate : (hrtime_t)0) 75*0Sstevel@tonic-gate #define NEXT_REPORT_EVENT_TIME(base, seconds) \ 76*0Sstevel@tonic-gate ((rcfg.rcfg_stat_file[0] != 0) ? \ 77*0Sstevel@tonic-gate NEXT_EVENT_TIME(gethrtime(), seconds) : (hrtime_t)0) 78*0Sstevel@tonic-gate #define EVENT_TIME(time, eventtime) \ 79*0Sstevel@tonic-gate (((time) > (eventtime)) && (eventtime) != 0) 80*0Sstevel@tonic-gate #define STAT_TEMPLATE_SUFFIX ".XXXXXX" /* suffix of mkstemp() arg */ 81*0Sstevel@tonic-gate #define DAEMON_UID 1 /* uid to use */ 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate typedef struct soft_scan_arg { 84*0Sstevel@tonic-gate uint64_t ssa_sum_excess; 85*0Sstevel@tonic-gate int64_t ssa_scan_goal; 86*0Sstevel@tonic-gate } soft_scan_arg_t; 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate static int debug_mode = 0; /* debug mode flag */ 89*0Sstevel@tonic-gate static pid_t rcapd_pid; /* rcapd's pid to ensure it's not */ 90*0Sstevel@tonic-gate /* scanned */ 91*0Sstevel@tonic-gate static kstat_ctl_t *kctl; /* kstat chain */ 92*0Sstevel@tonic-gate static uint64_t new_sp = 0, old_sp = 0; /* measure delta in page scan count */ 93*0Sstevel@tonic-gate static int enforce_caps = 0; /* cap enforcement flag, dependent on */ 94*0Sstevel@tonic-gate /* enforce_soft_caps and */ 95*0Sstevel@tonic-gate /* global_scanner_running */ 96*0Sstevel@tonic-gate static int enforce_soft_caps = 0; /* soft cap enforcement flag, */ 97*0Sstevel@tonic-gate /* depending on memory pressure */ 98*0Sstevel@tonic-gate static int memory_pressure = 0; /* physical memory utilization (%) */ 99*0Sstevel@tonic-gate static int memory_pressure_sample = 0; /* count of samples */ 100*0Sstevel@tonic-gate static int global_scanner_running = 0; /* global scanning flag, to avoid */ 101*0Sstevel@tonic-gate /* interference with kernel's page */ 102*0Sstevel@tonic-gate /* scanner */ 103*0Sstevel@tonic-gate static hrtime_t next_report; /* time of next report */ 104*0Sstevel@tonic-gate static int termination_signal = 0; /* terminating signal */ 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate rcfg_t rcfg; 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate /* 109*0Sstevel@tonic-gate * Flags. 110*0Sstevel@tonic-gate */ 111*0Sstevel@tonic-gate static int ever_ran; 112*0Sstevel@tonic-gate int should_run; 113*0Sstevel@tonic-gate static int should_reconfigure; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate static int verify_statistics(void); 116*0Sstevel@tonic-gate static int update_statistics(void); 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate /* 119*0Sstevel@tonic-gate * Checks if a process is marked 'system'. Returns zero only when it is not. 120*0Sstevel@tonic-gate */ 121*0Sstevel@tonic-gate static int 122*0Sstevel@tonic-gate proc_issystem(pid_t pid) 123*0Sstevel@tonic-gate { 124*0Sstevel@tonic-gate char pc_clname[PC_CLNMSZ]; 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate if (priocntl(P_PID, pid, PC_GETXPARMS, NULL, PC_KY_CLNAME, pc_clname, 127*0Sstevel@tonic-gate PC_KY_NULL) != -1) { 128*0Sstevel@tonic-gate return (strcmp(pc_clname, "SYS") == 0); 129*0Sstevel@tonic-gate } else { 130*0Sstevel@tonic-gate debug("cannot get class-specific scheduling parameters; " 131*0Sstevel@tonic-gate "assuming system process"); 132*0Sstevel@tonic-gate return (-1); 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate /* 137*0Sstevel@tonic-gate * fname is the process name, for debugging messages, and unscannable is a flag 138*0Sstevel@tonic-gate * indicating whether the process should be scanned. 139*0Sstevel@tonic-gate */ 140*0Sstevel@tonic-gate static void 141*0Sstevel@tonic-gate lprocess_insert_mark(pid_t pid, id_t colid, char *fname, int unscannable) 142*0Sstevel@tonic-gate { 143*0Sstevel@tonic-gate lcollection_t *lcol; 144*0Sstevel@tonic-gate lprocess_t *lproc; 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate if ((lcol = lcollection_find(colid)) == NULL) 147*0Sstevel@tonic-gate return; 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate /* 150*0Sstevel@tonic-gate * If the process is already being tracked, update the unscannable flag, 151*0Sstevel@tonic-gate * as determined by the caller, from the process's psinfo. 152*0Sstevel@tonic-gate */ 153*0Sstevel@tonic-gate lproc = lcol->lcol_lprocess; 154*0Sstevel@tonic-gate while (lproc != NULL) { 155*0Sstevel@tonic-gate if (lproc->lpc_pid == pid) { 156*0Sstevel@tonic-gate lproc->lpc_mark = 1; 157*0Sstevel@tonic-gate if (unscannable != 0 && lproc->lpc_unscannable == 0) { 158*0Sstevel@tonic-gate debug("process %d: became unscannable\n", 159*0Sstevel@tonic-gate (int)lproc->lpc_pid); 160*0Sstevel@tonic-gate lproc->lpc_unscannable = 1; 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate return; 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate lproc = lproc->lpc_next; 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate /* 168*0Sstevel@tonic-gate * We've fallen off the list without finding our current process; 169*0Sstevel@tonic-gate * insert it at the list head. 170*0Sstevel@tonic-gate */ 171*0Sstevel@tonic-gate if ((lproc = malloc(sizeof (*lproc))) == NULL) 172*0Sstevel@tonic-gate debug("insufficient memory to track new process %d", (int)pid); 173*0Sstevel@tonic-gate else { 174*0Sstevel@tonic-gate (void) bzero(lproc, sizeof (*lproc)); 175*0Sstevel@tonic-gate lproc->lpc_pid = pid; 176*0Sstevel@tonic-gate lproc->lpc_mark = 1; 177*0Sstevel@tonic-gate lproc->lpc_collection = lcol; 178*0Sstevel@tonic-gate lproc->lpc_psinfo_fd = -1; 179*0Sstevel@tonic-gate lproc->lpc_pgdata_fd = -1; 180*0Sstevel@tonic-gate lproc->lpc_xmap_fd = -1; 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate /* 183*0Sstevel@tonic-gate * If the caller didn't flag this process as unscannable 184*0Sstevel@tonic-gate * already, do some more checking. 185*0Sstevel@tonic-gate */ 186*0Sstevel@tonic-gate lproc->lpc_unscannable = unscannable || proc_issystem(pid); 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate #ifdef DEBUG 189*0Sstevel@tonic-gate /* 190*0Sstevel@tonic-gate * Verify the sanity of lprocess. It should not contain the 191*0Sstevel@tonic-gate * process we are about to prepend. 192*0Sstevel@tonic-gate */ 193*0Sstevel@tonic-gate if (lcollection_member(lcol, lproc)) { 194*0Sstevel@tonic-gate lprocess_t *cur = lcol->lcol_lprocess; 195*0Sstevel@tonic-gate debug("The collection %lld already has these members, " 196*0Sstevel@tonic-gate "including me, %d!\n", (long long)lcol->lcol_id, 197*0Sstevel@tonic-gate (int)lproc->lpc_pid); 198*0Sstevel@tonic-gate while (cur != NULL) { 199*0Sstevel@tonic-gate debug("\t%d\n", (int)cur->lpc_pid); 200*0Sstevel@tonic-gate cur = cur->lpc_next; 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate info(gettext("process already on lprocess\n")); 203*0Sstevel@tonic-gate abort(); 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate #endif /* DEBUG */ 206*0Sstevel@tonic-gate lproc->lpc_next = lcol->lcol_lprocess; 207*0Sstevel@tonic-gate if (lproc->lpc_next != NULL) 208*0Sstevel@tonic-gate lproc->lpc_next->lpc_prev = lproc; 209*0Sstevel@tonic-gate lproc->lpc_prev = NULL; 210*0Sstevel@tonic-gate lcol->lcol_lprocess = lproc; 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate debug("tracking %d %d %s%s\n", (int)colid, (int)pid, fname, 213*0Sstevel@tonic-gate (lproc->lpc_unscannable != 0) ? " (not scannable)" : ""); 214*0Sstevel@tonic-gate lcol->lcol_stat.lcols_proc_in++; 215*0Sstevel@tonic-gate } 216*0Sstevel@tonic-gate } 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate static int 219*0Sstevel@tonic-gate list_walk_process_cb(lcollection_t *lcol, void *arg) 220*0Sstevel@tonic-gate { 221*0Sstevel@tonic-gate int (*cb)(lcollection_t *, lprocess_t *) = 222*0Sstevel@tonic-gate (int(*)(lcollection_t *, lprocess_t *))arg; 223*0Sstevel@tonic-gate lprocess_t *member; 224*0Sstevel@tonic-gate lprocess_t *next; 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate member = lcol->lcol_lprocess; 227*0Sstevel@tonic-gate while (member != NULL) { 228*0Sstevel@tonic-gate pid_t pid = member->lpc_pid; 229*0Sstevel@tonic-gate next = member->lpc_next; 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate debug_high("list_walk_all lpc %d\n", (int)pid); 232*0Sstevel@tonic-gate if (cb(lcol, member) != 0) { 233*0Sstevel@tonic-gate debug_high("list_walk_all aborted at lpc %d\n", 234*0Sstevel@tonic-gate (int)pid); 235*0Sstevel@tonic-gate return (1); 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate member = next; 238*0Sstevel@tonic-gate } 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate return (0); 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate /* 244*0Sstevel@tonic-gate * Invoke the given callback for each process in each collection. Callbacks 245*0Sstevel@tonic-gate * are allowed to change the linkage of the process on which they act. 246*0Sstevel@tonic-gate */ 247*0Sstevel@tonic-gate static void 248*0Sstevel@tonic-gate list_walk_all(int (*cb)(lcollection_t *, lprocess_t *)) 249*0Sstevel@tonic-gate { 250*0Sstevel@tonic-gate list_walk_collection(list_walk_process_cb, (void *)cb); 251*0Sstevel@tonic-gate } 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate static void 254*0Sstevel@tonic-gate revoke_psinfo(rfd_t *rfd) 255*0Sstevel@tonic-gate { 256*0Sstevel@tonic-gate lprocess_t *lpc = (lprocess_t *)rfd->rfd_data; 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate if (lpc != NULL) { 259*0Sstevel@tonic-gate debug("revoking psinfo fd for process %d\n", (int)lpc->lpc_pid); 260*0Sstevel@tonic-gate ASSERT(lpc->lpc_psinfo_fd != -1); 261*0Sstevel@tonic-gate lpc->lpc_psinfo_fd = -1; 262*0Sstevel@tonic-gate } else 263*0Sstevel@tonic-gate debug("revoking psinfo fd for unknown process\n"); 264*0Sstevel@tonic-gate } 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate /* 267*0Sstevel@tonic-gate * Retrieve a process's psinfo via an already-opened or new file descriptor. 268*0Sstevel@tonic-gate * The supplied descriptor will be closed on failure. An optional callback 269*0Sstevel@tonic-gate * will be invoked with the last descriptor tried, and a supplied callback 270*0Sstevel@tonic-gate * argument, as its arguments, such that the new descriptor may be cached, or 271*0Sstevel@tonic-gate * an old one may be invalidated. If the result of the callback is zero, the 272*0Sstevel@tonic-gate * the caller is to assume responsibility for the file descriptor, to close it 273*0Sstevel@tonic-gate * with rfd_close(). 274*0Sstevel@tonic-gate * 275*0Sstevel@tonic-gate * On failure, a nonzero value is returned. 276*0Sstevel@tonic-gate */ 277*0Sstevel@tonic-gate int 278*0Sstevel@tonic-gate get_psinfo(pid_t pid, psinfo_t *psinfo, int cached_fd, 279*0Sstevel@tonic-gate int(*fd_update_cb)(void *, int), void *arg, lprocess_t *lpc) 280*0Sstevel@tonic-gate { 281*0Sstevel@tonic-gate int fd; 282*0Sstevel@tonic-gate int can_try_uncached; 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate ASSERT(!(cached_fd > 0 && fd_update_cb == NULL)); 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate do { 287*0Sstevel@tonic-gate if (cached_fd >= 0) { 288*0Sstevel@tonic-gate fd = cached_fd; 289*0Sstevel@tonic-gate can_try_uncached = 1; 290*0Sstevel@tonic-gate debug_high("%d/psinfo, trying cached fd %d\n", 291*0Sstevel@tonic-gate (int)pid, fd); 292*0Sstevel@tonic-gate } else { 293*0Sstevel@tonic-gate char pathbuf[PROC_PATH_MAX]; 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate can_try_uncached = 0; 296*0Sstevel@tonic-gate (void) snprintf(pathbuf, sizeof (pathbuf), 297*0Sstevel@tonic-gate "/proc/%d/psinfo", (int)pid); 298*0Sstevel@tonic-gate if ((fd = rfd_open(pathbuf, 1, RFD_PSINFO, 299*0Sstevel@tonic-gate revoke_psinfo, lpc, O_RDONLY, 0000)) < 0) { 300*0Sstevel@tonic-gate debug("cannot open %s", pathbuf); 301*0Sstevel@tonic-gate break; 302*0Sstevel@tonic-gate } else 303*0Sstevel@tonic-gate debug_high("opened %s, fd %d\n", pathbuf, fd); 304*0Sstevel@tonic-gate } 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate if (pread(fd, psinfo, sizeof (*psinfo), 0) == 307*0Sstevel@tonic-gate sizeof (*psinfo) && psinfo->pr_pid == pid) 308*0Sstevel@tonic-gate break; 309*0Sstevel@tonic-gate else { 310*0Sstevel@tonic-gate debug_high("closed fd %d\n", fd); 311*0Sstevel@tonic-gate if (rfd_close(fd) != 0) 312*0Sstevel@tonic-gate debug("could not close fd %d", fd); 313*0Sstevel@tonic-gate fd = cached_fd = -1; 314*0Sstevel@tonic-gate } 315*0Sstevel@tonic-gate } while (can_try_uncached == 1); 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate if (fd_update_cb == NULL || fd_update_cb(arg, fd) != 0) 318*0Sstevel@tonic-gate if (fd >= 0) { 319*0Sstevel@tonic-gate debug_high("closed %s fd %d\n", fd_update_cb == NULL ? 320*0Sstevel@tonic-gate "uncached" : "cached", fd); 321*0Sstevel@tonic-gate if (rfd_close(fd) != 0) 322*0Sstevel@tonic-gate debug("could not close fd %d", fd); 323*0Sstevel@tonic-gate } 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate debug_high("get_psinfo ret %d, fd %d, %s\n", ((fd >= 0) ? 0 : -1), fd, 326*0Sstevel@tonic-gate fd_update_cb != NULL ? "cached" : "uncached"); 327*0Sstevel@tonic-gate return ((fd >= 0) ? 0 : -1); 328*0Sstevel@tonic-gate } 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate /* 331*0Sstevel@tonic-gate * Retrieve the collection membership of all processes in our zone, and update 332*0Sstevel@tonic-gate * the psinfo of those non-system, non-zombie ones in collections. 333*0Sstevel@tonic-gate */ 334*0Sstevel@tonic-gate static void 335*0Sstevel@tonic-gate proc_cb(const pid_t pid) 336*0Sstevel@tonic-gate { 337*0Sstevel@tonic-gate static zoneid_t ours = (zoneid_t)-1; 338*0Sstevel@tonic-gate psinfo_t psinfo; 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gate if (ours == (zoneid_t)-1) 341*0Sstevel@tonic-gate ours = getzoneid(); 342*0Sstevel@tonic-gate 343*0Sstevel@tonic-gate if (get_psinfo(pid, &psinfo, -1, NULL, NULL, NULL) == 0 && 344*0Sstevel@tonic-gate psinfo.pr_zoneid == ours) 345*0Sstevel@tonic-gate lprocess_insert_mark(psinfo.pr_pid, rc_getidbypsinfo(&psinfo), 346*0Sstevel@tonic-gate psinfo.pr_psargs, psinfo.pr_nlwp == 0); 347*0Sstevel@tonic-gate } 348*0Sstevel@tonic-gate 349*0Sstevel@tonic-gate /* 350*0Sstevel@tonic-gate * Cache the process' psinfo fd, taking responsibility for freeing it. 351*0Sstevel@tonic-gate */ 352*0Sstevel@tonic-gate int 353*0Sstevel@tonic-gate lprocess_update_psinfo_fd_cb(void *arg, int fd) 354*0Sstevel@tonic-gate { 355*0Sstevel@tonic-gate lprocess_t *lpc = arg; 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gate lpc->lpc_psinfo_fd = fd; 358*0Sstevel@tonic-gate return (0); 359*0Sstevel@tonic-gate } 360*0Sstevel@tonic-gate 361*0Sstevel@tonic-gate /* 362*0Sstevel@tonic-gate * Update the RSS of processes in monitored collections. 363*0Sstevel@tonic-gate */ 364*0Sstevel@tonic-gate /*ARGSUSED*/ 365*0Sstevel@tonic-gate static int 366*0Sstevel@tonic-gate mem_sample_cb(lcollection_t *lcol, lprocess_t *lpc) 367*0Sstevel@tonic-gate { 368*0Sstevel@tonic-gate psinfo_t psinfo; 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gate if (get_psinfo(lpc->lpc_pid, &psinfo, lpc->lpc_psinfo_fd, 371*0Sstevel@tonic-gate lprocess_update_psinfo_fd_cb, lpc, lpc) == 0) { 372*0Sstevel@tonic-gate lpc->lpc_rss = psinfo.pr_rssize; 373*0Sstevel@tonic-gate lpc->lpc_size = psinfo.pr_size; 374*0Sstevel@tonic-gate } else { 375*0Sstevel@tonic-gate if (errno == ENOENT) 376*0Sstevel@tonic-gate debug("process %d finished\n", (int)lpc->lpc_pid); 377*0Sstevel@tonic-gate else 378*0Sstevel@tonic-gate debug("process %d: cannot read psinfo", 379*0Sstevel@tonic-gate (int)lpc->lpc_pid); 380*0Sstevel@tonic-gate lprocess_free(lpc); 381*0Sstevel@tonic-gate } 382*0Sstevel@tonic-gate 383*0Sstevel@tonic-gate return (0); 384*0Sstevel@tonic-gate } 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gate /* 387*0Sstevel@tonic-gate * Sample the collection RSS, updating the collection's statistics with the 388*0Sstevel@tonic-gate * results. 389*0Sstevel@tonic-gate */ 390*0Sstevel@tonic-gate /*ARGSUSED*/ 391*0Sstevel@tonic-gate static int 392*0Sstevel@tonic-gate rss_sample_col_cb(lcollection_t *lcol, void *arg) 393*0Sstevel@tonic-gate { 394*0Sstevel@tonic-gate int64_t excess; 395*0Sstevel@tonic-gate uint64_t rss; 396*0Sstevel@tonic-gate 397*0Sstevel@tonic-gate /* 398*0Sstevel@tonic-gate * If updating statistics for a new interval, reset the affected 399*0Sstevel@tonic-gate * counters. 400*0Sstevel@tonic-gate */ 401*0Sstevel@tonic-gate if (lcol->lcol_stat_invalidate != 0) { 402*0Sstevel@tonic-gate lcol->lcol_stat_old = lcol->lcol_stat; 403*0Sstevel@tonic-gate lcol->lcol_stat.lcols_min_rss = (int64_t)-1; 404*0Sstevel@tonic-gate lcol->lcol_stat.lcols_max_rss = 0; 405*0Sstevel@tonic-gate lcol->lcol_stat_invalidate = 0; 406*0Sstevel@tonic-gate } 407*0Sstevel@tonic-gate 408*0Sstevel@tonic-gate lcol->lcol_stat.lcols_rss_sample++; 409*0Sstevel@tonic-gate excess = lcol->lcol_rss - lcol->lcol_rss_cap; 410*0Sstevel@tonic-gate rss = lcol->lcol_rss; 411*0Sstevel@tonic-gate if (excess > 0) 412*0Sstevel@tonic-gate lcol->lcol_stat.lcols_rss_act_sum += rss; 413*0Sstevel@tonic-gate lcol->lcol_stat.lcols_rss_sum += rss; 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate if (lcol->lcol_stat.lcols_min_rss > rss) 416*0Sstevel@tonic-gate lcol->lcol_stat.lcols_min_rss = rss; 417*0Sstevel@tonic-gate if (lcol->lcol_stat.lcols_max_rss < rss) 418*0Sstevel@tonic-gate lcol->lcol_stat.lcols_max_rss = rss; 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate return (0); 421*0Sstevel@tonic-gate } 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gate /* 424*0Sstevel@tonic-gate * Open /proc and walk entries. 425*0Sstevel@tonic-gate */ 426*0Sstevel@tonic-gate static void 427*0Sstevel@tonic-gate proc_walk_all(void (*cb)(const pid_t)) 428*0Sstevel@tonic-gate { 429*0Sstevel@tonic-gate DIR *pdir; 430*0Sstevel@tonic-gate struct dirent *dirent; 431*0Sstevel@tonic-gate pid_t pid; 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate (void) rfd_reserve(1); 434*0Sstevel@tonic-gate if ((pdir = opendir("/proc")) == NULL) 435*0Sstevel@tonic-gate die(gettext("couldn't open /proc!")); 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gate while ((dirent = readdir(pdir)) != NULL) { 438*0Sstevel@tonic-gate if (strcmp(".", dirent->d_name) == 0 || 439*0Sstevel@tonic-gate strcmp("..", dirent->d_name) == 0) 440*0Sstevel@tonic-gate continue; 441*0Sstevel@tonic-gate pid = atoi(dirent->d_name); 442*0Sstevel@tonic-gate ASSERT(pid != 0 || strcmp(dirent->d_name, "0") == 0); 443*0Sstevel@tonic-gate if (pid == rcapd_pid) 444*0Sstevel@tonic-gate continue; 445*0Sstevel@tonic-gate else 446*0Sstevel@tonic-gate cb(pid); 447*0Sstevel@tonic-gate } 448*0Sstevel@tonic-gate (void) closedir(pdir); 449*0Sstevel@tonic-gate } 450*0Sstevel@tonic-gate 451*0Sstevel@tonic-gate /* 452*0Sstevel@tonic-gate * Memory update callback. 453*0Sstevel@tonic-gate */ 454*0Sstevel@tonic-gate static int 455*0Sstevel@tonic-gate memory_all_cb(lcollection_t *lcol, lprocess_t *lpc) 456*0Sstevel@tonic-gate { 457*0Sstevel@tonic-gate debug_high("%s %s, pid %d: rss += %llu/%llu\n", rcfg.rcfg_mode_name, 458*0Sstevel@tonic-gate lcol->lcol_name, (int)lpc->lpc_pid, 459*0Sstevel@tonic-gate (unsigned long long)lpc->lpc_rss, 460*0Sstevel@tonic-gate (unsigned long long)lpc->lpc_size); 461*0Sstevel@tonic-gate ASSERT(lpc->lpc_rss <= lpc->lpc_size); 462*0Sstevel@tonic-gate lcol->lcol_rss += lpc->lpc_rss; 463*0Sstevel@tonic-gate lcol->lcol_image_size += lpc->lpc_size; 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate return (0); 466*0Sstevel@tonic-gate } 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gate /* 469*0Sstevel@tonic-gate * Clear unmarked callback. 470*0Sstevel@tonic-gate */ 471*0Sstevel@tonic-gate /*ARGSUSED*/ 472*0Sstevel@tonic-gate static int 473*0Sstevel@tonic-gate sweep_process_cb(lcollection_t *lcol, lprocess_t *lpc) 474*0Sstevel@tonic-gate { 475*0Sstevel@tonic-gate if (lpc->lpc_mark) { 476*0Sstevel@tonic-gate lpc->lpc_mark = 0; 477*0Sstevel@tonic-gate } else { 478*0Sstevel@tonic-gate debug("process %d finished\n", (int)lpc->lpc_pid); 479*0Sstevel@tonic-gate lprocess_free(lpc); 480*0Sstevel@tonic-gate } 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate return (0); 483*0Sstevel@tonic-gate } 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate /* 486*0Sstevel@tonic-gate * Memory clear callback. 487*0Sstevel@tonic-gate */ 488*0Sstevel@tonic-gate /*ARGSUSED*/ 489*0Sstevel@tonic-gate static int 490*0Sstevel@tonic-gate collection_zero_mem_cb(lcollection_t *lcol, void *arg) 491*0Sstevel@tonic-gate { 492*0Sstevel@tonic-gate lcol->lcol_rss = 0; 493*0Sstevel@tonic-gate lcol->lcol_image_size = 0; 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gate return (0); 496*0Sstevel@tonic-gate } 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gate /* 499*0Sstevel@tonic-gate * Print, for debugging purposes, a collection's recently-sampled RSS and 500*0Sstevel@tonic-gate * excess. 501*0Sstevel@tonic-gate */ 502*0Sstevel@tonic-gate /*ARGSUSED*/ 503*0Sstevel@tonic-gate static int 504*0Sstevel@tonic-gate excess_print_cb(lcollection_t *lcol, void *arg) 505*0Sstevel@tonic-gate { 506*0Sstevel@tonic-gate int64_t excess = lcol->lcol_rss - lcol->lcol_rss_cap; 507*0Sstevel@tonic-gate 508*0Sstevel@tonic-gate debug("%s %s rss/cap: %llu/%llu, excess = %lld kB\n", 509*0Sstevel@tonic-gate rcfg.rcfg_mode_name, lcol->lcol_name, 510*0Sstevel@tonic-gate (unsigned long long)lcol->lcol_rss, 511*0Sstevel@tonic-gate (unsigned long long)lcol->lcol_rss_cap, 512*0Sstevel@tonic-gate (long long)excess); 513*0Sstevel@tonic-gate 514*0Sstevel@tonic-gate return (0); 515*0Sstevel@tonic-gate } 516*0Sstevel@tonic-gate 517*0Sstevel@tonic-gate /* 518*0Sstevel@tonic-gate * Scan those collections which have exceeded their caps. 519*0Sstevel@tonic-gate */ 520*0Sstevel@tonic-gate /*ARGSUSED*/ 521*0Sstevel@tonic-gate static int 522*0Sstevel@tonic-gate scan_cb(lcollection_t *lcol, void *arg) 523*0Sstevel@tonic-gate { 524*0Sstevel@tonic-gate int64_t excess; 525*0Sstevel@tonic-gate 526*0Sstevel@tonic-gate if ((excess = lcol->lcol_rss - lcol->lcol_rss_cap) > 0) { 527*0Sstevel@tonic-gate scan(lcol, excess); 528*0Sstevel@tonic-gate lcol->lcol_stat.lcols_scan++; 529*0Sstevel@tonic-gate } 530*0Sstevel@tonic-gate 531*0Sstevel@tonic-gate return (0); 532*0Sstevel@tonic-gate } 533*0Sstevel@tonic-gate 534*0Sstevel@tonic-gate /* 535*0Sstevel@tonic-gate * Do a soft scan of those collections which have excesses. A soft scan is one 536*0Sstevel@tonic-gate * in which the cap enforcement pressure is taken into account. The difference 537*0Sstevel@tonic-gate * between the utilized physical memory and the cap enforcement pressure will 538*0Sstevel@tonic-gate * be scanned-for, and each collection will be scanned proportionally by their 539*0Sstevel@tonic-gate * present excesses. 540*0Sstevel@tonic-gate */ 541*0Sstevel@tonic-gate static int 542*0Sstevel@tonic-gate soft_scan_cb(lcollection_t *lcol, void *a) 543*0Sstevel@tonic-gate { 544*0Sstevel@tonic-gate int64_t excess; 545*0Sstevel@tonic-gate soft_scan_arg_t *arg = a; 546*0Sstevel@tonic-gate 547*0Sstevel@tonic-gate if ((excess = lcol->lcol_rss - lcol->lcol_rss_cap) > 0) { 548*0Sstevel@tonic-gate debug("col %lld excess %lld scan_goal %lld sum_excess %llu, " 549*0Sstevel@tonic-gate "scanning %lld\n", (long long)lcol->lcol_id, 550*0Sstevel@tonic-gate (long long)excess, (long long)arg->ssa_scan_goal, 551*0Sstevel@tonic-gate (unsigned long long)arg->ssa_sum_excess, 552*0Sstevel@tonic-gate (long long)(excess * arg->ssa_scan_goal / 553*0Sstevel@tonic-gate arg->ssa_sum_excess)); 554*0Sstevel@tonic-gate 555*0Sstevel@tonic-gate scan(lcol, (int64_t)(excess * arg->ssa_scan_goal / 556*0Sstevel@tonic-gate arg->ssa_sum_excess)); 557*0Sstevel@tonic-gate lcol->lcol_stat.lcols_scan++; 558*0Sstevel@tonic-gate } 559*0Sstevel@tonic-gate 560*0Sstevel@tonic-gate return (0); 561*0Sstevel@tonic-gate } 562*0Sstevel@tonic-gate 563*0Sstevel@tonic-gate /* 564*0Sstevel@tonic-gate * When a scan could happen, but caps aren't enforced tick the 565*0Sstevel@tonic-gate * lcols_unenforced_cap counter. 566*0Sstevel@tonic-gate */ 567*0Sstevel@tonic-gate /*ARGSUSED*/ 568*0Sstevel@tonic-gate static int 569*0Sstevel@tonic-gate unenforced_cap_cb(lcollection_t *lcol, void *arg) 570*0Sstevel@tonic-gate { 571*0Sstevel@tonic-gate lcol->lcol_stat.lcols_unenforced_cap++; 572*0Sstevel@tonic-gate 573*0Sstevel@tonic-gate return (0); 574*0Sstevel@tonic-gate } 575*0Sstevel@tonic-gate 576*0Sstevel@tonic-gate /* 577*0Sstevel@tonic-gate * Update the count of physically installed memory. 578*0Sstevel@tonic-gate */ 579*0Sstevel@tonic-gate static void 580*0Sstevel@tonic-gate update_phys_total(void) 581*0Sstevel@tonic-gate { 582*0Sstevel@tonic-gate uint64_t old_phys_total; 583*0Sstevel@tonic-gate 584*0Sstevel@tonic-gate old_phys_total = phys_total; 585*0Sstevel@tonic-gate phys_total = (uint64_t)sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) 586*0Sstevel@tonic-gate / 1024; 587*0Sstevel@tonic-gate if (phys_total != old_phys_total) 588*0Sstevel@tonic-gate debug("physical memory%s: %lluM\n", (old_phys_total == 0 ? 589*0Sstevel@tonic-gate "" : " adjusted"), (unsigned long long)(phys_total / 1024)); 590*0Sstevel@tonic-gate } 591*0Sstevel@tonic-gate 592*0Sstevel@tonic-gate /* 593*0Sstevel@tonic-gate * Unlink a process from its collection, updating relevant statistics, and 594*0Sstevel@tonic-gate * freeing its associated memory. 595*0Sstevel@tonic-gate */ 596*0Sstevel@tonic-gate void 597*0Sstevel@tonic-gate lprocess_free(lprocess_t *lpc) 598*0Sstevel@tonic-gate { 599*0Sstevel@tonic-gate pid_t pid; 600*0Sstevel@tonic-gate 601*0Sstevel@tonic-gate lpc->lpc_collection->lcol_stat.lcols_proc_out++; 602*0Sstevel@tonic-gate 603*0Sstevel@tonic-gate if (lpc->lpc_prev != NULL) 604*0Sstevel@tonic-gate lpc->lpc_prev->lpc_next = lpc->lpc_next; 605*0Sstevel@tonic-gate if (lpc->lpc_next != NULL) 606*0Sstevel@tonic-gate lpc->lpc_next->lpc_prev = lpc->lpc_prev; 607*0Sstevel@tonic-gate if (lpc->lpc_collection->lcol_lprocess == lpc) 608*0Sstevel@tonic-gate lpc->lpc_collection->lcol_lprocess = (lpc->lpc_next != 609*0Sstevel@tonic-gate lpc ? lpc->lpc_next : NULL); 610*0Sstevel@tonic-gate lpc->lpc_next = lpc->lpc_prev = NULL; 611*0Sstevel@tonic-gate 612*0Sstevel@tonic-gate if (lpc->lpc_prpageheader != NULL) 613*0Sstevel@tonic-gate free(lpc->lpc_prpageheader); 614*0Sstevel@tonic-gate if (lpc->lpc_xmap != NULL) 615*0Sstevel@tonic-gate free(lpc->lpc_xmap); 616*0Sstevel@tonic-gate if (lpc->lpc_psinfo_fd >= 0) { 617*0Sstevel@tonic-gate if (rfd_close(lpc->lpc_psinfo_fd) != 0) 618*0Sstevel@tonic-gate debug("could not close %d lpc_psinfo_fd %d", 619*0Sstevel@tonic-gate (int)lpc->lpc_pid, lpc->lpc_psinfo_fd); 620*0Sstevel@tonic-gate lpc->lpc_psinfo_fd = -1; 621*0Sstevel@tonic-gate } 622*0Sstevel@tonic-gate if (lpc->lpc_pgdata_fd >= 0) { 623*0Sstevel@tonic-gate if (rfd_close(lpc->lpc_pgdata_fd) != 0) 624*0Sstevel@tonic-gate debug("could not close %d lpc_pgdata_fd %d", 625*0Sstevel@tonic-gate (int)lpc->lpc_pid, lpc->lpc_pgdata_fd); 626*0Sstevel@tonic-gate lpc->lpc_pgdata_fd = -1; 627*0Sstevel@tonic-gate } 628*0Sstevel@tonic-gate if (lpc->lpc_xmap_fd >= 0) { 629*0Sstevel@tonic-gate if (rfd_close(lpc->lpc_xmap_fd) != 0) 630*0Sstevel@tonic-gate debug("could not close %d lpc_xmap_fd %d", 631*0Sstevel@tonic-gate (int)lpc->lpc_pid, lpc->lpc_xmap_fd); 632*0Sstevel@tonic-gate lpc->lpc_xmap_fd = -1; 633*0Sstevel@tonic-gate } 634*0Sstevel@tonic-gate if (lpc->lpc_ignore != NULL) 635*0Sstevel@tonic-gate lmapping_free(&lpc->lpc_ignore); 636*0Sstevel@tonic-gate pid = lpc->lpc_pid; 637*0Sstevel@tonic-gate free(lpc); 638*0Sstevel@tonic-gate debug_high("process %d freed\n", (int)pid); 639*0Sstevel@tonic-gate } 640*0Sstevel@tonic-gate 641*0Sstevel@tonic-gate /* 642*0Sstevel@tonic-gate * Collection clear callback. 643*0Sstevel@tonic-gate */ 644*0Sstevel@tonic-gate /*ARGSUSED*/ 645*0Sstevel@tonic-gate static int 646*0Sstevel@tonic-gate collection_clear_cb(lcollection_t *lcol, void *arg) 647*0Sstevel@tonic-gate { 648*0Sstevel@tonic-gate lcol->lcol_mark = 0; 649*0Sstevel@tonic-gate 650*0Sstevel@tonic-gate return (0); 651*0Sstevel@tonic-gate } 652*0Sstevel@tonic-gate 653*0Sstevel@tonic-gate /* 654*0Sstevel@tonic-gate * Respond to a terminating signal by setting a termination flag. 655*0Sstevel@tonic-gate */ 656*0Sstevel@tonic-gate /*ARGSUSED*/ 657*0Sstevel@tonic-gate static void 658*0Sstevel@tonic-gate terminate_signal(int signal) 659*0Sstevel@tonic-gate { 660*0Sstevel@tonic-gate if (termination_signal == 0) 661*0Sstevel@tonic-gate termination_signal = signal; 662*0Sstevel@tonic-gate should_run = 0; 663*0Sstevel@tonic-gate } 664*0Sstevel@tonic-gate 665*0Sstevel@tonic-gate /* 666*0Sstevel@tonic-gate * Handle any synchronous or asynchronous signals that would ordinarily cause a 667*0Sstevel@tonic-gate * process to abort. 668*0Sstevel@tonic-gate */ 669*0Sstevel@tonic-gate /*ARGSUSED*/ 670*0Sstevel@tonic-gate static void 671*0Sstevel@tonic-gate abort_signal(int signal) 672*0Sstevel@tonic-gate { 673*0Sstevel@tonic-gate /* 674*0Sstevel@tonic-gate * Allow the scanner to make a last-ditch effort to resume any stopped 675*0Sstevel@tonic-gate * processes. 676*0Sstevel@tonic-gate */ 677*0Sstevel@tonic-gate scan_abort(); 678*0Sstevel@tonic-gate abort(); 679*0Sstevel@tonic-gate } 680*0Sstevel@tonic-gate 681*0Sstevel@tonic-gate /* 682*0Sstevel@tonic-gate * Clean up collections which have been removed due to configuration. Unlink 683*0Sstevel@tonic-gate * the collection from lcollection and free it. 684*0Sstevel@tonic-gate */ 685*0Sstevel@tonic-gate /*ARGSUSED*/ 686*0Sstevel@tonic-gate static int 687*0Sstevel@tonic-gate collection_sweep_cb(lcollection_t *lcol, void *arg) 688*0Sstevel@tonic-gate { 689*0Sstevel@tonic-gate if (lcol->lcol_mark == 0) { 690*0Sstevel@tonic-gate debug("freeing %s %s\n", rcfg.rcfg_mode_name, lcol->lcol_name); 691*0Sstevel@tonic-gate lcollection_free(lcol); 692*0Sstevel@tonic-gate } 693*0Sstevel@tonic-gate 694*0Sstevel@tonic-gate return (0); 695*0Sstevel@tonic-gate } 696*0Sstevel@tonic-gate 697*0Sstevel@tonic-gate /* 698*0Sstevel@tonic-gate * Set those variables which depend on the global configuration. 699*0Sstevel@tonic-gate */ 700*0Sstevel@tonic-gate static void 701*0Sstevel@tonic-gate finish_configuration(void) 702*0Sstevel@tonic-gate { 703*0Sstevel@tonic-gate /* 704*0Sstevel@tonic-gate * Warn that any lnode (or non-project) mode specification (by an SRM 705*0Sstevel@tonic-gate * 1.3 configuration file, for example) is ignored. 706*0Sstevel@tonic-gate */ 707*0Sstevel@tonic-gate if (strcmp(rcfg.rcfg_mode_name, "project") != 0) { 708*0Sstevel@tonic-gate warn(gettext("%s mode specification ignored -- using project" 709*0Sstevel@tonic-gate " mode\n"), rcfg.rcfg_mode_name); 710*0Sstevel@tonic-gate rcfg.rcfg_mode_name = "project"; 711*0Sstevel@tonic-gate rcfg.rcfg_mode = rctype_project; 712*0Sstevel@tonic-gate } 713*0Sstevel@tonic-gate 714*0Sstevel@tonic-gate lcollection_set_type(rcfg.rcfg_mode); 715*0Sstevel@tonic-gate } 716*0Sstevel@tonic-gate 717*0Sstevel@tonic-gate /* 718*0Sstevel@tonic-gate * Cause the configuration file to be reread and applied. 719*0Sstevel@tonic-gate */ 720*0Sstevel@tonic-gate static void 721*0Sstevel@tonic-gate reread_configuration_file(void) 722*0Sstevel@tonic-gate { 723*0Sstevel@tonic-gate rcfg_t rcfg_new; 724*0Sstevel@tonic-gate struct stat st; 725*0Sstevel@tonic-gate 726*0Sstevel@tonic-gate if (stat(rcfg.rcfg_filename, &st) == 0 && st.st_mtime == 727*0Sstevel@tonic-gate rcfg.rcfg_last_modification) 728*0Sstevel@tonic-gate return; 729*0Sstevel@tonic-gate 730*0Sstevel@tonic-gate if (rcfg_read(rcfg.rcfg_filename, rcfg.rcfg_fd, &rcfg_new, 731*0Sstevel@tonic-gate update_statistics) != 0) 732*0Sstevel@tonic-gate warn(gettext("can't reread configuration")); 733*0Sstevel@tonic-gate else { 734*0Sstevel@tonic-gate /* 735*0Sstevel@tonic-gate * The configuration file has been read. Remove existing 736*0Sstevel@tonic-gate * collections in case there is a change in collection type. 737*0Sstevel@tonic-gate */ 738*0Sstevel@tonic-gate if (rcfg.rcfg_mode != rcfg_new.rcfg_mode) { 739*0Sstevel@tonic-gate list_walk_collection(collection_clear_cb, NULL); 740*0Sstevel@tonic-gate list_walk_collection(collection_sweep_cb, NULL); 741*0Sstevel@tonic-gate } 742*0Sstevel@tonic-gate 743*0Sstevel@tonic-gate /* 744*0Sstevel@tonic-gate * Make the newly-read configuration the global one, and update 745*0Sstevel@tonic-gate * any variables that depend on it. 746*0Sstevel@tonic-gate */ 747*0Sstevel@tonic-gate rcfg = rcfg_new; 748*0Sstevel@tonic-gate finish_configuration(); 749*0Sstevel@tonic-gate } 750*0Sstevel@tonic-gate } 751*0Sstevel@tonic-gate 752*0Sstevel@tonic-gate /* 753*0Sstevel@tonic-gate * Reread the configuration filex, then examine changes, additions, and 754*0Sstevel@tonic-gate * deletions to cap definitions. 755*0Sstevel@tonic-gate */ 756*0Sstevel@tonic-gate static void 757*0Sstevel@tonic-gate reconfigure(void) 758*0Sstevel@tonic-gate { 759*0Sstevel@tonic-gate debug("reconfigure...\n"); 760*0Sstevel@tonic-gate 761*0Sstevel@tonic-gate /* 762*0Sstevel@tonic-gate * Reread the configuration data. 763*0Sstevel@tonic-gate */ 764*0Sstevel@tonic-gate reread_configuration_file(); 765*0Sstevel@tonic-gate 766*0Sstevel@tonic-gate /* 767*0Sstevel@tonic-gate * Walk the lcollection, marking active collections so inactive ones 768*0Sstevel@tonic-gate * can be freed. 769*0Sstevel@tonic-gate */ 770*0Sstevel@tonic-gate list_walk_collection(collection_clear_cb, NULL); 771*0Sstevel@tonic-gate lcollection_update(LCU_ACTIVE_ONLY); /* mark */ 772*0Sstevel@tonic-gate list_walk_collection(collection_sweep_cb, NULL); 773*0Sstevel@tonic-gate } 774*0Sstevel@tonic-gate 775*0Sstevel@tonic-gate /* 776*0Sstevel@tonic-gate * Respond to SIGHUP by triggering the rereading the configuration file and cap 777*0Sstevel@tonic-gate * definitions. 778*0Sstevel@tonic-gate */ 779*0Sstevel@tonic-gate /*ARGSUSED*/ 780*0Sstevel@tonic-gate static void 781*0Sstevel@tonic-gate sighup(int signal) 782*0Sstevel@tonic-gate { 783*0Sstevel@tonic-gate should_reconfigure = 1; 784*0Sstevel@tonic-gate } 785*0Sstevel@tonic-gate 786*0Sstevel@tonic-gate /* 787*0Sstevel@tonic-gate * Print, for debugging purposes, each collection's interval statistics. 788*0Sstevel@tonic-gate */ 789*0Sstevel@tonic-gate /*ARGSUSED*/ 790*0Sstevel@tonic-gate static int 791*0Sstevel@tonic-gate simple_report_collection_cb(lcollection_t *lcol, void *arg) 792*0Sstevel@tonic-gate { 793*0Sstevel@tonic-gate #define DELTA(field) \ 794*0Sstevel@tonic-gate (unsigned long long)(lcol->lcol_stat_invalidate ? 0 : \ 795*0Sstevel@tonic-gate (lcol->lcol_stat.field - lcol->lcol_stat_old.field)) 796*0Sstevel@tonic-gate #define VALID(field) \ 797*0Sstevel@tonic-gate (unsigned long long)(lcol->lcol_stat_invalidate ? 0 : \ 798*0Sstevel@tonic-gate lcol->lcol_stat.field) 799*0Sstevel@tonic-gate 800*0Sstevel@tonic-gate debug("%s %s status: succeeded/attempted (k): %llu/%llu, " 801*0Sstevel@tonic-gate "ineffective/scans/unenforced/samplings: %llu/%llu/%llu/%llu, RSS " 802*0Sstevel@tonic-gate "min/max (k): %llu/%llu, cap %llu kB, processes/thpt: %llu/%llu, " 803*0Sstevel@tonic-gate "%llu scans over %llu ms\n", rcfg.rcfg_mode_name, lcol->lcol_name, 804*0Sstevel@tonic-gate DELTA(lcols_pg_eff), DELTA(lcols_pg_att), 805*0Sstevel@tonic-gate DELTA(lcols_scan_ineffective), DELTA(lcols_scan), 806*0Sstevel@tonic-gate DELTA(lcols_unenforced_cap), DELTA(lcols_rss_sample), 807*0Sstevel@tonic-gate VALID(lcols_min_rss), VALID(lcols_max_rss), 808*0Sstevel@tonic-gate (unsigned long long)lcol->lcol_rss_cap, 809*0Sstevel@tonic-gate (unsigned long long)(lcol->lcol_stat.lcols_proc_in - 810*0Sstevel@tonic-gate lcol->lcol_stat.lcols_proc_out), DELTA(lcols_proc_out), 811*0Sstevel@tonic-gate DELTA(lcols_scan_count), DELTA(lcols_scan_time_complete) / (NANOSEC 812*0Sstevel@tonic-gate / MILLISEC)); 813*0Sstevel@tonic-gate 814*0Sstevel@tonic-gate #undef DELTA 815*0Sstevel@tonic-gate #undef VALID 816*0Sstevel@tonic-gate 817*0Sstevel@tonic-gate return (0); 818*0Sstevel@tonic-gate } 819*0Sstevel@tonic-gate 820*0Sstevel@tonic-gate /* 821*0Sstevel@tonic-gate * Record each collection's interval statistics in the statistics file. 822*0Sstevel@tonic-gate */ 823*0Sstevel@tonic-gate static int 824*0Sstevel@tonic-gate report_collection_cb(lcollection_t *lcol, void *arg) 825*0Sstevel@tonic-gate { 826*0Sstevel@tonic-gate lcollection_report_t dc; 827*0Sstevel@tonic-gate int fd = (intptr_t)arg; 828*0Sstevel@tonic-gate 829*0Sstevel@tonic-gate /* 830*0Sstevel@tonic-gate * Copy the relevant fields to the collection's record. 831*0Sstevel@tonic-gate */ 832*0Sstevel@tonic-gate bzero(&dc, sizeof (dc)); 833*0Sstevel@tonic-gate dc.lcol_id = lcol->lcol_id; 834*0Sstevel@tonic-gate (void) strcpy(dc.lcol_name, lcol->lcol_name); 835*0Sstevel@tonic-gate dc.lcol_rss = lcol->lcol_rss; 836*0Sstevel@tonic-gate dc.lcol_image_size = lcol->lcol_image_size; 837*0Sstevel@tonic-gate dc.lcol_rss_cap = lcol->lcol_rss_cap; 838*0Sstevel@tonic-gate dc.lcol_stat = lcol->lcol_stat; 839*0Sstevel@tonic-gate 840*0Sstevel@tonic-gate if (write(fd, &dc, sizeof (dc)) == sizeof (dc)) { 841*0Sstevel@tonic-gate /* 842*0Sstevel@tonic-gate * Set a flag to indicate that the exported interval snapshot 843*0Sstevel@tonic-gate * values should be reset at the next sample. 844*0Sstevel@tonic-gate */ 845*0Sstevel@tonic-gate lcol->lcol_stat_invalidate = 1; 846*0Sstevel@tonic-gate } else { 847*0Sstevel@tonic-gate debug("can't write %s %s statistics", rcfg.rcfg_mode_name, 848*0Sstevel@tonic-gate lcol->lcol_name); 849*0Sstevel@tonic-gate } 850*0Sstevel@tonic-gate 851*0Sstevel@tonic-gate return (0); 852*0Sstevel@tonic-gate } 853*0Sstevel@tonic-gate 854*0Sstevel@tonic-gate /* 855*0Sstevel@tonic-gate * Determine the count of pages scanned by the global page scanner, obtained 856*0Sstevel@tonic-gate * from the cpu_stat:*::scan kstats. Return zero on success. 857*0Sstevel@tonic-gate */ 858*0Sstevel@tonic-gate static int 859*0Sstevel@tonic-gate get_globally_scanned_pages(uint64_t *scannedp) 860*0Sstevel@tonic-gate { 861*0Sstevel@tonic-gate kstat_t *ksp; 862*0Sstevel@tonic-gate uint64_t scanned = 0; 863*0Sstevel@tonic-gate 864*0Sstevel@tonic-gate if (kstat_chain_update(kctl) == -1) { 865*0Sstevel@tonic-gate warn(gettext("can't update kstat chain")); 866*0Sstevel@tonic-gate return (0); 867*0Sstevel@tonic-gate } 868*0Sstevel@tonic-gate 869*0Sstevel@tonic-gate for (ksp = kctl->kc_chain; ksp != NULL; ksp = ksp->ks_next) { 870*0Sstevel@tonic-gate if (strcmp(ksp->ks_module, "cpu_stat") == 0) { 871*0Sstevel@tonic-gate if (kstat_read(kctl, ksp, NULL) != -1) { 872*0Sstevel@tonic-gate scanned += ((cpu_stat_t *) 873*0Sstevel@tonic-gate ksp->ks_data)->cpu_vminfo.scan; 874*0Sstevel@tonic-gate } else 875*0Sstevel@tonic-gate return (-1); 876*0Sstevel@tonic-gate } 877*0Sstevel@tonic-gate } 878*0Sstevel@tonic-gate 879*0Sstevel@tonic-gate *scannedp = scanned; 880*0Sstevel@tonic-gate return (0); 881*0Sstevel@tonic-gate } 882*0Sstevel@tonic-gate 883*0Sstevel@tonic-gate /* 884*0Sstevel@tonic-gate * Update the shared statistics file with each collection's current statistics. 885*0Sstevel@tonic-gate * Return zero on success. 886*0Sstevel@tonic-gate */ 887*0Sstevel@tonic-gate static int 888*0Sstevel@tonic-gate update_statistics(void) 889*0Sstevel@tonic-gate { 890*0Sstevel@tonic-gate int fd, res; 891*0Sstevel@tonic-gate static char template[LINELEN]; 892*0Sstevel@tonic-gate 893*0Sstevel@tonic-gate /* 894*0Sstevel@tonic-gate * Create a temporary file. 895*0Sstevel@tonic-gate */ 896*0Sstevel@tonic-gate if (sizeof (template) < (strlen(rcfg.rcfg_stat_file) + 897*0Sstevel@tonic-gate strlen(STAT_TEMPLATE_SUFFIX) + 1)) { 898*0Sstevel@tonic-gate debug("temporary file template size too small\n"); 899*0Sstevel@tonic-gate return (-1); 900*0Sstevel@tonic-gate } 901*0Sstevel@tonic-gate (void) strcpy(template, rcfg.rcfg_stat_file); 902*0Sstevel@tonic-gate (void) strcat(template, STAT_TEMPLATE_SUFFIX); 903*0Sstevel@tonic-gate (void) rfd_reserve(1); 904*0Sstevel@tonic-gate fd = mkstemp(template); 905*0Sstevel@tonic-gate 906*0Sstevel@tonic-gate /* 907*0Sstevel@tonic-gate * Write the header and per-collection statistics. 908*0Sstevel@tonic-gate */ 909*0Sstevel@tonic-gate if (fd >= 0) { 910*0Sstevel@tonic-gate rcapd_stat_hdr_t rs; 911*0Sstevel@tonic-gate 912*0Sstevel@tonic-gate rs.rs_pid = rcapd_pid; 913*0Sstevel@tonic-gate rs.rs_time = gethrtime(); 914*0Sstevel@tonic-gate ASSERT(sizeof (rs.rs_mode) > strlen(rcfg.rcfg_mode_name)); 915*0Sstevel@tonic-gate (void) strcpy(rs.rs_mode, rcfg.rcfg_mode_name); 916*0Sstevel@tonic-gate rs.rs_pressure_cur = memory_pressure; 917*0Sstevel@tonic-gate rs.rs_pressure_cap = rcfg.rcfg_memory_cap_enforcement_pressure; 918*0Sstevel@tonic-gate rs.rs_pressure_sample = memory_pressure_sample; 919*0Sstevel@tonic-gate 920*0Sstevel@tonic-gate if (fchmod(fd, 0644) == 0 && write(fd, &rs, sizeof (rs)) == 921*0Sstevel@tonic-gate sizeof (rs)) { 922*0Sstevel@tonic-gate list_walk_collection(report_collection_cb, 923*0Sstevel@tonic-gate (void *)(intptr_t)fd); 924*0Sstevel@tonic-gate /* 925*0Sstevel@tonic-gate * Replace the existing statistics file with this new 926*0Sstevel@tonic-gate * one. 927*0Sstevel@tonic-gate */ 928*0Sstevel@tonic-gate res = rename(template, rcfg.rcfg_stat_file); 929*0Sstevel@tonic-gate } else 930*0Sstevel@tonic-gate res = -1; 931*0Sstevel@tonic-gate (void) close(fd); 932*0Sstevel@tonic-gate } else 933*0Sstevel@tonic-gate res = -1; 934*0Sstevel@tonic-gate 935*0Sstevel@tonic-gate return (res); 936*0Sstevel@tonic-gate } 937*0Sstevel@tonic-gate 938*0Sstevel@tonic-gate /* 939*0Sstevel@tonic-gate * Verify the statistics file can be created and written to, and die if an 940*0Sstevel@tonic-gate * existing file may be in use by another rcapd. 941*0Sstevel@tonic-gate */ 942*0Sstevel@tonic-gate static int 943*0Sstevel@tonic-gate verify_statistics(void) 944*0Sstevel@tonic-gate { 945*0Sstevel@tonic-gate pid_t pid; 946*0Sstevel@tonic-gate 947*0Sstevel@tonic-gate /* 948*0Sstevel@tonic-gate * Warn if another instance of rcapd might be active. 949*0Sstevel@tonic-gate */ 950*0Sstevel@tonic-gate (void) rfd_reserve(1); 951*0Sstevel@tonic-gate pid = stat_get_rcapd_pid(rcfg.rcfg_stat_file); 952*0Sstevel@tonic-gate if (pid != rcapd_pid && pid != -1) 953*0Sstevel@tonic-gate die(gettext("%s exists; rcapd may already be active\n"), 954*0Sstevel@tonic-gate rcfg.rcfg_stat_file); 955*0Sstevel@tonic-gate 956*0Sstevel@tonic-gate return (update_statistics()); 957*0Sstevel@tonic-gate } 958*0Sstevel@tonic-gate 959*0Sstevel@tonic-gate static int 960*0Sstevel@tonic-gate sum_excess_cb(lcollection_t *lcol, void *arg) 961*0Sstevel@tonic-gate { 962*0Sstevel@tonic-gate uint64_t *sum_excess = arg; 963*0Sstevel@tonic-gate 964*0Sstevel@tonic-gate *sum_excess += MAX((int64_t)0, (int64_t)(lcol->lcol_rss - 965*0Sstevel@tonic-gate lcol->lcol_rss_cap)); 966*0Sstevel@tonic-gate return (0); 967*0Sstevel@tonic-gate } 968*0Sstevel@tonic-gate 969*0Sstevel@tonic-gate static void 970*0Sstevel@tonic-gate rcapd_usage(void) 971*0Sstevel@tonic-gate { 972*0Sstevel@tonic-gate info(gettext("usage: rcapd [-d]\n")); 973*0Sstevel@tonic-gate } 974*0Sstevel@tonic-gate 975*0Sstevel@tonic-gate void 976*0Sstevel@tonic-gate check_update_statistics(void) 977*0Sstevel@tonic-gate { 978*0Sstevel@tonic-gate hrtime_t now = gethrtime(); 979*0Sstevel@tonic-gate 980*0Sstevel@tonic-gate if (EVENT_TIME(now, next_report)) { 981*0Sstevel@tonic-gate debug("updating statistics...\n"); 982*0Sstevel@tonic-gate list_walk_collection(simple_report_collection_cb, NULL); 983*0Sstevel@tonic-gate if (update_statistics() != 0) 984*0Sstevel@tonic-gate debug("couldn't update statistics"); 985*0Sstevel@tonic-gate next_report = NEXT_REPORT_EVENT_TIME(now, 986*0Sstevel@tonic-gate rcfg.rcfg_report_interval); 987*0Sstevel@tonic-gate } 988*0Sstevel@tonic-gate } 989*0Sstevel@tonic-gate 990*0Sstevel@tonic-gate static void 991*0Sstevel@tonic-gate verify_and_set_privileges(void) 992*0Sstevel@tonic-gate { 993*0Sstevel@tonic-gate priv_set_t *required = 994*0Sstevel@tonic-gate priv_str_to_set("zone,sys_resource,proc_owner", ",", NULL); 995*0Sstevel@tonic-gate 996*0Sstevel@tonic-gate /* 997*0Sstevel@tonic-gate * Ensure the required privileges, suitable for controlling processes, 998*0Sstevel@tonic-gate * are possessed. 999*0Sstevel@tonic-gate */ 1000*0Sstevel@tonic-gate if (setppriv(PRIV_SET, PRIV_PERMITTED, required) != 0 || setppriv( 1001*0Sstevel@tonic-gate PRIV_SET, PRIV_EFFECTIVE, required) != 0) 1002*0Sstevel@tonic-gate die(gettext("can't set requisite privileges")); 1003*0Sstevel@tonic-gate 1004*0Sstevel@tonic-gate /* 1005*0Sstevel@tonic-gate * Ensure access to /var/run/daemon. 1006*0Sstevel@tonic-gate */ 1007*0Sstevel@tonic-gate if (setreuid(DAEMON_UID, DAEMON_UID) != 0) 1008*0Sstevel@tonic-gate die(gettext("cannot become user daemon")); 1009*0Sstevel@tonic-gate 1010*0Sstevel@tonic-gate priv_freeset(required); 1011*0Sstevel@tonic-gate } 1012*0Sstevel@tonic-gate 1013*0Sstevel@tonic-gate int 1014*0Sstevel@tonic-gate main(int argc, char *argv[]) 1015*0Sstevel@tonic-gate { 1016*0Sstevel@tonic-gate int res; 1017*0Sstevel@tonic-gate int should_fork = 1; /* fork flag */ 1018*0Sstevel@tonic-gate hrtime_t now; /* current time */ 1019*0Sstevel@tonic-gate hrtime_t next; /* time of next event */ 1020*0Sstevel@tonic-gate int sig; /* signal iteration */ 1021*0Sstevel@tonic-gate struct rlimit rl; 1022*0Sstevel@tonic-gate hrtime_t next_proc_walk; /* time of next /proc scan */ 1023*0Sstevel@tonic-gate hrtime_t next_configuration; /* time of next configuration */ 1024*0Sstevel@tonic-gate hrtime_t next_rss_sample; /* (latest) time of next RSS sample */ 1025*0Sstevel@tonic-gate int old_enforce_caps; /* track changes in enforcement */ 1026*0Sstevel@tonic-gate /* conditions */ 1027*0Sstevel@tonic-gate soft_scan_arg_t arg; 1028*0Sstevel@tonic-gate 1029*0Sstevel@tonic-gate (void) set_message_priority(RCM_INFO); 1030*0Sstevel@tonic-gate (void) setprogname("rcapd"); 1031*0Sstevel@tonic-gate rcapd_pid = getpid(); 1032*0Sstevel@tonic-gate (void) chdir("/"); 1033*0Sstevel@tonic-gate should_run = 1; 1034*0Sstevel@tonic-gate ever_ran = 0; 1035*0Sstevel@tonic-gate 1036*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1037*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1038*0Sstevel@tonic-gate 1039*0Sstevel@tonic-gate /* 1040*0Sstevel@tonic-gate * Parse command-line options. 1041*0Sstevel@tonic-gate */ 1042*0Sstevel@tonic-gate while ((res = getopt(argc, argv, "dF")) > 0) 1043*0Sstevel@tonic-gate switch (res) { 1044*0Sstevel@tonic-gate case 'd': 1045*0Sstevel@tonic-gate should_fork = 0; 1046*0Sstevel@tonic-gate if (debug_mode == 0) { 1047*0Sstevel@tonic-gate debug_mode = 1; 1048*0Sstevel@tonic-gate (void) set_message_priority(RCM_DEBUG); 1049*0Sstevel@tonic-gate } else 1050*0Sstevel@tonic-gate (void) set_message_priority(RCM_DEBUG_HIGH); 1051*0Sstevel@tonic-gate break; 1052*0Sstevel@tonic-gate case 'F': 1053*0Sstevel@tonic-gate should_fork = 0; 1054*0Sstevel@tonic-gate break; 1055*0Sstevel@tonic-gate default: 1056*0Sstevel@tonic-gate rcapd_usage(); 1057*0Sstevel@tonic-gate return (E_USAGE); 1058*0Sstevel@tonic-gate /*NOTREACHED*/ 1059*0Sstevel@tonic-gate } 1060*0Sstevel@tonic-gate 1061*0Sstevel@tonic-gate /* 1062*0Sstevel@tonic-gate * If not debugging, fork and continue operating, changing the 1063*0Sstevel@tonic-gate * destination of messages to syslog(). 1064*0Sstevel@tonic-gate */ 1065*0Sstevel@tonic-gate if (should_fork == 1) { 1066*0Sstevel@tonic-gate pid_t child; 1067*0Sstevel@tonic-gate debug("forking\n"); 1068*0Sstevel@tonic-gate child = fork(); 1069*0Sstevel@tonic-gate if (child == -1) 1070*0Sstevel@tonic-gate die(gettext("cannot fork")); 1071*0Sstevel@tonic-gate if (child > 0) 1072*0Sstevel@tonic-gate return (0); 1073*0Sstevel@tonic-gate else { 1074*0Sstevel@tonic-gate rcapd_pid = getpid(); 1075*0Sstevel@tonic-gate (void) set_message_destination(RCD_SYSLOG); 1076*0Sstevel@tonic-gate (void) fclose(stdin); 1077*0Sstevel@tonic-gate (void) fclose(stdout); 1078*0Sstevel@tonic-gate (void) fclose(stderr); 1079*0Sstevel@tonic-gate } 1080*0Sstevel@tonic-gate /* 1081*0Sstevel@tonic-gate * Start a new session and detatch from the controlling tty. 1082*0Sstevel@tonic-gate */ 1083*0Sstevel@tonic-gate if (setsid() == (pid_t)-1) 1084*0Sstevel@tonic-gate debug(gettext("setsid() failed; cannot detach from " 1085*0Sstevel@tonic-gate "terminal")); 1086*0Sstevel@tonic-gate } 1087*0Sstevel@tonic-gate 1088*0Sstevel@tonic-gate /* 1089*0Sstevel@tonic-gate * Read the configuration file. 1090*0Sstevel@tonic-gate */ 1091*0Sstevel@tonic-gate if (rcfg_read(RCAPD_DEFAULT_CONF_FILE, -1, &rcfg, verify_statistics) 1092*0Sstevel@tonic-gate != 0) 1093*0Sstevel@tonic-gate die(gettext("invalid configuration: %s"), 1094*0Sstevel@tonic-gate RCAPD_DEFAULT_CONF_FILE); 1095*0Sstevel@tonic-gate finish_configuration(); 1096*0Sstevel@tonic-gate should_reconfigure = 0; 1097*0Sstevel@tonic-gate 1098*0Sstevel@tonic-gate /* 1099*0Sstevel@tonic-gate * Check that required privileges are possessed. 1100*0Sstevel@tonic-gate */ 1101*0Sstevel@tonic-gate verify_and_set_privileges(); 1102*0Sstevel@tonic-gate 1103*0Sstevel@tonic-gate now = next_report = next_proc_walk = next_rss_sample = gethrtime(); 1104*0Sstevel@tonic-gate next_configuration = NEXT_EVENT_TIME(gethrtime(), 1105*0Sstevel@tonic-gate rcfg.rcfg_reconfiguration_interval); 1106*0Sstevel@tonic-gate 1107*0Sstevel@tonic-gate if (rcfg.rcfg_memory_cap_enforcement_pressure == 0) { 1108*0Sstevel@tonic-gate /* 1109*0Sstevel@tonic-gate * Always enforce caps when strict caps are used. 1110*0Sstevel@tonic-gate */ 1111*0Sstevel@tonic-gate enforce_caps = 1; 1112*0Sstevel@tonic-gate } 1113*0Sstevel@tonic-gate 1114*0Sstevel@tonic-gate /* 1115*0Sstevel@tonic-gate * Open the kstat chain. 1116*0Sstevel@tonic-gate */ 1117*0Sstevel@tonic-gate kctl = kstat_open(); 1118*0Sstevel@tonic-gate if (kctl == NULL) 1119*0Sstevel@tonic-gate die(gettext("can't open kstats")); 1120*0Sstevel@tonic-gate 1121*0Sstevel@tonic-gate /* 1122*0Sstevel@tonic-gate * Set RLIMIT_NOFILE as high as practical, so roughly 10K processes can 1123*0Sstevel@tonic-gate * be effectively managed without revoking descriptors (at 3 per 1124*0Sstevel@tonic-gate * process). 1125*0Sstevel@tonic-gate */ 1126*0Sstevel@tonic-gate rl.rlim_cur = 32 * 1024; 1127*0Sstevel@tonic-gate rl.rlim_max = 32 * 1024; 1128*0Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rl) != 0 && 1129*0Sstevel@tonic-gate getrlimit(RLIMIT_NOFILE, &rl) == 0) { 1130*0Sstevel@tonic-gate rl.rlim_cur = rl.rlim_max; 1131*0Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &rl); 1132*0Sstevel@tonic-gate } 1133*0Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rl) == 0) 1134*0Sstevel@tonic-gate debug("fd limit: %lu\n", rl.rlim_cur); 1135*0Sstevel@tonic-gate else 1136*0Sstevel@tonic-gate debug("fd limit: unknown\n"); 1137*0Sstevel@tonic-gate 1138*0Sstevel@tonic-gate /* 1139*0Sstevel@tonic-gate * Handle those signals whose (default) exit disposition 1140*0Sstevel@tonic-gate * prevents rcapd from finishing scanning before terminating. 1141*0Sstevel@tonic-gate */ 1142*0Sstevel@tonic-gate (void) sigset(SIGINT, terminate_signal); 1143*0Sstevel@tonic-gate (void) sigset(SIGQUIT, abort_signal); 1144*0Sstevel@tonic-gate (void) sigset(SIGILL, abort_signal); 1145*0Sstevel@tonic-gate (void) sigset(SIGEMT, abort_signal); 1146*0Sstevel@tonic-gate (void) sigset(SIGFPE, abort_signal); 1147*0Sstevel@tonic-gate (void) sigset(SIGBUS, abort_signal); 1148*0Sstevel@tonic-gate (void) sigset(SIGSEGV, abort_signal); 1149*0Sstevel@tonic-gate (void) sigset(SIGSYS, abort_signal); 1150*0Sstevel@tonic-gate (void) sigset(SIGPIPE, terminate_signal); 1151*0Sstevel@tonic-gate (void) sigset(SIGALRM, terminate_signal); 1152*0Sstevel@tonic-gate (void) sigset(SIGTERM, terminate_signal); 1153*0Sstevel@tonic-gate (void) sigset(SIGUSR1, terminate_signal); 1154*0Sstevel@tonic-gate (void) sigset(SIGUSR2, terminate_signal); 1155*0Sstevel@tonic-gate (void) sigset(SIGPOLL, terminate_signal); 1156*0Sstevel@tonic-gate (void) sigset(SIGVTALRM, terminate_signal); 1157*0Sstevel@tonic-gate (void) sigset(SIGXCPU, abort_signal); 1158*0Sstevel@tonic-gate (void) sigset(SIGXFSZ, abort_signal); 1159*0Sstevel@tonic-gate for (sig = SIGRTMIN; sig <= SIGRTMAX; sig++) 1160*0Sstevel@tonic-gate (void) sigset(sig, terminate_signal); 1161*0Sstevel@tonic-gate 1162*0Sstevel@tonic-gate /* 1163*0Sstevel@tonic-gate * Install a signal handler for reconfiguration processing. 1164*0Sstevel@tonic-gate */ 1165*0Sstevel@tonic-gate (void) sigset(SIGHUP, sighup); 1166*0Sstevel@tonic-gate 1167*0Sstevel@tonic-gate /* 1168*0Sstevel@tonic-gate * Determine which process collections to cap. 1169*0Sstevel@tonic-gate */ 1170*0Sstevel@tonic-gate lcollection_update(LCU_COMPLETE); 1171*0Sstevel@tonic-gate 1172*0Sstevel@tonic-gate /* 1173*0Sstevel@tonic-gate * Loop forever, monitoring collections' resident set sizes and 1174*0Sstevel@tonic-gate * enforcing their caps. Look for changes in caps and process 1175*0Sstevel@tonic-gate * membership, as well as responding to requests to reread the 1176*0Sstevel@tonic-gate * configuration. Update per-collection statistics periodically. 1177*0Sstevel@tonic-gate */ 1178*0Sstevel@tonic-gate while (should_run != 0) { 1179*0Sstevel@tonic-gate struct timespec ts; 1180*0Sstevel@tonic-gate 1181*0Sstevel@tonic-gate /* 1182*0Sstevel@tonic-gate * Announce that rcapd is starting. 1183*0Sstevel@tonic-gate */ 1184*0Sstevel@tonic-gate if (ever_ran == 0) { 1185*0Sstevel@tonic-gate info(gettext("starting\n")); 1186*0Sstevel@tonic-gate ever_ran = 1; 1187*0Sstevel@tonic-gate } 1188*0Sstevel@tonic-gate 1189*0Sstevel@tonic-gate /* 1190*0Sstevel@tonic-gate * Update the process list once every proc_walk_interval. The 1191*0Sstevel@tonic-gate * condition of global memory pressure is also checked at the 1192*0Sstevel@tonic-gate * same frequency, if strict caps are in use. 1193*0Sstevel@tonic-gate */ 1194*0Sstevel@tonic-gate now = gethrtime(); 1195*0Sstevel@tonic-gate 1196*0Sstevel@tonic-gate /* 1197*0Sstevel@tonic-gate * Detect configuration and cap changes at every 1198*0Sstevel@tonic-gate * reconfiguration_interval, or when SIGHUP has been received. 1199*0Sstevel@tonic-gate */ 1200*0Sstevel@tonic-gate if (EVENT_TIME(now, next_configuration) || 1201*0Sstevel@tonic-gate should_reconfigure == 1) { 1202*0Sstevel@tonic-gate reconfigure(); 1203*0Sstevel@tonic-gate next_configuration = NEXT_EVENT_TIME(now, 1204*0Sstevel@tonic-gate rcfg.rcfg_reconfiguration_interval); 1205*0Sstevel@tonic-gate 1206*0Sstevel@tonic-gate /* 1207*0Sstevel@tonic-gate * Reset each event time to the shorter of the 1208*0Sstevel@tonic-gate * previous and new intervals. 1209*0Sstevel@tonic-gate */ 1210*0Sstevel@tonic-gate if (next_report == 0 && 1211*0Sstevel@tonic-gate rcfg.rcfg_report_interval > 0) 1212*0Sstevel@tonic-gate next_report = now; 1213*0Sstevel@tonic-gate else 1214*0Sstevel@tonic-gate next_report = POSITIVE_MIN(next_report, 1215*0Sstevel@tonic-gate NEXT_REPORT_EVENT_TIME(now, 1216*0Sstevel@tonic-gate rcfg.rcfg_report_interval)); 1217*0Sstevel@tonic-gate if (next_proc_walk == 0 && 1218*0Sstevel@tonic-gate rcfg.rcfg_proc_walk_interval > 0) 1219*0Sstevel@tonic-gate next_proc_walk = now; 1220*0Sstevel@tonic-gate else 1221*0Sstevel@tonic-gate next_proc_walk = POSITIVE_MIN(next_proc_walk, 1222*0Sstevel@tonic-gate NEXT_EVENT_TIME(now, 1223*0Sstevel@tonic-gate rcfg.rcfg_proc_walk_interval)); 1224*0Sstevel@tonic-gate if (next_rss_sample == 0 && 1225*0Sstevel@tonic-gate rcfg.rcfg_rss_sample_interval > 0) 1226*0Sstevel@tonic-gate next_rss_sample = now; 1227*0Sstevel@tonic-gate else 1228*0Sstevel@tonic-gate next_rss_sample = POSITIVE_MIN(next_rss_sample, 1229*0Sstevel@tonic-gate NEXT_EVENT_TIME(now, 1230*0Sstevel@tonic-gate rcfg.rcfg_rss_sample_interval)); 1231*0Sstevel@tonic-gate 1232*0Sstevel@tonic-gate should_reconfigure = 0; 1233*0Sstevel@tonic-gate continue; 1234*0Sstevel@tonic-gate } 1235*0Sstevel@tonic-gate 1236*0Sstevel@tonic-gate if (EVENT_TIME(now, next_proc_walk)) { 1237*0Sstevel@tonic-gate debug("scanning process list...\n"); 1238*0Sstevel@tonic-gate proc_walk_all(proc_cb); /* mark */ 1239*0Sstevel@tonic-gate list_walk_all(sweep_process_cb); 1240*0Sstevel@tonic-gate next_proc_walk = NEXT_EVENT_TIME(now, 1241*0Sstevel@tonic-gate rcfg.rcfg_proc_walk_interval); 1242*0Sstevel@tonic-gate } 1243*0Sstevel@tonic-gate 1244*0Sstevel@tonic-gate if (EVENT_TIME(now, next_rss_sample)) { 1245*0Sstevel@tonic-gate /* 1246*0Sstevel@tonic-gate * Check for changes to the amount of installed 1247*0Sstevel@tonic-gate * physical memory, to compute the current memory 1248*0Sstevel@tonic-gate * pressure. 1249*0Sstevel@tonic-gate */ 1250*0Sstevel@tonic-gate update_phys_total(); 1251*0Sstevel@tonic-gate 1252*0Sstevel@tonic-gate /* 1253*0Sstevel@tonic-gate * If soft caps are in use, determine if global memory 1254*0Sstevel@tonic-gate * pressure exceeds the configured maximum above which 1255*0Sstevel@tonic-gate * soft caps are enforced. 1256*0Sstevel@tonic-gate */ 1257*0Sstevel@tonic-gate memory_pressure = 100 - 1258*0Sstevel@tonic-gate (int)((sysconf(_SC_AVPHYS_PAGES) * 1259*0Sstevel@tonic-gate (sysconf(_SC_PAGESIZE) / 1024)) * 100.0 / 1260*0Sstevel@tonic-gate phys_total); 1261*0Sstevel@tonic-gate memory_pressure_sample++; 1262*0Sstevel@tonic-gate if (rcfg.rcfg_memory_cap_enforcement_pressure > 0) { 1263*0Sstevel@tonic-gate if (memory_pressure > 1264*0Sstevel@tonic-gate rcfg.rcfg_memory_cap_enforcement_pressure) { 1265*0Sstevel@tonic-gate if (enforce_soft_caps == 0) { 1266*0Sstevel@tonic-gate debug("memory pressure %d%%\n", 1267*0Sstevel@tonic-gate memory_pressure); 1268*0Sstevel@tonic-gate enforce_soft_caps = 1; 1269*0Sstevel@tonic-gate } 1270*0Sstevel@tonic-gate } else { 1271*0Sstevel@tonic-gate if (enforce_soft_caps == 1) 1272*0Sstevel@tonic-gate enforce_soft_caps = 0; 1273*0Sstevel@tonic-gate } 1274*0Sstevel@tonic-gate } 1275*0Sstevel@tonic-gate 1276*0Sstevel@tonic-gate /* 1277*0Sstevel@tonic-gate * Determine if the global page scanner is running, 1278*0Sstevel@tonic-gate * while which no memory caps should be enforced, to 1279*0Sstevel@tonic-gate * prevent interference with the global page scanner. 1280*0Sstevel@tonic-gate */ 1281*0Sstevel@tonic-gate if (get_globally_scanned_pages(&new_sp) == 0) { 1282*0Sstevel@tonic-gate if (old_sp == 0) 1283*0Sstevel@tonic-gate /*EMPTY*/ 1284*0Sstevel@tonic-gate ; 1285*0Sstevel@tonic-gate else if ((new_sp - old_sp) > 0) { 1286*0Sstevel@tonic-gate if (global_scanner_running == 0) { 1287*0Sstevel@tonic-gate debug("global memory pressure " 1288*0Sstevel@tonic-gate "detected (%llu pages " 1289*0Sstevel@tonic-gate "scanned since last " 1290*0Sstevel@tonic-gate "interval)\n", 1291*0Sstevel@tonic-gate (unsigned long long) 1292*0Sstevel@tonic-gate (new_sp - old_sp)); 1293*0Sstevel@tonic-gate global_scanner_running = 1; 1294*0Sstevel@tonic-gate } 1295*0Sstevel@tonic-gate } else if (global_scanner_running == 1) { 1296*0Sstevel@tonic-gate debug("global memory pressure " 1297*0Sstevel@tonic-gate "relieved\n"); 1298*0Sstevel@tonic-gate global_scanner_running = 0; 1299*0Sstevel@tonic-gate } 1300*0Sstevel@tonic-gate old_sp = new_sp; 1301*0Sstevel@tonic-gate } else { 1302*0Sstevel@tonic-gate warn(gettext("kstat_read() failed")); 1303*0Sstevel@tonic-gate new_sp = old_sp; 1304*0Sstevel@tonic-gate } 1305*0Sstevel@tonic-gate 1306*0Sstevel@tonic-gate /* 1307*0Sstevel@tonic-gate * Cap enforcement is determined by the previous two 1308*0Sstevel@tonic-gate * conditions. 1309*0Sstevel@tonic-gate */ 1310*0Sstevel@tonic-gate old_enforce_caps = enforce_caps; 1311*0Sstevel@tonic-gate enforce_caps = 1312*0Sstevel@tonic-gate (rcfg.rcfg_memory_cap_enforcement_pressure == 1313*0Sstevel@tonic-gate 0 || enforce_soft_caps == 1) && 1314*0Sstevel@tonic-gate !global_scanner_running; 1315*0Sstevel@tonic-gate if (old_enforce_caps != enforce_caps) 1316*0Sstevel@tonic-gate debug("%senforcing caps\n", enforce_caps == 0 ? 1317*0Sstevel@tonic-gate "not " : ""); 1318*0Sstevel@tonic-gate 1319*0Sstevel@tonic-gate /* 1320*0Sstevel@tonic-gate * Sample collections' member processes' RSSes and 1321*0Sstevel@tonic-gate * recompute collections' excess. 1322*0Sstevel@tonic-gate */ 1323*0Sstevel@tonic-gate list_walk_all(mem_sample_cb); 1324*0Sstevel@tonic-gate list_walk_collection(collection_zero_mem_cb, NULL); 1325*0Sstevel@tonic-gate list_walk_all(memory_all_cb); 1326*0Sstevel@tonic-gate list_walk_collection(rss_sample_col_cb, NULL); 1327*0Sstevel@tonic-gate if (rcfg.rcfg_memory_cap_enforcement_pressure > 0) 1328*0Sstevel@tonic-gate debug("memory pressure %d%%\n", 1329*0Sstevel@tonic-gate memory_pressure); 1330*0Sstevel@tonic-gate list_walk_collection(excess_print_cb, NULL); 1331*0Sstevel@tonic-gate 1332*0Sstevel@tonic-gate /* 1333*0Sstevel@tonic-gate * If soft caps are in use, determine the size of the 1334*0Sstevel@tonic-gate * portion from each collection to scan for. 1335*0Sstevel@tonic-gate */ 1336*0Sstevel@tonic-gate if (enforce_soft_caps == 1) { 1337*0Sstevel@tonic-gate /* 1338*0Sstevel@tonic-gate * Compute the sum of the collections' 1339*0Sstevel@tonic-gate * excesses, which will be the denominator. 1340*0Sstevel@tonic-gate */ 1341*0Sstevel@tonic-gate arg.ssa_sum_excess = 0; 1342*0Sstevel@tonic-gate list_walk_collection(sum_excess_cb, 1343*0Sstevel@tonic-gate &arg.ssa_sum_excess); 1344*0Sstevel@tonic-gate 1345*0Sstevel@tonic-gate /* 1346*0Sstevel@tonic-gate * Compute the quantity of memory (in 1347*0Sstevel@tonic-gate * kilobytes) above the cap enforcement 1348*0Sstevel@tonic-gate * pressure. Set the scan goal to that 1349*0Sstevel@tonic-gate * quantity (or at most the excess). 1350*0Sstevel@tonic-gate */ 1351*0Sstevel@tonic-gate arg.ssa_scan_goal = MIN(( 1352*0Sstevel@tonic-gate sysconf(_SC_PHYS_PAGES) * (100 - 1353*0Sstevel@tonic-gate rcfg.rcfg_memory_cap_enforcement_pressure) 1354*0Sstevel@tonic-gate / 100 - sysconf(_SC_AVPHYS_PAGES)) * 1355*0Sstevel@tonic-gate (sysconf(_SC_PAGESIZE) / 1024), 1356*0Sstevel@tonic-gate arg.ssa_sum_excess); 1357*0Sstevel@tonic-gate } 1358*0Sstevel@tonic-gate 1359*0Sstevel@tonic-gate /* 1360*0Sstevel@tonic-gate * Victimize offending collections. 1361*0Sstevel@tonic-gate */ 1362*0Sstevel@tonic-gate if (enforce_caps == 1 && ((enforce_soft_caps == 1 && 1363*0Sstevel@tonic-gate arg.ssa_scan_goal > 0 && arg.ssa_sum_excess > 0) || 1364*0Sstevel@tonic-gate (enforce_soft_caps == 0))) 1365*0Sstevel@tonic-gate if (enforce_soft_caps == 1) { 1366*0Sstevel@tonic-gate debug("scan goal is %lldKB\n", 1367*0Sstevel@tonic-gate (long long)arg.ssa_scan_goal); 1368*0Sstevel@tonic-gate list_walk_collection(soft_scan_cb, 1369*0Sstevel@tonic-gate &arg); 1370*0Sstevel@tonic-gate } else 1371*0Sstevel@tonic-gate list_walk_collection(scan_cb, NULL); 1372*0Sstevel@tonic-gate else 1373*0Sstevel@tonic-gate list_walk_collection(unenforced_cap_cb, NULL); 1374*0Sstevel@tonic-gate 1375*0Sstevel@tonic-gate next_rss_sample = NEXT_EVENT_TIME(now, 1376*0Sstevel@tonic-gate rcfg.rcfg_rss_sample_interval); 1377*0Sstevel@tonic-gate } 1378*0Sstevel@tonic-gate 1379*0Sstevel@tonic-gate /* 1380*0Sstevel@tonic-gate * Update the statistics file, if it's time. 1381*0Sstevel@tonic-gate */ 1382*0Sstevel@tonic-gate check_update_statistics(); 1383*0Sstevel@tonic-gate 1384*0Sstevel@tonic-gate /* 1385*0Sstevel@tonic-gate * Sleep for some time before repeating. 1386*0Sstevel@tonic-gate */ 1387*0Sstevel@tonic-gate now = gethrtime(); 1388*0Sstevel@tonic-gate next = next_configuration; 1389*0Sstevel@tonic-gate next = POSITIVE_MIN(next, next_proc_walk); 1390*0Sstevel@tonic-gate next = POSITIVE_MIN(next, next_report); 1391*0Sstevel@tonic-gate next = POSITIVE_MIN(next, next_rss_sample); 1392*0Sstevel@tonic-gate if (next > now && should_run != 0) { 1393*0Sstevel@tonic-gate debug("sleeping %-4.2f seconds\n", (float)(next - 1394*0Sstevel@tonic-gate now) / (float)NANOSEC); 1395*0Sstevel@tonic-gate hrt2ts(next - now, &ts); 1396*0Sstevel@tonic-gate (void) nanosleep(&ts, NULL); 1397*0Sstevel@tonic-gate } 1398*0Sstevel@tonic-gate } 1399*0Sstevel@tonic-gate if (termination_signal != 0) 1400*0Sstevel@tonic-gate debug("exiting due to signal %d\n", termination_signal); 1401*0Sstevel@tonic-gate if (ever_ran != 0) 1402*0Sstevel@tonic-gate info(gettext("exiting\n")); 1403*0Sstevel@tonic-gate 1404*0Sstevel@tonic-gate /* 1405*0Sstevel@tonic-gate * Unlink the statistics file before exiting. 1406*0Sstevel@tonic-gate */ 1407*0Sstevel@tonic-gate if (rcfg.rcfg_stat_file[0] != 0) 1408*0Sstevel@tonic-gate (void) unlink(rcfg.rcfg_stat_file); 1409*0Sstevel@tonic-gate 1410*0Sstevel@tonic-gate return (E_SUCCESS); 1411*0Sstevel@tonic-gate } 1412