4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include "statcommon.h"
35 extern int caught_cont
;
39 fail(int do_perror
, char *message
, ...)
42 int save_errno
= errno
;
44 va_start(args
, message
);
45 (void) fprintf(stderr
, "%s: ", cmdname
);
46 (void) vfprintf(stderr
, message
, args
);
49 (void) fprintf(stderr
, ": %s", strerror(save_errno
));
50 (void) fprintf(stderr
, "\n");
55 * Sleep until *wakeup + interval, keeping cadence where desired
57 * *wakeup - The time we last wanted to wake up. Updated.
58 * interval - We want to sleep until *wakeup + interval
59 * forever - Running for infinite periods, so cadence not important
60 * *caught_cont - Global set by signal handler if we got a SIGCONT
63 sleep_until(hrtime_t
*wakeup
, hrtime_t interval
, int forever
,
66 hrtime_t now
, pause
, pause_left
;
67 struct timespec pause_tv
;
71 pause
= *wakeup
+ interval
- now
;
73 if (pause
<= 0 || pause
< (interval
/ 4))
74 if (forever
|| *caught_cont
) {
75 /* Reset our cadence (see comment below) */
76 *wakeup
= now
+ interval
;
80 * If we got here, then the time between the
81 * output we just did, and the scheduled time
82 * for the next output is < 1/4 of our requested
83 * interval AND the number of intervals has been
84 * requested AND we have never caught a SIGCONT
85 * (so we have never been suspended). In this
86 * case, we'll try to stay to the desired
87 * cadence, and we will pause for 1/2 the normal
99 /* Now do the actual sleep */
102 pause_tv
.tv_sec
= pause_left
/ NANOSEC
;
103 pause_tv
.tv_nsec
= pause_left
% NANOSEC
;
104 status
= nanosleep(&pause_tv
, NULL
);
106 if (errno
== EINTR
) {
108 pause_left
= *wakeup
- now
;
109 if (pause_left
< 1000)
113 fail(1, "nanosleep failed");
115 } while (status
!= 0);
119 * Signal handler - so we can be aware of SIGCONT
122 cont_handler(int sig_number
)
124 /* Re-set the signal handler */
125 (void) signal(sig_number
, cont_handler
);