summaryrefslogtreecommitdiff
path: root/usr/src/cmd/rcap/rcapadm/rcapadm.c
blob: b92115469a7ed86c08754dca8db6bfdb573c141d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright (c) 2011, Joyent, Inc. All rights reserved.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <libscf.h>
#include <libscf_priv.h>
#include <libintl.h>
#include <locale.h>
#include <zone.h>
#include <libzonecfg.h>

#include "utils.h"
#include "rcapd.h"
#include "rcapd_conf.h"
#include "rcapd_stat.h"

static void
usage()
{
	(void) fprintf(stderr,
	    gettext("usage: rcapadm\n"
	    "               [-E|-D]                                "
	    "# enable/disable rcapd\n"
	    "               [-n]                                   "
	    "# don't start/stop rcapd\n"
	    "               [-i <scan|sample|report|config>=value] "
	    "# set intervals\n"
	    "               [-c <percent>]                         "
	    "# set memory cap\n"
	    "                                                      "
	    "# enforcement threshold\n"
	    "               [-z <zonename> -m <max-rss>]               "
	    "# update zone memory cap\n"));
	exit(E_USAGE);
}

static rcfg_t conf;
static int enable = -1;
static int disable = -1;
static int pressure = -1;
static int no_starting_stopping = -1;
static int scan_interval = -1;
static int report_interval = -1;
static int config_interval = -1;
static int sample_interval = -1;

static char *subopt_v[] = {
	"scan",
	"sample",
	"report",
	"config",
	NULL
};

typedef enum {
	OPT_SCAN = 0,
	OPT_SAMPLE,
	OPT_REPORT,
	OPT_CONFIG
} subopt_idx_t;

static void
print_state(void)
{
	scf_simple_prop_t *persistent_prop = NULL;
	scf_simple_prop_t *temporary_prop = NULL;
	uint8_t *persistent = NULL;
	uint8_t *temporary = NULL;
	scf_handle_t *h;
	/* LINTED: conditionally assigned and used in function */
	ssize_t numvals;

	if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
	    scf_handle_bind(h) != 0)
		goto out;

	if ((persistent_prop = scf_simple_prop_get(h, RCAP_FMRI,
	    SCF_PG_GENERAL, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
	    scf_simple_prop_numvalues(persistent_prop)) > 0)
		persistent = scf_simple_prop_next_boolean(persistent_prop);

	if ((temporary_prop = scf_simple_prop_get(h, RCAP_FMRI,
	    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED)) != NULL && (numvals =
	    scf_simple_prop_numvalues(temporary_prop)) > 0)
		temporary = scf_simple_prop_next_boolean(temporary_prop);

out:
	if (!persistent)
		(void) printf(gettext("                                      "
		    "state: unknown"));
	else if (temporary && *temporary != *persistent)
		(void) printf(gettext("                                      "
		    "state: %s (%s at next boot)\n"), *temporary ?
		    gettext("enabled") : gettext("disabled"), *persistent ?
		    gettext("enabled") : gettext("disabled"));
	else
		(void) printf(gettext("                                      "
		    "state: %s\n"), *persistent ? gettext("enabled") :
		    gettext("disabled"));

	(void) printf(gettext("           memory cap enforcement"
	    " threshold: %d%%\n"), conf.rcfg_memory_cap_enforcement_pressure);
	(void) printf(gettext("                    process scan rate"
	    " (sec): %d\n"), conf.rcfg_proc_walk_interval);
	(void) printf(gettext("                 reconfiguration rate"
	    " (sec): %d\n"), conf.rcfg_reconfiguration_interval);
	(void) printf(gettext("                          report rate"
	    " (sec): %d\n"), conf.rcfg_report_interval);
	(void) printf(gettext("                    RSS sampling rate"
	    " (sec): %d\n"), conf.rcfg_rss_sample_interval);

	scf_simple_prop_free(temporary_prop);
	scf_simple_prop_free(persistent_prop);
	scf_handle_destroy(h);
}

static int
set_zone_cap(char *zonename, uint64_t mcap)
{
	char cmd[128 + ZONENAME_MAX];

	(void) snprintf(cmd, sizeof (cmd), "/usr/bin/prctl -r "
	    "-n zone.max-physical-memory -v %llu -i zone %s", mcap, zonename);
	return (system(cmd));
}

/*
 * Update the in-kernel memory cap for the specified zone.
 */
static int
update_zone_mcap(char *zonename, char *maxrss)
{
	uint64_t num;

	if (getzoneid() != GLOBAL_ZONEID || zonecfg_in_alt_root())
		return (E_SUCCESS);

	/* get the running zone from the kernel */
	if (getzoneidbyname(zonename) == -1) {
		(void) fprintf(stderr, gettext("zone '%s' must be running\n"),
		    zonename);
		return (E_ERROR);
	}

	if (zonecfg_str_to_bytes(maxrss, &num) == -1) {
		(void) fprintf(stderr, gettext("invalid max-rss value\n"));
		return (E_ERROR);
	}

	if (set_zone_cap(zonename, num) == -1) {
		(void) fprintf(stderr, gettext("could not set memory "
		    "cap for zone '%s'\n"), zonename);
		return (E_ERROR);
	}

	return (E_SUCCESS);
}

int
main(int argc, char *argv[])
{
	char *subopts, *optval;
	int modified = 0;
	boolean_t refresh = B_FALSE;
	int opt;
	char *zonename;
	char *maxrss = NULL;

	(void) setpname("rcapadm");
	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	while ((opt = getopt(argc, argv, "DEc:i:m:nz:")) != EOF) {
		switch (opt) {
		case 'n':
			no_starting_stopping = 1;
			break;
		case 'c':
			if ((pressure = xatoi(optarg)) < 0 ||
			    pressure > 100 ||
			    errno == EINVAL)
				usage();
			modified++;
			break;
		case 'E':
			enable = 1;
			disable = 0;
			break;
		case 'D':
			disable = 1;
			enable = 0;
			break;
		case 'i':
			subopts = optarg;
			while (*subopts != '\0') {
				switch (getsubopt(&subopts, subopt_v,
				    &optval)) {
					case OPT_SCAN:
						if (optval == NULL ||
						    (scan_interval =
						    xatoi(optval)) <= 0)
							usage();
						break;
					case OPT_SAMPLE:
						if (optval == NULL ||
						    (sample_interval =
						    xatoi(optval)) <= 0)
							usage();
						break;
					case OPT_REPORT:
						if (optval == NULL ||
						    (report_interval =
						    xatoi(optval)) < 0)
							usage();
						break;
					case OPT_CONFIG:
						if (optval == NULL ||
						    (config_interval =
						    xatoi(optval)) < 0)
							usage();
						break;
					default:
						usage();
				}
			}
			modified++;
			break;
		case 'm':
			maxrss = optarg;
			break;
		case 'z':
			refresh = B_TRUE;
			zonename = optarg;
			break;
		default:
			usage();
		}
	}

	/* the -z & -m options must be used together */
	if (argc > optind || (refresh && maxrss == NULL) ||
	    (!refresh && maxrss != NULL))
		usage();

	if (refresh && (no_starting_stopping > 0 || modified))
		usage();

	/*
	 * disable/enable before reading configuration from the repository
	 * which may fail and prevents the disabling/enabling to complete.
	 */
	if (disable > 0) {
		if (smf_disable_instance(RCAP_FMRI, no_starting_stopping > 0
		    ? SMF_AT_NEXT_BOOT : 0) != 0)
			die(gettext("cannot disable service: %s\n"),
			    scf_strerror(scf_error()));
	}

	if (enable > 0) {
		if (smf_enable_instance(RCAP_FMRI, no_starting_stopping > 0
		    ? SMF_AT_NEXT_BOOT : 0) != 0)
			die(gettext("cannot enable service: %s\n"),
			    scf_strerror(scf_error()));
	}

	if (rcfg_read(&conf, NULL) != E_SUCCESS) {
		/*
		 * If instance is enabled, put it in maintenance since we
		 * failed to read configuration from the repository or
		 * create statistics file.
		 */
		if (strcmp(smf_get_state(RCAP_FMRI),
		    SCF_STATE_STRING_DISABLED) != 0)
			(void) smf_maintain_instance(RCAP_FMRI, 0);

		die(gettext("resource caps not configured\n"));
	} else {
		/* Done reading configuration */
		if (strcmp(conf.rcfg_mode_name, "project") != 0) {
			warn(gettext("%s mode specification ignored -- using"
			    " project mode\n"), conf.rcfg_mode_name);
			conf.rcfg_mode_name = "project";
			conf.rcfg_mode = rctype_project;
		}
	}

	if (refresh)
		return (update_zone_mcap(zonename, maxrss));

	if (modified) {
		if (pressure >= 0)
			conf.rcfg_memory_cap_enforcement_pressure = pressure;
		if (config_interval >= 0)
			conf.rcfg_reconfiguration_interval = config_interval;
		if (scan_interval >= 0)
			conf.rcfg_proc_walk_interval = scan_interval;
		if (report_interval >= 0)
			conf.rcfg_report_interval = report_interval;
		if (sample_interval >= 0)
			conf.rcfg_rss_sample_interval = sample_interval;

		/*
		 * Modify configuration with the new parameter(s). The
		 * function will exit if it fails.
		 */
		if ((modify_config(&conf)) != 0)
			die(gettext("Error updating repository \n"));

		if (smf_refresh_instance(RCAP_FMRI) != 0)
			die(gettext("cannot refresh service: %s\n"),
			    scf_strerror(scf_error()));
	}

	/*
	 * Display current configuration
	 */
	print_state();
	return (E_SUCCESS);
}