Cosmetic fixes for vsched.
[util-vserver.git] / src / vsched.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // Copyright (C) 2006 Daniel Hokka Zakrisson <daniel@hozac.com>
5 //  
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; version 2 of the License.
9 //  
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //  
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "util.h"
25 #include "vserver.h"
26
27 #include <errno.h>
28 #include <unistd.h>
29 #include <getopt.h>
30 #include <libgen.h>
31 #include <dirent.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34
35 #define ENSC_WRAPPERS_PREFIX    "vsched: "
36 #define ENSC_WRAPPERS_VSERVER   1
37 #define ENSC_WRAPPERS_UNISTD    1
38 #define ENSC_WRAPPERS_FCNTL     1
39 #define ENSC_WRAPPERS_DIRENT    1
40 #define ENSC_WRAPPERS_STAT      1
41 #include <wrappers.h>
42
43 #define CMD_HELP                0x1000
44 #define CMD_VERSION             0x1001
45 #define CMD_XID                 0x4000
46 #define CMD_FRATE               0x4001
47 #define CMD_INTERVAL            0x4002
48 #define CMD_TOKENS              0x4003
49 #define CMD_TOK_MIN             0x4004
50 #define CMD_TOK_MAX             0x4005
51 #define CMD_CPU_MASK            0x4006
52 #define CMD_PRIO_BIAS           0x4007
53 #define CMD_FRATE2              0x4008
54 #define CMD_INTERVAL2           0x4009
55 #define CMD_CPUID               0x400a
56 #define CMD_BUCKETID            0x400b
57 #define CMD_FORCE               0x400c
58 #define CMD_IDLE_TIME           0x400d
59 #define CMD_DIR                 0x400e
60 #define CMD_MISSING             0x400f
61
62 int                     wrapper_exit_code = 255;
63
64 struct option const
65 CMDLINE_OPTIONS[] = {
66   { "help",     no_argument,  0, CMD_HELP },
67   { "version",  no_argument,  0, CMD_VERSION },
68   { "ctx",           required_argument, 0, CMD_XID },
69   { "xid",           required_argument, 0, CMD_XID },
70   { "fill-rate",     required_argument, 0, CMD_FRATE },
71   { "interval",      required_argument, 0, CMD_INTERVAL },
72   { "tokens",        required_argument, 0, CMD_TOKENS },
73   { "tokens_min",    required_argument, 0, CMD_TOK_MIN },
74   { "tokens-min",    required_argument, 0, CMD_TOK_MIN },
75   { "tokens_max",    required_argument, 0, CMD_TOK_MAX },
76   { "tokens-max",    required_argument, 0, CMD_TOK_MAX },
77   { "prio_bias",     required_argument, 0, CMD_PRIO_BIAS },
78   { "prio-bias",     required_argument, 0, CMD_PRIO_BIAS },
79   { "priority_bias", required_argument, 0, CMD_PRIO_BIAS },
80   { "priority-bias", required_argument, 0, CMD_PRIO_BIAS },
81   { "cpu_mask",      required_argument, 0, CMD_CPU_MASK },
82   { "fill-rate2",    required_argument, 0, CMD_FRATE2 },
83   { "interval2",     required_argument, 0, CMD_INTERVAL2 },
84   { "cpu-id",        required_argument, 0, CMD_CPUID },
85   { "bucket-id",     required_argument, 0, CMD_BUCKETID },
86   { "force",         no_argument,       0, CMD_FORCE },
87   { "idle-time",     no_argument,       0, CMD_IDLE_TIME },
88   { "dir",           required_argument, 0, CMD_DIR },
89   { "missingok",     no_argument,       0, CMD_MISSING },
90   {0,0,0,0}
91 };
92
93 struct sched_opt {
94   const char * const    name;
95   uint_least32_t        mask;
96   size_t                offset;
97 };
98 #define FOPT(NAME,MASK,FIELD)   { #NAME, MASK, offsetof(struct vc_set_sched, FIELD) }
99 static struct sched_opt FILE_OPTIONS[] = {
100   FOPT(fill-rate,       VC_VXSM_FILL_RATE,                      fill_rate),
101   FOPT(interval,        VC_VXSM_INTERVAL,                       interval),
102   FOPT(tokens,          VC_VXSM_TOKENS,                         tokens),
103   FOPT(tokens-min,      VC_VXSM_TOKENS_MIN,                     tokens_min),
104   FOPT(tokens-max,      VC_VXSM_TOKENS_MAX,                     tokens_max),
105   FOPT(prio-bias,       VC_VXSM_PRIO_BIAS,                      priority_bias),
106   FOPT(priority-bias,   VC_VXSM_PRIO_BIAS,                      priority_bias),
107   FOPT(fill-rate2,      VC_VXSM_FILL_RATE2|VC_VXSM_IDLE_TIME,   fill_rate2),
108   FOPT(interval2,       VC_VXSM_INTERVAL2|VC_VXSM_IDLE_TIME,    interval2),
109   FOPT(cpu-id,          VC_VXSM_CPU_ID,                         cpu_id),
110   FOPT(bucket-id,       VC_VXSM_BUCKET_ID,                      bucket_id),
111   FOPT(idle-time,       VC_VXSM_IDLE_TIME,                      set_mask),
112   {0,0,0}
113 };
114
115 static void
116 showHelp(int fd, char const *cmd, int res)
117 {
118   VSERVER_DECLARE_CMD(cmd);
119
120   WRITE_MSG(fd, "Usage:\n  ");
121   WRITE_STR(fd, cmd);
122   WRITE_MSG(fd,
123             " [--xid <xid>] <sched options>* [--dir <dir>] [--] [<command> <args>*]\n"
124             "\n"
125             "Options:\n"
126             "    --fill-rate <rate>\n"
127             "    --interval <interval>\n"
128             "    --tokens <tokens>\n"
129             "    --tokens-min <tokens>\n"
130             "    --tokens-max <tokens>\n"
131             "    --prio-bias <bias>\n"
132             "    --fill-rate2 <rate>\n"
133             "    --interval2 <interval>\n"
134             "    --cpu-id <CPU id>\n"
135             "    --bucket-id <bucket id>\n"
136             "    --idle-time    ...  set the idle time flag; this is required for\n"
137             "                        all updates to the scheduler to keep it enabled\n"
138             "    --force        ...  force update of all per-CPU schedulers now\n"
139             "    --dir <dir>    ...  read settings from <dir>\n"
140             "    --missingok    ...  do not fail when <dir> does not exist\n"
141             "\nPlease report bugs to " PACKAGE_BUGREPORT "\n");
142
143   exit(res);
144 }
145
146 static void
147 showVersion()
148 {
149   WRITE_MSG(1,
150             "vsched " VERSION " -- modifies scheduling parameters\n"
151             "This program is part of " PACKAGE_STRING "\n\n"
152             "Copyright (C) 2003,2004 Enrico Scholz\n"
153             "Copyright (C) 2006 Daniel Hokka Zakrisson\n"
154             VERSION_COPYRIGHT_DISCLAIMER);
155   exit(0);
156 }
157
158 static void do_dir_entry(struct vc_set_sched *sched, const char *name)
159 {
160   int fd;
161   char buf[128];
162   signed long val;
163   struct sched_opt *opt;
164   ssize_t len;
165   char *newline;
166
167   for (opt = FILE_OPTIONS; opt->name != 0; opt++) {
168     if (strcmp(name, opt->name) == 0)
169       break;
170   }
171
172   fd = Eopen(name, O_RDONLY, 0);
173   len = Eread(fd, buf, sizeof(buf)-1);
174   Eclose(fd);
175   buf[len] = '\0';
176   if ((newline=strchr(buf, '\n')) != NULL)
177     *newline = '\0';
178
179   if (!isNumber(buf, &val, true)) {
180     WRITE_MSG(2, ENSC_WRAPPERS_PREFIX);
181     WRITE_STR(2, name);
182     WRITE_MSG(2, ": is not a number\n");
183     exit(1);
184   }
185
186   if (opt->offset != offsetof(struct vc_set_sched, set_mask))
187     *(int_least32_t *)(((char *)sched)+opt->offset) = (int_least32_t) val;
188
189   sched->set_mask |= opt->mask;
190 }
191
192 static void do_dir(xid_t xid, struct vc_set_sched *sched, const char *dir, int missing_ok, int per_cpu)
193 {
194   DIR                   *dp;
195   struct dirent         *de;
196   int                   cur_fd = Eopen(".", O_RDONLY, 0);
197   struct stat           st;
198
199   if (chdir(dir)!=-1) {
200     dp = Eopendir(".");
201     while ((de = Ereaddir(dp)) != NULL) {
202       if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || (de->d_name[1] == '.' && de->d_name[2] == '\0')))
203         continue;
204       Estat(de->d_name, &st);
205       if (S_ISDIR(st.st_mode))
206         continue;
207       do_dir_entry(sched, de->d_name);
208     }
209
210     /* set the values now */
211     if (vc_set_sched(xid, sched) == -1) {
212       perror(ENSC_WRAPPERS_PREFIX "vc_set_sched()");
213       exit(1);
214     }
215
216     if (!per_cpu) {
217       struct vc_set_sched per_cpu_sched;
218
219       rewinddir(dp);
220       while ((de = Ereaddir(dp)) != NULL) {
221         if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || (de->d_name[1] == '.' && de->d_name[2] == '\0')))
222           continue;
223         Estat(de->d_name, &st);
224         if (S_ISDIR(st.st_mode)) {
225           per_cpu_sched.set_mask = sched->set_mask & (VC_VXSM_IDLE_TIME|VC_VXSM_FORCE);
226           do_dir(xid, &per_cpu_sched, de->d_name, 0, 1);
227         }
228       }
229     }
230
231     Eclosedir(dp);
232   }
233   else if (!missing_ok) {
234     perror(ENSC_WRAPPERS_PREFIX "chdir()");
235     exit(wrapper_exit_code);
236   }
237
238   Efchdir(cur_fd);
239 }
240
241 #define SETVAL(ATTR,MASK) \
242   if (!isNumber(optarg, &tmp, false)) { \
243     WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "non-numeric value specified for '--" #ATTR "'\n"); \
244     exit(wrapper_exit_code); \
245   } \
246   else { \
247     sched.ATTR      = tmp; \
248     sched.set_mask |= MASK; \
249   }
250
251 int main(int argc, char *argv[])
252 {
253   xid_t                 xid   = VC_NOCTX;
254   signed long           tmp;
255   struct vc_set_sched   sched = {
256     .set_mask = 0
257   };
258   const char            *dir = NULL;
259   int                   missing_ok = 0;
260   
261   while (1) {
262     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
263     if (c==-1) break;
264
265     switch (c) {
266       case CMD_HELP     :  showHelp(1, argv[0], 0);
267       case CMD_VERSION  :  showVersion();
268       case CMD_XID      :  xid = Evc_xidopt2xid(optarg,true);         break;
269       case CMD_FRATE    :  SETVAL(fill_rate,     VC_VXSM_FILL_RATE);  break;
270       case CMD_INTERVAL :  SETVAL(interval,      VC_VXSM_INTERVAL);   break;
271       case CMD_TOKENS   :  SETVAL(tokens,        VC_VXSM_TOKENS);     break;
272       case CMD_TOK_MIN  :  SETVAL(tokens_min,    VC_VXSM_TOKENS_MIN); break;
273       case CMD_TOK_MAX  :  SETVAL(tokens_max,    VC_VXSM_TOKENS_MAX); break;
274       case CMD_PRIO_BIAS:  SETVAL(priority_bias, VC_VXSM_PRIO_BIAS);  break;
275       case CMD_CPU_MASK :
276         WRITE_MSG(2, "vsched: WARNING: the '--cpu_mask' parameter is deprecated and will not have any effect\n");
277         break;
278       case CMD_FRATE2   :  SETVAL(fill_rate2,    VC_VXSM_FILL_RATE2); break;
279       case CMD_INTERVAL2:  SETVAL(interval2,     VC_VXSM_INTERVAL2);  break;
280       case CMD_CPUID    :  SETVAL(cpu_id,        VC_VXSM_CPU_ID);     break;
281       case CMD_BUCKETID :  SETVAL(bucket_id,     VC_VXSM_BUCKET_ID);  break;
282       case CMD_DIR      :  dir = optarg;                              break;
283       case CMD_MISSING  :  missing_ok = 1;                            break;
284       case CMD_FORCE    :  sched.set_mask |= VC_VXSM_FORCE;           break;
285       case CMD_IDLE_TIME:  sched.set_mask |= VC_VXSM_IDLE_TIME;       break;
286       default           :
287         WRITE_MSG(2, "Try '");
288         WRITE_STR(2, argv[0]);
289         WRITE_MSG(2, " --help' for more information.\n");
290         return EXIT_FAILURE;
291         break;
292     }
293   }
294
295   if (xid==VC_NOCTX && optind==argc) {
296     WRITE_MSG(2, "Without a program, '--xid' must be used; try '--help' for more information\n");
297     exit(wrapper_exit_code);
298   }
299
300   if (sched.set_mask==0 && dir==NULL && optind==argc) {
301     WRITE_MSG(2, "Neither an option nor a program was specified; try '--help' for more information\n");
302     exit(wrapper_exit_code);
303   }
304
305   if (xid==VC_NOCTX)
306     xid = Evc_get_task_xid(0);
307
308   if (dir) {
309     do_dir(xid, &sched, dir, missing_ok, 0);
310   }
311   else {
312     if (sched.set_mask!=0 && vc_set_sched(xid, &sched)==-1) {
313       perror("vc_set_sched()");
314       exit(255);
315     }
316   }
317
318   if (optind<argc)
319     EexecvpD(argv[optind],argv+optind);
320
321   return EXIT_SUCCESS;
322 }