xref: /titanic_50/usr/src/cmd/stat/common/common.c (revision 8ef10d209f1443865a8ce5b1eb59838b64a64e67)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include "statcommon.h"
29 
30 #include <stdarg.h>
31 #include <signal.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <stdlib.h>
35 
36 extern char *cmdname;
37 extern int caught_cont;
38 
39 /*PRINTFLIKE2*/
40 void
41 fail(int do_perror, char *message, ...)
42 {
43 	va_list args;
44 	int save_errno = errno;
45 
46 	va_start(args, message);
47 	(void) fprintf(stderr, "%s: ", cmdname);
48 	(void) vfprintf(stderr, message, args);
49 	va_end(args);
50 	if (do_perror)
51 		(void) fprintf(stderr, ": %s", strerror(save_errno));
52 	(void) fprintf(stderr, "\n");
53 	exit(2);
54 }
55 
56 /*
57  * Sleep until *wakeup + interval, keeping cadence where desired
58  *
59  * *wakeup -	The time we last wanted to wake up. Updated.
60  * interval -	We want to sleep until *wakeup + interval
61  * forever -	Running for infinite periods, so cadence not important
62  * *caught_cont - Global set by signal handler if we got a SIGCONT
63  */
64 void
65 sleep_until(hrtime_t *wakeup, hrtime_t interval, int forever,
66     int *caught_cont)
67 {
68 	hrtime_t now, pause, pause_left;
69 	struct timespec pause_tv;
70 	int status;
71 
72 	now = gethrtime();
73 	pause = *wakeup + interval - now;
74 
75 	if (pause <= 0 || pause < (interval / 4))
76 		if (forever || *caught_cont) {
77 			/* Reset our cadence (see comment below) */
78 			*wakeup = now + interval;
79 			pause = interval;
80 		} else {
81 			/*
82 			 * If we got here, then the time between the
83 			 * output we just did, and the scheduled time
84 			 * for the next output is < 1/4 of our requested
85 			 * interval AND the number of intervals has been
86 			 * requested AND we have never caught a SIGCONT
87 			 * (so we have never been suspended).  In this
88 			 * case, we'll try to stay to the desired
89 			 * cadence, and we will pause for 1/2 the normal
90 			 * interval this time.
91 			 */
92 			pause = interval / 2;
93 			*wakeup += interval;
94 		}
95 	else
96 		*wakeup += interval;
97 	if (pause < 1000)
98 		/* Near enough */
99 		return;
100 
101 	/* Now do the actual sleep */
102 	pause_left = pause;
103 	do {
104 		pause_tv.tv_sec = pause_left / NANOSEC;
105 		pause_tv.tv_nsec = pause_left % NANOSEC;
106 		status = nanosleep(&pause_tv, (struct timespec *)NULL);
107 		if (status < 0)
108 			if (errno == EINTR) {
109 				now = gethrtime();
110 				pause_left = *wakeup - now;
111 				if (pause_left < 1000)
112 					/* Near enough */
113 					return;
114 			} else {
115 				fail(1, "nanosleep failed");
116 			}
117 	} while (status != 0);
118 }
119 
120 /*
121  * Signal handler - so we can be aware of SIGCONT
122  */
123 void
124 cont_handler(int sig_number)
125 {
126 	/* Re-set the signal handler */
127 	(void) signal(sig_number, cont_handler);
128 	caught_cont = 1;
129 }
130