6f93a610ab723fab18567d7272d39b7bcdb86d58
[util-vserver.git] / src / vsched.c
1 // $Id: vsched.c,v 1.6 2004/12/21 07:19:20 ensc Exp $    --*- 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
31 #define ENSC_WRAPPERS_PREFIX    "vsched: "
32 #define ENSC_WRAPPERS_VSERVER   1
33 #define ENSC_WRAPPERS_UNISTD    1
34 #include <wrappers.h>
35
36 #define CMD_HELP                0x1000
37 #define CMD_VERSION             0x1001
38 #define CMD_XID                 0x4000
39 #define CMD_FRATE               0x4001
40 #define CMD_INTERVAL            0x4002
41 #define CMD_TOKENS              0x4003
42 #define CMD_TOK_MIN             0x4004
43 #define CMD_TOK_MAX             0x4005
44 #define CMD_CPU_MASK            0x4006
45 #define CMD_PRIO_BIAS           0x4007
46
47 int                     wrapper_exit_code = 255;
48
49 struct option const
50 CMDLINE_OPTIONS[] = {
51   { "help",     no_argument,  0, CMD_HELP },
52   { "version",  no_argument,  0, CMD_VERSION },
53   { "ctx",           required_argument, 0, CMD_XID },
54   { "xid",           required_argument, 0, CMD_XID },
55   { "fill-rate",     required_argument, 0, CMD_FRATE },
56   { "interval",      required_argument, 0, CMD_INTERVAL },
57   { "tokens",        required_argument, 0, CMD_TOKENS },
58   { "tokens_min",    required_argument, 0, CMD_TOK_MIN },
59   { "tokens-min",    required_argument, 0, CMD_TOK_MIN },
60   { "tokens_max",    required_argument, 0, CMD_TOK_MAX },
61   { "tokens-max",    required_argument, 0, CMD_TOK_MAX },
62   { "prio_bias",     required_argument, 0, CMD_PRIO_BIAS },
63   { "prio-bias",     required_argument, 0, CMD_PRIO_BIAS },
64   { "priority_bias", required_argument, 0, CMD_PRIO_BIAS },
65   { "priority-bias", required_argument, 0, CMD_PRIO_BIAS },
66   { "cpu_mask",      required_argument, 0, CMD_CPU_MASK },
67   {0,0,0,0}
68 };
69
70 static void
71 showHelp(int fd, char const *cmd, int res)
72 {
73   VSERVER_DECLARE_CMD(cmd);
74
75   WRITE_MSG(fd, "Usage:\n  ");
76   WRITE_STR(fd, cmd);
77   WRITE_MSG(fd,
78             " [--xid <xid>] [--fill-rate <rate>] [--interval <interval>] [--tokens <tokens>] [--tokens-min <tokens>] [--tokens-max <tokens>] [--prio-bias <bias>] [--] [<command> <args>*]\n"
79             "\n"
80             "Please report bugs to " PACKAGE_BUGREPORT "\n");
81
82   exit(res);
83 }
84
85 static void
86 showVersion()
87 {
88   WRITE_MSG(1,
89             "vsched " VERSION " -- modifies scheduling parameters\n"
90             "This program is part of " PACKAGE_STRING "\n\n"
91             "Copyright (C) 2003,2004 Enrico Scholz\n"
92             VERSION_COPYRIGHT_DISCLAIMER);
93   exit(0);
94 }
95
96 #define SETVAL(ATTR,MASK) \
97   sched.ATTR      = atoi(optarg); \
98   sched.set_mask |= MASK;
99
100 int main(int argc, char *argv[])
101 {
102   xid_t                 xid   = VC_NOCTX;
103   struct vc_set_sched   sched = {
104     .set_mask = 0
105   };
106   
107   while (1) {
108     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
109     if (c==-1) break;
110
111     switch (c) {
112       case CMD_HELP     :  showHelp(1, argv[0], 0);
113       case CMD_VERSION  :  showVersion();
114       case CMD_XID      :  xid = Evc_xidopt2xid(optarg,true);         break;
115       case CMD_FRATE    :  SETVAL(fill_rate,     VC_VXSM_FILL_RATE);  break;
116       case CMD_INTERVAL :  SETVAL(interval,      VC_VXSM_INTERVAL);   break;
117       case CMD_TOKENS   :  SETVAL(tokens,        VC_VXSM_TOKENS);     break;
118       case CMD_TOK_MIN  :  SETVAL(tokens_min,    VC_VXSM_TOKENS_MIN); break;
119       case CMD_TOK_MAX  :  SETVAL(tokens_max,    VC_VXSM_TOKENS_MAX); break;
120       case CMD_PRIO_BIAS:  SETVAL(priority_bias, VC_VXSM_PRIO_BIAS);  break;
121       case CMD_CPU_MASK :
122         WRITE_MSG(2, "vsched: WARNING: the '--cpu_mask' parameter is deprecated and will not have any effect\n");
123         break;
124       default           :
125         WRITE_MSG(2, "Try '");
126         WRITE_STR(2, argv[0]);
127         WRITE_MSG(2, " --help\" for more information.\n");
128         return EXIT_FAILURE;
129         break;
130     }
131   }
132
133   if (xid==VC_NOCTX && optind==argc) {
134     WRITE_MSG(2, "Without a program, '--xid' must be used; try '--help' for more information\n");
135     exit(wrapper_exit_code);
136   }
137
138   if (sched.set_mask==0 && optind==argc) {
139     WRITE_MSG(2, "Neither an option nor a program was specified; try '--help' for more information\n");
140     exit(wrapper_exit_code);
141   }
142
143   if (xid==VC_NOCTX)
144     xid = Evc_get_task_xid(0);
145
146   if (sched.set_mask!=0 && vc_set_sched(xid, &sched)==-1) {
147     perror("vc_set_sched()");
148     exit(255);
149   }
150
151   if (optind<argc)
152     EexecvpD(argv[optind],argv+optind);
153
154   return EXIT_SUCCESS;
155 }