]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/net_perf.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / net / net_perf.c
1 /*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <net/if_var.h>
29 #include <net/net_perf.h>
30 #include <netinet/in_var.h>
31 #include <sys/sysctl.h>
32
33 static void ip_perf_record_stats(net_perf_t *npp, struct timeval *tv1,
34 struct timeval *tv2, uint64_t num_pkts);
35 static void update_bins(net_perf_t *npp, uint64_t bins);
36
37 void
38 net_perf_start_time(net_perf_t *npp, struct timeval *tv)
39 {
40 #pragma unused(npp)
41 microtime(tv);
42 }
43
44 void
45 net_perf_measure_time(net_perf_t *npp, struct timeval *start, uint64_t num_pkts)
46 {
47 struct timeval stop;
48 microtime(&stop);
49 ip_perf_record_stats(npp, start, &stop, num_pkts);
50 }
51
52 static void
53 ip_perf_record_stats(net_perf_t *npp, struct timeval *tv1, struct timeval *tv2, uint64_t num_pkts)
54 {
55 struct timeval tv_diff;
56 uint64_t usecs;
57 timersub(tv2, tv1, &tv_diff);
58 usecs = tv_diff.tv_sec * 1000000ULL + tv_diff.tv_usec;
59 OSAddAtomic64(usecs, &npp->np_total_usecs);
60 OSAddAtomic64(num_pkts, &npp->np_total_pkts);
61 }
62
63 static void
64 update_bins(net_perf_t *npp, uint64_t bins)
65 {
66 bzero(&npp->np_hist_bars, sizeof(npp->np_hist_bars));
67
68 for (uint8_t i = 1, j = 0; i <= 64 && j < NET_PERF_BARS; i++) {
69 if (bins & 0x1) {
70 npp->np_hist_bars[j] = i;
71 j++;
72 }
73 bins >>= 1;
74 }
75 }
76
77 void
78 net_perf_initialize(net_perf_t *npp, uint64_t bins)
79 {
80 bzero(npp, sizeof(net_perf_t));
81 /* initialize np_hist_bars array */
82 update_bins(npp, bins);
83 }
84
85 void
86 net_perf_histogram(net_perf_t *npp, uint64_t num_pkts)
87 {
88 if (num_pkts <= npp->np_hist_bars[0]) {
89 OSAddAtomic64(num_pkts, &npp->np_hist1);
90 } else if (npp->np_hist_bars[0] < num_pkts && num_pkts <= npp->np_hist_bars[1]) {
91 OSAddAtomic64(num_pkts, &npp->np_hist2);
92 } else if (npp->np_hist_bars[1] < num_pkts && num_pkts <= npp->np_hist_bars[2]) {
93 OSAddAtomic64(num_pkts, &npp->np_hist3);
94 } else if (npp->np_hist_bars[2] < num_pkts && num_pkts <= npp->np_hist_bars[3]) {
95 OSAddAtomic64(num_pkts, &npp->np_hist4);
96 } else if (npp->np_hist_bars[3] < num_pkts) {
97 OSAddAtomic64(num_pkts, &npp->np_hist5);
98 }
99 }
100
101 boolean_t
102 net_perf_validate_bins(uint64_t bins)
103 {
104 return NET_PERF_BARS == __builtin_popcountll(bins);
105 }