Every so often I get concerned because OVS does most of its time arithmetic
in "long long int" but poll_timer_wait() takes an "int", so there is
potential for truncating a large value to a small value or a positive value
to a negative value. That would cause excessive wakeups and possibly 100%
CPU usage.
This commit therefore changes poll_timer_wait()'s parameter type from "int"
to "long long int". The file-scope 'timeout' variable remains type "int"
because that is the type of poll()'s timeout argument.
Factoring poll_timer_wait() into two functions is not necessary here but it
comes in handy in the following patch.
wait_timeout(long long int started)
{
long long int now = time_msec();
wait_timeout(long long int started)
{
long long int now = time_msec();
- long long int timeout = 10000 - (now - started);
- if (timeout <= 0) {
- poll_immediate_wake();
- } else {
- poll_timer_wait(timeout);
- }
+ poll_timer_wait(10000 - (now - started));
return new_waiter(fd, events);
}
return new_waiter(fd, events);
}
+/* The caller must ensure that 'msec' is not negative. */
+static void
+poll_timer_wait__(int msec)
+{
+ if (timeout < 0 || msec < timeout) {
+ timeout = msec;
+ if (VLOG_IS_DBG_ENABLED()) {
+ backtrace_capture(&timeout_backtrace);
+ }
+ }
+}
+
/* Causes the following call to poll_block() to block for no more than 'msec'
* milliseconds. If 'msec' is nonpositive, the following call to poll_block()
* will not block at all.
/* Causes the following call to poll_block() to block for no more than 'msec'
* milliseconds. If 'msec' is nonpositive, the following call to poll_block()
* will not block at all.
* is affected. The timer will need to be re-registered after poll_block() is
* called if it is to persist. */
void
* is affected. The timer will need to be re-registered after poll_block() is
* called if it is to persist. */
void
-poll_timer_wait(int msec)
+poll_timer_wait(long long int msec)
- if (timeout < 0 || msec < timeout) {
- timeout = MAX(0, msec);
- if (VLOG_IS_DBG_ENABLED()) {
- backtrace_capture(&timeout_backtrace);
- }
- }
+ poll_timer_wait__(msec < 0 ? 0
+ : msec > INT_MAX ? INT_MAX
+ : msec);
}
/* Causes the following call to poll_block() to wake up immediately, without
}
/* Causes the following call to poll_block() to wake up immediately, without
- * Copyright (c) 2008, 2009 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/* Schedule events to wake up the following poll_block(). */
struct poll_waiter *poll_fd_wait(int fd, short int events);
/* Schedule events to wake up the following poll_block(). */
struct poll_waiter *poll_fd_wait(int fd, short int events);
-void poll_timer_wait(int msec);
+void poll_timer_wait(long long int msec);
void poll_immediate_wake(void);
/* Wait until an event occurs. */
void poll_immediate_wake(void);
/* Wait until an event occurs. */
-/* Copyright (c) 2009 Nicira Networks
+/* Copyright (c) 2009, 2010 Nicira Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
}
if (deadline < LLONG_MAX) {
}
if (deadline < LLONG_MAX) {
- poll_timer_wait(MIN(deadline - now, INT_MAX));
+ poll_timer_wait(deadline - now);