QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // QuicBandwidth represents a bandwidth, stored in bits per second resolution. |
| 6 | |
| 7 | #ifndef QUICHE_QUIC_CORE_QUIC_BANDWIDTH_H_ |
| 8 | #define QUICHE_QUIC_CORE_QUIC_BANDWIDTH_H_ |
| 9 | |
| 10 | #include <cmath> |
| 11 | #include <cstdint> |
| 12 | #include <limits> |
| 13 | #include <ostream> |
| 14 | |
| 15 | #include "net/third_party/quiche/src/quic/core/quic_constants.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/quic_time.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
| 19 | #include "net/third_party/quiche/src/quic/platform/api/quic_string.h" |
| 20 | |
| 21 | namespace quic { |
| 22 | |
| 23 | class QUIC_EXPORT_PRIVATE QuicBandwidth { |
| 24 | public: |
| 25 | // Creates a new QuicBandwidth with an internal value of 0. |
| 26 | static constexpr QuicBandwidth Zero() { return QuicBandwidth(0); } |
| 27 | |
| 28 | // Creates a new QuicBandwidth with an internal value of INT64_MAX. |
| 29 | static constexpr QuicBandwidth Infinite() { |
| 30 | return QuicBandwidth(std::numeric_limits<int64_t>::max()); |
| 31 | } |
| 32 | |
| 33 | // Create a new QuicBandwidth holding the bits per second. |
| 34 | static constexpr QuicBandwidth FromBitsPerSecond(int64_t bits_per_second) { |
| 35 | return QuicBandwidth(bits_per_second); |
| 36 | } |
| 37 | |
| 38 | // Create a new QuicBandwidth holding the kilo bits per second. |
| 39 | static constexpr QuicBandwidth FromKBitsPerSecond(int64_t k_bits_per_second) { |
| 40 | return QuicBandwidth(k_bits_per_second * 1000); |
| 41 | } |
| 42 | |
| 43 | // Create a new QuicBandwidth holding the bytes per second. |
| 44 | static constexpr QuicBandwidth FromBytesPerSecond(int64_t bytes_per_second) { |
| 45 | return QuicBandwidth(bytes_per_second * 8); |
| 46 | } |
| 47 | |
| 48 | // Create a new QuicBandwidth holding the kilo bytes per second. |
| 49 | static constexpr QuicBandwidth FromKBytesPerSecond( |
| 50 | int64_t k_bytes_per_second) { |
| 51 | return QuicBandwidth(k_bytes_per_second * 8000); |
| 52 | } |
| 53 | |
| 54 | // Create a new QuicBandwidth based on the bytes per the elapsed delta. |
| 55 | static inline QuicBandwidth FromBytesAndTimeDelta(QuicByteCount bytes, |
| 56 | QuicTime::Delta delta) { |
| 57 | return QuicBandwidth((bytes * kNumMicrosPerSecond) / |
| 58 | delta.ToMicroseconds() * 8); |
| 59 | } |
| 60 | |
| 61 | inline int64_t ToBitsPerSecond() const { return bits_per_second_; } |
| 62 | |
| 63 | inline int64_t ToKBitsPerSecond() const { return bits_per_second_ / 1000; } |
| 64 | |
| 65 | inline int64_t ToBytesPerSecond() const { return bits_per_second_ / 8; } |
| 66 | |
| 67 | inline int64_t ToKBytesPerSecond() const { return bits_per_second_ / 8000; } |
| 68 | |
| 69 | inline QuicByteCount ToBytesPerPeriod(QuicTime::Delta time_period) const { |
| 70 | return ToBytesPerSecond() * time_period.ToMicroseconds() / |
| 71 | kNumMicrosPerSecond; |
| 72 | } |
| 73 | |
| 74 | inline int64_t ToKBytesPerPeriod(QuicTime::Delta time_period) const { |
| 75 | return ToKBytesPerSecond() * time_period.ToMicroseconds() / |
| 76 | kNumMicrosPerSecond; |
| 77 | } |
| 78 | |
| 79 | inline bool IsZero() const { return bits_per_second_ == 0; } |
| 80 | |
| 81 | inline QuicTime::Delta TransferTime(QuicByteCount bytes) const { |
| 82 | if (bits_per_second_ == 0) { |
| 83 | return QuicTime::Delta::Zero(); |
| 84 | } |
| 85 | return QuicTime::Delta::FromMicroseconds(bytes * 8 * kNumMicrosPerSecond / |
| 86 | bits_per_second_); |
| 87 | } |
| 88 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 89 | std::string ToDebugValue() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | explicit constexpr QuicBandwidth(int64_t bits_per_second) |
| 93 | : bits_per_second_(bits_per_second >= 0 ? bits_per_second : 0) {} |
| 94 | |
| 95 | int64_t bits_per_second_; |
| 96 | |
| 97 | friend QuicBandwidth operator+(QuicBandwidth lhs, QuicBandwidth rhs); |
| 98 | friend QuicBandwidth operator-(QuicBandwidth lhs, QuicBandwidth rhs); |
| 99 | friend QuicBandwidth operator*(QuicBandwidth lhs, float factor); |
| 100 | }; |
| 101 | |
| 102 | // Non-member relational operators for QuicBandwidth. |
| 103 | inline bool operator==(QuicBandwidth lhs, QuicBandwidth rhs) { |
| 104 | return lhs.ToBitsPerSecond() == rhs.ToBitsPerSecond(); |
| 105 | } |
| 106 | inline bool operator!=(QuicBandwidth lhs, QuicBandwidth rhs) { |
| 107 | return !(lhs == rhs); |
| 108 | } |
| 109 | inline bool operator<(QuicBandwidth lhs, QuicBandwidth rhs) { |
| 110 | return lhs.ToBitsPerSecond() < rhs.ToBitsPerSecond(); |
| 111 | } |
| 112 | inline bool operator>(QuicBandwidth lhs, QuicBandwidth rhs) { |
| 113 | return rhs < lhs; |
| 114 | } |
| 115 | inline bool operator<=(QuicBandwidth lhs, QuicBandwidth rhs) { |
| 116 | return !(rhs < lhs); |
| 117 | } |
| 118 | inline bool operator>=(QuicBandwidth lhs, QuicBandwidth rhs) { |
| 119 | return !(lhs < rhs); |
| 120 | } |
| 121 | |
| 122 | // Non-member arithmetic operators for QuicBandwidth. |
| 123 | inline QuicBandwidth operator+(QuicBandwidth lhs, QuicBandwidth rhs) { |
| 124 | return QuicBandwidth(lhs.bits_per_second_ + rhs.bits_per_second_); |
| 125 | } |
| 126 | inline QuicBandwidth operator-(QuicBandwidth lhs, QuicBandwidth rhs) { |
| 127 | return QuicBandwidth(lhs.bits_per_second_ - rhs.bits_per_second_); |
| 128 | } |
| 129 | inline QuicBandwidth operator*(QuicBandwidth lhs, float rhs) { |
| 130 | return QuicBandwidth( |
| 131 | static_cast<int64_t>(std::llround(lhs.bits_per_second_ * rhs))); |
| 132 | } |
| 133 | inline QuicBandwidth operator*(float lhs, QuicBandwidth rhs) { |
| 134 | return rhs * lhs; |
| 135 | } |
| 136 | inline QuicByteCount operator*(QuicBandwidth lhs, QuicTime::Delta rhs) { |
| 137 | return lhs.ToBytesPerPeriod(rhs); |
| 138 | } |
| 139 | inline QuicByteCount operator*(QuicTime::Delta lhs, QuicBandwidth rhs) { |
| 140 | return rhs * lhs; |
| 141 | } |
| 142 | |
| 143 | // Override stream output operator for gtest. |
| 144 | inline std::ostream& operator<<(std::ostream& output, |
| 145 | const QuicBandwidth bandwidth) { |
| 146 | output << bandwidth.ToDebugValue(); |
| 147 | return output; |
| 148 | } |
| 149 | |
| 150 | } // namespace quic |
| 151 | #endif // QUICHE_QUIC_CORE_QUIC_BANDWIDTH_H_ |