xref: /titanic_52/usr/src/cmd/rcap/rcapadm/rcapadm.c (revision 23a1ccea6aac035f084a7a4cdc968687d1b02daf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/wait.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <strings.h>
35 #include <unistd.h>
36 #include <libscf.h>
37 #include <libscf_priv.h>
38 #include <libintl.h>
39 #include <locale.h>
40 #include <zone.h>
41 #include <libzonecfg.h>
42 
43 #include "utils.h"
44 #include "rcapd.h"
45 #include "rcapd_conf.h"
46 #include "rcapd_stat.h"
47 
48 static void
49 usage()
50 {
51 	(void) fprintf(stderr,
52 	    gettext("usage: rcapadm\n"
53 	    "               [-E|-D]                                "
54 	    "# enable/disable rcapd\n"
55 	    "               [-n]                                   "
56 	    "# don't start/stop rcapd\n"
57 	    "               [-i <scan|sample|report|config>=value] "
58 	    "# set intervals\n"
59 	    "               [-c <percent>]                         "
60 	    "# set memory cap\n"
61 	    "                                                      "
62 	    "# enforcement threshold\n"
63 	    "               [-z <zonename> -m <max-rss>]               "
64 	    "# update zone memory cap\n"));
65 	exit(E_USAGE);
66 }
67 
68 static rcfg_t conf;
69 static int enable = -1;
70 static int disable = -1;
71 static int pressure = -1;
72 static int no_starting_stopping = -1;
73 static int scan_interval = -1;
74 static int report_interval = -1;
75 static int config_interval = -1;
76 static int sample_interval = -1;
77 
78 static char *subopt_v[] = {
79 	"scan",
80 	"sample",
81 	"report",
82 	"config",
83 	NULL
84 };
85 
86 typedef enum {
87 	OPT_SCAN = 0,
88 	OPT_SAMPLE,
89 	OPT_REPORT,
90 	OPT_CONFIG
91 } subopt_idx_t;
92 
93 static void
94 print_state(void)
95 {
96 	scf_simple_prop_t *persistent_prop = NULL;
97 	scf_simple_prop_t *temporary_prop = NULL;
98 	uint8_t *persistent = NULL;
99 	uint8_t *temporary = NULL;
100 	scf_handle_t *h;
101 	/* LINTED: conditionally assigned and used in function */
102 	ssize_t numvals;
103 
104 	if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
105 	    scf_handle_bind(h) != 0)
106 		goto out;
107 
108 	if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI,
109 	    SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
110 	    scf_simple_prop_numvalues(persistent_prop)) > 0)
111 		persistent = scf_simple_prop_next_boolean(persistent_prop);
112 
113 	if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI,
114 	    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
115 	    scf_simple_prop_numvalues(temporary_prop)) > 0)
116 		temporary = scf_simple_prop_next_boolean(temporary_prop);
117 
118 out:
119 	if (!persistent)
120 		(void) printf(gettext("                                      "
121 		    "state: unknown"));
122 	else if (temporary && *temporary != *persistent)
123 		(void) printf(gettext("                                      "
124 		    "state: %s (%s at next boot)\n"), *temporary ?
125 		    gettext("enabled") : gettext("disabled"), *persistent ?
126 		    gettext("enabled") : gettext("disabled"));
127 	else
128 		(void) printf(gettext("                                      "
129 		    "state: %s\n"), *persistent ? gettext("enabled") :
130 		    gettext("disabled"));
131 
132 	(void) printf(gettext("           memory cap enforcement"
133 	    " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure);
134 	(void) printf(gettext("                    process scan rate"
135 	    " (sec): %d\n"), conf.rcfg_proc_walk_interval);
136 	(void) printf(gettext("                 reconfiguration rate"
137 	    " (sec): %d\n"), conf.rcfg_reconfiguration_interval);
138 	(void) printf(gettext("                          report rate"
139 	    " (sec): %d\n"), conf.rcfg_report_interval);
140 	(void) printf(gettext("                    RSS sampling rate"
141 	    " (sec): %d\n"), conf.rcfg_rss_sample_interval);
142 
143 	scf_simple_prop_free(temporary_prop);
144 	scf_simple_prop_free(persistent_prop);
145 	scf_handle_destroy(h);
146 }
147 
148 /*
149  * Update the in-kernel memory cap for the specified zone.
150  */
151 static int
152 update_zone_mcap(char *zonename, char *maxrss)
153 {
154 	zoneid_t zone_id;
155 	uint64_t num;
156 
157 	if (getzoneid() != GLOBAL_ZONEID || zonecfg_in_alt_root())
158 		return (E_SUCCESS);
159 
160 	/* get the running zone from the kernel */
161 	if ((zone_id = getzoneidbyname(zonename)) == -1) {
162 		(void) fprintf(stderr, gettext("zone '%s' must be running\n"),
163 		    zonename);
164 		return (E_ERROR);
165 	}
166 
167 	if (zonecfg_str_to_bytes(maxrss, &num) == -1) {
168 		(void) fprintf(stderr, gettext("invalid max-rss value\n"));
169 		return (E_ERROR);
170 	}
171 
172 	if (zone_setattr(zone_id, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
173 		(void) fprintf(stderr, gettext("could not set memory "
174 		    "cap for zone '%s'\n"), zonename);
175 		return (E_ERROR);
176 	}
177 
178 	return (E_SUCCESS);
179 }
180 
181 int
182 main(int argc, char *argv[])
183 {
184 	char *subopts, *optval;
185 	int modified = 0;
186 	boolean_t refresh = B_FALSE;
187 	int opt;
188 	char *zonename;
189 	char *maxrss = NULL;
190 
191 	(void) setpname("rcapadm");
192 	(void) setlocale(LC_ALL, "");
193 	(void) textdomain(TEXT_DOMAIN);
194 
195 	while ((opt = getopt(argc, argv, "DEc:i:m:nz:")) != EOF) {
196 		switch (opt) {
197 		case 'n':
198 			no_starting_stopping = 1;
199 			break;
200 		case 'c':
201 			if ((pressure = xatoi(optarg)) < 0 ||
202 			    pressure > 100 ||
203 			    errno == EINVAL)
204 				usage();
205 			modified++;
206 			break;
207 		case 'E':
208 			enable = 1;
209 			disable = 0;
210 			break;
211 		case 'D':
212 			disable = 1;
213 			enable = 0;
214 			break;
215 		case 'i':
216 			subopts = optarg;
217 			while (*subopts != '\0') {
218 				switch (getsubopt(&subopts, subopt_v,
219 				    &optval)) {
220 					case OPT_SCAN:
221 						if (optval == NULL ||
222 						    (scan_interval =
223 						    xatoi(optval)) <= 0)
224 							usage();
225 						break;
226 					case OPT_SAMPLE:
227 						if (optval == NULL ||
228 						    (sample_interval =
229 						    xatoi(optval)) <= 0)
230 							usage();
231 						break;
232 					case OPT_REPORT:
233 						if (optval == NULL ||
234 						    (report_interval =
235 						    xatoi(optval)) < 0)
236 							usage();
237 						break;
238 					case OPT_CONFIG:
239 						if (optval == NULL ||
240 						    (config_interval =
241 						    xatoi(optval)) < 0)
242 							usage();
243 						break;
244 					default:
245 						usage();
246 				}
247 			}
248 			modified++;
249 			break;
250 		case 'm':
251 			maxrss = optarg;
252 			break;
253 		case 'z':
254 			refresh = B_TRUE;
255 			zonename = optarg;
256 			break;
257 		default:
258 			usage();
259 		}
260 	}
261 
262 	/* the -z & -m options must be used together */
263 	if (argc > optind || (refresh && maxrss == NULL) ||
264 	    (!refresh && maxrss != NULL))
265 		usage();
266 
267 	if (refresh && (no_starting_stopping > 0 || modified))
268 		usage();
269 
270 	/*
271 	 * disable/enable before reading configuration from the repository
272 	 * which may fail and prevents the disabling/enabling to complete.
273 	 */
274 	if (disable > 0) {
275 		if (smf_disable_instance(RCAP_FMRI, no_starting_stopping > 0
276 		    ? SMF_AT_NEXT_BOOT : 0) != 0)
277 			die(gettext("cannot disable service: %s\n"),
278 			    scf_strerror(scf_error()));
279 	}
280 
281 	if (enable > 0) {
282 		if (smf_enable_instance(RCAP_FMRI, no_starting_stopping > 0
283 		    ? SMF_AT_NEXT_BOOT : 0) != 0)
284 			die(gettext("cannot enable service: %s\n"),
285 			    scf_strerror(scf_error()));
286 	}
287 
288 	if (rcfg_read(&conf, NULL) != E_SUCCESS) {
289 		/*
290 		 * If instance is enabled, put it in maintenance since we
291 		 * failed to read configuration from the repository or
292 		 * create statistics file.
293 		 */
294 		if (strcmp(smf_get_state(RCAP_FMRI),
295 		    SCF_STATE_STRING_DISABLED) != 0)
296 			(void) smf_maintain_instance(RCAP_FMRI, 0);
297 
298 		die(gettext("resource caps not configured\n"));
299 	} else {
300 		/* Done reading configuration */
301 		if (strcmp(conf.rcfg_mode_name, "project") != 0) {
302 			warn(gettext("%s mode specification ignored -- using"
303 			    " project mode\n"), conf.rcfg_mode_name);
304 			conf.rcfg_mode_name = "project";
305 			conf.rcfg_mode = rctype_project;
306 		}
307 	}
308 
309 	if (refresh)
310 		return (update_zone_mcap(zonename, maxrss));
311 
312 	if (modified) {
313 		if (pressure >= 0)
314 			conf.rcfg_memory_cap_enforcement_pressure = pressure;
315 		if (config_interval >= 0)
316 			conf.rcfg_reconfiguration_interval = config_interval;
317 		if (scan_interval >= 0)
318 			conf.rcfg_proc_walk_interval = scan_interval;
319 		if (report_interval >= 0)
320 			conf.rcfg_report_interval = report_interval;
321 		if (sample_interval >= 0)
322 			conf.rcfg_rss_sample_interval = sample_interval;
323 
324 		/*
325 		 * Modify configuration with the new parameter(s). The
326 		 * function will exit if it fails.
327 		 */
328 		if ((modify_config(&conf)) != 0)
329 			die(gettext("Error updating repository \n"));
330 
331 		if (smf_refresh_instance(RCAP_FMRI) != 0)
332 			die(gettext("cannot refresh service: %s\n"),
333 			    scf_strerror(scf_error()));
334 	}
335 
336 	/*
337 	 * Display current configuration
338 	 */
339 	print_state();
340 	return (E_SUCCESS);
341 }
342