Add support for guaranteed CPU shares
[util-vserver.git] / lib / planetlab.c
1 /* Copyright 2005 Princeton University
2
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met: 
6
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9       
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.
14       
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.
18       
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. 
31
32 */
33
34 #include <errno.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <sys/resource.h>
39
40 #include "config.h"
41 #include "planetlab.h"
42 #include "sched_cmd.h"
43 #include "virtual.h"
44 #include "vserver.h"
45
46 static int
47 create_context(xid_t ctx, uint32_t flags, uint64_t bcaps, const rspec_t *rspec)
48 {
49   struct vc_ctx_caps  vc_caps;
50   struct vc_ctx_flags  vc_flags;
51   struct vc_rlimit  vc_rlimit;
52
53   /*
54    * Create context info - this sets the STATE_SETUP and STATE_INIT flags.
55    * Don't ever clear the STATE_INIT flag, that makes us the init task.
56    */
57   if (vc_ctx_create(ctx) == VC_NOCTX)
58     return -1;
59
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))
66     return -1;
67
68   /* set scheduler parameters */
69   if (pl_setsched(ctx, rspec->cpu_share, rspec->cpu_sched_flags))
70     return -1;
71
72   /* set resource limits */
73   vc_rlimit.min = VC_LIM_KEEP;
74   vc_rlimit.soft = VC_LIM_KEEP;
75   vc_rlimit.hard = rspec->mem_limit;
76   if (vc_set_rlimit(ctx, RLIMIT_RSS, &vc_rlimit))
77     return -1;
78
79   /* assume min and soft unchanged by set_rlimit */
80   vc_rlimit.hard = rspec->task_limit;
81   if (vc_set_rlimit(ctx, RLIMIT_NPROC, &vc_rlimit))
82     return -1;
83
84   /* set flags, unset SETUP flag - this allows other processes to migrate */
85   vc_flags.mask = VC_VXF_STATE_SETUP | VC_VXF_SCHED_FLAGS;
86   flags = 0;  /* XXX - ignore flags parameter */
87   vc_flags.flagword = flags | rspec->cpu_sched_flags;  /* SETUP cleared */
88   if (vc_set_cflags(ctx, &vc_flags))
89     return -1;
90
91   return 0;
92 }
93
94 int
95 pl_chcontext(xid_t ctx, uint32_t flags, uint64_t bcaps, const rspec_t *rspec)
96 {
97   for (;;)
98     {
99       struct vc_ctx_flags  vc_flags;
100
101       if (vc_get_cflags(ctx, &vc_flags))
102         {
103           /* context doesn't exist - create it */
104           if (create_context(ctx, flags, bcaps, rspec))
105             {
106               if (errno == EEXIST)
107                 /* another process beat us in a race */
108                 goto migrate;
109               if (errno == EBUSY)
110                 /* another process is creating - poll the SETUP flag */
111                 continue;
112               return -1;
113             }
114
115           /* created context and migrated to it i.e., we're done */
116           break;
117         }
118
119       /* check the SETUP flag */
120       if (vc_flags.flagword & VC_VXF_STATE_SETUP)
121         {
122           /* context is still being setup - wait a while then retry */
123           sleep(1);
124           continue;
125         }
126
127       /* context has been setup */
128     migrate:
129       if (!vc_ctx_migrate(ctx))
130         break;  /* done */
131
132       /* context disappeared - retry */
133     }
134
135   return 0;
136 }
137
138 /* it's okay for a syscall to fail because the context doesn't exist */
139 #define VC_SYSCALL(x)                           \
140 do                                              \
141 {                                               \
142   if (x)                                        \
143     return errno == ESRCH ? 0 : -1;             \
144 }                                               \
145 while (0)
146
147
148 int
149 pl_setsched(xid_t ctx, uint32_t cpu_share, uint32_t cpu_sched_flags)
150 {
151   struct vc_set_sched  vc_sched;
152   struct vc_ctx_flags  vc_flags;
153
154   if (cpu_sched_flags & ~VC_VXF_SCHED_FLAGS)
155     {
156       errno = EINVAL;
157       return -1;
158     }
159
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 */
167
168   VC_SYSCALL(vc_set_sched(ctx, &vc_sched));
169
170   /* get current flag values */
171   VC_SYSCALL(vc_get_cflags(ctx, &vc_flags));
172
173   /* the only flag which ever changes is the SCHED_SHARE bit */
174   if ((vc_flags.flagword ^ cpu_sched_flags) & VC_VXF_SCHED_SHARE)
175     {
176       vc_flags.mask = VC_VXF_SCHED_SHARE;
177       vc_flags.flagword = cpu_sched_flags & VC_VXF_SCHED_FLAGS;
178       VC_SYSCALL(vc_set_cflags(ctx, &vc_flags));
179     }
180
181   return 0;
182 }