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