summaryrefslogtreecommitdiffstats
path: root/patches/procps-3.2.8/0042-watch_precision_time.patch
blob: a5c047dc8aee4ee9b7e9d937d8325b3db849d6c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
From 6c6919c283c78b453320c344889de5892449b11c Mon Sep 17 00:00:00 2001
From: http://ftp.de.debian.org/debian/pool/main/p/procps/procps_3.2.8-8.debian.tar.gz <info@debian.org>
Date: Fri, 19 Mar 2010 21:58:46 +0100
Subject: [PATCH 42/70] watch_precision_time

===================================================================
---
 watch.1 |   54 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 watch.c |   31 +++++++++++++++++++++++++++++--
 2 files changed, 80 insertions(+), 5 deletions(-)

diff --git a/watch.1 b/watch.1
index 802ab84..40e6c8e 100644
--- a/watch.1
+++ b/watch.1
@@ -4,7 +4,7 @@ watch \- execute a program periodically, showing output fullscreen
 .SH SYNOPSIS
 .na
 .B watch
-.RB [ \-bdehvtx ]
+.RB [ \-bdehpvtx ]
 .RB [ \-n
 .IR seconds ]
 .RB [ \-\-beep ]
@@ -14,6 +14,7 @@ watch \- execute a program periodically, showing output fullscreen
 .RB [ \-\-help ]
 .RB [ \-\-interval=\fIseconds\fP]
 .RB [ \-\-no\-title ]
+.RB [ \-\-precise ]
 .RB [ \-\-version ]
 .I command
 .SH DESCRIPTION
@@ -27,7 +28,24 @@ every 2 seconds; use
 .B \-n
 or
 .B \-\-interval
-to specify a different interval.
+to specify a different interval. Normally, this interval is interpreted
+as the amout of time between the completion of one run of
+.I command
+and the beginning of the next run. However, with the
+.I \-p
+or
+.I \-\-precise
+option, you can make
+.BR watch
+attempt to run
+.I command
+every
+.I interval
+seconds. Try it with
+.B ntptime
+and notice how the fractional seconds stays
+(nearly) the same, as opposed to normal mode where they continuously
+increase.
 .PP
 The
 .B \-d
@@ -97,11 +115,21 @@ watch echo '$$'
 .br
 watch echo "'"'$$'"'"
 .PP
+To see the effect of precision time keeping, try adding
+.I \-p
+to
+.IP
+watch \-n 10 sleep 1
+.PP
 You can watch for your administrator to install the latest kernel with
 .IP
 watch uname \-r
 .PP
-(Just kidding.)
+(Note that
+.I \-p
+isn't guaranteed to work across reboots, especially in the face of
+.B ntpdate
+or other bootup time-changing mechanisms)
 .SH BUGS
 Upon terminal resize, the screen will not be correctly repainted until the
 next scheduled update.  All
@@ -110,6 +138,22 @@ highlighting is lost on that update as well.
 .PP
 Non-printing characters are stripped from program output.  Use "cat -v" as
 part of the command pipeline if you want to see them.
+.PP
+.I \-\-precise
+mode doesn't yet have advanced temporal distortion technology to
+compensate for a
+.I command
+that takes more than
+.I interval
+seconds to execute.
+.B watch
+also can get into a state where it rapid-fires as many executions of
+.I command
+as it can to catch up from a previous executions running longer than
+.I interval
+(for example,
+.B netstat
+taking ages on a DNS lookup).
 .SH AUTHORS
 The original
 .B watch
@@ -117,3 +161,7 @@ was written by Tony Rems <rembo@unisoft.com> in 1991, with mods and
 corrections by Francois Pinard.  It was reworked and new features added by
 Mike Coleman <mkc@acm.org> in 1999.  The beep, exec, and error handling
 features were added by Morty Abzug <morty@frakir.org> in 2008.
+On a not so dark and stormy morning
+in March of 2003, Anthony DeRobertis <asd@suespammers.org> got sick of
+his watches that should update every minute eventually updating many
+seconds after the minute started, and added microsecond precision.
diff --git a/watch.c b/watch.c
index acb152f..c7369b6 100644
--- a/watch.c
+++ b/watch.c
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/ioctl.h>
+#include <sys/time.h>
 #include <time.h>
 #include <unistd.h>
 #include <termios.h>
@@ -39,13 +40,14 @@ static struct option longopts[] = {
 	{"beep", no_argument, 0, 'b'},
 	{"errexit", no_argument, 0, 'e'},
 	{"exec", no_argument, 0, 'x'},
+	{"precise", no_argument, 0, 'p'},
 	{"no-title", no_argument, 0, 't'},
 	{"version", no_argument, 0, 'v'},
 	{0, 0, 0, 0}
 };
 
 static char usage[] =
-    "Usage: %s [-bdhntvx] [--beep] [--differences[=cumulative]] [--exec] [--help] [--interval=<n>] [--no-title] [--version] <command>\n";
+    "Usage: %s [-bdhnptvx] [--beep] [--differences[=cumulative]] [--exec] [--help] [--interval=<n>] [--no-title] [--version] <command>\n";
 
 static char *progname;
 
@@ -54,6 +56,7 @@ static int height = 24, width = 80;
 static int screen_size_changed = 0;
 static int first_screen = 1;
 static int show_title = 2;  // number of lines used, 2 or 0
+static int precise_timekeeping = 0;
 
 #define min(x,y) ((x) > (y) ? (y) : (x))
 
@@ -138,6 +141,15 @@ get_terminal_size(void)
 	}
 }
 
+/* get current time in usec */
+typedef unsigned long long watch_usec_t;
+#define USECS_PER_SEC (1000000ull)
+watch_usec_t get_time_usec() {
+	struct timeval now;
+	gettimeofday(&now, NULL);
+	return USECS_PER_SEC*now.tv_sec + now.tv_usec;
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -152,6 +164,8 @@ main(int argc, char *argv[])
 	char *command;
 	char **command_argv;
 	int command_length = 0;	/* not including final \0 */
+    watch_usec_t next_loop; /* next loop time in us, used for precise time
+                               keeping only */
 	int pipefd[2];
 	int status;
 	pid_t child;
@@ -159,7 +173,7 @@ main(int argc, char *argv[])
 	setlocale(LC_ALL, "");
 	progname = argv[0];
 
-	while ((optc = getopt_long(argc, argv, "+bed::hn:vtx", longopts, (int *) 0))
+	while ((optc = getopt_long(argc, argv, "+bed::hn:pvtx", longopts, (int *) 0))
 	       != EOF) {
 		switch (optc) {
 		case 'b':
@@ -194,6 +208,9 @@ main(int argc, char *argv[])
 					interval = ~0u/1000000;
 			}
 			break;
+		case 'p':
+			precise_timekeeping = 1;
+			break;
 		case 'v':
 			option_version = 1;
 			break;
@@ -217,6 +234,7 @@ main(int argc, char *argv[])
 		fputs("  -e, --errexit\t\t\t\texit watch if the command has a non-zero exit\n", stderr);
 		fputs("  -h, --help\t\t\t\tprint a summary of the options\n", stderr);
 		fputs("  -n, --interval=<seconds>\t\tseconds to wait between updates\n", stderr);
+        fputs("  -p, --precise\t\t\t\tprecise timing, ignore command run time\n", stderr);
 		fputs("  -v, --version\t\t\t\tprint the version number\n", stderr);
 		fputs("  -t, --no-title\t\t\tturns off showing the header\n", stderr);
 		fputs("  -x, --exec\t\t\t\tpass command to exec instead of sh\n", stderr);
@@ -256,6 +274,9 @@ main(int argc, char *argv[])
 	noecho();
 	cbreak();
 
+	if (precise_timekeeping)
+		next_loop = get_time_usec();
+
 	for (;;) {
 		time_t t = time(NULL);
 		char *ts = ctime(&t);
@@ -400,6 +421,12 @@ main(int argc, char *argv[])
 
 		first_screen = 0;
 		refresh();
+		if (precise_timekeeping) {
+			watch_usec_t cur_time = get_time_usec();
+			next_loop += USECS_PER_SEC*interval;
+			if (cur_time < next_loop)
+				usleep(next_loop - cur_time);
+		} else
 		usleep(interval * 1000000);
 	}
 
-- 
1.7.0