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