Skip to content

Commit

Permalink
SpeedMeter no longer uses NaN to represent invalid values.
Browse files Browse the repository at this point in the history
There is no portable way to check for NaN values before the
C++11 standard, so the old code didn't compile on OmniOS 11
and OpenIndiana 151a7.

Closes GH-1212.
  • Loading branch information
FooBarWidget committed Jun 26, 2014
1 parent 76f0df8 commit 7d4738f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
11 changes: 8 additions & 3 deletions ext/common/Utils/SpeedMeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class SpeedMeter {
return count;
}

/** Current speed over the configured window. Returns NaN if less than 2
/** Current speed over the configured window. Returns unknownSpeed() if less than 2
* samples have been collected so far.
*/
double currentSpeed() const {
Expand Down Expand Up @@ -221,16 +221,21 @@ class SpeedMeter {
}
avgWeight /= std::max(1, (int) count - 1 - begin);

interval = getSample(count - 1).timestamp - getSample(begin).timestamp;
interval = getSample((int) count - 1).timestamp - getSample(begin).timestamp;
if (interval > 0) {
// sum / interval is the speed per average delta interval,
// so we extrapolate that over the entire window interval.
return (sum / interval) * (window / avgWeight);
} else {
return numeric_limits<double>::quiet_NaN();
return unknownSpeed();
}
}

static ValueType unknownSpeed() {
assert(numeric_limits<ValueType>::is_iec559);
return -numeric_limits<ValueType>::max();
}

#if 1
void debug() const {
const unsigned long long timeThreshold = getTimeThreshold();
Expand Down
12 changes: 6 additions & 6 deletions ext/common/Utils/SystemMetricsCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#include <sys/sysinfo.h>
#include <Exceptions.h>
#include <Utils/StringScanning.h>
#include <Utils/SpeedMeter.h>
#include <Utils/IOUtils.h>
#endif
#ifdef __APPLE__
Expand All @@ -69,6 +68,7 @@
#include <Utils/StrIntUtils.h>
#include <Utils/SystemTime.h>
#include <Utils/AnsiColorConstants.h>
#include <Utils/SpeedMeter.h>

/*
* Useful resources
Expand Down Expand Up @@ -346,13 +346,13 @@ class SystemMetrics {
time_t boottime;

/** Speed at which processes are created per second.
* NaN if it's not yet known (because too few samples have been taken so far).
* SpeedMeter<>::unknownSpeed() if it's not yet known (because too few samples have been taken so far).
* -1 if there was an error querying this information.
* -2 if the OS does not support this metric.
*/
double forkRate;
/** Speed at which the OS swaps in and swaps out data, in KB/sec.
* NaN if it's not yet known (because too few samples have been taken so far).
* SpeedMeter<>::unknownSpeed() if it's not yet known (because too few samples have been taken so far).
* -1 if there was an error querying this information.
* -2 if the OS does not support this metric.
*/
Expand Down Expand Up @@ -537,7 +537,7 @@ class SystemMetrics {

if (forkRate != -2) {
stream << "Fork rate : ";
if (std::isnan(forkRate) || forkRate < 0) {
if (forkRate == SpeedMeter<>::unknownSpeed() || forkRate < 0) {
if (options.colors) {
stream << ANSI_COLOR_DGRAY;
}
Expand Down Expand Up @@ -648,7 +648,7 @@ class SystemMetrics {

if (swapInRate != -2) {
stream << "Swap in : ";
if (std::isnan(swapInRate) || swapInRate < 0) {
if (swapInRate == SpeedMeter<>::unknownSpeed() || swapInRate < 0) {
if (options.colors) {
stream << ANSI_COLOR_DGRAY;
}
Expand All @@ -668,7 +668,7 @@ class SystemMetrics {

if (swapOutRate != -2) {
stream << "Swap out : ";
if (std::isnan(swapOutRate) || swapOutRate < 0) {
if (swapOutRate == SpeedMeter<>::unknownSpeed() || swapOutRate < 0) {
if (options.colors) {
stream << ANSI_COLOR_DGRAY;
}
Expand Down

0 comments on commit 7d4738f

Please sign in to comment.