summaryrefslogtreecommitdiffstats
path: root/mDNSResponder/mDNSMacOSX/Metrics.m
blob: 56467dfc17eed848cd9b1ee9b07d2998a3e16a71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/* -*- Mode: C; tab-width: 4 -*-
 *
 * Copyright (c) 2016 Apple Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "Metrics.h"

#if TARGET_OS_EMBEDDED
#import <CoreUtils/SoftLinking.h>
#import <WirelessDiagnostics/AWDDNSDomainStats.h>
#import <WirelessDiagnostics/AWDMDNSResponderDNSStatistics.h>
#import <WirelessDiagnostics/AWDMetricIds_MDNSResponder.h>
#import <WirelessDiagnostics/WirelessDiagnostics.h>

#import "DNSCommon.h"
#import "mDNSMacOSX.h"
#import "DebugServices.h"

//===========================================================================================================================
//  External Frameworks
//===========================================================================================================================

SOFT_LINK_FRAMEWORK(PrivateFrameworks, WirelessDiagnostics)

SOFT_LINK_CLASS(WirelessDiagnostics, AWDServerConnection)
#define AWDServerConnectionSoft                 getAWDServerConnectionClass()

SOFT_LINK_CLASS(WirelessDiagnostics, AWDMDNSResponderDNSStatistics)
#define AWDMDNSResponderDNSStatisticsSoft       getAWDMDNSResponderDNSStatisticsClass()

SOFT_LINK_CLASS(WirelessDiagnostics, AWDDNSDomainStats)
#define AWDDNSDomainStatsSoft                   getAWDDNSDomainStatsClass()

//===========================================================================================================================
//  Macros
//===========================================================================================================================

#define countof(X)      (sizeof(X) / sizeof(X[0]))

//===========================================================================================================================
//  Constants
//===========================================================================================================================

#define kUDNSStatsMaxQuerySendCount     10

// Important: Do not update this list without getting privacy approval. See <rdar://problem/24155761>.

static const char * const       kUDNSStatsDomains[] =
{
    ".",
    "apple.com.",
    "icloud.com.",
    "me.com.",
    "google.com.",
    "facebook.com.",
    "youtube.com.",
    "baidu.com.",
    "amazon.com.",
    "yahoo.com.",
    "wikipedia.org."
};

static const mDNSu32        kResponseLatencyMsLimits[] =
{
        1,     2,     3,     4,     5,
       10,    20,    30,    40,    50,    60,    70,    80,    90,
      100,   110,   120,   130,   140,   150,   160,   170,   180,   190,
      200,   250,   300,   350,   400,   450,   500,   550,   600,   650,   700,   750,   800,   850,   900,   950,
     1000,  1500,  2000,  2500,  3000,  3500,  4000,  4500,
     5000,  6000,  7000,  8000,  9000,
    10000
};

//===========================================================================================================================
//  Data structures
//===========================================================================================================================

typedef struct
{
    uint32_t    answeredQuerySendCountBins[kUDNSStatsMaxQuerySendCount + 1];
    uint32_t    unansweredQuerySendCountBins[kUDNSStatsMaxQuerySendCount + 1];
    uint32_t    responseLatencyBins[countof(kResponseLatencyMsLimits) + 1];

}   DNSStats;

typedef struct DNSDomainStats * DNSDomainStatsRef;
struct DNSDomainStats
{
    DNSDomainStatsRef   next;
    domainname          domain;
    int                 domainLabelCount;
    char *              domainStr;
    DNSStats            stats;
    DNSStats            statsCellular;
};

//===========================================================================================================================
//  Globals
//===========================================================================================================================

extern mDNS     mDNSStorage;

static DNSDomainStatsRef            gDomainStatsList        = NULL;
static AWDServerConnection *        gAWDServerConnection    = nil;

//===========================================================================================================================
//  Local Prototypes
//===========================================================================================================================

mDNSlocal mStatus   DNSDomainStatsCreate(const char *inDomain, DNSDomainStatsRef *outStats);
mDNSlocal void      DNSDomainStatsFree(DNSDomainStatsRef inStats);
mDNSlocal void      DNSDomainStatsFreeList(DNSDomainStatsRef inList);

mDNSlocal mStatus   CreateDomainStatsList(DNSDomainStatsRef *outList);
mDNSlocal void      UpdateDNSStats(DNSStats *inStats, mDNSBool inAnswered, mDNSu32 inQuerySendCount, mDNSu32 inLatencyMs);
mDNSlocal mStatus   SubmitAWDMetric(void);
mDNSlocal mStatus   CreateAWDDNSDomainStats(DNSStats *inStats, const char *inDomain, mDNSBool inIsForCellular, AWDDNSDomainStats **outAWDStats);
mDNSlocal void      LogDNSStats(const DNSStats *inStats);

//===========================================================================================================================
//  MetricsInit
//===========================================================================================================================

mStatus MetricsInit(void)
{
    mStatus     err;

    err = CreateDomainStatsList(&gDomainStatsList);
    require_noerr_quiet(err, exit);

    @autoreleasepool
    {
        gAWDServerConnection = [[AWDServerConnectionSoft alloc]
            initWithComponentId:        AWDComponentId_MDNSResponder
            andBlockOnConfiguration:    NO];

        if (gAWDServerConnection)
        {
            [gAWDServerConnection
                registerQueriableMetricCallback: ^(UInt32 metricId)
                {
                    mStatus localErr;

                    (void) metricId;

                    localErr = SubmitAWDMetric();
                    if (localErr) LogMsg("SubmitAWDMetric failed with error %d", localErr);
                }
                forIdentifier: (UInt32)AWDMetricId_MDNSResponder_DNSStatistics];
        }
    }
exit:
    return (err);
}

//===========================================================================================================================
//  MetricsUpdateUDNSStats
//===========================================================================================================================

mDNSexport void MetricsUpdateUDNSStats(const domainname *inQueryName, mDNSBool inAnswered, mDNSu32 inSendCount, mDNSu32 inLatencyMs, mDNSBool inForCellular)
{
    DNSStats *              stats;
    DNSDomainStatsRef       domainStats;
    int                     queryLabelCount;
    mDNSBool                isQueryInDomain;

    queryLabelCount = CountLabels(inQueryName);

    for (domainStats = gDomainStatsList; domainStats; domainStats = domainStats->next)
    {
        isQueryInDomain = mDNSfalse;
        if (strcmp(domainStats->domainStr, ".") == 0)
        {
            // All queries are in the root domain.
            isQueryInDomain = mDNStrue;
        }
        else
        {
            int                     skipCount;
            const domainname *      queryParentDomain;

            skipCount = queryLabelCount - domainStats->domainLabelCount;
            if (skipCount >= 0)
            {
                queryParentDomain = SkipLeadingLabels(inQueryName, skipCount);
                isQueryInDomain = SameDomainName(queryParentDomain, &domainStats->domain);
            }
        }

        if (isQueryInDomain)
        {
            stats = inForCellular ? &domainStats->statsCellular : &domainStats->stats;
            UpdateDNSStats(stats, inAnswered, inSendCount, inLatencyMs);
        }
    }
}

//===========================================================================================================================
//  CreateDomainStatsList
//===========================================================================================================================

mDNSlocal mStatus CreateDomainStatsList(DNSDomainStatsRef *outList)
{
    mStatus                 err;
    size_t                  i;
    DNSDomainStatsRef       domainStats;
    DNSDomainStatsRef *     p;
    DNSDomainStatsRef       list = NULL;

    p = &list;
    for (i = 0; i < countof(kUDNSStatsDomains); ++i)
    {
        err = DNSDomainStatsCreate(kUDNSStatsDomains[i], &domainStats);
        require_noerr_quiet(err, exit);

        *p = domainStats;
        p = &domainStats->next;
    }

    *outList = list;
    list = NULL;

exit:
    DNSDomainStatsFreeList(list);
    return (err);
}

//===========================================================================================================================
//  UpdateDNSStats
//===========================================================================================================================

mDNSlocal void UpdateDNSStats(DNSStats *inStats, mDNSBool inAnswered, mDNSu32 inQuerySendCount, mDNSu32 inLatencyMs)
{
    size_t      i;

    if (inAnswered)
    {
        i = (inQuerySendCount <= kUDNSStatsMaxQuerySendCount) ? inQuerySendCount : kUDNSStatsMaxQuerySendCount;
        inStats->answeredQuerySendCountBins[i]++;

        if (inQuerySendCount > 0)
        {
            for (i = 0; (i < countof(kResponseLatencyMsLimits)) && (inLatencyMs >= kResponseLatencyMsLimits[i]); ++i) {}
            inStats->responseLatencyBins[i]++;
        }
    }
    else if (inQuerySendCount > 0)
    {
        i = (inQuerySendCount <= kUDNSStatsMaxQuerySendCount) ? inQuerySendCount : kUDNSStatsMaxQuerySendCount;
        inStats->unansweredQuerySendCountBins[i]++;
    }
}

//===========================================================================================================================
//  SubmitAWDMetric
//===========================================================================================================================

mDNSlocal mStatus SubmitAWDMetric(void)
{
    mStatus                             err;
    BOOL                                success;
    DNSDomainStatsRef                   domainStats;
    DNSDomainStatsRef                   newDomainStatsList;
    DNSDomainStatsRef                   domainStatsList = NULL;
    AWDMetricContainer *                container       = nil;
    AWDMDNSResponderDNSStatistics *     metric          = nil;
    AWDDNSDomainStats *                 awdDomainStats  = nil;

    err = CreateDomainStatsList(&newDomainStatsList);
    require_noerr_quiet(err, exit);

    domainStatsList = gDomainStatsList;

    KQueueLock(&mDNSStorage);
    gDomainStatsList = newDomainStatsList;
    KQueueUnlock(&mDNSStorage, "SubmitAWDMetric");

    container = [gAWDServerConnection newMetricContainerWithIdentifier:AWDMetricId_MDNSResponder_DNSStatistics];
    require_action_quiet(container, exit, err = mStatus_UnknownErr);

    metric = [[AWDMDNSResponderDNSStatisticsSoft alloc] init];
    require_action_quiet(metric, exit, err = mStatus_UnknownErr);

    for (domainStats = domainStatsList; domainStats; domainStats = domainStats->next)
    {
        err = CreateAWDDNSDomainStats(&domainStats->stats, domainStats->domainStr, mDNSfalse, &awdDomainStats);
        require_noerr_quiet(err, exit);

        [metric addStats:awdDomainStats];
        [awdDomainStats release];
        awdDomainStats = nil;

        err = CreateAWDDNSDomainStats(&domainStats->statsCellular, domainStats->domainStr, mDNStrue, &awdDomainStats);
        require_noerr_quiet(err, exit);

        [metric addStats:awdDomainStats];
        [awdDomainStats release];
        awdDomainStats = nil;
    }

    container.metric = metric;
    success = [gAWDServerConnection submitMetric:container];
    LogMsg("SubmitAWDMetric: metric submission %s.", success ? "succeeded" : "failed" );
    err = success ? mStatus_NoError : mStatus_UnknownErr;

exit:
    [awdDomainStats release];
    [metric release];
    [container release];
    DNSDomainStatsFreeList(domainStatsList);
    return (err);
}

//===========================================================================================================================
//  DNSDomainStatsCreate
//===========================================================================================================================

mDNSlocal mStatus DNSDomainStatsCreate(const char *inDomain, DNSDomainStatsRef *outStats)
{
    mStatus                 err;
    DNSDomainStatsRef       obj;
    mDNSu8 *                ptr;

    obj = (DNSDomainStatsRef) calloc(1, sizeof(*obj));
    require_action_quiet(obj, exit, err = mStatus_NoMemoryErr);

    obj->domainStr = strdup(inDomain);
    require_action_quiet(obj, exit, err = mStatus_NoMemoryErr);

    // Initialize domainname for non-root domains.

    if (strcmp(obj->domainStr, ".") != 0)
    {
        ptr = MakeDomainNameFromDNSNameString(&obj->domain, obj->domainStr);
        require_action_quiet(ptr, exit, err = mStatus_Invalid);
        obj->domainLabelCount = CountLabels(&obj->domain);
    }

    *outStats = obj;
    obj = NULL;
    err = mStatus_NoError;

exit:
    if (obj) DNSDomainStatsFree(obj);
    return (err);
}

//===========================================================================================================================
//  DNSDomainStatsFree
//===========================================================================================================================

mDNSlocal void DNSDomainStatsFree(DNSDomainStatsRef inStats)
{
    if (inStats->domainStr) free(inStats->domainStr);
    free(inStats);
}

//===========================================================================================================================
//  DNSDomainStatsFreeList
//===========================================================================================================================

mDNSlocal void DNSDomainStatsFreeList(DNSDomainStatsRef inList)
{
    DNSDomainStatsRef       stats;

    while ((stats = inList) != NULL)
    {
        inList = stats->next;
        DNSDomainStatsFree(stats);
    }
}

//===========================================================================================================================
//  CreateAWDDNSDomainStats
//===========================================================================================================================

mDNSlocal mStatus CreateAWDDNSDomainStats(DNSStats *inStats, const char *inDomain, mDNSBool inIsCellType, AWDDNSDomainStats **outAWDStats)
{
    mStatus                 err;
    AWDDNSDomainStats *     awdStats    = nil;
    NSString *              domain      = nil;

    awdStats = [[AWDDNSDomainStatsSoft alloc] init];
    require_action_quiet(awdStats, exit, err = mStatus_UnknownErr);

    domain = [[NSString alloc] initWithUTF8String:inDomain];
    require_action_quiet(domain, exit, err = mStatus_UnknownErr);

    awdStats.domain = domain;
    awdStats.networkType = inIsCellType ? AWDDNSDomainStats_NetworkType_Cellular : AWDDNSDomainStats_NetworkType_NonCellular;

    [awdStats
        setAnsweredQuerySendCounts: inStats->answeredQuerySendCountBins
        count:                      (NSUInteger)countof(inStats->answeredQuerySendCountBins)];

    [awdStats
        setUnansweredQuerySendCounts:   inStats->unansweredQuerySendCountBins
        count:                          (NSUInteger)countof(inStats->unansweredQuerySendCountBins)];

    [awdStats
        setResponseLatencyMs:   inStats->responseLatencyBins
        count:                  (NSUInteger)countof(inStats->responseLatencyBins)];

    *outAWDStats = awdStats;
    awdStats = nil;
    err = mStatus_NoError;

exit:
    [domain release];
    [awdStats release];
    return (err);
}

//===========================================================================================================================
//  LogDNSStats
//===========================================================================================================================

#define Percent(N, D)       ((N) * 100) / (D), (((N) * 10000) / (D)) % 100
#define PercentFmt          "%3u.%02u"
#define LogStat(LABEL, COUNT, ACCUMULATOR, TOTAL) \
    LogMsgNoIdent("%s %5u " PercentFmt " " PercentFmt, (LABEL), (COUNT), Percent(COUNT, TOTAL), Percent(ACCUMULATOR, TOTAL))

mDNSlocal void LogDNSStats(const DNSStats *inStats)
{
    uint32_t        total;
    uint32_t        totalUnanswered;
    size_t          i;
    char            label[16];

    totalUnanswered = 0;
    for (i = 0; i < countof(inStats->unansweredQuerySendCountBins); ++i)
    {
        totalUnanswered += inStats->unansweredQuerySendCountBins[i];
    }

    total = 0;
    for (i = 0; i <= countof(inStats->answeredQuerySendCountBins); ++i)
    {
        total += inStats->answeredQuerySendCountBins[i];
    }

    LogMsgNoIdent("Answered questions   %5u", total);
    LogMsgNoIdent("Unanswered questions %5u", totalUnanswered);
    LogMsgNoIdent("+++ Number of queries sent +++");
    if (total > 0)
    {
        uint32_t        accumulator = 0;

        for (i = 0; i < countof(inStats->answeredQuerySendCountBins); ++i)
        {
            uint32_t            count;
            const char *        suffix;

            count = inStats->answeredQuerySendCountBins[i];
            accumulator += count;
            suffix = (i < (countof(inStats->answeredQuerySendCountBins) - 1)) ? " " : "+";
            snprintf(label, sizeof(label), "%2d%s", (int)i, suffix);
            LogStat(label, count, accumulator, total);
        }
    }
    else
    {
        LogMsgNoIdent("No data.");
    }

    total = 0;
    for (i = 0; i < countof(inStats->responseLatencyBins); ++i)
    {
        total += inStats->responseLatencyBins[i];
    }

    LogMsgNoIdent("+++++++ Response times +++++++");
    if (total > 0)
    {
        uint32_t        accumulator = 0;

        for (i = 0; i < countof(inStats->responseLatencyBins); ++i)
        {
            uint32_t        count;

            count = inStats->responseLatencyBins[i];
            accumulator += count;
            if (i < countof(kResponseLatencyMsLimits))
            {
                snprintf(label, sizeof(label), "< %5u ms", kResponseLatencyMsLimits[i]);
            }
            else
            {
                snprintf(label, sizeof(label), "<     ∞ ms");
            }
            LogStat(label, count, accumulator, total);
            if (accumulator == total) break;
        }
    }
    else
    {
        LogMsgNoIdent("No data.");
    }
}

//===========================================================================================================================
//  LogMetrics
//===========================================================================================================================

mDNSexport void LogMetrics(void)
{
    DNSDomainStatsRef       domainStats;

    LogMsgNoIdent("---- DNS stats by domain -----");

    for (domainStats = gDomainStatsList; domainStats; domainStats = domainStats->next)
    {
        LogMsgNoIdent("Domain: %s (non-cellular)", domainStats->domainStr);
        LogDNSStats(&domainStats->stats);
        LogMsgNoIdent("Domain: %s (cellular)", domainStats->domainStr);
        LogDNSStats(&domainStats->statsCellular);
    }
}
#endif // TARGET_OS_EMBEDDED