]>
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
);
38 net_perf_start_time(net_perf_t
*npp
, struct timeval
*tv
)
45 net_perf_measure_time(net_perf_t
*npp
, struct timeval
*start
, uint64_t num_pkts
)
49 ip_perf_record_stats(npp
, start
, &stop
, num_pkts
);
53 ip_perf_record_stats(net_perf_t
*npp
, struct timeval
*tv1
, struct timeval
*tv2
, uint64_t num_pkts
)
55 struct timeval tv_diff
;
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
);
64 update_bins(net_perf_t
*npp
, uint64_t bins
)
66 bzero(&npp
->np_hist_bars
, sizeof(npp
->np_hist_bars
));
68 for (uint8_t i
= 1, j
= 0; i
<= 64 && j
< NET_PERF_BARS
; i
++) {
70 npp
->np_hist_bars
[j
] = i
;
78 net_perf_initialize(net_perf_t
*npp
, uint64_t bins
)
80 bzero(npp
, sizeof(net_perf_t
));
81 /* initialize np_hist_bars array */
82 update_bins(npp
, bins
);
86 net_perf_histogram(net_perf_t
*npp
, uint64_t num_pkts
)
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
);
102 net_perf_validate_bins(uint64_t bins
)
104 return NET_PERF_BARS
== __builtin_popcountll(bins
);