00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc/*
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * CDDL HEADER START
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc *
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * The contents of this file are subject to the terms of the
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * Common Development and Distribution License (the "License").
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * You may not use this file except in compliance with the License.
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc *
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * or http://www.opensolaris.org/os/licensing.
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * See the License for the specific language governing permissions
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * and limitations under the License.
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc *
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * When distributing Covered Code, include this CDDL HEADER in each
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * If applicable, add the following below this CDDL HEADER, with the
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * fields enclosed by brackets "[]" replaced with your own identifying
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * information: Portions Copyright [yyyy] [name of copyright owner]
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc *
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * CDDL HEADER END
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc */
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc/*
4944376cd5de3dcd3b4feeaad9cbedbc024d1474John Levon * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * Use is subject to license terms.
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc */
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc#include "statcommon.h"
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc#include <stdarg.h>
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc#include <signal.h>
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc#include <errno.h>
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc#include <string.h>
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc#include <stdlib.h>
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tcextern char *cmdname;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tcextern int caught_cont;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc/*PRINTFLIKE2*/
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tcvoid
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tcfail(int do_perror, char *message, ...)
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc{
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc va_list args;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc int save_errno = errno;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc va_start(args, message);
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc (void) fprintf(stderr, "%s: ", cmdname);
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc (void) vfprintf(stderr, message, args);
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc va_end(args);
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc if (do_perror)
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc (void) fprintf(stderr, ": %s", strerror(save_errno));
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc (void) fprintf(stderr, "\n");
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc exit(2);
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc}
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc/*
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * Sleep until *wakeup + interval, keeping cadence where desired
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc *
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * *wakeup - The time we last wanted to wake up. Updated.
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * interval - We want to sleep until *wakeup + interval
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * forever - Running for infinite periods, so cadence not important
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * *caught_cont - Global set by signal handler if we got a SIGCONT
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc */
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tcvoid
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tcsleep_until(hrtime_t *wakeup, hrtime_t interval, int forever,
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc int *caught_cont)
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc{
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc hrtime_t now, pause, pause_left;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc struct timespec pause_tv;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc int status;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc now = gethrtime();
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc pause = *wakeup + interval - now;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc if (pause <= 0 || pause < (interval / 4))
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc if (forever || *caught_cont) {
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc /* Reset our cadence (see comment below) */
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc *wakeup = now + interval;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc pause = interval;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc } else {
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc /*
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * If we got here, then the time between the
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * output we just did, and the scheduled time
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * for the next output is < 1/4 of our requested
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * interval AND the number of intervals has been
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * requested AND we have never caught a SIGCONT
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * (so we have never been suspended). In this
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * case, we'll try to stay to the desired
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * cadence, and we will pause for 1/2 the normal
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * interval this time.
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc */
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc pause = interval / 2;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc *wakeup += interval;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc }
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc else
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc *wakeup += interval;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc if (pause < 1000)
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc /* Near enough */
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc return;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc /* Now do the actual sleep */
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc pause_left = pause;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc do {
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc pause_tv.tv_sec = pause_left / NANOSEC;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc pause_tv.tv_nsec = pause_left % NANOSEC;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc status = nanosleep(&pause_tv, (struct timespec *)NULL);
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc if (status < 0)
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc if (errno == EINTR) {
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc now = gethrtime();
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc pause_left = *wakeup - now;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc if (pause_left < 1000)
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc /* Near enough */
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc return;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc } else {
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc fail(1, "nanosleep failed");
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc }
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc } while (status != 0);
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc}
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc/*
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc * Signal handler - so we can be aware of SIGCONT
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc */
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tcvoid
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tccont_handler(int sig_number)
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc{
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc /* Re-set the signal handler */
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc (void) signal(sig_number, cont_handler);
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc caught_cont = 1;
00c76d6fcc0e3d5821ed5ac5165f1835f8151454tc}