| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/core/quic_bandwidth.h" | 5 #include "net/quic/core/quic_bandwidth.h" |
| 6 | 6 |
| 7 #include <cinttypes> | 7 #include <cinttypes> |
| 8 #include <limits> | |
| 9 | 8 |
| 10 #include "net/quic/core/quic_constants.h" | |
| 11 #include "net/quic/core/quic_time.h" | |
| 12 #include "net/quic/core/quic_types.h" | |
| 13 #include "net/quic/platform/api/quic_bug_tracker.h" | |
| 14 #include "net/quic/platform/api/quic_str_cat.h" | 9 #include "net/quic/platform/api/quic_str_cat.h" |
| 15 | 10 |
| 16 namespace net { | 11 namespace net { |
| 17 | 12 |
| 18 // Highest number that QuicBandwidth can hold. | |
| 19 const int64_t kQuicInfiniteBandwidth = INT64_C(0x7fffffffffffffff); | |
| 20 | |
| 21 // static | |
| 22 QuicBandwidth QuicBandwidth::Zero() { | |
| 23 return QuicBandwidth(0); | |
| 24 } | |
| 25 | |
| 26 // static | |
| 27 QuicBandwidth QuicBandwidth::Infinite() { | |
| 28 return QuicBandwidth(std::numeric_limits<int64_t>::max()); | |
| 29 } | |
| 30 | |
| 31 // static | |
| 32 QuicBandwidth QuicBandwidth::FromBitsPerSecond(int64_t bits_per_second) { | |
| 33 return QuicBandwidth(bits_per_second); | |
| 34 } | |
| 35 | |
| 36 // static | |
| 37 QuicBandwidth QuicBandwidth::FromKBitsPerSecond(int64_t k_bits_per_second) { | |
| 38 DCHECK(k_bits_per_second < kQuicInfiniteBandwidth / 1000); | |
| 39 return QuicBandwidth(k_bits_per_second * 1000); | |
| 40 } | |
| 41 | |
| 42 // static | |
| 43 QuicBandwidth QuicBandwidth::FromBytesPerSecond(int64_t bytes_per_second) { | |
| 44 DCHECK(bytes_per_second < kQuicInfiniteBandwidth / 8); | |
| 45 return QuicBandwidth(bytes_per_second * 8); | |
| 46 } | |
| 47 | |
| 48 // static | |
| 49 QuicBandwidth QuicBandwidth::FromKBytesPerSecond(int64_t k_bytes_per_second) { | |
| 50 DCHECK(k_bytes_per_second < kQuicInfiniteBandwidth / 8000); | |
| 51 return QuicBandwidth(k_bytes_per_second * 8000); | |
| 52 } | |
| 53 | |
| 54 // static | |
| 55 QuicBandwidth QuicBandwidth::FromBytesAndTimeDelta(QuicByteCount bytes, | |
| 56 QuicTime::Delta delta) { | |
| 57 DCHECK_LT(bytes, static_cast<uint64_t>(kQuicInfiniteBandwidth / | |
| 58 (8 * kNumMicrosPerSecond))); | |
| 59 int64_t bytes_per_second = | |
| 60 (bytes * kNumMicrosPerSecond) / delta.ToMicroseconds(); | |
| 61 return QuicBandwidth(bytes_per_second * 8); | |
| 62 } | |
| 63 | |
| 64 QuicBandwidth::QuicBandwidth(int64_t bits_per_second) | |
| 65 : bits_per_second_(bits_per_second) { | |
| 66 if (bits_per_second < 0) { | |
| 67 QUIC_BUG << "Can't set negative bandwidth " << bits_per_second; | |
| 68 bits_per_second_ = 0; | |
| 69 return; | |
| 70 } | |
| 71 bits_per_second_ = bits_per_second; | |
| 72 } | |
| 73 | |
| 74 int64_t QuicBandwidth::ToBitsPerSecond() const { | |
| 75 return bits_per_second_; | |
| 76 } | |
| 77 | |
| 78 int64_t QuicBandwidth::ToKBitsPerSecond() const { | |
| 79 return bits_per_second_ / 1000; | |
| 80 } | |
| 81 | |
| 82 int64_t QuicBandwidth::ToBytesPerSecond() const { | |
| 83 return bits_per_second_ / 8; | |
| 84 } | |
| 85 | |
| 86 int64_t QuicBandwidth::ToKBytesPerSecond() const { | |
| 87 return bits_per_second_ / 8000; | |
| 88 } | |
| 89 | |
| 90 QuicByteCount QuicBandwidth::ToBytesPerPeriod( | |
| 91 QuicTime::Delta time_period) const { | |
| 92 return ToBytesPerSecond() * time_period.ToMicroseconds() / | |
| 93 kNumMicrosPerSecond; | |
| 94 } | |
| 95 | |
| 96 int64_t QuicBandwidth::ToKBytesPerPeriod(QuicTime::Delta time_period) const { | |
| 97 return ToKBytesPerSecond() * time_period.ToMicroseconds() / | |
| 98 kNumMicrosPerSecond; | |
| 99 } | |
| 100 | |
| 101 bool QuicBandwidth::IsZero() const { | |
| 102 return (bits_per_second_ == 0); | |
| 103 } | |
| 104 | |
| 105 QuicTime::Delta QuicBandwidth::TransferTime(QuicByteCount bytes) const { | |
| 106 if (bits_per_second_ == 0) { | |
| 107 return QuicTime::Delta::Zero(); | |
| 108 } | |
| 109 return QuicTime::Delta::FromMicroseconds(bytes * 8 * kNumMicrosPerSecond / | |
| 110 bits_per_second_); | |
| 111 } | |
| 112 | |
| 113 std::string QuicBandwidth::ToDebugValue() const { | 13 std::string QuicBandwidth::ToDebugValue() const { |
| 114 if (bits_per_second_ < 80000) { | 14 if (bits_per_second_ < 80000) { |
| 115 return QuicStringPrintf("%" PRId64 " bits/s (%" PRId64 " bytes/s)", | 15 return QuicStringPrintf("%" PRId64 " bits/s (%" PRId64 " bytes/s)", |
| 116 bits_per_second_, bits_per_second_ / 8); | 16 bits_per_second_, bits_per_second_ / 8); |
| 117 } | 17 } |
| 118 | 18 |
| 119 double divisor; | 19 double divisor; |
| 120 char unit; | 20 char unit; |
| 121 if (bits_per_second_ < 8 * 1000 * 1000) { | 21 if (bits_per_second_ < 8 * 1000 * 1000) { |
| 122 divisor = 1e3; | 22 divisor = 1e3; |
| 123 unit = 'k'; | 23 unit = 'k'; |
| 124 } else if (bits_per_second_ < INT64_C(8) * 1000 * 1000 * 1000) { | 24 } else if (bits_per_second_ < INT64_C(8) * 1000 * 1000 * 1000) { |
| 125 divisor = 1e6; | 25 divisor = 1e6; |
| 126 unit = 'M'; | 26 unit = 'M'; |
| 127 } else { | 27 } else { |
| 128 divisor = 1e9; | 28 divisor = 1e9; |
| 129 unit = 'G'; | 29 unit = 'G'; |
| 130 } | 30 } |
| 131 | 31 |
| 132 double bits_per_second_with_unit = bits_per_second_ / divisor; | 32 double bits_per_second_with_unit = bits_per_second_ / divisor; |
| 133 double bytes_per_second_with_unit = bits_per_second_with_unit / 8; | 33 double bytes_per_second_with_unit = bits_per_second_with_unit / 8; |
| 134 return QuicStringPrintf("%.2f %cbits/s (%.2f %cbytes/s)", | 34 return QuicStringPrintf("%.2f %cbits/s (%.2f %cbytes/s)", |
| 135 bits_per_second_with_unit, unit, | 35 bits_per_second_with_unit, unit, |
| 136 bytes_per_second_with_unit, unit); | 36 bytes_per_second_with_unit, unit); |
| 137 } | 37 } |
| 138 | 38 |
| 139 } // namespace net | 39 } // namespace net |
| OLD | NEW |