Debian Codegraph @

crt/aws-c-s3/include/aws/s3/s3_buffer_pool.h

raw symbols SCIP
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 * Note for custom pool implementations: Scheduler keeps track of all outstanding futures and will error them out when
28 * request is paused or cancelled. Its still fine for memory pool implementation to deliver ticket (it will just be
29 * released by future right away with no side effects) or just ignore the future if its already in error state.
30 * Note: buffer pool implementations should prefer returning reservations in the order they were requested.
31 * i.e. implementations should avoid returning reservations when there are pending reservations in the queue.
32 * Calling client has mitigations that will reinforce the order, but excessive out of order returns can result
33 * in resource exhaustion.
34 */
35
36 AWS_PUSH_SANE_WARNING_LEVEL
37 AWS_EXTERN_C_BEGIN
38 struct aws_s3_buffer_ticket;
39
40 /**
41 * aws_future<aws_s3_buffer_ticket*>
42 * Buffer ticket future used for reservations.
43 */
44 AWS_FUTURE_T_POINTER_WITH_RELEASE_DECLARATION(aws_future_s3_buffer_ticket, struct aws_s3_buffer_ticket, AWS_S3_API)
45
46 /**
47 * Meta information about ticket reservation request.
48 */
49 struct aws_s3_buffer_pool_reserve_meta {
50 /* client reserving the ticket. accounts for buffer pool being shared between clients. */
51 struct aws_s3_client *client;
52
53 /* meta request ticket is being reserved for. */
54 struct aws_s3_meta_request *meta_request;
55
56 /* size of the buffer to reserve. */
57 size_t size;
58
59 /* Whether withholding the reservation can block this meta request's progress (currently only the
60 * async-write flow). The pool can't hold such a reservation indefinitely (that can deadlock the
61 * client); it must resolve it, either synchronously (the default pool hands out a "forced" buffer
62 * that may exceed the memory limit) or by deferring. To defer, the pool returns a not-yet-done
63 * future and resolves it later as memory frees up; the caller waits on the future and resumes
64 * once it completes. A pool that defers must guarantee every deferred reservation is eventually
65 * resolved. */
66 bool can_block;
67 };
68
69 struct aws_s3_buffer_ticket;
70
71 struct aws_s3_buffer_ticket_vtable {
72 /**
73 * Get buffer associated with the ticket.
74 * Note: can be called multiple times and the same buffer should be returned. In some cases ticket might not be
75 * claimed at all.
76 */
77 struct aws_byte_buf (*claim)(struct aws_s3_buffer_ticket *ticket);
78
79 /* Implement below for custom ref count behavior. Alternatively set those to null and init the ref count. */
80 struct aws_s3_buffer_ticket *(*acquire)(struct aws_s3_buffer_ticket *ticket);
81 struct aws_s3_buffer_ticket *(*release)(struct aws_s3_buffer_ticket *ticket);
82 };
83
84 /**
85 * Polymorphic ticket.
86 */
87 struct aws_s3_buffer_ticket {
88 struct aws_s3_buffer_ticket_vtable *vtable;
89 struct aws_ref_count ref_count;
90 void *impl;
91 };
92
93 AWS_S3_API struct aws_byte_buf aws_s3_buffer_ticket_claim(struct aws_s3_buffer_ticket *ticket);
94
95 AWS_S3_API struct aws_s3_buffer_ticket *aws_s3_buffer_ticket_acquire(struct aws_s3_buffer_ticket *ticket);
96 AWS_S3_API struct aws_s3_buffer_ticket *aws_s3_buffer_ticket_release(struct aws_s3_buffer_ticket *ticket);
97
98 struct aws_s3_buffer_pool;
99
100 struct aws_s3_buffer_pool_vtable {
101 /* Reserve a ticket. Returns a future that is granted whenever reservation can be made.
102 Note: implementations should do a best effort of completing futures
103 in the order reservation requests arrived in. */
104 struct aws_future_s3_buffer_ticket *(
105 *reserve)(struct aws_s3_buffer_pool *pool, struct aws_s3_buffer_pool_reserve_meta meta);
106
107 /**
108 * Trim the pool. This is mostly a suggestion, which pool can decide to ignore. Triggered by CRT when
109 * client has been idle for some time.
110 **/
111 void (*trim)(struct aws_s3_buffer_pool *pool);
112
113 /**
114 * Optimize the buffer pool for allocations of a specific size.
115 * Creates a separate list of blocks dedicated to this size for better memory efficiency.
116 */
117 int (*add_special_size)(struct aws_s3_buffer_pool *pool, size_t buffer_size);
118
119 /**
120 * Release all special-sized blocks from the buffer pool.
121 * This frees all memory allocated for the special size optimization.
122 */
123 void (*release_special_size)(struct aws_s3_buffer_pool *pool, size_t buffer_size);
124
125 /**
126 * Align a range size to the buffer pool's allocation strategy.
127 * Returns the optimal aligned size based on the buffer pool's configuration.
128 */
129 uint64_t (*derive_aligned_buffer_size)(struct aws_s3_buffer_pool *pool, uint64_t size);
130
131 /* Implement below for custom ref count behavior. Alternatively set those to null and init the ref count. */
132 struct aws_s3_buffer_pool *(*acquire)(struct aws_s3_buffer_pool *pool);
133 struct aws_s3_buffer_pool *(*release)(struct aws_s3_buffer_pool *pool);
134 };
135
136 /**
137 * Polymorphic buffer pool.
138 */
139 struct aws_s3_buffer_pool {
140 struct aws_s3_buffer_pool_vtable *vtable;
141 struct aws_ref_count ref_count;
142 void *impl;
143 };
144
145 AWS_S3_API struct aws_future_s3_buffer_ticket *aws_s3_buffer_pool_reserve(
146 struct aws_s3_buffer_pool *buffer_pool,
147 struct aws_s3_buffer_pool_reserve_meta meta);
148 AWS_S3_API void aws_s3_buffer_pool_trim(struct aws_s3_buffer_pool *buffer_pool);
149
150 AWS_S3_API struct aws_s3_buffer_pool *aws_s3_buffer_pool_acquire(struct aws_s3_buffer_pool *buffer_pool);
151 AWS_S3_API struct aws_s3_buffer_pool *aws_s3_buffer_pool_release(struct aws_s3_buffer_pool *buffer_pool);
152
153 /**
154 * Buffer pool configuration options.
155 */
156 struct aws_s3_buffer_pool_config {
157 struct aws_s3_client *client; /* Client creating the pool. */
158 size_t part_size; /* Default part size of the client. */
159 size_t max_part_size; /* Max part size configured on the client. */
160 size_t memory_limit; /* Memory limit set on the client. */
161 };
162
163 /**
164 * Factory to construct the pool for the given config. Passes along buffer related info configured on the client, which
165 * factory may ignore when considering how to construct pool.
166 * This implementation should fail if pool cannot be constructed for some reason (ex. if config params cannot be met),
167 * by logging failure reason, returning null and raising aws_error.
168 */
169 typedef struct aws_s3_buffer_pool *(aws_s3_buffer_pool_factory_fn)(struct aws_allocator *allocator,
170 struct aws_s3_buffer_pool_config config,
171 void *user_data);
172
173 /**
174 * Optimize the buffer pool for allocations of a specific size.
175 * Creates a separate list of blocks dedicated to this size for better memory efficiency.
176 * Allocations of exactly this size will use these special blocks instead of the regular primary/secondary storage.
177 *
178 * @param buffer_pool The buffer pool to optimize
179 * @param buffer_size The size to optimize for (must be > 0)
180 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
181 */
182 AWS_S3_API
183 int aws_s3_buffer_pool_add_special_size(struct aws_s3_buffer_pool *buffer_pool, size_t buffer_size);
184
185 /**
186 * Release the special-sized blocks from the buffer pool.
187 * Should be called when done with the special-sized allocations.
188 *
189 * @param buffer_pool The buffer pool
190 * @param buffer_size The special size to release blocks for
191 */
192 AWS_S3_API
193 void aws_s3_buffer_pool_release_special_size(struct aws_s3_buffer_pool *buffer_pool, size_t buffer_size);
194
195 /**
196 * Align a range size to the buffer pool's allocation strategy.
197 * This function determines the optimal aligned size based on the buffer pool's configuration.
198 * For sizes within the primary allocation range, it aligns to chunk boundaries.
199 * For larger sizes that go to secondary storage, it returns the size as-is.
200 *
201 * @param buffer_pool The buffer pool to use for alignment (can be NULL, in which case size is returned unchanged)
202 * @param size The size to align
203 * @return The aligned size that's optimal for the buffer pool's allocation strategy
204 */
205 AWS_S3_API
206 uint64_t aws_s3_buffer_pool_derive_aligned_buffer_size(struct aws_s3_buffer_pool *buffer_pool, uint64_t size);
207
208 AWS_EXTERN_C_END
209 AWS_POP_SANE_WARNING_LEVEL
210
211 #endif /* AWS_S3_BUFFER_POOL_H */