Self-test ETA: Estimate from observed progress speed per percent

- Move the adaptive estimation into SelfTestEtaEstimator, which tracks
  observed seconds per percent instead of per-change "segment" durations.
  This fixes NVMe estimates (1% steps were counted as 10% segments, giving
  ~10x too short ETAs), and progress spanning several steps between two
  polls (e.g. 80% -> 60%) being counted as a single segment.
- Reset the estimator state in start().
- Return unknown ETA when the test is not running.
- Add unit tests for the estimator (the existing tests did not cover it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alexander ShaduriandClaude Opus 5.5 committed 2026-10-15 23:05:58 +02:00
1 parent 55931f2a88
commit 971aa8d1bf
3 files changed
+175 -32

No files matched your search

+60 -27
View File
@@ -84,31 +84,64 @@ int8_t SelfTest::get_remaining_percent() const
void SelfTestEtaEstimator::add_progress(int from_remaining_percent, int to_remaining_percent, double seconds)
{
if (from_remaining_percent < 0 || to_remaining_percent < 0
|| to_remaining_percent >= from_remaining_percent || seconds < 0.) {
return;
}
observed_seconds_ += seconds;
observed_percent_ += from_remaining_percent - to_remaining_percent;
}
bool SelfTestEtaEstimator::has_data() const
{
return observed_percent_ > 0;
}
std::chrono::seconds SelfTestEtaEstimator::estimate_remaining_seconds(int remaining_percent, double seconds_since_last_change) const
{
using namespace std::literals;
if (!has_data() || remaining_percent < 0) {
return -1s;
}
const double seconds_per_percent = observed_seconds_ / observed_percent_;
const double rem = seconds_per_percent * remaining_percent - seconds_since_last_change;
const auto rem_rounded = static_cast<int64_t>(std::round(rem));
if (rem_rounded < 0) {
return -1s; // estimate exceeded; unknown
}
return std::chrono::seconds(rem_rounded);
}
void SelfTestEtaEstimator::reset()
{
observed_seconds_ = 0.;
observed_percent_ = 0;
}
// Returns estimated time of completion for the test. returns -1 if n/a or unknown. 0 is a valid value.
std::chrono::seconds SelfTest::get_remaining_seconds() const
{
using namespace std::literals;
// Use adaptive estimation if we have observed at least one completed segment.
if (remaining_percent_ < 0) {
return -1s; // not running
}
// Use adaptive estimation if we have observed some progress.
// This works for all drive types including NVMe (which may not report total duration).
if (!segment_durations_.empty()) {
// Calculate average duration of observed segments
double sum = 0.0;
for (const auto& duration : segment_durations_) {
sum += duration;
}
const double avg_segment_duration = sum / segment_durations_.size();
// Estimate remaining time based on observed average and remaining segments
// remaining_percent_ goes from 100 (start) to 0 (end), in 10% decrements
const int remaining_segments = (remaining_percent_ + 9) / 10; // round up
const double estimated_remaining = avg_segment_duration * remaining_segments - timer_.elapsed();
const auto rem_rounded = static_cast<int64_t>(std::round(estimated_remaining));
if (rem_rounded < 0) {
return -1s; // estimate exhausted; return unknown
}
return std::chrono::seconds(rem_rounded);
if (eta_estimator_.has_data()) {
return eta_estimator_.estimate_remaining_seconds(remaining_percent_, timer_.elapsed());
}
// Fall back to drive's initial estimate when we don't have observed data yet
@@ -300,6 +333,8 @@ hz::ExpectedVoid<SelfTestExecutionError> SelfTest::start(const std::shared_ptr<C
last_seen_percent_ = 90;
poll_in_seconds_ = std::chrono::seconds(5); // first update() in 5 seconds
timer_.start();
eta_estimator_.reset();
first_change_seen_ = false;
drive_->set_test_is_active(true);
@@ -523,15 +558,13 @@ hz::ExpectedVoid<SelfTestExecutionError> SelfTest::update(const std::shared_ptr<
// and reaches 00% on completion. That's 9 pieces.
if (status_ == SelfTestStatus::InProgress) {
if (remaining_percent_ != last_seen_percent_) {
// Record the duration of the completed segment for adaptive ETA calculation.
// Skip the first segment (typically 90→80) as it may be instant or partially
// completed when monitoring begins, which would skew the average.
if (first_segment_seen_) {
const double elapsed = timer_.elapsed();
segment_durations_.push_back(elapsed);
} else {
first_segment_seen_ = true; // Mark that we've seen the first transition
// Record the observed progress speed for adaptive ETA calculation.
// Skip the first change, as it may be instant or partially completed
// (depending on the drive), which would skew the estimate.
if (first_change_seen_) {
eta_estimator_.add_progress(last_seen_percent_, remaining_percent_, timer_.elapsed());
}
first_change_seen_ = true;
last_seen_percent_ = remaining_percent_;
timer_.start(); // restart the timer
}
+41 -5
View File
@@ -18,7 +18,6 @@ Copyright:
#include <cstdint>
#include <chrono>
#include <unordered_map>
#include <vector>
#include "storage_device.h"
#include "command_executor.h"
@@ -94,6 +93,43 @@ enum class SelfTestStatusSeverity {
/// Estimates the remaining self-test time from the observed test progress speed.
/// This compensates for inaccurate drive-reported test durations (e.g. when the drive is under load).
class SelfTestEtaEstimator {
public:
/// Record that the reported remaining percentage changed from \c from_remaining_percent
/// to \c to_remaining_percent, which took \c seconds.
/// Changes that don't decrease the remaining percentage are ignored.
/// Works with any percentage granularity (10% for ATA, 1% for NVMe), and with
/// changes spanning several steps (e.g. 80% -> 60% between two polls).
void add_progress(int from_remaining_percent, int to_remaining_percent, double seconds);
/// Check if any progress has been recorded
[[nodiscard]] bool has_data() const;
/// Estimate the remaining time.
/// \param remaining_percent Currently reported remaining percentage.
/// \param seconds_since_last_change Seconds passed since the remaining percentage last changed.
/// \return -1 if unknown: no data, invalid percentage, or the estimate has been exceeded.
[[nodiscard]] std::chrono::seconds estimate_remaining_seconds(int remaining_percent, double seconds_since_last_change) const;
/// Forget all recorded progress
void reset();
private:
double observed_seconds_ = 0.; ///< Total duration of the recorded progress
int observed_percent_ = 0; ///< Total recorded progress, in percent
};
/// SMART self-test runner.
class SelfTest {
public:
@@ -129,8 +165,8 @@ class SelfTest {
/// Get estimated time of completion for the test.
/// The estimation uses an adaptive algorithm:
/// - Initially uses the drive's reported test duration estimate
/// - After completing one or more 10% segments, switches to using the observed
/// average segment duration to predict remaining time
/// - Once some progress has been observed, switches to using the observed
/// progress speed to predict remaining time (see SelfTestEtaEstimator)
/// - This provides more accurate ETAs when the drive's estimate is inaccurate
/// (e.g., under load or with drives that consistently under/overestimate)
/// \return -1 if N/A or unknown (including when the drive's estimated duration has been
@@ -189,8 +225,8 @@ class SelfTest {
std::chrono::seconds poll_in_seconds_ = std::chrono::seconds(-1); ///< The user is asked to poll after this much seconds have passed.
Glib::Timer timer_; ///< Counts time since the last percent change
std::vector<double> segment_durations_; ///< Actual durations of completed 10% segments (in seconds), for adaptive ETA calculation
bool first_segment_seen_ = false; ///< Whether we've observed the first percentage change (to skip the potentially instant/partial first segment)
SelfTestEtaEstimator eta_estimator_; ///< Observed progress speed, for adaptive ETA calculation
bool first_change_seen_ = false; ///< Whether we've observed the first percentage change (to skip the potentially instant/partial first segment)
};
+74
View File
@@ -97,6 +97,80 @@ TEST_CASE("SelfTest basic functionality", "[selftest]")
}
TEST_CASE("SelfTest adaptive ETA estimation", "[selftest][eta]")
{
using namespace std::literals;
SECTION("No data")
{
const SelfTestEtaEstimator eta;
REQUIRE_FALSE(eta.has_data());
REQUIRE(eta.estimate_remaining_seconds(80, 0.) == -1s);
}
SECTION("ATA 10% steps")
{
SelfTestEtaEstimator eta;
eta.add_progress(90, 80, 600.); // 60 sec per percent
REQUIRE(eta.has_data());
REQUIRE(eta.estimate_remaining_seconds(80, 0.) == 4800s);
REQUIRE(eta.estimate_remaining_seconds(80, 100.) == 4700s);
eta.add_progress(80, 70, 1200.); // slower segment, average is now 90 sec per percent
REQUIRE(eta.estimate_remaining_seconds(70, 0.) == 6300s);
}
SECTION("Several steps between polls count as several segments")
{
SelfTestEtaEstimator eta;
eta.add_progress(80, 60, 1200.); // two segments, 60 sec per percent
REQUIRE(eta.estimate_remaining_seconds(60, 0.) == 3600s);
}
SECTION("NVMe 1% steps")
{
SelfTestEtaEstimator eta;
eta.add_progress(99, 98, 30.); // 30 sec per percent
REQUIRE(eta.estimate_remaining_seconds(98, 0.) == 2940s);
}
SECTION("Estimate exceeded returns unknown, not 0")
{
SelfTestEtaEstimator eta;
eta.add_progress(90, 80, 600.);
REQUIRE(eta.estimate_remaining_seconds(10, 600.) == 0s);
REQUIRE(eta.estimate_remaining_seconds(10, 700.) == -1s);
}
SECTION("Invalid progress is ignored")
{
SelfTestEtaEstimator eta;
eta.add_progress(80, 80, 600.); // no change
eta.add_progress(80, 90, 600.); // remaining percentage increased
eta.add_progress(-1, 90, 600.);
eta.add_progress(90, -1, 600.);
eta.add_progress(90, 80, -1.);
REQUIRE_FALSE(eta.has_data());
REQUIRE(eta.estimate_remaining_seconds(80, 0.) == -1s);
}
SECTION("Invalid remaining percentage")
{
SelfTestEtaEstimator eta;
eta.add_progress(90, 80, 600.);
REQUIRE(eta.estimate_remaining_seconds(-1, 0.) == -1s);
}
SECTION("Reset")
{
SelfTestEtaEstimator eta;
eta.add_progress(90, 80, 600.);
eta.reset();
REQUIRE_FALSE(eta.has_data());
}
}
TEST_CASE("SelfTest EXT enum helpers", "[selftest][enum_helpers]")
{
SECTION("Status enum to string conversion")