1 /* Copyright 2005 Princeton University
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above
11 copyright notice, this list of conditions and the following
12 disclaimer in the documentation and/or other materials provided
13 with the distribution.
15 * Neither the name of the copyright holder nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PRINCETON
23 UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
45 #include <sys/resource.h>
49 #include "planetlab.h"
52 create_context(xid_t ctx, uint64_t bcaps)
54 struct vc_ctx_caps vc_caps;
55 struct vc_net_flags vc_nf;
57 /* Create network context */
58 if (vc_net_create(ctx) == VC_NOCTX) {
64 /* Make the network context persistent */
65 vc_nf.mask = vc_nf.flagword = VC_NXF_PERSISTENT;
66 if (vc_set_nflags(ctx, &vc_nf))
70 /* Create tag context */
71 if (vc_tag_create(ctx) == VC_NOCTX)
76 * Create context info - this sets the STATE_SETUP and STATE_INIT flags.
78 if (vc_ctx_create(ctx, 0) == VC_NOCTX)
81 /* Set capabilities - these don't take effect until SETUP flag is unset */
82 vc_caps.bcaps = bcaps;
83 vc_caps.bmask = ~0ULL; /* currently unused */
84 vc_caps.ccaps = 0; /* don't want any of these */
85 vc_caps.cmask = ~0ULL;
86 if (vc_set_ccaps(ctx, &vc_caps))
89 if (pl_setsched(ctx, 0, 1) < 0) {
90 PERROR("pl_setsched(%u)", ctx);
98 pl_setup_done(xid_t ctx)
100 struct vc_ctx_flags vc_flags;
102 /* unset SETUP flag - this allows other processes to migrate */
103 /* set the PERSISTENT flag - so the context doesn't vanish */
104 /* Don't clear the STATE_INIT flag, as that would make us the init task. */
105 vc_flags.mask = VC_VXF_STATE_SETUP|VC_VXF_PERSISTENT;
106 vc_flags.flagword = VC_VXF_PERSISTENT;
107 if (vc_set_cflags(ctx, &vc_flags))
113 #define RETRY_LIMIT 10
116 pl_chcontext(xid_t ctx, uint64_t bcaps, const struct sliver_resources *slr)
119 int net_migrated = 0;
125 struct vc_ctx_flags vc_flags;
127 if (vc_get_cflags(ctx, &vc_flags))
132 /* context doesn't exist - create it */
133 if (create_context(ctx, bcaps))
136 /* another process beat us in a race */
139 /* another process is creating - poll the SETUP flag */
144 /* created context and migrated to it i.e., we're done */
148 /* check the SETUP flag */
149 if (vc_flags.flagword & VC_VXF_STATE_SETUP)
151 /* context is still being setup - wait a while then retry */
152 if (retry_count++ >= RETRY_LIMIT)
161 /* context has been setup */
163 if (net_migrated || !vc_net_migrate(ctx))
165 if (!vc_tag_migrate(ctx) && !vc_ctx_migrate(ctx, 0))
170 /* context disappeared - retry */
176 /* it's okay for a syscall to fail because the context doesn't exist */
177 #define VC_SYSCALL(x) \
181 return errno == ESRCH ? 0 : -1; \
186 pl_setsched(xid_t ctx, uint32_t cpu_min, uint32_t cpu_share)
188 struct vc_set_sched vc_sched;
189 struct vc_ctx_flags vc_flags;
191 vc_sched.set_mask = (VC_VXSM_FILL_RATE | VC_VXSM_INTERVAL | VC_VXSM_TOKENS |
192 VC_VXSM_TOKENS_MIN | VC_VXSM_TOKENS_MAX | VC_VXSM_MSEC |
193 VC_VXSM_FILL_RATE2 | VC_VXSM_INTERVAL2 | VC_VXSM_FORCE);
194 vc_sched.fill_rate = cpu_min; /* percent reserved */
195 vc_sched.interval = 100;
196 vc_sched.fill_rate2 = cpu_share; /* best-effort fair share of unreserved */
197 vc_sched.interval2 = 1000; /* milliseconds */
198 vc_sched.tokens = 100; /* initial allocation of tokens */
199 vc_sched.tokens_min = 50; /* need this many tokens to run */
200 vc_sched.tokens_max = 100; /* max accumulated number of tokens */
203 if (cpu_share == (uint32_t)VC_LIM_KEEP)
204 vc_sched.set_mask &= ~(VC_VXSM_FILL_RATE|VC_VXSM_FILL_RATE2);
206 vc_sched.set_mask |= VC_VXSM_IDLE_TIME;
209 VC_SYSCALL(vc_set_sched(ctx, &vc_sched));
211 vc_flags.mask = VC_VXF_SCHED_FLAGS;
212 vc_flags.flagword = VC_VXF_SCHED_HARD;
213 VC_SYSCALL(vc_set_cflags(ctx, &vc_flags));
218 struct pl_resources {
220 unsigned long long *limit;
223 #define WHITESPACE(buffer,index,len) \
224 while(isspace((int)buffer[index])) \
225 if (index < len) index++; else goto out;
227 #define VSERVERCONF "/etc/vservers/"
229 pl_get_limits(const char *context, struct sliver_resources *slr)
233 size_t len = strlen(VSERVERCONF) + strlen(context) + NULLBYTE_SIZE;
234 char *conf = (char *)malloc(len + strlen("rlimits/openfd.hard"));
235 struct pl_resources *r;
236 struct pl_resources sliver_list[] = {
237 {"sched/fill-rate2", &slr->vs_cpu},
239 {"rlimits/nproc.hard", &slr->vs_nproc.hard},
240 {"rlimits/nproc.soft", &slr->vs_nproc.soft},
241 {"rlimits/nproc.min", &slr->vs_nproc.min},
243 {"rlimits/rss.hard", &slr->vs_rss.hard},
244 {"rlimits/rss.soft", &slr->vs_rss.soft},
245 {"rlimits/rss.min", &slr->vs_rss.min},
247 {"rlimits/as.hard", &slr->vs_as.hard},
248 {"rlimits/as.soft", &slr->vs_as.soft},
249 {"rlimits/as.min", &slr->vs_as.min},
251 {"rlimits/openfd.hard", &slr->vs_openfd.hard},
252 {"rlimits/openfd.soft", &slr->vs_openfd.soft},
253 {"rlimits/openfd.min", &slr->vs_openfd.min},
258 sprintf(conf, "%s%s", VSERVERCONF, context);
260 slr->vs_rss.hard = VC_LIM_KEEP;
261 slr->vs_rss.soft = VC_LIM_KEEP;
262 slr->vs_rss.min = VC_LIM_KEEP;
264 slr->vs_as.hard = VC_LIM_KEEP;
265 slr->vs_as.soft = VC_LIM_KEEP;
266 slr->vs_as.min = VC_LIM_KEEP;
268 slr->vs_nproc.hard = VC_LIM_KEEP;
269 slr->vs_nproc.soft = VC_LIM_KEEP;
270 slr->vs_nproc.min = VC_LIM_KEEP;
272 slr->vs_openfd.hard = VC_LIM_KEEP;
273 slr->vs_openfd.soft = VC_LIM_KEEP;
274 slr->vs_openfd.min = VC_LIM_KEEP;
276 cwd = open(".", O_RDONLY);
278 perror("cannot get a handle on .");
281 if (chdir(conf) == -1) {
282 fprintf(stderr, "cannot chdir to ");
287 for (r = &sliver_list[0]; r->name; r++) {
289 fb = fopen(r->name, "r");
292 if (fgets(buf, sizeof(buf), fb) != NULL && isdigit(*buf))
293 *r->limit = atoi(buf);
305 adjust_lim(const struct vc_rlimit *vcr, struct rlimit *lim)
308 if (vcr->min != VC_LIM_KEEP) {
309 if (vcr->min > lim->rlim_cur) {
310 lim->rlim_cur = vcr->min;
313 if (vcr->min > lim->rlim_max) {
314 lim->rlim_max = vcr->min;
319 if (vcr->soft != VC_LIM_KEEP) {
320 switch (vcr->min != VC_LIM_KEEP) {
322 if (vcr->soft < vcr->min)
325 lim->rlim_cur = vcr->soft;
330 if (vcr->hard != VC_LIM_KEEP) {
331 switch (vcr->min != VC_LIM_KEEP) {
333 if (vcr->hard < vcr->min)
336 lim->rlim_cur = vcr->hard;
344 set_one_ulimit(int resource, const struct vc_rlimit *limit)
347 getrlimit(resource, &lim);
348 adjust_lim(limit, &lim);
349 setrlimit(resource, &lim);
353 pl_set_ulimits(const struct sliver_resources *slr)
358 set_one_ulimit(RLIMIT_RSS, &slr->vs_rss);
359 set_one_ulimit(RLIMIT_AS, &slr->vs_as);
360 set_one_ulimit(RLIMIT_NPROC, &slr->vs_nproc);
361 set_one_ulimit(RLIMIT_NOFILE, &slr->vs_openfd);