From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KO8of-0002gs-Cf for qemu-devel@nongnu.org; Wed, 30 Jul 2008 06:21:57 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KO8oc-0002fm-Ut for qemu-devel@nongnu.org; Wed, 30 Jul 2008 06:21:56 -0400 Received: from [199.232.76.173] (port=33536 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KO8oc-0002fV-9c for qemu-devel@nongnu.org; Wed, 30 Jul 2008 06:21:54 -0400 Received: from smtp.eu.citrix.com ([62.200.22.115]:39984) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KO8oc-0005Wr-A5 for qemu-devel@nongnu.org; Wed, 30 Jul 2008 06:21:54 -0400 Message-ID: <489040FF.1070601@eu.citrix.com> Date: Wed, 30 Jul 2008 11:22:55 +0100 From: Stefano Stabellini MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] [PATCH] replacing gettimeofday with clock_gettime in hw/serial Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org This patch substitutes gettimeofday with clock_gettime in hw/serial.c. gettimeofday is unsafe because can lead to incorrect behaviors if the user changes the system's date. Signed-off-by: Stefano Stabellini --- diff --git a/hw/serial.c b/hw/serial.c index d49b5de..6ae3b20 100644 --- a/hw/serial.c +++ b/hw/serial.c @@ -155,8 +155,8 @@ struct SerialState { doesn't kill dom0. Simple token bucket. If we get some actual data from the user, instantly refil the bucket. */ -/* How long it takes to generate a token, in microseconds. */ -#define TOKEN_PERIOD 1000 +/* How long it takes to generate a token, in nanoseconds. */ +#define TOKEN_PERIOD 1000000 /* Maximum and initial size of token bucket */ #define TOKENS_MAX 100000 @@ -279,47 +279,47 @@ static void serial_update_parameters(SerialState *s) static void serial_get_token(void) { - static struct timeval last_refil_time; + static struct timespec last_refil_time; static int started; assert(tokens_avail >= 0); if (!tokens_avail) { - struct timeval delta, now; - int generated; + struct timespec delta, now; + long generated; if (!started) { - gettimeofday(&last_refil_time, NULL); + clock_gettime(CLOCK_MONOTONIC, &last_refil_time); tokens_avail = TOKENS_MAX; started = 1; return; } retry: - gettimeofday(&now, NULL); + clock_gettime(CLOCK_MONOTONIC, &now); delta.tv_sec = now.tv_sec - last_refil_time.tv_sec; - delta.tv_usec = now.tv_usec - last_refil_time.tv_usec; - if (delta.tv_usec < 0) { - delta.tv_usec += 1000000; + delta.tv_nsec = now.tv_nsec - last_refil_time.tv_nsec; + if (delta.tv_nsec < 0) { + delta.tv_nsec += 1000000000; delta.tv_sec--; } - assert(delta.tv_usec >= 0 && delta.tv_sec >= 0); - if (delta.tv_usec < TOKEN_PERIOD) { + assert(delta.tv_nsec >= 0 && delta.tv_sec >= 0); + if (delta.tv_nsec < TOKEN_PERIOD) { struct timespec ts; /* Wait until at least one token is available. */ - ts.tv_sec = TOKEN_PERIOD / 1000000; - ts.tv_nsec = (TOKEN_PERIOD % 1000000) * 1000; + ts.tv_sec = TOKEN_PERIOD / 1000000000; + ts.tv_nsec = TOKEN_PERIOD % 1000000000; while (nanosleep(&ts, &ts) < 0 && errno == EINTR) ; goto retry; } - generated = (delta.tv_sec * 1000000) / TOKEN_PERIOD; + generated = (delta.tv_sec * 1000000000) / TOKEN_PERIOD; generated += - ((delta.tv_sec * 1000000) % TOKEN_PERIOD + delta.tv_usec) / TOKEN_PERIOD; + ((delta.tv_sec * 1000000000) % TOKEN_PERIOD + delta.tv_nsec) / TOKEN_PERIOD; assert(generated > 0); - last_refil_time.tv_usec += (generated * TOKEN_PERIOD) % 1000000; - last_refil_time.tv_sec += last_refil_time.tv_usec / 1000000; - last_refil_time.tv_usec %= 1000000; - last_refil_time.tv_sec += (generated * TOKEN_PERIOD) / 1000000; + last_refil_time.tv_nsec += (generated * TOKEN_PERIOD) % 1000000000; + last_refil_time.tv_sec += last_refil_time.tv_nsec / 1000000000; + last_refil_time.tv_nsec %= 1000000000; + last_refil_time.tv_sec += (generated * TOKEN_PERIOD) / 1000000000; if (generated > TOKENS_MAX) generated = TOKENS_MAX; tokens_avail = generated;