17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5f1710550Stn143363 * Common Development and Distribution License (the "License"). 6f1710550Stn143363 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22f1710550Stn143363 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/stat.h> 307c478bd9Sstevel@tonic-gate #include <sys/wait.h> 317c478bd9Sstevel@tonic-gate #include <fcntl.h> 327c478bd9Sstevel@tonic-gate #include <errno.h> 337c478bd9Sstevel@tonic-gate #include <signal.h> 347c478bd9Sstevel@tonic-gate #include <stdio.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <strings.h> 377c478bd9Sstevel@tonic-gate #include <unistd.h> 387c478bd9Sstevel@tonic-gate #include <libscf.h> 397c478bd9Sstevel@tonic-gate #include <libscf_priv.h> 407c478bd9Sstevel@tonic-gate #include <libintl.h> 417c478bd9Sstevel@tonic-gate #include <locale.h> 42*0209230bSgjelinek #include <zone.h> 43*0209230bSgjelinek #include <libzonecfg.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #include "utils.h" 467c478bd9Sstevel@tonic-gate #include "rcapd.h" 477c478bd9Sstevel@tonic-gate #include "rcapd_conf.h" 487c478bd9Sstevel@tonic-gate #include "rcapd_stat.h" 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate #define RCAP_FMRI "system/rcap:default" 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate static void 537c478bd9Sstevel@tonic-gate usage() 547c478bd9Sstevel@tonic-gate { 557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 567c478bd9Sstevel@tonic-gate gettext("usage: rcapadm\n" 577c478bd9Sstevel@tonic-gate " [-E|-D] " 587c478bd9Sstevel@tonic-gate "# enable/disable rcapd\n" 597c478bd9Sstevel@tonic-gate " [-n] " 607c478bd9Sstevel@tonic-gate "# don't start/stop rcapd\n" 617c478bd9Sstevel@tonic-gate " [-i <scan|sample|report|config>=value] " 627c478bd9Sstevel@tonic-gate "# set intervals\n" 637c478bd9Sstevel@tonic-gate " [-c <percent>] " 647c478bd9Sstevel@tonic-gate "# set memory cap\n" 657c478bd9Sstevel@tonic-gate " " 66*0209230bSgjelinek "# enforcement threshold\n" 67*0209230bSgjelinek " [-z <zonename> -m <max-rss>] " 68*0209230bSgjelinek "# update zone memory cap\n")); 697c478bd9Sstevel@tonic-gate exit(E_USAGE); 707c478bd9Sstevel@tonic-gate } 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate static rcfg_t conf; 737c478bd9Sstevel@tonic-gate static int enable = -1; 747c478bd9Sstevel@tonic-gate static int disable = -1; 757c478bd9Sstevel@tonic-gate static int pressure = -1; 767c478bd9Sstevel@tonic-gate static int no_starting_stopping = -1; 777c478bd9Sstevel@tonic-gate static int scan_interval = -1; 787c478bd9Sstevel@tonic-gate static int report_interval = -1; 797c478bd9Sstevel@tonic-gate static int config_interval = -1; 807c478bd9Sstevel@tonic-gate static int sample_interval = -1; 817c478bd9Sstevel@tonic-gate static char *fname = RCAPD_DEFAULT_CONF_FILE; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate static char *subopt_v[] = { 847c478bd9Sstevel@tonic-gate "scan", 857c478bd9Sstevel@tonic-gate "sample", 867c478bd9Sstevel@tonic-gate "report", 877c478bd9Sstevel@tonic-gate "config", 887c478bd9Sstevel@tonic-gate NULL 897c478bd9Sstevel@tonic-gate }; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate typedef enum { 927c478bd9Sstevel@tonic-gate OPT_SCAN = 0, 937c478bd9Sstevel@tonic-gate OPT_SAMPLE, 947c478bd9Sstevel@tonic-gate OPT_REPORT, 957c478bd9Sstevel@tonic-gate OPT_CONFIG 967c478bd9Sstevel@tonic-gate } subopt_idx_t; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate static void 997c478bd9Sstevel@tonic-gate print_state(void) 1007c478bd9Sstevel@tonic-gate { 1017c478bd9Sstevel@tonic-gate scf_simple_prop_t *persistent_prop = NULL; 1027c478bd9Sstevel@tonic-gate scf_simple_prop_t *temporary_prop = NULL; 1037c478bd9Sstevel@tonic-gate uint8_t *persistent = NULL; 1047c478bd9Sstevel@tonic-gate uint8_t *temporary = NULL; 1057c478bd9Sstevel@tonic-gate scf_handle_t *h; 1067c478bd9Sstevel@tonic-gate /* LINTED: conditionally assigned and used in function */ 1077c478bd9Sstevel@tonic-gate ssize_t numvals; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate if ((h = scf_handle_create(SCF_VERSION)) == NULL || 1107c478bd9Sstevel@tonic-gate scf_handle_bind(h) != 0) 1117c478bd9Sstevel@tonic-gate goto out; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI, 1147c478bd9Sstevel@tonic-gate SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals = 1157c478bd9Sstevel@tonic-gate scf_simple_prop_numvalues(persistent_prop)) > 0) 1167c478bd9Sstevel@tonic-gate persistent = scf_simple_prop_next_boolean(persistent_prop); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI, 1197c478bd9Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals = 1207c478bd9Sstevel@tonic-gate scf_simple_prop_numvalues(temporary_prop)) > 0) 1217c478bd9Sstevel@tonic-gate temporary = scf_simple_prop_next_boolean(temporary_prop); 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate out: 1247c478bd9Sstevel@tonic-gate if (!persistent) 1257c478bd9Sstevel@tonic-gate (void) printf(gettext(" " 1267c478bd9Sstevel@tonic-gate "state: unknown")); 1277c478bd9Sstevel@tonic-gate else if (temporary && *temporary != *persistent) 1287c478bd9Sstevel@tonic-gate (void) printf(gettext(" " 1297c478bd9Sstevel@tonic-gate "state: %s (%s at next boot)\n"), *temporary ? 1307c478bd9Sstevel@tonic-gate gettext("enabled") : gettext("disabled"), *persistent ? 1317c478bd9Sstevel@tonic-gate gettext("enabled") : gettext("disabled")); 1327c478bd9Sstevel@tonic-gate else 1337c478bd9Sstevel@tonic-gate (void) printf(gettext(" " 1347c478bd9Sstevel@tonic-gate "state: %s\n"), *persistent ? gettext("enabled") : 1357c478bd9Sstevel@tonic-gate gettext("disabled")); 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate scf_simple_prop_free(temporary_prop); 1387c478bd9Sstevel@tonic-gate scf_simple_prop_free(persistent_prop); 1397c478bd9Sstevel@tonic-gate scf_handle_destroy(h); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate 142*0209230bSgjelinek /* 143*0209230bSgjelinek * Update the in-kernel memory cap for the specified zone. 144*0209230bSgjelinek */ 145*0209230bSgjelinek static int 146*0209230bSgjelinek update_zone_mcap(char *zonename, char *maxrss) 147*0209230bSgjelinek { 148*0209230bSgjelinek zoneid_t zone_id; 149*0209230bSgjelinek uint64_t num; 150*0209230bSgjelinek 151*0209230bSgjelinek if (getzoneid() != GLOBAL_ZONEID || zonecfg_in_alt_root()) 152*0209230bSgjelinek return (E_SUCCESS); 153*0209230bSgjelinek 154*0209230bSgjelinek /* get the running zone from the kernel */ 155*0209230bSgjelinek if ((zone_id = getzoneidbyname(zonename)) == -1) { 156*0209230bSgjelinek (void) fprintf(stderr, gettext("zone '%s' must be running\n"), 157*0209230bSgjelinek zonename); 158*0209230bSgjelinek return (E_ERROR); 159*0209230bSgjelinek } 160*0209230bSgjelinek 161*0209230bSgjelinek if (zonecfg_str_to_bytes(maxrss, &num) == -1) { 162*0209230bSgjelinek (void) fprintf(stderr, gettext("invalid max-rss value\n")); 163*0209230bSgjelinek return (E_ERROR); 164*0209230bSgjelinek } 165*0209230bSgjelinek 166*0209230bSgjelinek if (zone_setattr(zone_id, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 167*0209230bSgjelinek (void) fprintf(stderr, gettext("could not set memory " 168*0209230bSgjelinek "cap for zone '%s'\n"), zonename); 169*0209230bSgjelinek return (E_ERROR); 170*0209230bSgjelinek } 171*0209230bSgjelinek 172*0209230bSgjelinek return (E_SUCCESS); 173*0209230bSgjelinek } 174*0209230bSgjelinek 1757c478bd9Sstevel@tonic-gate int 1767c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1777c478bd9Sstevel@tonic-gate { 178f1710550Stn143363 char *subopts, *optval; 1797c478bd9Sstevel@tonic-gate int modified = 0; 180*0209230bSgjelinek boolean_t refresh = B_FALSE; 181f1710550Stn143363 int opt; 182*0209230bSgjelinek char *zonename; 183*0209230bSgjelinek char *maxrss = NULL; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate (void) setprogname("rcapadm"); 1867c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1877c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1887c478bd9Sstevel@tonic-gate 189*0209230bSgjelinek while ((opt = getopt(argc, argv, "DEc:i:m:nz:")) != EOF) { 1907c478bd9Sstevel@tonic-gate switch (opt) { 1917c478bd9Sstevel@tonic-gate case 'n': 1927c478bd9Sstevel@tonic-gate no_starting_stopping = 1; 1937c478bd9Sstevel@tonic-gate break; 1947c478bd9Sstevel@tonic-gate case 'c': 1957c478bd9Sstevel@tonic-gate if ((pressure = xatoi(optarg)) < 0 || 1967c478bd9Sstevel@tonic-gate pressure > 100 || 1977c478bd9Sstevel@tonic-gate errno == EINVAL) 1987c478bd9Sstevel@tonic-gate usage(); 1997c478bd9Sstevel@tonic-gate modified++; 2007c478bd9Sstevel@tonic-gate break; 2017c478bd9Sstevel@tonic-gate case 'E': 2027c478bd9Sstevel@tonic-gate enable = 1; 2037c478bd9Sstevel@tonic-gate disable = 0; 2047c478bd9Sstevel@tonic-gate modified++; 2057c478bd9Sstevel@tonic-gate break; 2067c478bd9Sstevel@tonic-gate case 'D': 2077c478bd9Sstevel@tonic-gate disable = 1; 2087c478bd9Sstevel@tonic-gate enable = 0; 2097c478bd9Sstevel@tonic-gate modified++; 2107c478bd9Sstevel@tonic-gate break; 2117c478bd9Sstevel@tonic-gate case 'i': 2127c478bd9Sstevel@tonic-gate subopts = optarg; 2137c478bd9Sstevel@tonic-gate while (*subopts != '\0') { 2147c478bd9Sstevel@tonic-gate switch (getsubopt(&subopts, subopt_v, 2157c478bd9Sstevel@tonic-gate &optval)) { 2167c478bd9Sstevel@tonic-gate case OPT_SCAN: 2177c478bd9Sstevel@tonic-gate if (optval == NULL || 2187c478bd9Sstevel@tonic-gate (scan_interval = 2197c478bd9Sstevel@tonic-gate xatoi(optval)) <= 0) 2207c478bd9Sstevel@tonic-gate usage(); 2217c478bd9Sstevel@tonic-gate break; 2227c478bd9Sstevel@tonic-gate case OPT_SAMPLE: 2237c478bd9Sstevel@tonic-gate if (optval == NULL || 2247c478bd9Sstevel@tonic-gate (sample_interval = 2257c478bd9Sstevel@tonic-gate xatoi(optval)) <= 0) 2267c478bd9Sstevel@tonic-gate usage(); 2277c478bd9Sstevel@tonic-gate break; 2287c478bd9Sstevel@tonic-gate case OPT_REPORT: 2297c478bd9Sstevel@tonic-gate if (optval == NULL || 2307c478bd9Sstevel@tonic-gate (report_interval = 2317c478bd9Sstevel@tonic-gate xatoi(optval)) < 0) 2327c478bd9Sstevel@tonic-gate usage(); 2337c478bd9Sstevel@tonic-gate break; 2347c478bd9Sstevel@tonic-gate case OPT_CONFIG: 2357c478bd9Sstevel@tonic-gate if (optval == NULL || 2367c478bd9Sstevel@tonic-gate (config_interval = 2377c478bd9Sstevel@tonic-gate xatoi(optval)) < 0) 2387c478bd9Sstevel@tonic-gate usage(); 2397c478bd9Sstevel@tonic-gate break; 2407c478bd9Sstevel@tonic-gate default: 2417c478bd9Sstevel@tonic-gate usage(); 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate modified++; 2457c478bd9Sstevel@tonic-gate break; 246*0209230bSgjelinek case 'm': 247*0209230bSgjelinek maxrss = optarg; 248*0209230bSgjelinek break; 249*0209230bSgjelinek case 'z': 250*0209230bSgjelinek refresh = B_TRUE; 251*0209230bSgjelinek zonename = optarg; 252*0209230bSgjelinek break; 2537c478bd9Sstevel@tonic-gate default: 2547c478bd9Sstevel@tonic-gate usage(); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 258*0209230bSgjelinek /* the -z & -m options must be used together */ 259*0209230bSgjelinek if (argc > optind || (refresh && maxrss == NULL) || 260*0209230bSgjelinek (!refresh && maxrss != NULL)) 261*0209230bSgjelinek usage(); 262*0209230bSgjelinek 263*0209230bSgjelinek if (refresh && (no_starting_stopping > 0 || modified)) 2647c478bd9Sstevel@tonic-gate usage(); 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate if (rcfg_read(fname, -1, &conf, NULL) < 0) { 2677c478bd9Sstevel@tonic-gate if (!(errno == ENOENT && modified)) { 2687c478bd9Sstevel@tonic-gate die(gettext("resource caps not configured\n")); 2697c478bd9Sstevel@tonic-gate return (E_ERROR); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate rcfg_init(&conf); 2727c478bd9Sstevel@tonic-gate conf.rcfg_mode_name = "project"; 2737c478bd9Sstevel@tonic-gate } else { 2747c478bd9Sstevel@tonic-gate /* 2757c478bd9Sstevel@tonic-gate * The configuration file has been read. Warn that any lnode 2767c478bd9Sstevel@tonic-gate * (or non-project) mode specification (by an SRM 2777c478bd9Sstevel@tonic-gate * 1.3 configuration file, for example) is ignored. 2787c478bd9Sstevel@tonic-gate */ 2797c478bd9Sstevel@tonic-gate if (strcmp(conf.rcfg_mode_name, "project") != 0) { 2807c478bd9Sstevel@tonic-gate warn(gettext("%s mode specification ignored -- using" 2817c478bd9Sstevel@tonic-gate " project mode\n"), conf.rcfg_mode_name); 2827c478bd9Sstevel@tonic-gate conf.rcfg_mode_name = "project"; 2837c478bd9Sstevel@tonic-gate conf.rcfg_mode = rctype_project; 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate 287*0209230bSgjelinek if (refresh) 288*0209230bSgjelinek return (update_zone_mcap(zonename, maxrss)); 289*0209230bSgjelinek 2907c478bd9Sstevel@tonic-gate if (modified) { 2917c478bd9Sstevel@tonic-gate if (pressure >= 0) 2927c478bd9Sstevel@tonic-gate conf.rcfg_memory_cap_enforcement_pressure = pressure; 2937c478bd9Sstevel@tonic-gate if (config_interval >= 0) 2947c478bd9Sstevel@tonic-gate conf.rcfg_reconfiguration_interval = config_interval; 2957c478bd9Sstevel@tonic-gate if (scan_interval >= 0) 2967c478bd9Sstevel@tonic-gate conf.rcfg_proc_walk_interval = scan_interval; 2977c478bd9Sstevel@tonic-gate if (report_interval >= 0) 2987c478bd9Sstevel@tonic-gate conf.rcfg_report_interval = report_interval; 2997c478bd9Sstevel@tonic-gate if (sample_interval >= 0) 3007c478bd9Sstevel@tonic-gate conf.rcfg_rss_sample_interval = sample_interval; 3017c478bd9Sstevel@tonic-gate 302f1710550Stn143363 /* 303f1710550Stn143363 * Create config file with the new parameter(s). The 304f1710550Stn143363 * create_config_file will exit if it fails. 305f1710550Stn143363 */ 306f1710550Stn143363 create_config_file(&conf); 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate if (enable > 0 && smf_enable_instance(RCAP_FMRI, 3097c478bd9Sstevel@tonic-gate no_starting_stopping > 0 ? SMF_AT_NEXT_BOOT : 0) != 0) 3107c478bd9Sstevel@tonic-gate die(gettext("cannot enable service: %s\n"), 3117c478bd9Sstevel@tonic-gate scf_strerror(scf_error())); 3127c478bd9Sstevel@tonic-gate else if (disable > 0 && smf_disable_instance(RCAP_FMRI, 3137c478bd9Sstevel@tonic-gate no_starting_stopping > 0 ? SMF_AT_NEXT_BOOT : 0) != 0) 3147c478bd9Sstevel@tonic-gate die(gettext("cannot disable service: %s\n"), 3157c478bd9Sstevel@tonic-gate scf_strerror(scf_error())); 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate return (E_SUCCESS); 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate /* 3217c478bd9Sstevel@tonic-gate * Display current configuration 3227c478bd9Sstevel@tonic-gate */ 3237c478bd9Sstevel@tonic-gate print_state(); 3247c478bd9Sstevel@tonic-gate (void) printf(gettext(" memory cap enforcement" 3257c478bd9Sstevel@tonic-gate " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure); 3267c478bd9Sstevel@tonic-gate (void) printf(gettext(" process scan rate" 3277c478bd9Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_proc_walk_interval); 3287c478bd9Sstevel@tonic-gate (void) printf(gettext(" reconfiguration rate" 3297c478bd9Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_reconfiguration_interval); 3307c478bd9Sstevel@tonic-gate (void) printf(gettext(" report rate" 3317c478bd9Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_report_interval); 3327c478bd9Sstevel@tonic-gate (void) printf(gettext(" RSS sampling rate" 3337c478bd9Sstevel@tonic-gate " (sec): %d\n"), conf.rcfg_rss_sample_interval); 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate return (E_SUCCESS); 3367c478bd9Sstevel@tonic-gate } 337