From 26a30376d8fc8a4ffe868b4e178f416dfefda924 Mon Sep 17 00:00:00 2001
From: Nadav Har'El <nyh@cloudius-systems.com>
Date: Sat, 14 Sep 2013 16:01:21 +0300
Subject: [PATCH] Change "hz" to fix poll() premature timeout

msleep() measure times in units of 1/hz seconds. We had hz = 1,000,000,
which gives excellent resolution (microsecond) but a terible range
(limits msleep()'s timeout to 35 minutes).

We had a program (Cassandra) doing poll() with a timeout of 2 hours,
which caused msleep to think we gave a negative timeout.

This patch reduces hz to 1,000, i.e., have msleep() operate in the same units
as poll(). Looking at the code, I don't believe this change will have any
ill-effects - we don't need higher resolution (freebsd code is used to
hz=1,000, which is the default there), and the code converts time units to
hz's correctly, always using the hz macro. The allowed range for timeouts will
grow to over 24 days - and match poll()'s allowed range.
---
 bsd/porting/netport.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/bsd/porting/netport.h b/bsd/porting/netport.h
index 874b705cb..6e6380a5f 100644
--- a/bsd/porting/netport.h
+++ b/bsd/porting/netport.h
@@ -97,8 +97,16 @@ extern int tick;
 #define TSECOND (1000000000L)
 #define TMILISECOND (1000000L)
 
-/* Defines how many ticks are in 1 second */
-#define hz (1000000L)
+/* BSD-originated functions like msleep() measure time in units of "ticks",
+ * which are defined here by the macro hz (there are hz ticks per second).
+ * hz can be changed to anything, and does not impose, for example, a timer
+ * interrupt hz times a second. A very high hz, for example 1000000, is
+ * fine, but because the timeout parameter to msleep is an int, it results
+ * in a maximum timeout of just 36 minutes.
+ * As a compromise, we currently choose hz=1000 (same as poll()'s resolution),
+ * which gets us 1ms resolution, and a 24 day range.
+ */
+#define hz (1000L)
 #define ticks2ns(ticks) (ticks * (TSECOND / hz))
 #define ns2ticks(ns)    (ns / (TSECOND / hz))
 
-- 
GitLab