dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / dispadmin / fssdispadmin.c
blob0a404bec62171bb21415c5596d82179318c31d92
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/fss.h>
35 #include <sys/priocntl.h>
36 #include <sys/fsspriocntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include "dispadmin.h"
45 * This file contains the class specific code implementing the fair-share
46 * scheduler dispadmin sub-command.
49 #define BASENMSZ 16
51 extern char *basename();
52 static void getadmin(), setadmin();
54 static char usage[] = "usage: dispadmin -l\n"
55 "dispadmin -c FSS -g [-r res]\n"
56 "dispadmin -c FSS -s infile\n";
58 static char basenm[BASENMSZ];
59 static char cmdpath[256];
61 int
62 main(int argc, char **argv)
64 int c;
65 int lflag, gflag, rflag, sflag;
66 ulong_t res;
67 char *infile;
68 char *endp;
70 (void) strcpy(cmdpath, argv[0]);
71 (void) strcpy(basenm, basename(argv[0]));
72 lflag = gflag = rflag = sflag = 0;
73 while ((c = getopt(argc, argv, "lc:gr:s:")) != -1) {
74 switch (c) {
76 case 'l':
77 lflag++;
78 break;
80 case 'c':
81 if (strcmp(optarg, "FSS") != 0)
82 fatalerr("error: %s executed for %s class, "
83 "%s is actually sub-command for FSS "
84 "class\n", cmdpath, optarg, cmdpath);
85 break;
87 case 'g':
88 gflag++;
89 break;
91 case 'r':
92 rflag++;
93 errno = 0;
94 res = strtoul(optarg, &endp, 10);
95 if (res == 0 || errno != 0 || *endp != '\0')
96 fatalerr("%s: Can't convert to requested "
97 "resolution\n", basenm);
98 break;
100 case 's':
101 sflag++;
102 infile = optarg;
103 break;
105 case '?':
106 fatalerr(usage);
108 default:
109 break;
113 if (lflag) {
114 if (gflag || rflag || sflag)
115 fatalerr(usage);
117 (void) printf("FSS\t(Fair Share)\n");
118 return (0);
120 } else if (gflag) {
121 if (lflag || sflag)
122 fatalerr(usage);
124 if (rflag == 0)
125 res = 1000;
127 getadmin(res);
128 return (0);
130 } else if (sflag) {
131 if (lflag || gflag || rflag)
132 fatalerr(usage);
134 setadmin(infile);
135 return (0);
137 } else {
138 fatalerr(usage);
140 return (1);
144 * Retrieve the current settings from kernel, convert the time quantum
145 * value to the resolution specified by res and print out the results.
147 static void
148 getadmin(ulong_t res)
150 pcinfo_t pcinfo;
151 pcadmin_t pcadmin;
152 fssadmin_t fssadmin;
153 hrtimer_t hrtime;
154 long fss_quantum;
156 (void) strcpy(pcinfo.pc_clname, "FSS");
157 if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
158 fatalerr("%s: Can't get FSS class ID, priocntl system call "
159 "failed (%s)\n", basenm, strerror(errno));
161 pcadmin.pc_cid = pcinfo.pc_cid;
162 pcadmin.pc_cladmin = (char *)&fssadmin;
163 fssadmin.fss_cmd = FSS_GETADMIN;
165 if (priocntl(0, 0, PC_ADMIN, (caddr_t)&pcadmin) == -1)
166 fatalerr("%s: Can't get scheduler configuration, priocntl "
167 "system call failed (%s)\n", basenm, strerror(errno));
169 hrtime.hrt_secs = 0;
170 hrtime.hrt_rem = fssadmin.fss_quantum;
171 hrtime.hrt_res = HZ;
172 if (_hrtnewres(&hrtime, res, HRT_RNDUP) == -1)
173 fatalerr("%s: Can't convert to requested resolution\n", basenm);
174 if ((fss_quantum = hrtconvert(&hrtime)) == -1)
175 fatalerr("%s: Can't express time quantum in "
176 "requested resolution,\n"
177 "try coarser resolution\n", basenm);
179 (void) printf("#\n# Fair Share Scheduler Configuration\n#\n");
180 (void) printf("RES=%ld\n", res);
181 (void) printf("#\n# Time Quantum\n#\n");
182 (void) printf("QUANTUM=%ld\n", fss_quantum);
186 * Read the scheduler settings from infile, convert the time quantum values
187 * to HZ resolution, do a little sanity checking and overwrite kernel settings
188 * with the values from the file.
190 static void
191 setadmin(char *infile)
193 int i;
194 pcinfo_t pcinfo;
195 pcadmin_t pcadmin;
196 fssadmin_t fssadmin;
197 int line;
198 ulong_t res;
199 long fss_quantum;
200 hrtimer_t hrtime;
201 FILE *fp;
202 char buf[512];
203 char *endp;
204 int nparams = 0;
206 (void) strcpy(pcinfo.pc_clname, "FSS");
207 if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
208 fatalerr("%s: Can't get FSS class ID, priocntl system call "
209 "failed (%s)\n", basenm, strerror(errno));
211 if ((fp = fopen(infile, "r")) == NULL)
212 fatalerr("%s: Can't open %s for input\n", basenm, infile);
214 for (line = 1; fgets(buf, sizeof (buf), fp) != NULL; line++) {
215 char name[512], value[512];
216 int len;
218 if (buf[0] == '#' || buf[0] == '\n')
219 continue;
221 * Look for "name=value", with optional whitespaces on either
222 * side, terminated by a newline, and consuming the whole line.
224 /* LINTED - unbounded string specifier */
225 if (sscanf(buf, " %[^=]=%s \n%n", name, value, &len) == 2 &&
226 name[0] != '\0' && value[0] != '\0' && len == strlen(buf)) {
228 if (strcmp(name, "RES") == 0) {
229 errno = 0;
230 i = (int)strtol(value, &endp, 10);
231 if (errno != 0 || endp == value ||
232 i < 0 || *endp != '\0')
233 fatalerr("%s, line %d: illegal "
234 "resolution value\n", infile, line);
235 else
236 res = i;
237 nparams++;
238 } else if (strcmp(name, "QUANTUM") == 0) {
239 errno = 0;
240 i = (int)strtol(value, &endp, 10);
241 if (errno != 0 || endp == value ||
242 i < 0 || *endp != '\0')
243 fatalerr("%s, line %d: illegal time "
244 "quantum value\n", infile, line);
245 else
246 fss_quantum = i;
247 nparams++;
248 } else {
249 fatalerr("%s, line %d: invalid token\n",
250 infile, line);
252 } else {
253 fatalerr("%s, line %d: syntax error\n", infile, line);
256 if (line == 1 || nparams < 2)
257 fatalerr("cannot read settings from %s\n", infile);
258 if (res != HZ) {
259 hrtime.hrt_secs = 0;
260 hrtime.hrt_rem = fss_quantum;
261 hrtime.hrt_res = res;
262 if (_hrtnewres(&hrtime, HZ, HRT_RNDUP) == -1)
263 fatalerr("%s: Can't convert specified "
264 "resolution to ticks\n", basenm);
265 if ((fssadmin.fss_quantum = hrtconvert(&hrtime)) == -1)
266 fatalerr("%s, line %d: time quantum value out of "
267 "valid range\n", infile, line);
268 } else {
269 fssadmin.fss_quantum = (short)fss_quantum;
272 pcadmin.pc_cid = pcinfo.pc_cid;
273 pcadmin.pc_cladmin = (char *)&fssadmin;
274 fssadmin.fss_cmd = FSS_SETADMIN;
276 if (priocntl(0, 0, PC_ADMIN, (caddr_t)&pcadmin) == -1)
277 fatalerr("%s: Can't set scheduler parameters, priocntl "
278 "system call failed (%s)\n", basenm, strerror(errno));