Chromium Code Reviews| Index: components/data_usage/android/traffic_stats_amortizer.h |
| diff --git a/components/data_usage/android/traffic_stats_amortizer.h b/components/data_usage/android/traffic_stats_amortizer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..17ec8c73dab410dd820410ca694a6cb98ea2cd97 |
| --- /dev/null |
| +++ b/components/data_usage/android/traffic_stats_amortizer.h |
| @@ -0,0 +1,137 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_DATA_USAGE_ANDROID_TRAFFIC_STATS_AMORTIZER_H_ |
| +#define COMPONENTS_DATA_USAGE_ANDROID_TRAFFIC_STATS_AMORTIZER_H_ |
| + |
| +#include <stdint.h> |
| + |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/linked_ptr.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "base/time/time.h" |
| +#include "components/data_usage/core/data_use_amortizer.h" |
| + |
| +namespace base { |
| +class TickClock; |
| +class Timer; |
| +} |
| + |
| +namespace data_usage { |
| + |
| +struct DataUse; |
| + |
| +namespace android { |
| + |
| +// Class that uses Android TrafficStats to amortize any unincluded overhead into |
| +// the data usage reported by the network stack. Should only be used on the IO |
| +// thread. Only one TrafficStatsAmortizer should be active globally at a time, |
| +// since TrafficStats measurements are global for the entire application, so |
| +// having multiple active TrafficStatsAmortizers at a time would cause each of |
| +// them to overestimate the overhead here. |
| +class TrafficStatsAmortizer : public DataUseAmortizer { |
| + public: |
| + TrafficStatsAmortizer(); |
| + ~TrafficStatsAmortizer() override; |
| + |
| + void AmortizeDataUse(scoped_ptr<DataUse> data_use, |
|
tbansal1
2015/11/10 18:53:55
Method comments please.
sclittle
2015/11/11 02:10:07
Done.
|
| + const DataUseConsumerCallback& callback) override; |
| + |
| + void OnExtraBytes(int64_t extra_tx_bytes, int64_t extra_rx_bytes) override; |
| + |
| + base::WeakPtr<TrafficStatsAmortizer> GetWeakPtr(); |
| + |
| + protected: |
| + // Constructor for testing purposes, allowing for tests to take full control |
| + // over the timing of the TrafficStatsAmortizer and the byte counts returned |
| + // from TrafficStats. |traffic_stats_query_timer| must not be a repeating |
| + // timer. |
| + TrafficStatsAmortizer(scoped_ptr<base::TickClock> tick_clock, |
| + scoped_ptr<base::Timer> traffic_stats_query_timer, |
| + const base::TimeDelta& traffic_stats_query_delay, |
| + const base::TimeDelta& max_amortization_delay, |
| + size_t max_data_use_buffer_size); |
| + |
| + // Query the total transmitted and received bytes for the application from |
| + // TrafficStats. Stores the byte counts in |tx_bytes| and |rx_bytes| |
| + // respectively and returns true if both values are available from |
| + // TrafficStats, otherwise returns false. |
| + // Virtual for testing. |
| + virtual bool QueryTrafficStats(int64_t* tx_bytes, int64_t* rx_bytes) const; |
| + |
| + private: |
| + void AddPreAmortizationBytes(int64_t tx_bytes, int64_t rx_bytes); |
|
bengr
2015/11/10 18:12:50
Please comment on what this and AmortizeNow() do.
sclittle
2015/11/11 02:10:07
Done.
|
| + |
| + void AmortizeNow(); |
| + |
| + base::ThreadChecker thread_checker_; |
| + |
| + // TickClock for determining the current time tick. |
| + scoped_ptr<base::TickClock> tick_clock_; |
| + |
| + // One-shot timer for polling TrafficStats byte counts until they stabilize. |
|
bengr
2015/11/10 18:12:50
Please document the algorithm for taking TrafficSt
sclittle
2015/11/11 02:10:07
Done. This comment was wrong.
|
| + // This must not be a repeating timer. |
| + scoped_ptr<base::Timer> traffic_stats_query_timer_; |
| + |
| + // The delay between polls of TrafficStats byte counts for waiting for the |
| + // TrafficStats byte counts to stabilize. |
|
bengr
2015/11/10 18:12:50
What does "stabilize" mean?
sclittle
2015/11/11 02:10:07
Oops, I forgot to update this and a few other comm
|
| + const base::TimeDelta traffic_stats_query_delay_; |
| + |
| + // The maximum amount of time that the TrafficStatsAmortizer is allowed to |
| + // spend waiting to do an amortization run. |
| + const base::TimeDelta max_amortization_delay_; |
| + |
| + // The maximum allowed size of the |buffered_data_use_| buffer before DataUse |
| + // elements start to be passed through without performing any amortization. |
|
bengr
2015/11/10 18:12:50
Why is this needed?
sclittle
2015/11/11 02:10:07
Added comment. It's here so that we don't hog memo
|
| + const size_t max_data_use_buffer_size_; |
| + |
| + // Indicates whether or not the TrafficStatsAmortizer currently has |
| + // pre-amortization bytes waiting for amortization to be performed. |
| + bool is_amortization_run_in_progress_; |
| + |
| + // The time when the first pre-amortization bytes for the current amortization |
| + // run were given to this TrafficStatsAmortizer. |
| + base::TimeTicks current_amortization_run_start_time_; |
| + |
| + // Buffer of pre-amortization data use that has accumulated since the last |
| + // time amortization was performed, paired with the callbacks for each DataUse |
| + // object. Nothing but |buffered_data_use_| is allowed to hold linked_ptrs to |
| + // the DataUse objects, so that these linked_ptrs can be released later. |
| + std::vector<std::pair<linked_ptr<DataUse>, DataUseConsumerCallback>> |
| + buffered_data_use_; |
| + |
| + // Indicates if TrafficStats byte counts were available during the last time |
| + // amortization was performed. |
| + bool are_last_amortization_traffic_stats_available_; |
| + // The total transmitted bytes according to TrafficStats during the last time |
| + // amortization was performed, if they were available. |
| + int64_t last_amortization_traffic_stats_tx_bytes_; |
| + // The total received bytes according to TrafficStats during the last time |
| + // amortization was performed, if they were available. |
| + int64_t last_amortization_traffic_stats_rx_bytes_; |
| + |
| + // Total pre-amortization transmitted bytes since the last time amortization |
| + // was performed, including bytes from |buffered_data_use_| and any extra |
| + // bytes that were added. |
| + int64_t pre_amortization_tx_bytes_; |
| + |
| + // Total pre-amortization received bytes since the last time amortization was |
| + // performed, including bytes from |buffered_data_use_| and any extra bytes |
| + // that were added. |
| + int64_t pre_amortization_rx_bytes_; |
| + |
| + base::WeakPtrFactory<TrafficStatsAmortizer> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TrafficStatsAmortizer); |
| +}; |
| + |
| +} // namespace android |
| +} // namespace data_usage |
| + |
| +#endif // COMPONENTS_DATA_USAGE_ANDROID_TRAFFIC_STATS_AMORTIZER_H_ |