Commit d2cc7586 authored by chcunningham's avatar chcunningham Committed by Commit Bot

MediaCapabilities: make DB configurable via Finch.

Adds flags for configuring the size of the rolling window and the number
of days to keep stats before expiring.

Bug: 910404
Test: new unit tests
Change-Id: I8a88a81154c710a1cc9b029e2b8ef8af61d945e0
Reviewed-on: https://chromium-review.googlesource.com/c/1355996Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Commit-Queue: Chrome Cunningham <chcunningham@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612479}
parent 2a3c852f
......@@ -10,11 +10,13 @@
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_macros.h"
#include "base/sequence_checker.h"
#include "base/task/post_task.h"
#include "base/time/default_clock.h"
#include "components/leveldb_proto/proto_database_impl.h"
#include "media/base/media_switches.h"
#include "media/capabilities/video_decode_stats.pb.h"
namespace media {
......@@ -27,8 +29,32 @@ namespace {
// See comments in components/leveldb_proto/leveldb_database.h
const char kDatabaseClientName[] = "VideoDecodeStatsDB";
const int kMaxFramesPerBufferDefault = 2500;
const int kMaxDaysToKeepStatsDefault = 30;
}; // namespace
const char VideoDecodeStatsDBImpl::kMaxFramesPerBufferParamName[] =
"db_frames_buffer_size";
const char VideoDecodeStatsDBImpl::kMaxDaysToKeepStatsParamName[] =
"db_days_to_keep_stats";
// static
int VideoDecodeStatsDBImpl::GetMaxFramesPerBuffer() {
return base::GetFieldTrialParamByFeatureAsDouble(
kMediaCapabilitiesWithParameters, kMaxFramesPerBufferParamName,
kMaxFramesPerBufferDefault);
}
// static
int VideoDecodeStatsDBImpl::GetMaxDaysToKeepStats() {
return base::GetFieldTrialParamByFeatureAsDouble(
kMediaCapabilitiesWithParameters, kMaxDaysToKeepStatsParamName,
kMaxDaysToKeepStatsDefault);
}
// static
std::unique_ptr<VideoDecodeStatsDBImpl> VideoDecodeStatsDBImpl::Create(
base::FilePath db_dir) {
......@@ -137,8 +163,11 @@ bool VideoDecodeStatsDBImpl::AreStatsExpired(
last_write_date = default_write_time_.ToJsTime();
}
const int kMaxDaysToKeepStats = GetMaxDaysToKeepStats();
DCHECK_GT(kMaxDaysToKeepStats, 0);
return wall_clock_->Now() - base::Time::FromJsTime(last_write_date) >
base::TimeDelta::FromDays(VideoDecodeStatsDBImpl::kMaxDaysToKeepStats);
base::TimeDelta::FromDays(kMaxDaysToKeepStats);
}
void VideoDecodeStatsDBImpl::WriteUpdatedEntry(
......@@ -170,6 +199,9 @@ void VideoDecodeStatsDBImpl::WriteUpdatedEntry(
uint64_t old_frames_dropped = stats_proto->frames_dropped();
uint64_t old_frames_power_efficient = stats_proto->frames_power_efficient();
const uint64_t kMaxFramesPerBuffer = GetMaxFramesPerBuffer();
DCHECK_GT(kMaxFramesPerBuffer, 0UL);
if (old_frames_decoded + new_entry.frames_decoded > kMaxFramesPerBuffer) {
// The |new_entry| is pushing out some or all of the old data. Achieve this
// by weighting the dropped and power efficiency stats by the ratio of the
......
......@@ -29,19 +29,8 @@ class DecodeStatsProto;
// construction. API callbacks will also occur on this sequence.
class MEDIA_EXPORT VideoDecodeStatsDBImpl : public VideoDecodeStatsDB {
public:
enum : int {
// Number chosen after manual review of metrics (accuracy, precision,
// recall).
// TODO(chcunningham): Run experiments with different values. Metrics
// suggest different platforms, even different stream properties (e.g.
// framerate) could benefit from customized thresholds. Machine learning
// would probably be best.
kMaxFramesPerBuffer = 2500,
// Number of days after which stats will be discarded if not updated. This
// avoids users getting stuck with a bad capability prediction that may have
// been due to one-off circumstances.
kMaxDaysToKeepStats = 30,
};
static const char kMaxFramesPerBufferParamName[];
static const char kMaxDaysToKeepStatsParamName[];
// Create an instance! |db_dir| specifies where to store LevelDB files to
// disk. LevelDB generates a handful of files, so its recommended to provide a
......@@ -74,6 +63,15 @@ class MEDIA_EXPORT VideoDecodeStatsDBImpl : public VideoDecodeStatsDB {
// after the proto update hits the chrome Stable channel (M71).
static constexpr char kDefaultWriteTime[] = "01-FEB-2019 12:00pm";
// Number of decoded frames to keep in the rolling "window" for a given entry
// in the database.
static int GetMaxFramesPerBuffer();
// Number of days after which stats will be discarded if not updated. This
// avoids users getting stuck with a bad capability prediction that may have
// been due to one-off circumstances.
static int GetMaxDaysToKeepStats();
// Called when the database has been initialized. Will immediately call
// |init_cb| to forward |success|.
void OnInit(InitializeCB init_cb, bool success);
......@@ -111,7 +109,7 @@ class MEDIA_EXPORT VideoDecodeStatsDBImpl : public VideoDecodeStatsDB {
void OnStatsCleared(base::OnceClosure clear_done_cb, bool success);
// Return true if:
// "now" - stats_proto.last_write_date > kMaxDaysToKeepStats
// "now" - stats_proto.last_write_date > GeMaxDaysToKeepStats()
bool AreStatsExpired(const DecodeStatsProto* const stats_proto);
void set_wall_clock_for_test(const base::Clock* tick_clock) {
......
......@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <memory>
#include "base/bind.h"
......@@ -9,9 +10,13 @@
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial_param_associator.h"
#include "base/metrics/field_trial_params.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/simple_test_clock.h"
#include "components/leveldb_proto/testing/fake_db.h"
#include "media/base/media_switches.h"
#include "media/base/test_data_util.h"
#include "media/base/video_codecs.h"
#include "media/capabilities/video_decode_stats.pb.h"
......@@ -56,6 +61,14 @@ class VideoDecodeStatsDBImplTest : public ::testing::Test {
base::FilePath(FILE_PATH_LITERAL("/fake/path"))));
}
int GetMaxFramesPerBuffer() {
return VideoDecodeStatsDBImpl::GetMaxFramesPerBuffer();
}
int GetMaxDaysToKeepStats() {
return VideoDecodeStatsDBImpl::GetMaxDaysToKeepStats();
}
void SetDBClock(base::Clock* clock) {
stats_db_->set_wall_clock_for_test(clock);
}
......@@ -169,6 +182,128 @@ TEST_F(VideoDecodeStatsDBImplTest, WriteReadAndClear) {
VerifyEmptyStats(kStatsKeyVp9);
}
TEST_F(VideoDecodeStatsDBImplTest, ConfigureMaxFramesPerBuffer) {
// Setup field trial to use a tiny window of 1 decoded frame.
base::test::ScopedFeatureList scoped_feature_list;
std::unique_ptr<base::FieldTrialList> field_trial_list;
int previous_max_frames_per_buffer = GetMaxFramesPerBuffer();
int new_max_frames_per_buffer = 1;
ASSERT_LT(new_max_frames_per_buffer, previous_max_frames_per_buffer);
// Override field trial.
std::map<std::string, std::string> params;
params[VideoDecodeStatsDBImpl::kMaxFramesPerBufferParamName] =
std::to_string(new_max_frames_per_buffer);
const std::string kTrialName = "TrialName";
const std::string kGroupName = "GroupName";
field_trial_list.reset();
field_trial_list.reset(new base::FieldTrialList(nullptr));
base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
base::AssociateFieldTrialParams(kTrialName, kGroupName, params);
base::FieldTrial* field_trial =
base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName);
std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
feature_list->RegisterFieldTrialOverride(
media::kMediaCapabilitiesWithParameters.name,
base::FeatureList::OVERRIDE_ENABLE_FEATURE, field_trial);
base::FeatureList::ClearInstanceForTesting();
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
std::map<std::string, std::string> actual_params;
EXPECT_TRUE(base::GetFieldTrialParamsByFeature(
media::kMediaCapabilitiesWithParameters, &actual_params));
EXPECT_EQ(params, actual_params);
EXPECT_EQ(new_max_frames_per_buffer, GetMaxFramesPerBuffer());
// Now verify the configured window is used by writing a frame and then
// writing another.
InitializeDB();
// Append single frame which was, sadly, dropped and not efficient.
DecodeStatsEntry entry(1, 1, 0);
AppendStats(kStatsKeyVp9, entry);
VerifyReadStats(kStatsKeyVp9, entry);
// Appending another frame which is *not* dropped and *is* efficient.
// Verify that only this last entry is still in the buffer (no aggregation).
DecodeStatsEntry second_entry(1, 0, 1);
AppendStats(kStatsKeyVp9, second_entry);
VerifyReadStats(kStatsKeyVp9, second_entry);
}
TEST_F(VideoDecodeStatsDBImplTest, ConfigureExpireDays) {
base::test::ScopedFeatureList scoped_feature_list;
std::unique_ptr<base::FieldTrialList> field_trial_list;
int previous_max_days_to_keep_stats = GetMaxDaysToKeepStats();
int new_max_days_to_keep_stats = 4;
ASSERT_LT(new_max_days_to_keep_stats, previous_max_days_to_keep_stats);
// Override field trial.
std::map<std::string, std::string> params;
params[VideoDecodeStatsDBImpl::kMaxDaysToKeepStatsParamName] =
std::to_string(new_max_days_to_keep_stats);
const std::string kTrialName = "TrialName";
const std::string kGroupName = "GroupName";
field_trial_list.reset();
field_trial_list.reset(new base::FieldTrialList(nullptr));
base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
base::AssociateFieldTrialParams(kTrialName, kGroupName, params);
base::FieldTrial* field_trial =
base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName);
std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
feature_list->RegisterFieldTrialOverride(
media::kMediaCapabilitiesWithParameters.name,
base::FeatureList::OVERRIDE_ENABLE_FEATURE, field_trial);
base::FeatureList::ClearInstanceForTesting();
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
std::map<std::string, std::string> actual_params;
EXPECT_TRUE(base::GetFieldTrialParamsByFeature(
media::kMediaCapabilitiesWithParameters, &actual_params));
EXPECT_EQ(params, actual_params);
EXPECT_EQ(new_max_days_to_keep_stats, GetMaxDaysToKeepStats());
InitializeDB();
// Inject a test clock and initialize with the current time.
base::SimpleTestClock clock;
SetDBClock(&clock);
clock.SetNow(base::Time::Now());
// Append and verify read-back.
AppendStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));
VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));
// Advance time half way through grace period. Verify stats not expired.
clock.Advance(base::TimeDelta::FromDays(new_max_days_to_keep_stats / 2));
VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));
// Advance time 1 day beyond grace period, verify stats are expired.
clock.Advance(
base::TimeDelta::FromDays((new_max_days_to_keep_stats / 2) + 1));
VerifyEmptyStats(kStatsKeyVp9);
// Advance the clock 100 extra days. Verify stats still expired.
clock.Advance(base::TimeDelta::FromDays(100));
VerifyEmptyStats(kStatsKeyVp9);
}
TEST_F(VideoDecodeStatsDBImplTest, FailedWrite) {
InitializeDB();
......@@ -189,7 +324,7 @@ TEST_F(VideoDecodeStatsDBImplTest, FillBufferInMixedIncrements) {
// Setup DB entry that half fills the buffer with 10% of frames dropped and
// 50% of frames power efficient.
const int kNumFramesEntryA = VideoDecodeStatsDBImpl::kMaxFramesPerBuffer / 2;
const int kNumFramesEntryA = GetMaxFramesPerBuffer() / 2;
DecodeStatsEntry entryA(kNumFramesEntryA, std::round(0.1 * kNumFramesEntryA),
std::round(0.5 * kNumFramesEntryA));
const double kDropRateA =
......@@ -207,11 +342,9 @@ TEST_F(VideoDecodeStatsDBImplTest, FillBufferInMixedIncrements) {
AppendStats(kStatsKeyVp9, entryA);
VerifyReadStats(
kStatsKeyVp9,
DecodeStatsEntry(
VideoDecodeStatsDBImpl::kMaxFramesPerBuffer,
std::round(kDropRateA * VideoDecodeStatsDBImpl::kMaxFramesPerBuffer),
std::round(kEfficientRateA *
VideoDecodeStatsDBImpl::kMaxFramesPerBuffer)));
DecodeStatsEntry(GetMaxFramesPerBuffer(),
std::round(kDropRateA * GetMaxFramesPerBuffer()),
std::round(kEfficientRateA * GetMaxFramesPerBuffer())));
// This row in the DB is now "full" (appended frames >= kMaxFramesPerBuffer)!
//
......@@ -236,17 +369,14 @@ TEST_F(VideoDecodeStatsDBImplTest, FillBufferInMixedIncrements) {
AppendStats(kStatsKeyVp9, entryA);
VerifyReadStats(
kStatsKeyVp9,
DecodeStatsEntry(
VideoDecodeStatsDBImpl::kMaxFramesPerBuffer,
std::round(kDropRateA * VideoDecodeStatsDBImpl::kMaxFramesPerBuffer),
std::round(kEfficientRateA *
VideoDecodeStatsDBImpl::kMaxFramesPerBuffer)));
DecodeStatsEntry(GetMaxFramesPerBuffer(),
std::round(kDropRateA * GetMaxFramesPerBuffer()),
std::round(kEfficientRateA * GetMaxFramesPerBuffer())));
// Append entryB that will fill just 10% of the buffer. The new entry has
// different rates of dropped and power efficient frames to help verify that
// it is given proper weight as it mixes with existing data in the buffer.
const int kNumFramesEntryB =
std::round(.1 * VideoDecodeStatsDBImpl::kMaxFramesPerBuffer);
const int kNumFramesEntryB = std::round(.1 * GetMaxFramesPerBuffer());
DecodeStatsEntry entryB(kNumFramesEntryB, std::round(0.25 * kNumFramesEntryB),
std::round(1 * kNumFramesEntryB));
const double kDropRateB =
......@@ -261,11 +391,10 @@ TEST_F(VideoDecodeStatsDBImplTest, FillBufferInMixedIncrements) {
double mixed_effiency_rate = .1 * kEfficientRateB + .9 * kEfficientRateA;
VerifyReadStats(
kStatsKeyVp9,
DecodeStatsEntry(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer,
std::round(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer *
mixed_drop_rate),
std::round(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer *
mixed_effiency_rate)));
DecodeStatsEntry(
GetMaxFramesPerBuffer(),
std::round(GetMaxFramesPerBuffer() * mixed_drop_rate),
std::round(GetMaxFramesPerBuffer() * mixed_effiency_rate)));
// After appending entryB again, verify aggregate ratios behave according to
// the formula above (appending repeated entryB brings ratios closer to those
......@@ -275,11 +404,10 @@ TEST_F(VideoDecodeStatsDBImplTest, FillBufferInMixedIncrements) {
mixed_effiency_rate = .1 * kEfficientRateB + .9 * mixed_effiency_rate;
VerifyReadStats(
kStatsKeyVp9,
DecodeStatsEntry(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer,
std::round(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer *
mixed_drop_rate),
std::round(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer *
mixed_effiency_rate)));
DecodeStatsEntry(
GetMaxFramesPerBuffer(),
std::round(GetMaxFramesPerBuffer() * mixed_drop_rate),
std::round(GetMaxFramesPerBuffer() * mixed_effiency_rate)));
// Appending entry*A* again, verify aggregate ratios behave according to
// the formula above (ratio's move back in direction of those in entryA).
......@@ -290,15 +418,14 @@ TEST_F(VideoDecodeStatsDBImplTest, FillBufferInMixedIncrements) {
mixed_effiency_rate = .5 * kEfficientRateA + .5 * mixed_effiency_rate;
VerifyReadStats(
kStatsKeyVp9,
DecodeStatsEntry(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer,
std::round(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer *
mixed_drop_rate),
std::round(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer *
mixed_effiency_rate)));
DecodeStatsEntry(
GetMaxFramesPerBuffer(),
std::round(GetMaxFramesPerBuffer() * mixed_drop_rate),
std::round(GetMaxFramesPerBuffer() * mixed_effiency_rate)));
// Now append entryC with a frame count of 2x the buffer max. Verify entryC
// gets 100% of the weight, erasing the mixed stats from earlier appends.
const int kNumFramesEntryC = 2 * VideoDecodeStatsDBImpl::kMaxFramesPerBuffer;
const int kNumFramesEntryC = 2 * GetMaxFramesPerBuffer();
DecodeStatsEntry entryC(kNumFramesEntryC, std::round(0.3 * kNumFramesEntryC),
std::round(0.25 * kNumFramesEntryC));
const double kDropRateC =
......@@ -309,11 +436,9 @@ TEST_F(VideoDecodeStatsDBImplTest, FillBufferInMixedIncrements) {
AppendStats(kStatsKeyVp9, entryC);
VerifyReadStats(
kStatsKeyVp9,
DecodeStatsEntry(
VideoDecodeStatsDBImpl::kMaxFramesPerBuffer,
std::round(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer * kDropRateC),
std::round(VideoDecodeStatsDBImpl::kMaxFramesPerBuffer *
kEfficientRateC)));
DecodeStatsEntry(GetMaxFramesPerBuffer(),
std::round(GetMaxFramesPerBuffer() * kDropRateC),
std::round(GetMaxFramesPerBuffer() * kEfficientRateC)));
}
TEST_F(VideoDecodeStatsDBImplTest, NoWriteDateReadAndExpire) {
......@@ -339,15 +464,13 @@ TEST_F(VideoDecodeStatsDBImplTest, NoWriteDateReadAndExpire) {
// Set "now" to be in the middle of the grace period. Verify stats are still
// readable (not expired).
clock.SetNow(kDefaultWriteTime +
base::TimeDelta::FromDays(
VideoDecodeStatsDBImpl::kMaxDaysToKeepStats / 2));
base::TimeDelta::FromDays(GetMaxDaysToKeepStats() / 2));
VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(100, 10, 1));
// Set the clock 1 day beyond the expiry date. Verify stats are no longer
// readable due to expiration.
clock.SetNow(kDefaultWriteTime +
base::TimeDelta::FromDays(
VideoDecodeStatsDBImpl::kMaxDaysToKeepStats + 1));
base::TimeDelta::FromDays(GetMaxDaysToKeepStats() + 1));
VerifyEmptyStats(kStatsKeyVp9);
// Write some stats to the entry. Verify we get back exactly what's written
......@@ -384,15 +507,13 @@ TEST_F(VideoDecodeStatsDBImplTest, NoWriteDateAppendReadAndExpire) {
// Set "now" to be in the middle of the grace period. Verify stats are still
// readable (not expired).
clock.SetNow(kDefaultWriteTime +
base::TimeDelta::FromDays(
VideoDecodeStatsDBImpl::kMaxDaysToKeepStats / 2));
base::TimeDelta::FromDays(GetMaxDaysToKeepStats() / 2));
VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(300, 30, 3));
// Set the clock 1 day beyond the expiry date. Verify stats are no longer
// readable due to expiration.
clock.SetNow(kDefaultWriteTime +
base::TimeDelta::FromDays(
VideoDecodeStatsDBImpl::kMaxDaysToKeepStats + 1));
base::TimeDelta::FromDays(GetMaxDaysToKeepStats() + 1));
VerifyEmptyStats(kStatsKeyVp9);
}
......@@ -409,13 +530,11 @@ TEST_F(VideoDecodeStatsDBImplTest, AppendAndExpire) {
VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));
// Advance time half way through grace period. Verify stats not expired.
clock.Advance(base::TimeDelta::FromDays(
VideoDecodeStatsDBImpl::kMaxDaysToKeepStats / 2));
clock.Advance(base::TimeDelta::FromDays(GetMaxDaysToKeepStats() / 2));
VerifyReadStats(kStatsKeyVp9, DecodeStatsEntry(200, 20, 2));
// Advance time 1 day beyond grace period, verify stats are expired.
clock.Advance(base::TimeDelta::FromDays(
(VideoDecodeStatsDBImpl::kMaxDaysToKeepStats / 2) + 1));
clock.Advance(base::TimeDelta::FromDays((GetMaxDaysToKeepStats() / 2) + 1));
VerifyEmptyStats(kStatsKeyVp9);
// Advance the clock 100 days. Verify stats still expired.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment