]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/net_perf.c
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <net/if_var.h>
29 #include <net/net_perf.h>
30 #include <netinet/in_var.h>
31 #include <sys/sysctl.h>
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
);
37 void net_perf_start_time(net_perf_t
*npp
, struct timeval
*tv
)
43 void net_perf_measure_time(net_perf_t
*npp
, struct timeval
*start
, uint64_t num_pkts
)
47 ip_perf_record_stats(npp
, start
, &stop
, num_pkts
);
51 ip_perf_record_stats(net_perf_t
*npp
, struct timeval
*tv1
, struct timeval
*tv2
, uint64_t num_pkts
)
53 struct timeval tv_diff
;
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
);
62 update_bins(net_perf_t
*npp
, uint64_t bins
)
64 bzero(&npp
->np_hist_bars
, sizeof(npp
->np_hist_bars
));
66 for (int i
= 1, j
= 0; i
<= 64 && j
< NET_PERF_BARS
; i
++) {
68 npp
->np_hist_bars
[j
] = i
;
76 net_perf_initialize(net_perf_t
*npp
, uint64_t bins
)
78 bzero(npp
, sizeof(net_perf_t
));
79 /* initialize np_hist_bars array */
80 update_bins(npp
, bins
);
84 net_perf_histogram(net_perf_t
*npp
, uint64_t num_pkts
)
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
);
100 net_perf_validate_bins(uint64_t bins
)
102 return (NET_PERF_BARS
== __builtin_popcountll(bins
));