10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52517Stn143363 * Common Development and Distribution License (the "License"). 62517Stn143363 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 222517Stn143363 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/types.h> 290Sstevel@tonic-gate #include <sys/stat.h> 300Sstevel@tonic-gate #include <sys/wait.h> 310Sstevel@tonic-gate #include <fcntl.h> 320Sstevel@tonic-gate #include <errno.h> 330Sstevel@tonic-gate #include <signal.h> 340Sstevel@tonic-gate #include <stdio.h> 350Sstevel@tonic-gate #include <stdlib.h> 360Sstevel@tonic-gate #include <strings.h> 370Sstevel@tonic-gate #include <unistd.h> 380Sstevel@tonic-gate #include <libscf.h> 390Sstevel@tonic-gate #include <libscf_priv.h> 400Sstevel@tonic-gate #include <libintl.h> 410Sstevel@tonic-gate #include <locale.h> 42*3247Sgjelinek #include <zone.h> 43*3247Sgjelinek #include <libzonecfg.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include "utils.h" 460Sstevel@tonic-gate #include "rcapd.h" 470Sstevel@tonic-gate #include "rcapd_conf.h" 480Sstevel@tonic-gate #include "rcapd_stat.h" 490Sstevel@tonic-gate 500Sstevel@tonic-gate #define RCAP_FMRI "system/rcap:default" 510Sstevel@tonic-gate 520Sstevel@tonic-gate static void 530Sstevel@tonic-gate usage() 540Sstevel@tonic-gate { 550Sstevel@tonic-gate (void) fprintf(stderr, 560Sstevel@tonic-gate gettext("usage: rcapadm\n" 570Sstevel@tonic-gate " [-E|-D] " 580Sstevel@tonic-gate "# enable/disable rcapd\n" 590Sstevel@tonic-gate " [-n] " 600Sstevel@tonic-gate "# don't start/stop rcapd\n" 610Sstevel@tonic-gate " [-i <scan|sample|report|config>=value] " 620Sstevel@tonic-gate "# set intervals\n" 630Sstevel@tonic-gate " [-c <percent>] " 640Sstevel@tonic-gate "# set memory cap\n" 650Sstevel@tonic-gate " " 66*3247Sgjelinek "# enforcement threshold\n" 67*3247Sgjelinek " [-z <zonename> -m <max-rss>] " 68*3247Sgjelinek "# update zone memory cap\n")); 690Sstevel@tonic-gate exit(E_USAGE); 700Sstevel@tonic-gate } 710Sstevel@tonic-gate 720Sstevel@tonic-gate static rcfg_t conf; 730Sstevel@tonic-gate static int enable = -1; 740Sstevel@tonic-gate static int disable = -1; 750Sstevel@tonic-gate static int pressure = -1; 760Sstevel@tonic-gate static int no_starting_stopping = -1; 770Sstevel@tonic-gate static int scan_interval = -1; 780Sstevel@tonic-gate static int report_interval = -1; 790Sstevel@tonic-gate static int config_interval = -1; 800Sstevel@tonic-gate static int sample_interval = -1; 810Sstevel@tonic-gate static char *fname = RCAPD_DEFAULT_CONF_FILE; 820Sstevel@tonic-gate 830Sstevel@tonic-gate static char *subopt_v[] = { 840Sstevel@tonic-gate "scan", 850Sstevel@tonic-gate "sample", 860Sstevel@tonic-gate "report", 870Sstevel@tonic-gate "config", 880Sstevel@tonic-gate NULL 890Sstevel@tonic-gate }; 900Sstevel@tonic-gate 910Sstevel@tonic-gate typedef enum { 920Sstevel@tonic-gate OPT_SCAN = 0, 930Sstevel@tonic-gate OPT_SAMPLE, 940Sstevel@tonic-gate OPT_REPORT, 950Sstevel@tonic-gate OPT_CONFIG 960Sstevel@tonic-gate } subopt_idx_t; 970Sstevel@tonic-gate 980Sstevel@tonic-gate static void 990Sstevel@tonic-gate print_state(void) 1000Sstevel@tonic-gate { 1010Sstevel@tonic-gate scf_simple_prop_t *persistent_prop = NULL; 1020Sstevel@tonic-gate scf_simple_prop_t *temporary_prop = NULL; 1030Sstevel@tonic-gate uint8_t *persistent = NULL; 1040Sstevel@tonic-gate uint8_t *temporary = NULL; 1050Sstevel@tonic-gate scf_handle_t *h; 1060Sstevel@tonic-gate /* LINTED: conditionally assigned and used in function */ 1070Sstevel@tonic-gate ssize_t numvals; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate if ((h = scf_handle_create(SCF_VERSION)) == NULL || 1100Sstevel@tonic-gate scf_handle_bind(h) != 0) 1110Sstevel@tonic-gate goto out; 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI, 1140Sstevel@tonic-gate SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals = 1150Sstevel@tonic-gate scf_simple_prop_numvalues(persistent_prop)) > 0) 1160Sstevel@tonic-gate persistent = scf_simple_prop_next_boolean(persistent_prop); 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI, 1190Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals = 1200Sstevel@tonic-gate scf_simple_prop_numvalues(temporary_prop)) > 0) 1210Sstevel@tonic-gate temporary = scf_simple_prop_next_boolean(temporary_prop); 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate out: 1240Sstevel@tonic-gate if (!persistent) 1250Sstevel@tonic-gate (void) printf(gettext(" " 1260Sstevel@tonic-gate "state: unknown")); 1270Sstevel@tonic-gate else if (temporary && *temporary != *persistent) 1280Sstevel@tonic-gate (void) printf(gettext(" " 1290Sstevel@tonic-gate "state: %s (%s at next boot)\n"), *temporary ? 1300Sstevel@tonic-gate gettext("enabled") : gettext("disabled"), *persistent ? 1310Sstevel@tonic-gate gettext("enabled") : gettext("disabled")); 1320Sstevel@tonic-gate else 1330Sstevel@tonic-gate (void) printf(gettext(" " 1340Sstevel@tonic-gate "state: %s\n"), *persistent ? gettext("enabled") : 1350Sstevel@tonic-gate gettext("disabled")); 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate scf_simple_prop_free(temporary_prop); 1380Sstevel@tonic-gate scf_simple_prop_free(persistent_prop); 1390Sstevel@tonic-gate scf_handle_destroy(h); 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate 142*3247Sgjelinek /* 143*3247Sgjelinek * Update the in-kernel memory cap for the specified zone. 144*3247Sgjelinek */ 145*3247Sgjelinek static int 146*3247Sgjelinek update_zone_mcap(char *zonename, char *maxrss) 147*3247Sgjelinek { 148*3247Sgjelinek zoneid_t zone_id; 149*3247Sgjelinek uint64_t num; 150*3247Sgjelinek 151*3247Sgjelinek if (getzoneid() != GLOBAL_ZONEID || zonecfg_in_alt_root()) 152*3247Sgjelinek return (E_SUCCESS); 153*3247Sgjelinek 154*3247Sgjelinek /* get the running zone from the kernel */ 155*3247Sgjelinek if ((zone_id = getzoneidbyname(zonename)) == -1) { 156*3247Sgjelinek (void) fprintf(stderr, gettext("zone '%s' must be running\n"), 157*3247Sgjelinek zonename); 158*3247Sgjelinek return (E_ERROR); 159*3247Sgjelinek } 160*3247Sgjelinek 161*3247Sgjelinek if (zonecfg_str_to_bytes(maxrss, &num) == -1) { 162*3247Sgjelinek (void) fprintf(stderr, gettext("invalid max-rss value\n")); 163*3247Sgjelinek return (E_ERROR); 164*3247Sgjelinek } 165*3247Sgjelinek 166*3247Sgjelinek if (zone_setattr(zone_id, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 167*3247Sgjelinek (void) fprintf(stderr, gettext("could not set memory " 168*3247Sgjelinek "cap for zone '%s'\n"), zonename); 169*3247Sgjelinek return (E_ERROR); 170*3247Sgjelinek } 171*3247Sgjelinek 172*3247Sgjelinek return (E_SUCCESS); 173*3247Sgjelinek } 174*3247Sgjelinek 1750Sstevel@tonic-gate int 1760Sstevel@tonic-gate main(int argc, char *argv[]) 1770Sstevel@tonic-gate { 1782517Stn143363 char *subopts, *optval; 1790Sstevel@tonic-gate int modified = 0; 180*3247Sgjelinek boolean_t refresh = B_FALSE; 1812517Stn143363 int opt; 182*3247Sgjelinek char *zonename; 183*3247Sgjelinek char *maxrss = NULL; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate (void) setprogname("rcapadm"); 1860Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1870Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1880Sstevel@tonic-gate 189*3247Sgjelinek while ((opt = getopt(argc, argv, "DEc:i:m:nz:")) != EOF) { 1900Sstevel@tonic-gate switch (opt) { 1910Sstevel@tonic-gate case 'n': 1920Sstevel@tonic-gate no_starting_stopping = 1; 1930Sstevel@tonic-gate break; 1940Sstevel@tonic-gate case 'c': 1950Sstevel@tonic-gate if ((pressure = xatoi(optarg)) < 0 || 1960Sstevel@tonic-gate pressure > 100 || 1970Sstevel@tonic-gate errno == EINVAL) 1980Sstevel@tonic-gate usage(); 1990Sstevel@tonic-gate modified++; 2000Sstevel@tonic-gate break; 2010Sstevel@tonic-gate case 'E': 2020Sstevel@tonic-gate enable = 1; 2030Sstevel@tonic-gate disable = 0; 2040Sstevel@tonic-gate modified++; 2050Sstevel@tonic-gate break; 2060Sstevel@tonic-gate case 'D': 2070Sstevel@tonic-gate disable = 1; 2080Sstevel@tonic-gate enable = 0; 2090Sstevel@tonic-gate modified++; 2100Sstevel@tonic-gate break; 2110Sstevel@tonic-gate case 'i': 2120Sstevel@tonic-gate subopts = optarg; 2130Sstevel@tonic-gate while (*subopts != '\0') { 2140Sstevel@tonic-gate switch (getsubopt(&subopts, subopt_v, 2150Sstevel@tonic-gate &optval)) { 2160Sstevel@tonic-gate case OPT_SCAN: 2170Sstevel@tonic-gate if (optval == NULL || 2180Sstevel@tonic-gate (scan_interval = 2190Sstevel@tonic-gate xatoi(optval)) <= 0) 2200Sstevel@tonic-gate usage(); 2210Sstevel@tonic-gate break; 2220Sstevel@tonic-gate case OPT_SAMPLE: 2230Sstevel@tonic-gate if (optval == NULL || 2240Sstevel@tonic-gate (sample_interval = 2250Sstevel@tonic-gate xatoi(optval)) <= 0) 2260Sstevel@tonic-gate usage(); 2270Sstevel@tonic-gate break; 2280Sstevel@tonic-gate case OPT_REPORT: 2290Sstevel@tonic-gate if (optval == NULL || 2300Sstevel@tonic-gate (report_interval = 2310Sstevel@tonic-gate xatoi(optval)) < 0) 2320Sstevel@tonic-gate usage(); 2330Sstevel@tonic-gate break; 2340Sstevel@tonic-gate case OPT_CONFIG: 2350Sstevel@tonic-gate if (optval == NULL || 2360Sstevel@tonic-gate (config_interval = 2370Sstevel@tonic-gate xatoi(optval)) < 0) 2380Sstevel@tonic-gate usage(); 2390Sstevel@tonic-gate break; 2400Sstevel@tonic-gate default: 2410Sstevel@tonic-gate usage(); 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate modified++; 2450Sstevel@tonic-gate break; 246*3247Sgjelinek case 'm': 247*3247Sgjelinek maxrss = optarg; 248*3247Sgjelinek break; 249*3247Sgjelinek case 'z': 250*3247Sgjelinek refresh = B_TRUE; 251*3247Sgjelinek zonename = optarg; 252*3247Sgjelinek break; 2530Sstevel@tonic-gate default: 2540Sstevel@tonic-gate usage(); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate 258*3247Sgjelinek /* the -z & -m options must be used together */ 259*3247Sgjelinek if (argc > optind || (refresh && maxrss == NULL) || 260*3247Sgjelinek (!refresh && maxrss != NULL)) 261*3247Sgjelinek usage(); 262*3247Sgjelinek 263*3247Sgjelinek if (refresh && (no_starting_stopping > 0 || modified)) 2640Sstevel@tonic-gate usage(); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate if (rcfg_read(fname, -1, &conf, NULL) < 0) { 2670Sstevel@tonic-gate if (!(errno == ENOENT && modified)) { 2680Sstevel@tonic-gate die(gettext("resource caps not configured\n")); 2690Sstevel@tonic-gate return (E_ERROR); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate rcfg_init(&conf); 2720Sstevel@tonic-gate conf.rcfg_mode_name = "project"; 2730Sstevel@tonic-gate } else { 2740Sstevel@tonic-gate /* 2750Sstevel@tonic-gate * The configuration file has been read. Warn that any lnode 2760Sstevel@tonic-gate * (or non-project) mode specification (by an SRM 2770Sstevel@tonic-gate * 1.3 configuration file, for example) is ignored. 2780Sstevel@tonic-gate */ 2790Sstevel@tonic-gate if (strcmp(conf.rcfg_mode_name, "project") != 0) { 2800Sstevel@tonic-gate warn(gettext("%s mode specification ignored -- using" 2810Sstevel@tonic-gate " project mode\n"), conf.rcfg_mode_name); 2820Sstevel@tonic-gate conf.rcfg_mode_name = "project"; 2830Sstevel@tonic-gate conf.rcfg_mode = rctype_project; 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate 287*3247Sgjelinek if (refresh) 288*3247Sgjelinek return (update_zone_mcap(zonename, maxrss)); 289*3247Sgjelinek 2900Sstevel@tonic-gate if (modified) { 2910Sstevel@tonic-gate if (pressure >= 0) 2920Sstevel@tonic-gate conf.rcfg_memory_cap_enforcement_pressure = pressure; 2930Sstevel@tonic-gate if (config_interval >= 0) 2940Sstevel@tonic-gate conf.rcfg_reconfiguration_interval = config_interval; 2950Sstevel@tonic-gate if (scan_interval >= 0) 2960Sstevel@tonic-gate conf.rcfg_proc_walk_interval = scan_interval; 2970Sstevel@tonic-gate if (report_interval >= 0) 2980Sstevel@tonic-gate conf.rcfg_report_interval = report_interval; 2990Sstevel@tonic-gate if (sample_interval >= 0) 3000Sstevel@tonic-gate conf.rcfg_rss_sample_interval = sample_interval; 3010Sstevel@tonic-gate 3022517Stn143363 /* 3032517Stn143363 * Create config file with the new parameter(s). The 3042517Stn143363 * create_config_file will exit if it fails. 3052517Stn143363 */ 3062517Stn143363 create_config_file(&conf); 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate if (enable > 0 && smf_enable_instance(RCAP_FMRI, 3090Sstevel@tonic-gate no_starting_stopping > 0 ? SMF_AT_NEXT_BOOT : 0) != 0) 3100Sstevel@tonic-gate die(gettext("cannot enable service: %s\n"), 3110Sstevel@tonic-gate scf_strerror(scf_error())); 3120Sstevel@tonic-gate else if (disable > 0 && smf_disable_instance(RCAP_FMRI, 3130Sstevel@tonic-gate no_starting_stopping > 0 ? SMF_AT_NEXT_BOOT : 0) != 0) 3140Sstevel@tonic-gate die(gettext("cannot disable service: %s\n"), 3150Sstevel@tonic-gate scf_strerror(scf_error())); 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate return (E_SUCCESS); 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* 3210Sstevel@tonic-gate * Display current configuration 3220Sstevel@tonic-gate */ 3230Sstevel@tonic-gate print_state(); 3240Sstevel@tonic-gate (void) printf(gettext(" memory cap enforcement" 3250Sstevel@tonic-gate " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure); 3260Sstevel@tonic-gate (void) printf(gettext(" process scan rate" 3270Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_proc_walk_interval); 3280Sstevel@tonic-gate (void) printf(gettext(" reconfiguration rate" 3290Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_reconfiguration_interval); 3300Sstevel@tonic-gate (void) printf(gettext(" report rate" 3310Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_report_interval); 3320Sstevel@tonic-gate (void) printf(gettext(" RSS sampling rate" 3330Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_rss_sample_interval); 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate return (E_SUCCESS); 3360Sstevel@tonic-gate } 337