xref: /titanic_50/usr/src/cmd/stat/common/common.c (revision d6114e2d100d9ec3b45f9968d45ac2e3a0827af0)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include "statcommon.h"
27 
28 #include <stdarg.h>
29 #include <signal.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <stdlib.h>
33 
34 extern char *cmdname;
35 extern int caught_cont;
36 
37 /*PRINTFLIKE2*/
38 void
39 fail(int do_perror, char *message, ...)
40 {
41 	va_list args;
42 	int save_errno = errno;
43 
44 	va_start(args, message);
45 	(void) fprintf(stderr, "%s: ", cmdname);
46 	(void) vfprintf(stderr, message, args);
47 	va_end(args);
48 	if (do_perror)
49 		(void) fprintf(stderr, ": %s", strerror(save_errno));
50 	(void) fprintf(stderr, "\n");
51 	exit(2);
52 }
53 
54 /*
55  * Sleep until *wakeup + interval, keeping cadence where desired
56  *
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
61  */
62 void
63 sleep_until(hrtime_t *wakeup, hrtime_t interval, int forever,
64     int *caught_cont)
65 {
66 	hrtime_t now, pause, pause_left;
67 	struct timespec pause_tv;
68 	int status;
69 
70 	now = gethrtime();
71 	pause = *wakeup + interval - now;
72 
73 	if (pause <= 0 || pause < (interval / 4))
74 		if (forever || *caught_cont) {
75 			/* Reset our cadence (see comment below) */
76 			*wakeup = now + interval;
77 			pause = interval;
78 		} else {
79 			/*
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
88 			 * interval this time.
89 			 */
90 			pause = interval / 2;
91 			*wakeup += interval;
92 		}
93 	else
94 		*wakeup += interval;
95 	if (pause < 1000)
96 		/* Near enough */
97 		return;
98 
99 	/* Now do the actual sleep */
100 	pause_left = pause;
101 	do {
102 		pause_tv.tv_sec = pause_left / NANOSEC;
103 		pause_tv.tv_nsec = pause_left % NANOSEC;
104 		status = nanosleep(&pause_tv, (struct timespec *)NULL);
105 		if (status < 0)
106 			if (errno == EINTR) {
107 				now = gethrtime();
108 				pause_left = *wakeup - now;
109 				if (pause_left < 1000)
110 					/* Near enough */
111 					return;
112 			} else {
113 				fail(1, "nanosleep failed");
114 			}
115 	} while (status != 0);
116 }
117 
118 /*
119  * Signal handler - so we can be aware of SIGCONT
120  */
121 void
122 cont_handler(int sig_number)
123 {
124 	/* Re-set the signal handler */
125 	(void) signal(sig_number, cont_handler);
126 	caught_cont = 1;
127 }
128