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.
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.
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]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/types.h>
37 #include <libscf_priv.h>
41 #include <libzonecfg.h>
45 #include "rcapd_conf.h"
46 #include "rcapd_stat.h"
51 (void) fprintf(stderr
,
52 gettext("usage: rcapadm\n"
54 "# enable/disable rcapd\n"
56 "# don't start/stop rcapd\n"
57 " [-i <scan|sample|report|config>=value] "
62 "# enforcement threshold\n"
63 " [-z <zonename> -m <max-rss>] "
64 "# update zone memory cap\n"));
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;
78 static char *subopt_v
[] = {
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
;
101 /* LINTED: conditionally assigned and used in function */
104 if ((h
= scf_handle_create(SCF_VERSION
)) == NULL
||
105 scf_handle_bind(h
) != 0)
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
);
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
);
120 (void) printf(gettext(" "
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"));
128 (void) printf(gettext(" "
129 "state: %s\n"), *persistent
? gettext("enabled") :
130 gettext("disabled"));
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
);
143 scf_simple_prop_free(temporary_prop
);
144 scf_simple_prop_free(persistent_prop
);
145 scf_handle_destroy(h
);
149 * Update the in-kernel memory cap for the specified zone.
152 update_zone_mcap(char *zonename
, char *maxrss
)
157 if (getzoneid() != GLOBAL_ZONEID
|| zonecfg_in_alt_root())
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"),
167 if (zonecfg_str_to_bytes(maxrss
, &num
) == -1) {
168 (void) fprintf(stderr
, gettext("invalid max-rss value\n"));
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
);
182 main(int argc
, char *argv
[])
184 char *subopts
, *optval
;
186 boolean_t refresh
= B_FALSE
;
191 (void) setpname("rcapadm");
192 (void) setlocale(LC_ALL
, "");
193 (void) textdomain(TEXT_DOMAIN
);
195 while ((opt
= getopt(argc
, argv
, "DEc:i:m:nz:")) != EOF
) {
198 no_starting_stopping
= 1;
201 if ((pressure
= xatoi(optarg
)) < 0 ||
217 while (*subopts
!= '\0') {
218 switch (getsubopt(&subopts
, subopt_v
,
221 if (optval
== NULL
||
227 if (optval
== NULL
||
233 if (optval
== NULL
||
239 if (optval
== NULL
||
262 /* the -z & -m options must be used together */
263 if (argc
> optind
|| (refresh
&& maxrss
== NULL
) ||
264 (!refresh
&& maxrss
!= NULL
))
267 if (refresh
&& (no_starting_stopping
> 0 || modified
))
271 * disable/enable before reading configuration from the repository
272 * which may fail and prevents the disabling/enabling to complete.
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()));
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()));
288 if (rcfg_read(&conf
, NULL
) != E_SUCCESS
) {
290 * If instance is enabled, put it in maintenance since we
291 * failed to read configuration from the repository or
292 * create statistics file.
294 if (strcmp(smf_get_state(RCAP_FMRI
),
295 SCF_STATE_STRING_DISABLED
) != 0)
296 (void) smf_maintain_instance(RCAP_FMRI
, 0);
298 die(gettext("resource caps not configured\n"));
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
;
310 return (update_zone_mcap(zonename
, maxrss
));
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
;
325 * Modify configuration with the new parameter(s). The
326 * function will exit if it fails.
328 if ((modify_config(&conf
)) != 0)
329 die(gettext("Error updating repository \n"));
331 if (smf_refresh_instance(RCAP_FMRI
) != 0)
332 die(gettext("cannot refresh service: %s\n"),
333 scf_strerror(scf_error()));
337 * Display current configuration