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