|
| 1 | +#ifndef AWS_S3_BUFFER_POOL_H |
| 2 | +#define AWS_S3_BUFFER_POOL_H |
| 3 | + |
| 4 | +#include <aws/common/ref_count.h> |
| 5 | +#include <aws/io/future.h> |
| 6 | +#include <aws/s3/s3.h> |
| 7 | + |
| 8 | +/** |
| 9 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 10 | + * SPDX-License-Identifier: Apache-2.0. |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * Generic memory pool interface. |
| 15 | + * Allows consumers of aws-c-s3 to override how buffer allocation for part buffers is done. |
| 16 | + * Refer to docs/memory_aware_request_execution.md for details on how default implementation works. |
| 17 | + * WARNING: this is currently experimental feature and does not provide API stability guarantees so should be used with |
| 18 | + * caution. At highlevel the flow is as follows: |
| 19 | + * - crt scheduler queues up requests to be prepared |
| 20 | + * - requests being prepared will try to reserve mem (i.e. obtain a ticket) and wait until they get it before proceeding |
| 21 | + * - once mem is reserved requests will proceed with the pipeline |
| 22 | + * - request will acquire buffer from the ticket when needed |
| 23 | + * - ticket is released when request is done with the buffer |
| 24 | + * Note: in some cases pipeline can stall if new buffer cannot be allocated (ex. async writes flow). |
| 25 | + * In this case reserve request will indicate that not granting the ticket can block and buffer pool should try to |
| 26 | + * allocate ticket right away (or wait and call waker when mem is allocated for the case of async writes). |
| 27 | + */ |
| 28 | + |
| 29 | +AWS_PUSH_SANE_WARNING_LEVEL |
| 30 | +AWS_EXTERN_C_BEGIN |
| 31 | +struct aws_s3_buffer_ticket; |
| 32 | + |
| 33 | +/** |
| 34 | + * aws_future<aws_s3_buffer_ticket*> |
| 35 | + * Buffer ticket future used for reservations. |
| 36 | + */ |
| 37 | +AWS_FUTURE_T_POINTER_WITH_RELEASE_DECLARATION(aws_future_s3_buffer_ticket, struct aws_s3_buffer_ticket, AWS_S3_API) |
| 38 | + |
| 39 | +/** |
| 40 | + * Meta information about ticket reservation request. |
| 41 | + */ |
| 42 | +struct aws_s3_buffer_pool_reserve_meta { |
| 43 | + /* client reserving the ticket. accounts for buffer pool being shared between clients. */ |
| 44 | + struct aws_s3_client *client; |
| 45 | + |
| 46 | + /* meta request ticket is being reserved for. */ |
| 47 | + struct aws_s3_meta_request *meta_request; |
| 48 | + |
| 49 | + /* size of the buffer to reserve. */ |
| 50 | + size_t size; |
| 51 | + |
| 52 | + /* whether not granting reservation can result in request pipeline being blocked. |
| 53 | + * Note: blocking is currently a terminal condition and that cannot be recovered from, |
| 54 | + * i.e. meta request will be stuck and not make any process. |
| 55 | + * As such buffer pool should either grant or error out reservation in sync. |
| 56 | + * This scenario currently only occurs in the async_write flows. */ |
| 57 | + bool can_block; |
| 58 | +}; |
| 59 | + |
| 60 | +struct aws_s3_buffer_ticket; |
| 61 | + |
| 62 | +struct aws_s3_buffer_ticket_vtable { |
| 63 | + /** |
| 64 | + * Get buffer associated with the ticket. |
| 65 | + * Note: can be called multiple times and the same buffer should be returned. In some cases ticket might not be |
| 66 | + * claimed at all. |
| 67 | + */ |
| 68 | + struct aws_byte_buf (*claim)(struct aws_s3_buffer_ticket *ticket); |
| 69 | + |
| 70 | + /* Implement below for custom ref count behavior. Alternatively set those to null and init the ref count. */ |
| 71 | + struct aws_s3_buffer_ticket *(*acquire)(struct aws_s3_buffer_ticket *ticket); |
| 72 | + struct aws_s3_buffer_ticket *(*release)(struct aws_s3_buffer_ticket *ticket); |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * Polymorphic ticket. |
| 77 | + */ |
| 78 | +struct aws_s3_buffer_ticket { |
| 79 | + struct aws_s3_buffer_ticket_vtable *vtable; |
| 80 | + struct aws_ref_count ref_count; |
| 81 | + void *impl; |
| 82 | +}; |
| 83 | + |
| 84 | +AWS_S3_API struct aws_byte_buf aws_s3_buffer_ticket_claim(struct aws_s3_buffer_ticket *ticket); |
| 85 | + |
| 86 | +AWS_S3_API struct aws_s3_buffer_ticket *aws_s3_buffer_ticket_acquire(struct aws_s3_buffer_ticket *ticket); |
| 87 | +AWS_S3_API struct aws_s3_buffer_ticket *aws_s3_buffer_ticket_release(struct aws_s3_buffer_ticket *ticket); |
| 88 | + |
| 89 | +struct aws_s3_buffer_pool; |
| 90 | + |
| 91 | +struct aws_s3_buffer_pool_vtable { |
| 92 | + /* Reserve a ticket. Returns a future that is granted whenever reservation can be made. */ |
| 93 | + struct aws_future_s3_buffer_ticket *( |
| 94 | + *reserve)(struct aws_s3_buffer_pool *pool, struct aws_s3_buffer_pool_reserve_meta meta); |
| 95 | + |
| 96 | + /** |
| 97 | + * Trim the pool. This is mostly a suggestion, which pool can decide to ignore. Triggered by CRT when |
| 98 | + * client has been idle for some time. |
| 99 | + **/ |
| 100 | + void (*trim)(struct aws_s3_buffer_pool *pool); |
| 101 | + |
| 102 | + /* Implement below for custom ref count behavior. Alternatively set those to null and init the ref count. */ |
| 103 | + struct aws_s3_buffer_pool *(*acquire)(struct aws_s3_buffer_pool *pool); |
| 104 | + struct aws_s3_buffer_pool *(*release)(struct aws_s3_buffer_pool *pool); |
| 105 | +}; |
| 106 | + |
| 107 | +/** |
| 108 | + * Polymorphic buffer pool. |
| 109 | + */ |
| 110 | +struct aws_s3_buffer_pool { |
| 111 | + struct aws_s3_buffer_pool_vtable *vtable; |
| 112 | + struct aws_ref_count ref_count; |
| 113 | + void *impl; |
| 114 | +}; |
| 115 | + |
| 116 | +AWS_S3_API struct aws_future_s3_buffer_ticket *aws_s3_buffer_pool_reserve( |
| 117 | + struct aws_s3_buffer_pool *buffer_pool, |
| 118 | + struct aws_s3_buffer_pool_reserve_meta meta); |
| 119 | +AWS_S3_API void aws_s3_buffer_pool_trim(struct aws_s3_buffer_pool *buffer_pool); |
| 120 | + |
| 121 | +AWS_S3_API struct aws_s3_buffer_pool *aws_s3_buffer_pool_acquire(struct aws_s3_buffer_pool *buffer_pool); |
| 122 | +AWS_S3_API struct aws_s3_buffer_pool *aws_s3_buffer_pool_release(struct aws_s3_buffer_pool *buffer_pool); |
| 123 | + |
| 124 | +/** |
| 125 | + * Buffer pool configuration options. |
| 126 | + */ |
| 127 | +struct aws_s3_buffer_pool_config { |
| 128 | + struct aws_s3_client *client; /* Client creating the pool. */ |
| 129 | + size_t part_size; /* Default part size of the client. */ |
| 130 | + size_t max_part_size; /* Max part size configured on the client. */ |
| 131 | + size_t memory_limit; /* Memory limit set on the client. */ |
| 132 | +}; |
| 133 | + |
| 134 | +/** |
| 135 | + * Factory to construct the pool for the given config. Passes along buffer related info configured on the client, which |
| 136 | + * factory may ignore when considering how to construct pool. |
| 137 | + * This implementation should fail if pool cannot be constructed for some reason (ex. if config params cannot be met), |
| 138 | + * by logging failure reason, returning null and raising aws_error. |
| 139 | + */ |
| 140 | +typedef struct aws_s3_buffer_pool *(aws_s3_buffer_pool_factory_fn)(struct aws_allocator *allocator, |
| 141 | + struct aws_s3_buffer_pool_config config); |
| 142 | + |
| 143 | +AWS_EXTERN_C_END |
| 144 | +AWS_POP_SANE_WARNING_LEVEL |
| 145 | + |
| 146 | +#endif /* AWS_S3_BUFFER_POOL_H */ |
0 commit comments