OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COMPONENTS_DATA_USAGE_ANDROID_TRAFFIC_STATS_AMORTIZER_H_ | |
6 #define COMPONENTS_DATA_USAGE_ANDROID_TRAFFIC_STATS_AMORTIZER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/macros.h" | |
14 #include "base/memory/linked_ptr.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/threading/thread_checker.h" | |
18 #include "base/time/time.h" | |
19 #include "components/data_usage/core/data_use_amortizer.h" | |
20 | |
21 namespace base { | |
22 class TickClock; | |
23 class Timer; | |
24 } | |
25 | |
26 namespace data_usage { | |
27 | |
28 struct DataUse; | |
29 | |
30 namespace android { | |
31 | |
32 // Class that uses Android TrafficStats to amortize any unincluded overhead into | |
33 // the data usage reported by the network stack. Should only be used on the IO | |
34 // thread. Only one TrafficStatsAmortizer should be active globally at a time, | |
35 // since TrafficStats measurements are global for the entire application, so | |
36 // having multiple active TrafficStatsAmortizers at a time would cause each of | |
37 // them to overestimate the overhead here. | |
38 class TrafficStatsAmortizer : public DataUseAmortizer { | |
39 public: | |
40 TrafficStatsAmortizer(); | |
41 ~TrafficStatsAmortizer() override; | |
42 | |
43 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.
| |
44 const DataUseConsumerCallback& callback) override; | |
45 | |
46 void OnExtraBytes(int64_t extra_tx_bytes, int64_t extra_rx_bytes) override; | |
47 | |
48 base::WeakPtr<TrafficStatsAmortizer> GetWeakPtr(); | |
49 | |
50 protected: | |
51 // Constructor for testing purposes, allowing for tests to take full control | |
52 // over the timing of the TrafficStatsAmortizer and the byte counts returned | |
53 // from TrafficStats. |traffic_stats_query_timer| must not be a repeating | |
54 // timer. | |
55 TrafficStatsAmortizer(scoped_ptr<base::TickClock> tick_clock, | |
56 scoped_ptr<base::Timer> traffic_stats_query_timer, | |
57 const base::TimeDelta& traffic_stats_query_delay, | |
58 const base::TimeDelta& max_amortization_delay, | |
59 size_t max_data_use_buffer_size); | |
60 | |
61 // Query the total transmitted and received bytes for the application from | |
62 // TrafficStats. Stores the byte counts in |tx_bytes| and |rx_bytes| | |
63 // respectively and returns true if both values are available from | |
64 // TrafficStats, otherwise returns false. | |
65 // Virtual for testing. | |
66 virtual bool QueryTrafficStats(int64_t* tx_bytes, int64_t* rx_bytes) const; | |
67 | |
68 private: | |
69 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.
| |
70 | |
71 void AmortizeNow(); | |
72 | |
73 base::ThreadChecker thread_checker_; | |
74 | |
75 // TickClock for determining the current time tick. | |
76 scoped_ptr<base::TickClock> tick_clock_; | |
77 | |
78 // 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.
| |
79 // This must not be a repeating timer. | |
80 scoped_ptr<base::Timer> traffic_stats_query_timer_; | |
81 | |
82 // The delay between polls of TrafficStats byte counts for waiting for the | |
83 // 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
| |
84 const base::TimeDelta traffic_stats_query_delay_; | |
85 | |
86 // The maximum amount of time that the TrafficStatsAmortizer is allowed to | |
87 // spend waiting to do an amortization run. | |
88 const base::TimeDelta max_amortization_delay_; | |
89 | |
90 // The maximum allowed size of the |buffered_data_use_| buffer before DataUse | |
91 // 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
| |
92 const size_t max_data_use_buffer_size_; | |
93 | |
94 // Indicates whether or not the TrafficStatsAmortizer currently has | |
95 // pre-amortization bytes waiting for amortization to be performed. | |
96 bool is_amortization_run_in_progress_; | |
97 | |
98 // The time when the first pre-amortization bytes for the current amortization | |
99 // run were given to this TrafficStatsAmortizer. | |
100 base::TimeTicks current_amortization_run_start_time_; | |
101 | |
102 // Buffer of pre-amortization data use that has accumulated since the last | |
103 // time amortization was performed, paired with the callbacks for each DataUse | |
104 // object. Nothing but |buffered_data_use_| is allowed to hold linked_ptrs to | |
105 // the DataUse objects, so that these linked_ptrs can be released later. | |
106 std::vector<std::pair<linked_ptr<DataUse>, DataUseConsumerCallback>> | |
107 buffered_data_use_; | |
108 | |
109 // Indicates if TrafficStats byte counts were available during the last time | |
110 // amortization was performed. | |
111 bool are_last_amortization_traffic_stats_available_; | |
112 // The total transmitted bytes according to TrafficStats during the last time | |
113 // amortization was performed, if they were available. | |
114 int64_t last_amortization_traffic_stats_tx_bytes_; | |
115 // The total received bytes according to TrafficStats during the last time | |
116 // amortization was performed, if they were available. | |
117 int64_t last_amortization_traffic_stats_rx_bytes_; | |
118 | |
119 // Total pre-amortization transmitted bytes since the last time amortization | |
120 // was performed, including bytes from |buffered_data_use_| and any extra | |
121 // bytes that were added. | |
122 int64_t pre_amortization_tx_bytes_; | |
123 | |
124 // Total pre-amortization received bytes since the last time amortization was | |
125 // performed, including bytes from |buffered_data_use_| and any extra bytes | |
126 // that were added. | |
127 int64_t pre_amortization_rx_bytes_; | |
128 | |
129 base::WeakPtrFactory<TrafficStatsAmortizer> weak_ptr_factory_; | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(TrafficStatsAmortizer); | |
132 }; | |
133 | |
134 } // namespace android | |
135 } // namespace data_usage | |
136 | |
137 #endif // COMPONENTS_DATA_USAGE_ANDROID_TRAFFIC_STATS_AMORTIZER_H_ | |
OLD | NEW |