Debian Codegraph @

crt/aws-c-io/tests/statistics_handler_test.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 "statistics_handler_test.h"
7
8 #include <aws/common/clock.h>
9 #include <aws/common/thread.h>
10 #include <aws/io/channel.h>
11 #include <aws/io/event_loop.h>
12 #include <aws/io/tls_channel_handler.h>
13 #include <aws/testing/aws_test_harness.h>
14
15 static void s_process_statistics(
16 struct aws_crt_statistics_handler *handler,
17 struct aws_crt_statistics_sample_interval *interval,
18 struct aws_array_list *stats_list,
19 void *context) {
20
21 (void)context;
22
23 struct aws_statistics_handler_test_impl *impl = handler->impl;
24
25 aws_mutex_lock(&impl->lock);
26
27 if (impl->start_time_ns == 0) {
28 impl->start_time_ns =
29 aws_timestamp_convert(interval->begin_time_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
30 }
31
32 size_t stats_count = aws_array_list_length(stats_list);
33 for (size_t i = 0; i < stats_count; ++i) {
34 struct aws_crt_statistics_base *stats_base = NULL;
35 if (aws_array_list_get_at(stats_list, &stats_base, i)) {
36 continue;
37 }
38
39 switch (stats_base->category) {
40 case AWSCRT_STAT_CAT_SOCKET: {
41 struct aws_crt_statistics_socket *socket_stats = (struct aws_crt_statistics_socket *)stats_base;
42 impl->total_bytes_read += socket_stats->bytes_read;
43 impl->total_bytes_written += socket_stats->bytes_written;
44 break;
45 }
46
47 case AWSCRT_STAT_CAT_TLS: {
48 struct aws_crt_statistics_tls *tls_stats = (struct aws_crt_statistics_tls *)stats_base;
49 impl->tls_status = tls_stats->handshake_status;
50 break;
51 }
52
53 default:
54 break;
55 }
56 }
57
58 aws_mutex_unlock(&impl->lock);
59 aws_condition_variable_notify_one(&impl->signal);
60 }
61
62 static void s_destroy_handler(struct aws_crt_statistics_handler *handler) {
63 struct aws_statistics_handler_test_impl *impl = handler->impl;
64
65 aws_mutex_clean_up(&impl->lock);
66 aws_condition_variable_clean_up(&impl->signal);
67
68 /* impl and handler allocated via acquire_many */
69 aws_mem_release(handler->allocator, handler);
70 }
71
72 static uint64_t s_get_report_interval_ms(struct aws_crt_statistics_handler *handler) {
73 (void)handler;
74
75 /*
76 * Making this a very small number means the stat task will be in the very near future and thus a very
77 * short wait.
78 */
79 return 1;
80 }
81
82 static struct aws_crt_statistics_handler_vtable s_test_statistics_handler_vtable = {
83 .process_statistics = s_process_statistics,
84 .destroy = s_destroy_handler,
85 .get_report_interval_ms = s_get_report_interval_ms};
86
87 struct aws_crt_statistics_handler *aws_statistics_handler_new_test(struct aws_allocator *allocator) {
88 struct aws_crt_statistics_handler *handler = NULL;
89 struct aws_statistics_handler_test_impl *impl = NULL;
90
91 if (!aws_mem_acquire_many(
92 allocator,
93 2,
94 &handler,
95 sizeof(struct aws_crt_statistics_handler),
96 &impl,
97 sizeof(struct aws_statistics_handler_test_impl))) {
98 return NULL;
99 }
100
101 AWS_ZERO_STRUCT(*handler);
102 AWS_ZERO_STRUCT(*impl);
103
104 aws_mutex_init(&impl->lock);
105 aws_condition_variable_init(&impl->signal);
106
107 handler->vtable = &s_test_statistics_handler_vtable;
108 handler->allocator = allocator;
109 handler->impl = impl;
110
111 return handler;
112 }