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 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2011 Joyent, Inc. All rights reserved.
25 */
26
27 #include <procfs.h>
28 #include <project.h>
29 #include <stdlib.h>
30 #include <strings.h>
31 #include <zone.h>
32 #include <libzonecfg.h>
33 #include <dirent.h>
34 #include <libproc.h>
35 #include "rcapd.h"
36 #include "utils.h"
37
38 extern boolean_t gz_capped;
39
40 /* round up to next y = 2^n */
41 #define ROUNDUP(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
42
43 static struct ps_prochandle *
44 grab_zone_proc(zoneid_t zid)
45 {
46 DIR *dirp;
47 struct dirent *dentp;
48 int pid, pid_self, tmp;
49 psinfo_t psinfo;
50 struct ps_prochandle *pr = NULL;
51
52 pid_self = getpid();
53
54 if ((dirp = opendir("/proc")) == NULL)
55 return (NULL);
56
57 while (dentp = readdir(dirp)) {
58 pid = atoi(dentp->d_name);
59
60 /* Skip self */
61 if (pid == pid_self)
62 continue;
63
64 if (proc_get_psinfo(pid, &psinfo) != 0)
65 continue;
66
67 if (psinfo.pr_zoneid != zid)
68 continue;
69
70 /* attempt to grab process */
71 if ((pr = Pgrab(pid, 0, &tmp)) != NULL) {
72 if (Psetflags(pr, PR_RLC) != 0) {
73 Prelease(pr, 0);
74 }
75 if (Pcreate_agent(pr) == 0) {
76 if (pr_getzoneid(pr) != zid) {
77 Prelease(pr, 0);
78 continue;
79 }
80
81 (void) closedir(dirp);
82 return (pr);
83 } else {
84 Prelease(pr, 0);
85 }
86 }
87 }
88
89 (void) closedir(dirp);
90 return (NULL);
91 }
92
93 static uint64_t
94 get_zone_cap(zoneid_t zid)
95 {
96 rctlblk_t *rblk;
97 uint64_t mcap;
98 struct ps_prochandle *pr;
99
100 if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL)
101 return (UINT64_MAX);
102
103 if ((pr = grab_zone_proc(zid)) == NULL) {
104 free(rblk);
105 return (UINT64_MAX);
106 }
107
108 if (pr_getrctl(pr, "zone.max-physical-memory", NULL, rblk,
109 RCTL_FIRST)) {
110 Pdestroy_agent(pr);
111 Prelease(pr, 0);
112 free(rblk);
113 return (UINT64_MAX);
114 }
115
116 Pdestroy_agent(pr);
117 Prelease(pr, 0);
118
119 mcap = rctlblk_get_value(rblk);
120 free(rblk);
121 return (mcap);
122 }
123
124 /*
125 * For zones, rcapd only caps the global zone, since each non-global zone
126 * caps itself.
127 */
128 /* ARGSUSED */
129 void
130 lcollection_update_zone(lcollection_update_type_t ut,
131 void(*update_notification_cb)(char *, char *, int, uint64_t, int))
132 {
133 int changes;
134 int64_t max_rss;
135 uint64_t mcap;
136 lcollection_t *lcol;
137 rcid_t colid;
138
139 mcap = get_zone_cap(GLOBAL_ZONEID);
140 if (mcap != 0 && mcap != UINT64_MAX) {
141 max_rss = ROUNDUP(mcap, 1024) / 1024;
142 gz_capped = B_TRUE;
143 } else {
144 max_rss = UINT64_MAX / 1024;
145 gz_capped = B_FALSE;
146 }
147
148 colid.rcid_type = RCIDT_ZONE;
149 colid.rcid_val = GLOBAL_ZONEID;
150
151 lcol = lcollection_insert_update(&colid, max_rss, GLOBAL_ZONENAME,
152 &changes);
153 if (update_notification_cb != NULL)
154 update_notification_cb("zone", GLOBAL_ZONENAME, changes,
155 max_rss, (lcol != NULL) ? lcol->lcol_mark : 0);
156 }