100c76d6fStc /*
200c76d6fStc * CDDL HEADER START
300c76d6fStc *
400c76d6fStc * The contents of this file are subject to the terms of the
500c76d6fStc * Common Development and Distribution License (the "License").
600c76d6fStc * You may not use this file except in compliance with the License.
700c76d6fStc *
800c76d6fStc * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
900c76d6fStc * or http://www.opensolaris.org/os/licensing.
1000c76d6fStc * See the License for the specific language governing permissions
1100c76d6fStc * and limitations under the License.
1200c76d6fStc *
1300c76d6fStc * When distributing Covered Code, include this CDDL HEADER in each
1400c76d6fStc * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1500c76d6fStc * If applicable, add the following below this CDDL HEADER, with the
1600c76d6fStc * fields enclosed by brackets "[]" replaced with your own identifying
1700c76d6fStc * information: Portions Copyright [yyyy] [name of copyright owner]
1800c76d6fStc *
1900c76d6fStc * CDDL HEADER END
2000c76d6fStc */
2100c76d6fStc /*
22*4944376cSJohn Levon * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2300c76d6fStc * Use is subject to license terms.
2400c76d6fStc */
2500c76d6fStc
2600c76d6fStc #include "statcommon.h"
2700c76d6fStc
2800c76d6fStc #include <stdarg.h>
2900c76d6fStc #include <signal.h>
3000c76d6fStc #include <errno.h>
3100c76d6fStc #include <string.h>
3200c76d6fStc #include <stdlib.h>
3300c76d6fStc
3400c76d6fStc extern char *cmdname;
3500c76d6fStc extern int caught_cont;
3600c76d6fStc
3700c76d6fStc /*PRINTFLIKE2*/
3800c76d6fStc void
fail(int do_perror,char * message,...)3900c76d6fStc fail(int do_perror, char *message, ...)
4000c76d6fStc {
4100c76d6fStc va_list args;
4200c76d6fStc int save_errno = errno;
4300c76d6fStc
4400c76d6fStc va_start(args, message);
4500c76d6fStc (void) fprintf(stderr, "%s: ", cmdname);
4600c76d6fStc (void) vfprintf(stderr, message, args);
4700c76d6fStc va_end(args);
4800c76d6fStc if (do_perror)
4900c76d6fStc (void) fprintf(stderr, ": %s", strerror(save_errno));
5000c76d6fStc (void) fprintf(stderr, "\n");
5100c76d6fStc exit(2);
5200c76d6fStc }
5300c76d6fStc
5400c76d6fStc /*
5500c76d6fStc * Sleep until *wakeup + interval, keeping cadence where desired
5600c76d6fStc *
5700c76d6fStc * *wakeup - The time we last wanted to wake up. Updated.
5800c76d6fStc * interval - We want to sleep until *wakeup + interval
5900c76d6fStc * forever - Running for infinite periods, so cadence not important
6000c76d6fStc * *caught_cont - Global set by signal handler if we got a SIGCONT
6100c76d6fStc */
6200c76d6fStc void
sleep_until(hrtime_t * wakeup,hrtime_t interval,int forever,int * caught_cont)6300c76d6fStc sleep_until(hrtime_t *wakeup, hrtime_t interval, int forever,
6400c76d6fStc int *caught_cont)
6500c76d6fStc {
6600c76d6fStc hrtime_t now, pause, pause_left;
6700c76d6fStc struct timespec pause_tv;
6800c76d6fStc int status;
6900c76d6fStc
7000c76d6fStc now = gethrtime();
7100c76d6fStc pause = *wakeup + interval - now;
7200c76d6fStc
7300c76d6fStc if (pause <= 0 || pause < (interval / 4))
7400c76d6fStc if (forever || *caught_cont) {
7500c76d6fStc /* Reset our cadence (see comment below) */
7600c76d6fStc *wakeup = now + interval;
7700c76d6fStc pause = interval;
7800c76d6fStc } else {
7900c76d6fStc /*
8000c76d6fStc * If we got here, then the time between the
8100c76d6fStc * output we just did, and the scheduled time
8200c76d6fStc * for the next output is < 1/4 of our requested
8300c76d6fStc * interval AND the number of intervals has been
8400c76d6fStc * requested AND we have never caught a SIGCONT
8500c76d6fStc * (so we have never been suspended). In this
8600c76d6fStc * case, we'll try to stay to the desired
8700c76d6fStc * cadence, and we will pause for 1/2 the normal
8800c76d6fStc * interval this time.
8900c76d6fStc */
9000c76d6fStc pause = interval / 2;
9100c76d6fStc *wakeup += interval;
9200c76d6fStc }
9300c76d6fStc else
9400c76d6fStc *wakeup += interval;
9500c76d6fStc if (pause < 1000)
9600c76d6fStc /* Near enough */
9700c76d6fStc return;
9800c76d6fStc
9900c76d6fStc /* Now do the actual sleep */
10000c76d6fStc pause_left = pause;
10100c76d6fStc do {
10200c76d6fStc pause_tv.tv_sec = pause_left / NANOSEC;
10300c76d6fStc pause_tv.tv_nsec = pause_left % NANOSEC;
10400c76d6fStc status = nanosleep(&pause_tv, (struct timespec *)NULL);
10500c76d6fStc if (status < 0)
10600c76d6fStc if (errno == EINTR) {
10700c76d6fStc now = gethrtime();
10800c76d6fStc pause_left = *wakeup - now;
10900c76d6fStc if (pause_left < 1000)
11000c76d6fStc /* Near enough */
11100c76d6fStc return;
11200c76d6fStc } else {
11300c76d6fStc fail(1, "nanosleep failed");
11400c76d6fStc }
11500c76d6fStc } while (status != 0);
11600c76d6fStc }
11700c76d6fStc
11800c76d6fStc /*
11900c76d6fStc * Signal handler - so we can be aware of SIGCONT
12000c76d6fStc */
12100c76d6fStc void
cont_handler(int sig_number)12200c76d6fStc cont_handler(int sig_number)
12300c76d6fStc {
12400c76d6fStc /* Re-set the signal handler */
12500c76d6fStc (void) signal(sig_number, cont_handler);
12600c76d6fStc caught_cont = 1;
12700c76d6fStc }
128