| 1 | #ifndef AWS_COMMON_STATISTICS_H |
| 2 | #define AWS_COMMON_STATISTICS_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/common/common.h> |
| 10 | #include <aws/common/package.h> |
| 11 | |
| 12 | #include <aws/common/stdint.h> |
| 13 | |
| 14 | AWS_PUSH_SANE_WARNING_LEVEL |
| 15 | |
| 16 | struct aws_array_list; |
| 17 | |
| 18 | typedef uint32_t aws_crt_statistics_category_t; |
| 19 | |
| 20 | /* Each library gets space for 2^^8 category entries */ |
| 21 | enum { |
| 22 | AWS_CRT_STATISTICS_CATEGORY_STRIDE_BITS = 8, |
| 23 | }; |
| 24 | |
| 25 | #define AWS_CRT_STATISTICS_CATEGORY_STRIDE (1U << AWS_CRT_STATISTICS_CATEGORY_STRIDE_BITS) |
| 26 | #define AWS_CRT_STATISTICS_CATEGORY_BEGIN_RANGE(x) ((x) * AWS_CRT_STATISTICS_CATEGORY_STRIDE) |
| 27 | #define AWS_CRT_STATISTICS_CATEGORY_END_RANGE(x) (((x) + 1) * AWS_CRT_STATISTICS_CATEGORY_STRIDE - 1) |
| 28 | |
| 29 | /** |
| 30 | * The common-specific range of the aws_crt_statistics_category cross-library enum. |
| 31 | * |
| 32 | * This enum functions as an RTTI value that lets statistics handler's interpret (via cast) a |
| 33 | * specific statistics structure if the RTTI value is understood. |
| 34 | * |
| 35 | * Common doesn't have any statistics structures presently, so its range is essentially empty. |
| 36 | * |
| 37 | */ |
| 38 | enum aws_crt_common_statistics_category { |
| 39 | AWSCRT_STAT_CAT_INVALID = AWS_CRT_STATISTICS_CATEGORY_BEGIN_RANGE(AWS_C_COMMON_PACKAGE_ID) |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Pattern-struct that functions as a base "class" for all statistics structures. To conform |
| 44 | * to the pattern, a statistics structure must have its first member be the category. In that |
| 45 | * case it becomes "safe" to cast from aws_crt_statistics_base to the specific statistics structure |
| 46 | * based on the category value. |
| 47 | */ |
| 48 | struct aws_crt_statistics_base { |
| 49 | aws_crt_statistics_category_t category; |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * The start and end time, in milliseconds-since-epoch, that a set of statistics was gathered over. |
| 54 | */ |
| 55 | struct aws_crt_statistics_sample_interval { |
| 56 | uint64_t begin_time_ms; |
| 57 | uint64_t end_time_ms; |
| 58 | }; |
| 59 | |
| 60 | struct aws_crt_statistics_handler; |
| 61 | |
| 62 | /* |
| 63 | * Statistics intake function. The array_list is a list of pointers to aws_crt_statistics_base "derived" (via |
| 64 | * pattern) objects. The handler should iterate the list and downcast elements whose RTTI category it understands, |
| 65 | * while skipping those it does not understand. |
| 66 | */ |
| 67 | typedef void(aws_crt_statistics_handler_process_statistics_fn)( |
| 68 | struct aws_crt_statistics_handler *handler, |
| 69 | struct aws_crt_statistics_sample_interval *interval, |
| 70 | struct aws_array_list *stats, |
| 71 | void *context); |
| 72 | |
| 73 | /* |
| 74 | * Destroys a statistics handler implementation |
| 75 | */ |
| 76 | typedef void(aws_crt_statistics_handler_destroy_fn)(struct aws_crt_statistics_handler *handler); |
| 77 | |
| 78 | /* |
| 79 | * The period, in milliseconds, that the handler would like to be informed of statistics. Statistics generators are |
| 80 | * not required to honor this value, but should if able. |
| 81 | */ |
| 82 | typedef uint64_t(aws_crt_statistics_handler_get_report_interval_ms_fn)(struct aws_crt_statistics_handler *); |
| 83 | |
| 84 | /** |
| 85 | * Vtable for functions that all statistics handlers must implement |
| 86 | */ |
| 87 | struct aws_crt_statistics_handler_vtable { |
| 88 | aws_crt_statistics_handler_process_statistics_fn *process_statistics; |
| 89 | aws_crt_statistics_handler_destroy_fn *destroy; |
| 90 | aws_crt_statistics_handler_get_report_interval_ms_fn *get_report_interval_ms; |
| 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * Base structure for all statistics handler implementations. |
| 95 | * |
| 96 | * A statistics handler is an object that listens to a stream of polymorphic (via the category RTTI enum) statistics |
| 97 | * structures emitted from some arbitrary source. In the initial implementation, statistics handlers are primarily |
| 98 | * attached to channels, where they monitor IO throughput and state data (from channel handlers) to determine a |
| 99 | * connection's health. |
| 100 | * |
| 101 | * Statistics handlers are a generalization of the timeout and bandwidth filters that are often associated with |
| 102 | * SDK network connections. Configurable, default implementations are defined at the protocol level (http, etc...) |
| 103 | * where they can be attached at connection (channel) creation time. |
| 104 | */ |
| 105 | struct aws_crt_statistics_handler { |
| 106 | struct aws_crt_statistics_handler_vtable *vtable; |
| 107 | struct aws_allocator *allocator; |
| 108 | void *impl; |
| 109 | }; |
| 110 | |
| 111 | AWS_EXTERN_C_BEGIN |
| 112 | |
| 113 | /** |
| 114 | * Submits a list of statistics objects to a statistics handler for processing |
| 115 | * |
| 116 | * handler - the statistics handler that will process the statistics objects |
| 117 | * interval - time period over which the statistics were gathered |
| 118 | * stats - list of pointers to structures that can be case to aws_crt_statistics_base (i.e. have category as a first |
| 119 | * member) |
| 120 | * context - (optional) additional context specific to where the statistics handler has been attached |
| 121 | */ |
| 122 | AWS_COMMON_API |
| 123 | void aws_crt_statistics_handler_process_statistics( |
| 124 | struct aws_crt_statistics_handler *handler, |
| 125 | struct aws_crt_statistics_sample_interval *interval, |
| 126 | struct aws_array_list *stats, |
| 127 | void *context); |
| 128 | |
| 129 | /** |
| 130 | * Queries the frequency (via an interval in milliseconds) which a statistics handler would like to be informed |
| 131 | * of statistics. |
| 132 | */ |
| 133 | AWS_COMMON_API |
| 134 | uint64_t aws_crt_statistics_handler_get_report_interval_ms(struct aws_crt_statistics_handler *handler); |
| 135 | |
| 136 | /** |
| 137 | * completely destroys a statistics handler. The handler's cleanup function must clean up the impl portion completely |
| 138 | * (including its allocation, if done separately). |
| 139 | */ |
| 140 | AWS_COMMON_API |
| 141 | void aws_crt_statistics_handler_destroy(struct aws_crt_statistics_handler *handler); |
| 142 | |
| 143 | AWS_EXTERN_C_END |
| 144 | AWS_POP_SANE_WARNING_LEVEL |
| 145 | |
| 146 | #endif /* AWS_COMMON_STATISTICS_H */ |