Debian Codegraph @

crt/aws-c-http/source/connection_monitor.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/http/private/connection_monitor.h>
7
8 #include <aws/http/connection.h>
9 #include <aws/http/statistics.h>
10 #include <aws/io/channel.h>
11 #include <aws/io/logging.h>
12 #include <aws/io/statistics.h>
13
14 #include <aws/common/clock.h>
15
16 #include <inttypes.h>
17
18 static void s_process_statistics(
19 struct aws_crt_statistics_handler *handler,
20 struct aws_crt_statistics_sample_interval *interval,
21 struct aws_array_list *stats_list,
22 void *context) {
23
24 (void)interval;
25
26 struct aws_statistics_handler_http_connection_monitor_impl *impl = handler->impl;
27 if (!aws_http_connection_monitoring_options_is_valid(&impl->options)) {
28 return;
29 }
30
31 uint64_t pending_read_interval_ms = 0;
32 uint64_t pending_write_interval_ms = 0;
33 uint64_t bytes_read = 0;
34 uint64_t bytes_written = 0;
35 uint32_t h1_current_outgoing_stream_id = 0;
36 uint32_t h1_current_incoming_stream_id = 0;
37
38 /*
39 * Pull out the data needed to perform the throughput calculation
40 */
41 size_t stats_count = aws_array_list_length(stats_list);
42 bool h2 = false;
43 bool h2_was_inactive = false;
44
45 for (size_t i = 0; i < stats_count; ++i) {
46 struct aws_crt_statistics_base *stats_base = NULL;
47 if (aws_array_list_get_at(stats_list, &stats_base, i)) {
48 continue;
49 }
50
51 switch (stats_base->category) {
52 case AWSCRT_STAT_CAT_SOCKET: {
53 struct aws_crt_statistics_socket *socket_stats = (struct aws_crt_statistics_socket *)stats_base;
54 bytes_read = socket_stats->bytes_read;
55 bytes_written = socket_stats->bytes_written;
56 break;
57 }
58
59 case AWSCRT_STAT_CAT_HTTP1_CHANNEL: {
60 AWS_ASSERT(!h2);
61 struct aws_crt_statistics_http1_channel *http1_stats =
62 (struct aws_crt_statistics_http1_channel *)stats_base;
63 pending_read_interval_ms = http1_stats->pending_incoming_stream_ms;
64 pending_write_interval_ms = http1_stats->pending_outgoing_stream_ms;
65 h1_current_outgoing_stream_id = http1_stats->current_outgoing_stream_id;
66 h1_current_incoming_stream_id = http1_stats->current_incoming_stream_id;
67
68 break;
69 }
70
71 case AWSCRT_STAT_CAT_HTTP2_CHANNEL: {
72 struct aws_crt_statistics_http2_channel *h2_stats =
73 (struct aws_crt_statistics_http2_channel *)stats_base;
74 pending_read_interval_ms = h2_stats->pending_incoming_stream_ms;
75 pending_write_interval_ms = h2_stats->pending_outgoing_stream_ms;
76 h2_was_inactive |= h2_stats->was_inactive;
77 h2 = true;
78 break;
79 }
80
81 default:
82 break;
83 }
84 }
85
86 if (impl->options.statistics_observer_fn) {
87 impl->options.statistics_observer_fn(
88 (size_t)(uintptr_t)(context), stats_list, impl->options.statistics_observer_user_data);
89 }
90
91 struct aws_channel *channel = context;
92
93 uint64_t bytes_per_second = 0;
94 uint64_t max_pending_io_interval_ms = 0;
95
96 if (pending_write_interval_ms > 0) {
97 double fractional_bytes_written_per_second =
98 (double)bytes_written * (double)AWS_TIMESTAMP_MILLIS / (double)pending_write_interval_ms;
99 if (fractional_bytes_written_per_second >= (double)UINT64_MAX) {
100 bytes_per_second = UINT64_MAX;
101 } else {
102 bytes_per_second = (uint64_t)fractional_bytes_written_per_second;
103 }
104 max_pending_io_interval_ms = pending_write_interval_ms;
105 }
106
107 if (pending_read_interval_ms > 0) {
108 double fractional_bytes_read_per_second =
109 (double)bytes_read * (double)AWS_TIMESTAMP_MILLIS / (double)pending_read_interval_ms;
110 if (fractional_bytes_read_per_second >= (double)UINT64_MAX) {
111 bytes_per_second = UINT64_MAX;
112 } else {
113 bytes_per_second = aws_add_u64_saturating(bytes_per_second, (uint64_t)fractional_bytes_read_per_second);
114 }
115 if (pending_read_interval_ms > max_pending_io_interval_ms) {
116 max_pending_io_interval_ms = pending_read_interval_ms;
117 }
118 }
119
120 AWS_LOGF_DEBUG(
121 AWS_LS_IO_CHANNEL,
122 "id=%p: channel throughput - %" PRIu64 " bytes per second",
123 (void *)channel,
124 bytes_per_second);
125
126 /*
127 * Check throughput only if the connection has active stream and no gap between.
128 */
129 bool check_throughput = false;
130 if (h2) {
131 /* For HTTP/2, check throughput only if there always has any active stream on the connection */
132 check_throughput = !h2_was_inactive;
133 } else {
134 /* For HTTP/1, check throughput only if at least one stream exists and was observed in that role previously */
135 check_throughput =
136 (h1_current_incoming_stream_id != 0 && h1_current_incoming_stream_id == impl->last_incoming_stream_id) ||
137 (h1_current_outgoing_stream_id != 0 && h1_current_outgoing_stream_id == impl->last_outgoing_stream_id);
138
139 impl->last_outgoing_stream_id = h1_current_outgoing_stream_id;
140 impl->last_incoming_stream_id = h1_current_incoming_stream_id;
141 }
142 impl->last_measured_throughput = bytes_per_second;
143
144 if (!check_throughput) {
145 AWS_LOGF_TRACE(AWS_LS_IO_CHANNEL, "id=%p: channel throughput does not need to be checked", (void *)channel);
146 impl->throughput_failure_time_ms = 0;
147 return;
148 }
149
150 if (bytes_per_second >= impl->options.minimum_throughput_bytes_per_second) {
151 impl->throughput_failure_time_ms = 0;
152 return;
153 }
154
155 impl->throughput_failure_time_ms =
156 aws_add_u64_saturating(impl->throughput_failure_time_ms, max_pending_io_interval_ms);
157
158 AWS_LOGF_INFO(
159 AWS_LS_IO_CHANNEL,
160 "id=%p: Channel low throughput warning. Currently %" PRIu64 " milliseconds of consecutive failure time",
161 (void *)channel,
162 impl->throughput_failure_time_ms);
163
164 uint64_t maximum_failure_time_ms = aws_timestamp_convert(
165 impl->options.allowable_throughput_failure_interval_seconds, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL);
166 if (impl->throughput_failure_time_ms <= maximum_failure_time_ms) {
167 return;
168 }
169
170 AWS_LOGF_INFO(
171 AWS_LS_IO_CHANNEL,
172 "id=%p: Channel low throughput threshold exceeded (< %" PRIu64
173 " bytes per second for more than %u seconds). Shutting down.",
174 (void *)channel,
175 impl->options.minimum_throughput_bytes_per_second,
176 impl->options.allowable_throughput_failure_interval_seconds);
177
178 aws_channel_shutdown(channel, AWS_ERROR_HTTP_CHANNEL_THROUGHPUT_FAILURE);
179 }
180
181 static void s_destroy(struct aws_crt_statistics_handler *handler) {
182 if (handler == NULL) {
183 return;
184 }
185
186 aws_mem_release(handler->allocator, handler);
187 }
188
189 static uint64_t s_get_report_interval_ms(struct aws_crt_statistics_handler *handler) {
190 (void)handler;
191
192 return 1000;
193 }
194
195 static struct aws_crt_statistics_handler_vtable s_http_connection_monitor_vtable = {
196 .process_statistics = s_process_statistics,
197 .destroy = s_destroy,
198 .get_report_interval_ms = s_get_report_interval_ms,
199 };
200
201 struct aws_crt_statistics_handler *aws_crt_statistics_handler_new_http_connection_monitor(
202 struct aws_allocator *allocator,
203 struct aws_http_connection_monitoring_options *options) {
204 struct aws_crt_statistics_handler *handler = NULL;
205 struct aws_statistics_handler_http_connection_monitor_impl *impl = NULL;
206
207 if (!aws_mem_acquire_many(
208 allocator,
209 2,
210 &handler,
211 sizeof(struct aws_crt_statistics_handler),
212 &impl,
213 sizeof(struct aws_statistics_handler_http_connection_monitor_impl))) {
214 return NULL;
215 }
216
217 AWS_ZERO_STRUCT(*handler);
218 AWS_ZERO_STRUCT(*impl);
219 impl->options = *options;
220
221 handler->vtable = &s_http_connection_monitor_vtable;
222 handler->allocator = allocator;
223 handler->impl = impl;
224
225 return handler;
226 }
227
228 bool aws_http_connection_monitoring_options_is_valid(const struct aws_http_connection_monitoring_options *options) {
229 if (options == NULL) {
230 return false;
231 }
232
233 return options->allowable_throughput_failure_interval_seconds > 0 &&
234 options->minimum_throughput_bytes_per_second > 0;
235 }