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.
38 #include <sys/resource.h>
41 #include "planetlab.h"
42 #include "sched_cmd.h"
47 create_context(xid_t ctx, uint32_t flags, uint64_t bcaps)
49 struct vc_ctx_caps vc_caps;
52 * Create context info - this sets the STATE_SETUP and STATE_INIT flags.
53 * Don't ever clear the STATE_INIT flag, that makes us the init task.
55 * XXX - the kernel code allows initial flags to be passed as an arg.
57 if (vc_ctx_create(ctx) == VC_NOCTX)
60 /* set capabilities - these don't take effect until SETUP flag is unset */
61 vc_caps.bcaps = bcaps;
62 vc_caps.bmask = ~0ULL; /* currently unused */
63 vc_caps.ccaps = 0; /* don't want any of these */
64 vc_caps.cmask = ~0ULL;
65 if (vc_set_ccaps(ctx, &vc_caps))
68 /* set default scheduling parameters */
69 pl_setsched(ctx, 1, 0);
75 pl_setup_done(xid_t ctx)
77 struct vc_ctx_flags vc_flags;
79 /* unset SETUP flag - this allows other processes to migrate */
80 vc_flags.mask = VC_VXF_STATE_SETUP;
81 vc_flags.flagword = 0;
82 if (vc_set_cflags(ctx, &vc_flags))
88 #define RETRY_LIMIT 10
91 pl_chcontext(xid_t ctx, uint32_t flags, uint64_t bcaps)
97 struct vc_ctx_flags vc_flags;
99 if (vc_get_cflags(ctx, &vc_flags))
104 /* context doesn't exist - create it */
105 if (create_context(ctx, flags, bcaps))
108 /* another process beat us in a race */
111 /* another process is creating - poll the SETUP flag */
116 /* created context and migrated to it i.e., we're done */
120 /* check the SETUP flag */
121 if (vc_flags.flagword & VC_VXF_STATE_SETUP)
123 /* context is still being setup - wait a while then retry */
124 if (retry_count++ >= RETRY_LIMIT)
133 /* context has been setup */
135 if (!vc_ctx_migrate(ctx))
138 /* context disappeared - retry */
144 /* it's okay for a syscall to fail because the context doesn't exist */
145 #define VC_SYSCALL(x) \
149 return errno == ESRCH ? 0 : -1; \
154 pl_setsched(xid_t ctx, uint32_t cpu_share, uint32_t cpu_sched_flags)
156 struct vc_set_sched vc_sched;
157 struct vc_ctx_flags vc_flags;
160 vc_sched.set_mask = (VC_VXSM_FILL_RATE | VC_VXSM_INTERVAL | VC_VXSM_TOKENS |
161 VC_VXSM_TOKENS_MIN | VC_VXSM_TOKENS_MAX);
162 vc_sched.fill_rate = cpu_share; /* tokens accumulated per interval */
163 vc_sched.interval = 1000; /* milliseconds */
164 vc_sched.tokens = 100; /* initial allocation of tokens */
165 vc_sched.tokens_min = 50; /* need this many tokens to run */
166 vc_sched.tokens_max = 100; /* max accumulated number of tokens */
168 VC_SYSCALL(vc_set_sched(ctx, &vc_sched));
170 /* get current flag values */
171 VC_SYSCALL(vc_get_cflags(ctx, &vc_flags));
173 /* guaranteed CPU corresponds to SCHED_SHARE flag being cleared */
174 new_flags = (cpu_sched_flags & VS_SCHED_CPU_GUARANTEED
176 : VC_VXF_SCHED_SHARE);
177 if ((vc_flags.flagword & VC_VXF_SCHED_SHARE) != new_flags)
179 vc_flags.mask = VC_VXF_SCHED_FLAGS;
180 vc_flags.flagword = new_flags | VC_VXF_SCHED_HARD;
181 VC_SYSCALL(vc_set_cflags(ctx, &vc_flags));