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 #include "components/data_usage/android/traffic_stats_amortizer.h" | |
6 | |
7 #include <cmath> | |
8 | |
9 #include "net/android/traffic_stats.h" | |
10 | |
11 namespace data_usage { | |
12 namespace android { | |
13 | |
14 namespace { | |
15 | |
16 int64_t AmortizeByRatio(int64_t bytes, double ratio, double* remainder) { | |
tbansal1
2015/10/12 21:23:20
please add comments.
| |
17 DCHECK(remainder); | |
18 | |
19 double intpart; | |
tbansal1
2015/10/12 21:23:20
may be initialize to -1, than DCHECK_GE() below wo
| |
20 *remainder = | |
21 std::modf(static_cast<double>(bytes) * ratio + (*remainder), &intpart); | |
22 | |
23 DCHECK_GE(intpart, 0.0); | |
24 DCHECK_LE(intpart, static_cast<double>(INT64_MAX)); | |
25 return static_cast<int64_t>(intpart); | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 TrafficStatsAmortizer::TrafficStatsAmortizer() | |
31 : are_traffic_stats_available_(false), | |
32 traffic_stats_tx_bytes_(0), | |
33 traffic_stats_rx_bytes_(0) {} | |
34 | |
35 TrafficStatsAmortizer::~TrafficStatsAmortizer() {} | |
36 | |
37 void TrafficStatsAmortizer::Amortize(std::vector<DataUse>* data_use_sequence, | |
38 int64_t extra_tx_bytes, | |
39 int64_t extra_rx_bytes) { | |
40 DCHECK(thread_checker_.CalledOnValidThread()); | |
41 DCHECK(!data_use_sequence->empty()); | |
42 | |
43 bool previous_are_traffic_stats_available = are_traffic_stats_available_; | |
44 int64_t previous_traffic_stats_tx_bytes = traffic_stats_tx_bytes_; | |
45 int64_t previous_traffic_stats_rx_bytes = traffic_stats_rx_bytes_; | |
46 | |
47 // TODO(sclittle): Currently, the TrafficStats numbers don't seem to update | |
48 // fast enough for the amortization to be accurate at this frequency. Find | |
49 // some way to smooth out these numbers nicely, e.g. amortize over a longer | |
50 // period of time, not just the most recent delta. | |
51 RefreshTrafficStats(); | |
bengr
2015/10/12 21:33:08
Say more. How far behind is TrafficStats?
| |
52 | |
53 if (!previous_are_traffic_stats_available || !are_traffic_stats_available_) | |
54 return; | |
55 | |
56 int64_t traffic_stats_tx_delta_bytes = | |
57 traffic_stats_tx_bytes_ - previous_traffic_stats_tx_bytes; | |
58 int64_t traffic_stats_rx_delta_bytes = | |
59 traffic_stats_rx_bytes_ - previous_traffic_stats_rx_bytes; | |
60 | |
61 int64_t total_unamortized_tx_delta_bytes = extra_tx_bytes; | |
62 int64_t total_unamortized_rx_delta_bytes = extra_rx_bytes; | |
63 for (const DataUse& data_use : *data_use_sequence) { | |
64 total_unamortized_tx_delta_bytes += data_use.tx_bytes; | |
65 total_unamortized_rx_delta_bytes += data_use.rx_bytes; | |
66 } | |
67 | |
68 const double tx_ratio = static_cast<double>(traffic_stats_tx_delta_bytes) / | |
69 static_cast<double>(total_unamortized_tx_delta_bytes); | |
tbansal1
2015/10/12 21:23:20
can this give divide by 0 error? It probably can d
bengr
2015/10/12 21:33:08
Agreed.
| |
70 const double rx_ratio = static_cast<double>(traffic_stats_rx_delta_bytes) / | |
71 static_cast<double>(total_unamortized_rx_delta_bytes); | |
72 | |
73 double tx_remainder = 0.0; | |
74 double rx_remainder = 0.0; | |
75 for (DataUse& data_use : *data_use_sequence) { | |
76 data_use.tx_bytes = | |
77 AmortizeByRatio(data_use.tx_bytes, tx_ratio, &tx_remainder); | |
78 data_use.rx_bytes = | |
79 AmortizeByRatio(data_use.rx_bytes, rx_ratio, &rx_remainder); | |
80 } | |
tbansal1
2015/10/12 21:23:20
DCHECK that tx_remainder and rx_remainder < 1 and
| |
81 | |
82 // TODO(sclittle): Maybe record some UMA comparing amortized values to | |
83 // unamortized values? | |
84 } | |
85 | |
86 bool TrafficStatsAmortizer::GetTrafficStatsTxBytes(int64_t* tx_bytes) const { | |
87 return net::android::traffic_stats::GetCurrentUidTxBytes(tx_bytes); | |
tbansal1
2015/10/12 21:23:20
DCHECK(thread_checker_...)
| |
88 } | |
89 | |
90 bool TrafficStatsAmortizer::GetTrafficStatsRxBytes(int64_t* rx_bytes) const { | |
91 return net::android::traffic_stats::GetCurrentUidRxBytes(rx_bytes); | |
92 } | |
93 | |
94 void TrafficStatsAmortizer::RefreshTrafficStats() { | |
95 DCHECK(thread_checker_.CalledOnValidThread()); | |
96 | |
97 are_traffic_stats_available_ = | |
98 GetTrafficStatsTxBytes(&traffic_stats_tx_bytes_) && | |
bengr
2015/10/12 21:33:08
Can tx_bytes_ or rx_bytes_ be 0?
| |
99 GetTrafficStatsRxBytes(&traffic_stats_rx_bytes_); | |
100 } | |
101 | |
102 } // namespace android | |
103 } // namespace data_usage | |
OLD | NEW |