Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _RESOLVER_STATS_H_ |
| 18 | #define _RESOLVER_STATS_H_ |
| 19 | |
| 20 | #include <time.h> |
| 21 | |
| 22 | namespace android { |
| 23 | namespace net { |
| 24 | |
| 25 | struct ResolverStats { |
| 26 | // Offsets into the per-server resolver stats as encoded in vector<int32_t> stats of |
| 27 | // getResolverInfo() of Netd's binder interface. The stats are based on data reported by |
| 28 | // android_net_res_stats_get_info_for_net(), the usability is calculated by applying |
| 29 | // android_net_res_stats_get_usable_servers() to this data. |
| 30 | enum ResolverStatsOffsets { |
| 31 | STATS_SUCCESSES = 0, // # successes counted for this server |
| 32 | STATS_ERRORS, // # errors |
| 33 | STATS_TIMEOUTS, // # timeouts |
| 34 | STATS_INTERNAL_ERRORS, // # internal errors |
| 35 | STATS_RTT_AVG, // average round-trip-time |
| 36 | STATS_LAST_SAMPLE_TIME, // time in s when the last sample was recorded |
| 37 | STATS_USABLE, // whether the server is considered usable |
| 38 | STATS_COUNT // total count of integers in the per-server data |
| 39 | }; |
| 40 | |
| 41 | int successes{-1}; |
| 42 | int errors{-1}; |
| 43 | int timeouts{-1}; |
| 44 | int internal_errors{-1}; |
| 45 | int rtt_avg{-1}; |
| 46 | time_t last_sample_time{0}; |
| 47 | bool usable{false}; |
| 48 | |
| 49 | // Serialize the resolver stats to the end of |out|. |
| 50 | void encode(std::vector<int32_t>* out) const; |
| 51 | |
| 52 | // Read the serialized resolverstats starting at |in[ofs]|. |
| 53 | ssize_t decode(const std::vector<int32_t>& in, ssize_t ofs); |
| 54 | |
| 55 | // Serialize the contents of |stats| and append them to the end of |out|. Multiple arrays |
| 56 | // can be written to the same output vector in sequence, however, the corresponding call |
| 57 | // to decodeAll() will return the combined contents in one vector. |
| 58 | static void encodeAll(const std::vector<ResolverStats>& stats, std::vector<int32_t>* out); |
| 59 | |
| 60 | // Decodes the serialized ResolverStats from |in| and appends them to stats. |
| 61 | static bool decodeAll(const std::vector<int32_t>& in, std::vector<ResolverStats>* stats); |
| 62 | }; |
| 63 | |
| 64 | inline void ResolverStats::encode(std::vector<int32_t>* out) const { |
| 65 | size_t ofs = out->size(); |
| 66 | out->resize(ofs + STATS_COUNT); |
| 67 | int32_t* cur = &(*out)[ofs]; |
| 68 | cur[STATS_SUCCESSES] = successes; |
| 69 | cur[STATS_ERRORS] = errors; |
| 70 | cur[STATS_TIMEOUTS] = timeouts; |
| 71 | cur[STATS_INTERNAL_ERRORS] = internal_errors; |
| 72 | cur[STATS_RTT_AVG] = rtt_avg; |
| 73 | cur[STATS_LAST_SAMPLE_TIME] = last_sample_time; |
| 74 | cur[STATS_USABLE] = usable; |
| 75 | } |
| 76 | |
| 77 | // Read the serialized resolverstats starting at |in[ofs]|. |
| 78 | inline ssize_t ResolverStats::decode(const std::vector<int32_t>& in, ssize_t ofs) { |
| 79 | if (ofs < 0 || static_cast<size_t>(ofs) + STATS_COUNT > in.size()) { |
| 80 | return -1; |
| 81 | } |
| 82 | const int32_t* cur = &in[ofs]; |
| 83 | successes = cur[STATS_SUCCESSES]; |
| 84 | errors = cur[STATS_ERRORS]; |
| 85 | timeouts = cur[STATS_TIMEOUTS]; |
| 86 | internal_errors = cur[STATS_INTERNAL_ERRORS]; |
| 87 | rtt_avg = cur[STATS_RTT_AVG]; |
| 88 | last_sample_time = cur[STATS_LAST_SAMPLE_TIME]; |
| 89 | usable = cur[STATS_USABLE]; |
| 90 | return ofs + STATS_COUNT; |
| 91 | } |
| 92 | |
| 93 | inline void ResolverStats::encodeAll(const std::vector<ResolverStats>& stats, |
| 94 | std::vector<int32_t>* out) { |
| 95 | for (const auto& s : stats) { |
| 96 | s.encode(out); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // TODO: Replace with a better representation, e.g. a Parcelable. |
| 101 | inline bool ResolverStats::decodeAll(const std::vector<int32_t>& in, |
| 102 | std::vector<ResolverStats>* stats) { |
| 103 | ssize_t size = in.size(); |
| 104 | if (size % STATS_COUNT) { |
| 105 | return false; |
| 106 | } |
| 107 | stats->resize(size / STATS_COUNT); |
| 108 | ssize_t ofs = 0; |
| 109 | for (auto& s : *stats) { |
| 110 | ofs = s.decode(in, ofs); |
| 111 | if (ofs < 0) { |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | } // namespace net |
| 119 | } // namespace android |
| 120 | |
| 121 | #endif /* _RESOLVER_STATS_H_ */ |