2 // Copyright (c) 2010-2019 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #ifndef _HANDLE_LAT_H_
18 #define _HANDLE_LAT_H_
24 #include "task_base.h"
27 #define LATENCY_ACCURACY 1
28 // If ACCURACY_WINDOW is too small, the accuracy for packet N can be received by lat BEFORE
29 // packet N is received (re-ordering) resulting in accuracy being unused
30 // 8192 packets is equivalent to 550 micro-seconds at 10Gbps for 64 bytes packets
31 #define ACCURACY_WINDOW 8192
33 #define LAT_BUCKET_COUNT 128
36 uint64_t tot_all_pkts;
41 unsigned __int128 var_lat; /* variance */
42 uint64_t accuracy_limit_tsc;
44 uint64_t max_lat_error;
45 uint64_t min_lat_error;
46 uint64_t tot_lat_error;
47 unsigned __int128 var_lat_error;
49 uint64_t buckets[LAT_BUCKET_COUNT];
51 uint64_t lost_packets;
54 static struct time_unit lat_test_get_accuracy_limit(struct lat_test *lat_test)
56 return tsc_to_time_unit(lat_test->accuracy_limit_tsc);
59 static struct time_unit_err lat_test_get_avg(struct lat_test *lat_test)
64 tsc = lat_test->tot_lat/lat_test->tot_pkts;
65 tsc_error = lat_test->tot_lat_error/lat_test->tot_pkts;
67 struct time_unit_err ret = {
68 .time = tsc_to_time_unit(tsc),
69 .error = tsc_to_time_unit(tsc_error),
75 static struct time_unit_err lat_test_get_min(struct lat_test *lat_test)
77 struct time_unit_err ret = {
78 .time = tsc_to_time_unit(lat_test->min_lat),
79 .error = tsc_to_time_unit(lat_test->min_lat_error),
85 static struct time_unit_err lat_test_get_max(struct lat_test *lat_test)
87 struct time_unit_err ret = {
88 .time = tsc_to_time_unit(lat_test->max_lat),
89 .error = tsc_to_time_unit(lat_test->max_lat_error),
95 static struct time_unit_err lat_test_get_stddev(struct lat_test *lat_test)
97 unsigned __int128 avg_tsc = lat_test->tot_lat/lat_test->tot_pkts;
98 unsigned __int128 avg_tsc_squared = avg_tsc * avg_tsc;
99 unsigned __int128 avg_squares_tsc = lat_test->var_lat/lat_test->tot_pkts;
101 /* The assumption is that variance fits into 64 bits, meaning
102 that standard deviation fits into 32 bits. In other words,
103 the assumption is that the standard deviation is not more
104 than approximately 1 second. */
105 uint64_t var_tsc = avg_squares_tsc - avg_tsc_squared;
106 uint64_t stddev_tsc = sqrt(var_tsc);
108 unsigned __int128 avg_tsc_error = lat_test->tot_lat_error / lat_test->tot_pkts;
109 unsigned __int128 avg_tsc_squared_error = 2 * avg_tsc * avg_tsc_error + avg_tsc_error * avg_tsc_error;
110 unsigned __int128 avg_squares_tsc_error = lat_test->var_lat_error / lat_test->tot_pkts;
112 uint64_t var_tsc_error = avg_squares_tsc_error + avg_tsc_squared_error;
114 /* sqrt(a+-b) = sqrt(a) +- (-sqrt(a) + sqrt(a + b)) */
116 uint64_t stddev_tsc_error = - stddev_tsc + sqrt(var_tsc + var_tsc_error);
118 struct time_unit_err ret = {
119 .time = tsc_to_time_unit(stddev_tsc),
120 .error = tsc_to_time_unit(stddev_tsc_error),
126 static void _lat_test_histogram_combine(struct lat_test *dst, struct lat_test *src)
128 for (size_t i = 0; i < sizeof(dst->buckets)/sizeof(dst->buckets[0]); ++i)
129 dst->buckets[i] += src->buckets[i];
132 static void lat_test_combine(struct lat_test *dst, struct lat_test *src)
134 dst->tot_all_pkts += src->tot_all_pkts;
136 dst->tot_pkts += src->tot_pkts;
138 dst->tot_lat += src->tot_lat;
139 dst->tot_lat_error += src->tot_lat_error;
141 /* (a +- b)^2 = a^2 +- (2ab + b^2) */
142 dst->var_lat += src->var_lat;
143 dst->var_lat_error += src->var_lat_error;
145 if (src->max_lat > dst->max_lat) {
146 dst->max_lat = src->max_lat;
147 dst->max_lat_error = src->max_lat_error;
149 if (src->min_lat < dst->min_lat) {
150 dst->min_lat = src->min_lat;
151 dst->min_lat_error = src->min_lat_error;
154 if (src->accuracy_limit_tsc > dst->accuracy_limit_tsc)
155 dst->accuracy_limit_tsc = src->accuracy_limit_tsc;
156 dst->lost_packets += src->lost_packets;
158 #ifdef LATENCY_HISTOGRAM
159 _lat_test_histogram_combine(dst, src);
163 static void lat_test_reset(struct lat_test *lat_test)
165 lat_test->tot_all_pkts = 0;
166 lat_test->tot_pkts = 0;
167 lat_test->max_lat = 0;
168 lat_test->min_lat = -1;
169 lat_test->tot_lat = 0;
170 lat_test->var_lat = 0;
171 lat_test->max_lat_error = 0;
172 lat_test->min_lat_error = 0;
173 lat_test->tot_lat_error = 0;
174 lat_test->var_lat_error = 0;
175 lat_test->accuracy_limit_tsc = 0;
177 lat_test->lost_packets = 0;
179 memset(lat_test->buckets, 0, sizeof(lat_test->buckets));
182 static void lat_test_copy(struct lat_test *dst, struct lat_test *src)
184 if (src->tot_all_pkts)
185 memcpy(dst, src, sizeof(struct lat_test));
190 struct lat_test *task_lat_get_latency_meassurement(struct task_lat *task);
191 uint32_t task_lat_get_latency_bucket_size(struct task_lat *task);
192 void task_lat_use_other_latency_meassurement(struct task_lat *task);
193 void task_lat_set_accuracy_limit(struct task_lat *task, uint32_t accuracy_limit_nsec);
195 #endif /* _HANDLE_LAT_H_ */