Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit be9e4f64 authored by Mike Yu's avatar Mike Yu
Browse files

Add stats samples by looking up server address

When adding a stats sample, it passes the server index rather than
the server address. It's not wrong if the order of dns servers in
lookup threads is fixed. However, it restrains the flexibility to
change the order as well.

This change fixes it to pass the server address so that the order
of dns servers in a lookup thread can change.

Bug: 137169582
Test: atest --include-subdirs packages/modules/DnsResolver
Change-Id: I1f4542165e90289efe8bf6d618639b82a8cc740e
parent 021c3e1e
Loading
Loading
Loading
Loading
+14 −7
Original line number Diff line number Diff line
@@ -1614,13 +1614,13 @@ void resolv_populate_res_for_net(ResState* statp) {

/* Resolver reachability statistics. */

static void _res_cache_add_stats_sample_locked(res_stats* stats, const res_sample* sample,
static void res_cache_add_stats_sample_locked(res_stats* stats, const res_sample& sample,
                                              int max_samples) {
    // Note: This function expects max_samples > 0, otherwise a (harmless) modification of the
    // allocated but supposedly unused memory for samples[0] will happen
    LOG(INFO) << __func__ << ": adding sample to stats, next = " << unsigned(stats->sample_next)
              << ", count = " << unsigned(stats->sample_count);
    stats->samples[stats->sample_next] = *sample;
    stats->samples[stats->sample_next] = sample;
    if (stats->sample_count < max_samples) {
        ++stats->sample_count;
    }
@@ -1733,15 +1733,22 @@ int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stat
    return -1;
}

void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
                                             const res_sample* sample, int max_samples) {
    if (max_samples <= 0) return;
void resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, const sockaddr* sa,
                                            const res_sample& sample, int max_samples) {
    if (max_samples <= 0 || sa == nullptr) return;

    std::lock_guard guard(cache_mutex);
    resolv_cache_info* info = find_cache_info_locked(netid);

    if (info && info->revision_id == revision_id) {
        _res_cache_add_stats_sample_locked(&info->nsstats[ns], sample, max_samples);
        const int serverNum = std::min(MAXNS, static_cast<int>(info->nameserverSockAddrs.size()));
        const IPSockAddr ipsa = IPSockAddr::toIPSockAddr(*sa);
        for (int ns = 0; ns < serverNum; ns++) {
            if (ipsa == info->nameserverSockAddrs.at(ns)) {
                res_cache_add_stats_sample_locked(&info->nsstats[ns], sample, max_samples);
                return;
            }
        }
    }
}

+4 −4
Original line number Diff line number Diff line
@@ -542,7 +542,7 @@ int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int
                if (shouldRecordStats) {
                    res_sample sample;
                    _res_stats_set_sample(&sample, now, *rcode, delay);
                    _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
                    resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, nsap, sample,
                                                           params.max_samples);
                    resolv_stats_add(statp->netid, IPSockAddr::toIPSockAddr(*nsap), dnsQueryEvent);
                }
@@ -576,7 +576,7 @@ int res_nsend(res_state statp, const uint8_t* buf, int buflen, uint8_t* ans, int
                if (attempt == 0) {
                    res_sample sample;
                    _res_stats_set_sample(&sample, now, *rcode, delay);
                    _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
                    resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, nsap, sample,
                                                           params.max_samples);
                    resolv_stats_add(statp->netid, IPSockAddr::toIPSockAddr(*nsap), dnsQueryEvent);
                }
+2 −2
Original line number Diff line number Diff line
@@ -114,8 +114,8 @@ int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stat
/* Add a sample to the shared struct for the given netid and server, provided that the
 * revision_id of the stored servers has not changed.
 */
void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
                                             const res_sample* sample, int max_samples);
void resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, const sockaddr* sa,
                                            const res_sample& sample, int max_samples);

// Calculate the round-trip-time from start time t0 and end time t1.
int _res_stats_calculate_rtt(const timespec* t1, const timespec* t0);