mirror of
https://github.com/nmap/nmap.git
synced 2025-12-06 12:41:29 +00:00
Avoid recalculating timeval that doesn't change for life of RateMeter
This commit is contained in:
12
timing.cc
12
timing.cc
@@ -302,6 +302,8 @@ RateMeter::RateMeter(double current_rate_history) {
|
|||||||
stop_tv.tv_usec = 0;
|
stop_tv.tv_usec = 0;
|
||||||
last_update_tv.tv_sec = 0;
|
last_update_tv.tv_sec = 0;
|
||||||
last_update_tv.tv_usec = 0;
|
last_update_tv.tv_usec = 0;
|
||||||
|
history_threshold.tv_sec = 0;
|
||||||
|
history_threshold.tv_usec = 0;
|
||||||
total = 0.0;
|
total = 0.0;
|
||||||
current_rate = 0.0;
|
current_rate = 0.0;
|
||||||
assert(!isSet(&start_tv));
|
assert(!isSet(&start_tv));
|
||||||
@@ -315,6 +317,10 @@ void RateMeter::start(const struct timeval *now) {
|
|||||||
gettimeofday(&start_tv, NULL);
|
gettimeofday(&start_tv, NULL);
|
||||||
else
|
else
|
||||||
start_tv = *now;
|
start_tv = *now;
|
||||||
|
/* Find the time current_rate_history seconds after the start. That's our
|
||||||
|
threshold for deciding how far back to look for moving average. */
|
||||||
|
TIMEVAL_ADD(history_threshold, start_tv,
|
||||||
|
(time_t) (current_rate_history * 1000000.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
void RateMeter::stop(const struct timeval *now) {
|
void RateMeter::stop(const struct timeval *now) {
|
||||||
@@ -369,11 +375,7 @@ void RateMeter::update(double amount, const struct timeval *now) {
|
|||||||
/* Find out how far back in time to look. We want to look back
|
/* Find out how far back in time to look. We want to look back
|
||||||
current_rate_history seconds, or to when the last update occurred,
|
current_rate_history seconds, or to when the last update occurred,
|
||||||
whichever is longer. However, we never look past the start. */
|
whichever is longer. However, we never look past the start. */
|
||||||
struct timeval tmp;
|
if (TIMEVAL_AFTER(*now, history_threshold))
|
||||||
/* Find the time current_rate_history seconds after the start. That's our
|
|
||||||
threshold for deciding how far back to look. */
|
|
||||||
TIMEVAL_ADD(tmp, start_tv, (time_t) (current_rate_history * 1000000.0));
|
|
||||||
if (TIMEVAL_AFTER(*now, tmp))
|
|
||||||
interval = MAX(current_rate_history, diff);
|
interval = MAX(current_rate_history, diff);
|
||||||
else
|
else
|
||||||
interval = TIMEVAL_FSEC_SUBTRACT(*now, start_tv);
|
interval = TIMEVAL_FSEC_SUBTRACT(*now, start_tv);
|
||||||
|
|||||||
2
timing.h
2
timing.h
@@ -183,6 +183,8 @@ class RateMeter {
|
|||||||
struct timeval stop_tv;
|
struct timeval stop_tv;
|
||||||
/* The last time the current sample rates were updated. */
|
/* The last time the current sample rates were updated. */
|
||||||
struct timeval last_update_tv;
|
struct timeval last_update_tv;
|
||||||
|
/* The time current_rate_history after start_tv. */
|
||||||
|
struct timeval history_threshold;
|
||||||
|
|
||||||
double total;
|
double total;
|
||||||
double current_rate;
|
double current_rate;
|
||||||
|
|||||||
Reference in New Issue
Block a user