]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/net_perf.c
xnu-3789.41.3.tar.gz
[apple/xnu.git] / bsd / net / net_perf.c
CommitLineData
3e170ce0
A
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
33static void ip_perf_record_stats(net_perf_t *npp, struct timeval *tv1,
34 struct timeval *tv2, uint64_t num_pkts);
35static void update_bins(net_perf_t *npp, uint64_t bins);
36
37void net_perf_start_time(net_perf_t *npp, struct timeval *tv)
38{
39#pragma unused(npp)
40 microtime(tv);
41}
42
43void net_perf_measure_time(net_perf_t *npp, struct timeval *start, uint64_t num_pkts)
44{
45 struct timeval stop;
46 microtime(&stop);
47 ip_perf_record_stats(npp, start, &stop, num_pkts);
48}
49
50static void
51ip_perf_record_stats(net_perf_t *npp, struct timeval *tv1, struct timeval *tv2, uint64_t num_pkts)
52{
53 struct timeval tv_diff;
54 uint64_t usecs;
55 timersub(tv2, tv1, &tv_diff);
56 usecs = tv_diff.tv_sec * 1000000ULL + tv_diff.tv_usec;
57 OSAddAtomic64(usecs, &npp->np_total_usecs);
58 OSAddAtomic64(num_pkts, &npp->np_total_pkts);
59}
60
61static void
62update_bins(net_perf_t *npp, uint64_t bins)
63{
64 bzero(&npp->np_hist_bars, sizeof(npp->np_hist_bars));
65
66 for (int i = 1, j = 0; i <= 64 && j < NET_PERF_BARS; i++) {
67 if (bins & 0x1) {
68 npp->np_hist_bars[j] = i;
69 j++;
70 }
71 bins >>= 1;
72 }
73}
74
75void
76net_perf_initialize(net_perf_t *npp, uint64_t bins)
77{
78 bzero(npp, sizeof(net_perf_t));
79 /* initialize np_hist_bars array */
80 update_bins(npp, bins);
81}
82
83void
84net_perf_histogram(net_perf_t *npp, uint64_t num_pkts)
85{
86 if (num_pkts <= npp->np_hist_bars[0]) {
87 OSAddAtomic64(num_pkts, &npp->np_hist1);
88 } else if (npp->np_hist_bars[0] < num_pkts && num_pkts <= npp->np_hist_bars[1]) {
89 OSAddAtomic64(num_pkts, &npp->np_hist2);
90 } else if (npp->np_hist_bars[1] < num_pkts && num_pkts <= npp->np_hist_bars[2]) {
91 OSAddAtomic64(num_pkts, &npp->np_hist3);
92 } else if (npp->np_hist_bars[2] < num_pkts && num_pkts <= npp->np_hist_bars[3]) {
93 OSAddAtomic64(num_pkts, &npp->np_hist4);
94 } else if (npp->np_hist_bars[3] < num_pkts) {
95 OSAddAtomic64(num_pkts, &npp->np_hist5);
96 }
97}
98
99boolean_t
100net_perf_validate_bins(uint64_t bins)
101{
102 return (NET_PERF_BARS == __builtin_popcountll(bins));
103}
104