Home | History | Annotate | Download | only in server
      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 
     65 inline void ResolverStats::encode(std::vector<int32_t>* out) const {
     66     size_t ofs = out->size();
     67     out->resize(ofs + STATS_COUNT);
     68     int32_t* cur = &(*out)[ofs];
     69     cur[STATS_SUCCESSES] = successes;
     70     cur[STATS_ERRORS] = errors;
     71     cur[STATS_TIMEOUTS] = timeouts;
     72     cur[STATS_INTERNAL_ERRORS] = internal_errors;
     73     cur[STATS_RTT_AVG] = rtt_avg;
     74     cur[STATS_LAST_SAMPLE_TIME] = last_sample_time;
     75     cur[STATS_USABLE] = usable;
     76 }
     77 
     78     // Read the serialized resolverstats starting at |in[ofs]|.
     79 inline ssize_t ResolverStats::decode(const std::vector<int32_t>& in, ssize_t ofs) {
     80     if (ofs < 0 || static_cast<size_t>(ofs) + STATS_COUNT > in.size()) {
     81         return -1;
     82     }
     83     const int32_t* cur = &in[ofs];
     84     successes = cur[STATS_SUCCESSES];
     85     errors = cur[STATS_ERRORS];
     86     timeouts = cur[STATS_TIMEOUTS];
     87     internal_errors = cur[STATS_INTERNAL_ERRORS];
     88     rtt_avg = cur[STATS_RTT_AVG];
     89     last_sample_time = cur[STATS_LAST_SAMPLE_TIME];
     90     usable = cur[STATS_USABLE];
     91     return ofs + STATS_COUNT;
     92 }
     93 
     94 inline void ResolverStats::encodeAll(const std::vector<ResolverStats>& stats,
     95         std::vector<int32_t>* out) {
     96     for (const auto& s : stats) {
     97         s.encode(out);
     98     }
     99 }
    100 
    101 // TODO: Replace with a better representation, e.g. a Parcelable.
    102 inline bool ResolverStats::decodeAll(const std::vector<int32_t>& in,
    103         std::vector<ResolverStats>* stats) {
    104     ssize_t size = in.size();
    105     if (size % STATS_COUNT) {
    106         return false;
    107     }
    108     stats->resize(size / STATS_COUNT);
    109     ssize_t ofs = 0;
    110     for (auto& s : *stats) {
    111         ofs = s.decode(in, ofs);
    112         if (ofs < 0) {
    113             return false;
    114         }
    115     }
    116     return true;
    117 }
    118 
    119 }  // namespace net
    120 }  // namespace android
    121 
    122 #endif /* _RESOLVER_STATS_H_ */
    123