1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23*0Sstevel@tonic-gate /* All Rights Reserved */
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28*0Sstevel@tonic-gate * Use is subject to license terms.
29*0Sstevel@tonic-gate */
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <stdarg.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <unistd.h>
37*0Sstevel@tonic-gate #include <sys/types.h>
38*0Sstevel@tonic-gate #include <sys/time.h>
39*0Sstevel@tonic-gate #include <limits.h>
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #include "dispadmin.h"
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate * Utility functions for dispadmin command.
46*0Sstevel@tonic-gate */
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate void
fatalerr(const char * format,...)50*0Sstevel@tonic-gate fatalerr(const char *format, ...)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate va_list ap;
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate (void) va_start(ap, format);
55*0Sstevel@tonic-gate (void) vfprintf(stderr, format, ap);
56*0Sstevel@tonic-gate va_end(ap);
57*0Sstevel@tonic-gate exit(1);
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate /*
62*0Sstevel@tonic-gate * hrtconvert() returns the interval specified by htp as a single
63*0Sstevel@tonic-gate * value in resolution htp->hrt_res. Returns -1 on overflow.
64*0Sstevel@tonic-gate */
65*0Sstevel@tonic-gate long
hrtconvert(hrtimer_t * htp)66*0Sstevel@tonic-gate hrtconvert(hrtimer_t *htp)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate long sum;
69*0Sstevel@tonic-gate long product;
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate product = htp->hrt_secs * htp->hrt_res;
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate if (product / htp->hrt_res == htp->hrt_secs) {
74*0Sstevel@tonic-gate sum = product + htp->hrt_rem;
75*0Sstevel@tonic-gate if (sum - htp->hrt_rem == product) {
76*0Sstevel@tonic-gate return (sum);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate return (-1);
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate * The following routine was removed from libc (libc/port/gen/hrtnewres.c).
84*0Sstevel@tonic-gate * It has also been added to priocntl, so if you fix it here, you should
85*0Sstevel@tonic-gate * also probably fix it there. In the long term, this should be recoded to
86*0Sstevel@tonic-gate * not be hrt'ish.
87*0Sstevel@tonic-gate */
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate /*
90*0Sstevel@tonic-gate * Convert interval expressed in htp->hrt_res to new_res.
91*0Sstevel@tonic-gate *
92*0Sstevel@tonic-gate * Calculate: (interval * new_res) / htp->hrt_res rounding off as
93*0Sstevel@tonic-gate * specified by round.
94*0Sstevel@tonic-gate *
95*0Sstevel@tonic-gate * Note: All args are assumed to be positive. If
96*0Sstevel@tonic-gate * the last divide results in something bigger than
97*0Sstevel@tonic-gate * a long, then -1 is returned instead.
98*0Sstevel@tonic-gate */
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate int
_hrtnewres(hrtimer_t * htp,ulong_t new_res,long round)101*0Sstevel@tonic-gate _hrtnewres(hrtimer_t *htp, ulong_t new_res, long round)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate long interval;
104*0Sstevel@tonic-gate longlong_t dint;
105*0Sstevel@tonic-gate longlong_t dto_res;
106*0Sstevel@tonic-gate longlong_t drem;
107*0Sstevel@tonic-gate longlong_t dfrom_res;
108*0Sstevel@tonic-gate longlong_t prod;
109*0Sstevel@tonic-gate longlong_t quot;
110*0Sstevel@tonic-gate long numerator;
111*0Sstevel@tonic-gate long result;
112*0Sstevel@tonic-gate ulong_t modulus;
113*0Sstevel@tonic-gate ulong_t twomodulus;
114*0Sstevel@tonic-gate long temp;
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate if (htp->hrt_res == 0 || new_res == 0 ||
117*0Sstevel@tonic-gate new_res > NANOSEC || htp->hrt_rem < 0)
118*0Sstevel@tonic-gate return (-1);
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate if (htp->hrt_rem >= htp->hrt_res) {
121*0Sstevel@tonic-gate htp->hrt_secs += htp->hrt_rem / htp->hrt_res;
122*0Sstevel@tonic-gate htp->hrt_rem = htp->hrt_rem % htp->hrt_res;
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate interval = htp->hrt_rem;
126*0Sstevel@tonic-gate if (interval == 0) {
127*0Sstevel@tonic-gate htp->hrt_res = new_res;
128*0Sstevel@tonic-gate return (0);
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate /*
132*0Sstevel@tonic-gate * Try to do the calculations in single precision first
133*0Sstevel@tonic-gate * (for speed). If they overflow, use double precision.
134*0Sstevel@tonic-gate * What we want to compute is:
135*0Sstevel@tonic-gate *
136*0Sstevel@tonic-gate * (interval * new_res) / hrt->hrt_res
137*0Sstevel@tonic-gate */
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate numerator = interval * new_res;
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate if (numerator / new_res == interval) {
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate /*
144*0Sstevel@tonic-gate * The above multiply didn't give overflow since
145*0Sstevel@tonic-gate * the division got back the original number. Go
146*0Sstevel@tonic-gate * ahead and compute the result.
147*0Sstevel@tonic-gate */
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate result = numerator / htp->hrt_res;
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate /*
152*0Sstevel@tonic-gate * For HRT_RND, compute the value of:
153*0Sstevel@tonic-gate *
154*0Sstevel@tonic-gate * (interval * new_res) % htp->hrt_res
155*0Sstevel@tonic-gate *
156*0Sstevel@tonic-gate * If it is greater than half of the htp->hrt_res,
157*0Sstevel@tonic-gate * then rounding increases the result by 1.
158*0Sstevel@tonic-gate *
159*0Sstevel@tonic-gate * For HRT_RNDUP, we increase the result by 1 if:
160*0Sstevel@tonic-gate *
161*0Sstevel@tonic-gate * result * htp->hrt_res != numerator
162*0Sstevel@tonic-gate *
163*0Sstevel@tonic-gate * because this tells us we truncated when calculating
164*0Sstevel@tonic-gate * result above.
165*0Sstevel@tonic-gate *
166*0Sstevel@tonic-gate * We also check for overflow when incrementing result
167*0Sstevel@tonic-gate * although this is extremely rare.
168*0Sstevel@tonic-gate */
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate if (round == HRT_RND) {
171*0Sstevel@tonic-gate modulus = numerator - result * htp->hrt_res;
172*0Sstevel@tonic-gate if ((twomodulus = 2 * modulus) / 2 == modulus) {
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate /*
175*0Sstevel@tonic-gate * No overflow (if we overflow in calculation
176*0Sstevel@tonic-gate * of twomodulus we fall through and use
177*0Sstevel@tonic-gate * double precision).
178*0Sstevel@tonic-gate */
179*0Sstevel@tonic-gate if (twomodulus >= htp->hrt_res) {
180*0Sstevel@tonic-gate temp = result + 1;
181*0Sstevel@tonic-gate if (temp - 1 == result)
182*0Sstevel@tonic-gate result++;
183*0Sstevel@tonic-gate else
184*0Sstevel@tonic-gate return (-1);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate htp->hrt_res = new_res;
187*0Sstevel@tonic-gate htp->hrt_rem = result;
188*0Sstevel@tonic-gate return (0);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate } else if (round == HRT_RNDUP) {
191*0Sstevel@tonic-gate if (result * htp->hrt_res != numerator) {
192*0Sstevel@tonic-gate temp = result + 1;
193*0Sstevel@tonic-gate if (temp - 1 == result)
194*0Sstevel@tonic-gate result++;
195*0Sstevel@tonic-gate else
196*0Sstevel@tonic-gate return (-1);
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate htp->hrt_res = new_res;
199*0Sstevel@tonic-gate htp->hrt_rem = result;
200*0Sstevel@tonic-gate return (0);
201*0Sstevel@tonic-gate } else { /* round == HRT_TRUNC */
202*0Sstevel@tonic-gate htp->hrt_res = new_res;
203*0Sstevel@tonic-gate htp->hrt_rem = result;
204*0Sstevel@tonic-gate return (0);
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate }
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gate /*
209*0Sstevel@tonic-gate * We would get overflow doing the calculation is
210*0Sstevel@tonic-gate * single precision so do it the slow but careful way.
211*0Sstevel@tonic-gate *
212*0Sstevel@tonic-gate * Compute the interval times the resolution we are
213*0Sstevel@tonic-gate * going to.
214*0Sstevel@tonic-gate */
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate dint = interval;
217*0Sstevel@tonic-gate dto_res = new_res;
218*0Sstevel@tonic-gate prod = dint * dto_res;
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate /*
221*0Sstevel@tonic-gate * For HRT_RND the result will be equal to:
222*0Sstevel@tonic-gate *
223*0Sstevel@tonic-gate * ((interval * new_res) + htp->hrt_res / 2) / htp->hrt_res
224*0Sstevel@tonic-gate *
225*0Sstevel@tonic-gate * and for HRT_RNDUP we use:
226*0Sstevel@tonic-gate *
227*0Sstevel@tonic-gate * ((interval * new_res) + htp->hrt_res - 1) / htp->hrt_res
228*0Sstevel@tonic-gate *
229*0Sstevel@tonic-gate * This is a different but equivalent way of rounding.
230*0Sstevel@tonic-gate */
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate if (round == HRT_RND) {
233*0Sstevel@tonic-gate drem = htp->hrt_res / 2;
234*0Sstevel@tonic-gate prod = prod + drem;
235*0Sstevel@tonic-gate } else if (round == HRT_RNDUP) {
236*0Sstevel@tonic-gate drem = htp->hrt_res - 1;
237*0Sstevel@tonic-gate prod = prod + drem;
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate dfrom_res = htp->hrt_res;
241*0Sstevel@tonic-gate quot = prod / dfrom_res;
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate /*
244*0Sstevel@tonic-gate * If the quotient won't fit in a long, then we have
245*0Sstevel@tonic-gate * overflow. Otherwise, return the result.
246*0Sstevel@tonic-gate */
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate if (quot > UINT_MAX) {
249*0Sstevel@tonic-gate return (-1);
250*0Sstevel@tonic-gate } else {
251*0Sstevel@tonic-gate htp->hrt_res = new_res;
252*0Sstevel@tonic-gate htp->hrt_rem = (int)quot;
253*0Sstevel@tonic-gate return (0);
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate }
256