Debian Codegraph @

crt/aws-c-s3/include/aws/s3/private/s3_default_buffer_pool.h

raw symbols SCIP
1 #ifndef AWS_S3_BUFFER_ALLOCATOR_H
2 #define AWS_S3_BUFFER_ALLOCATOR_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/hash_table.h>
10 #include <aws/common/mutex.h>
11 #include <aws/s3/s3.h>
12 #include <aws/s3/s3_buffer_pool.h>
13
14 /*
15 * S3 buffer pool.
16 * Buffer pool used for pooling part sized buffers for Put/Get operations.
17 * Provides additional functionally for limiting overall memory used.
18 * High-level buffer pool usage flow:
19 * - Create buffer with overall memory limit and common buffer size, aka chunk
20 * size (typically part size configured on client)
21 * - For each request:
22 * -- call reserve to acquire ticket for future buffer acquisition. this will
23 * mark memory reserved, but would not allocate it. if reserve call hits
24 * memory limit, it fails and reservation hold is put on the whole buffer
25 * pool. (aws_s3_buffer_pool_remove_reservation_hold can be used to remove
26 * reservation hold).
27 * -- once request needs memory, it can exchange ticket for a buffer using
28 * aws_s3_buffer_pool_acquire_buffer. this operation never fails, even if it
29 * ends up going over memory limit.
30 * -- buffer lifetime is tied to the ticket. so once request is done with the
31 * buffer, ticket is released and buffer returns back to the pool.
32 */
33
34 AWS_EXTERN_C_BEGIN
35
36 struct aws_s3_default_buffer_pool;
37 struct aws_s3_default_buffer_ticket;
38
39 struct aws_s3_default_buffer_pool_usage_stats {
40 /* Effective Max memory limit. Memory limit value provided during construction minus
41 * buffer reserved for overhead of the pool */
42 size_t mem_limit;
43
44 /* Max size of buffer to be allocated from primary. */
45 size_t primary_cutoff;
46
47 /* Min size of buffer to be allocated from primary. */
48 size_t primary_min_cutoff;
49
50 /* Overall memory allocated for blocks. */
51 size_t primary_allocated;
52 /* Number of blocks allocated in primary. */
53 size_t primary_num_blocks;
54 /* Memory used in primary storage.
55 * Does not account for wasted space if memory doesn't map perfectly into chunks.
56 * This is always <= primary_allocated */
57 size_t primary_used;
58 /* How much memory is reserved, but not yet used, in primary storage.
59 * Does not account for wasted space if memory doesn't map perfectly into chunks. */
60 size_t primary_reserved;
61
62 /* Secondary memory used. Accurate, maps directly to base allocator. */
63 size_t secondary_used;
64 /* Secondary memory reserved, but not yet used. Accurate, maps directly to base allocator. */
65 size_t secondary_reserved;
66
67 /* Overall memory allocated for special-sized blocks. */
68 size_t special_blocks_allocated;
69 /* Number of special block sizes created. */
70 size_t special_blocks_num;
71 /* Memory reserved in special-sized blocks. */
72 size_t special_blocks_reserved;
73 /* Memory used in special-sized blocks. */
74 size_t special_blocks_used;
75
76 /* Bytes used in "forced" buffers (created even if they exceed memory limits).
77 * This is always <= primary_used + secondary_used */
78 size_t forced_used;
79 };
80
81 /* Structure to track special-sized blocks */
82 struct s3_special_block_list {
83 struct aws_allocator *allocator;
84 size_t buffer_size; /* Size of buffers in this list */
85 struct aws_array_list blocks; /* Array of uint8_t* pointers to allocated blocks */
86 };
87
88 struct aws_s3_default_buffer_pool {
89 struct aws_allocator *base_allocator;
90 struct aws_mutex mutex;
91
92 size_t block_size;
93 size_t chunk_size;
94 /* size at which allocations should go to secondary */
95 size_t primary_size_cutoff;
96
97 /* size below which allocations should go to secondary */
98 size_t primary_size_min_cutoff;
99
100 /* NOTE: See aws_s3_buffer_pool_usage_stats for descriptions of most fields */
101
102 size_t mem_limit;
103
104 size_t primary_allocated;
105 size_t primary_reserved;
106 size_t primary_used;
107
108 size_t special_blocks_allocated;
109 size_t special_blocks_reserved;
110 size_t special_blocks_used;
111
112 size_t secondary_reserved;
113 size_t secondary_used;
114
115 size_t forced_used;
116
117 struct aws_array_list blocks;
118
119 struct aws_linked_list pending_reserves;
120
121 /* Special-sized blocks: hash table mapping size -> struct s3_special_block_list * */
122 /* TODO: let's discuss about the special list lifetime. Should we just keep it with the memory pool? Concern is that
123 * the pool will live with the client, and may result in all sorts of special lists to be around. */
124 struct aws_hash_table special_blocks;
125
126 /* TEST ONLY: to force the special blocks alive during trim. */
127 bool force_keeping_special_blocks;
128 };
129
130 struct s3_pending_reserve {
131 struct aws_linked_list_node node;
132 struct aws_future_s3_buffer_ticket *ticket_future;
133 struct aws_s3_default_buffer_ticket *ticket;
134 struct aws_s3_buffer_pool_reserve_meta meta;
135 };
136
137 struct s3_buffer_pool_block {
138 size_t block_size;
139 uint8_t *block_ptr;
140 uint16_t alloc_bit_mask;
141 };
142
143 /*
144 * Create new buffer pool.
145 * chunk_size - specifies the size of memory that will most commonly be acquired
146 * from the pool (typically part size).
147 * mem_limit - limit on how much mem buffer pool can use. once limit is hit,
148 * buffers can no longer be reserved from (reservation hold is placed on the pool).
149 * Returns buffer pool pointer on success and NULL on failure.
150 */
151 AWS_S3_API struct aws_s3_buffer_pool *aws_s3_default_buffer_pool_new(
152 struct aws_allocator *allocator,
153 struct aws_s3_buffer_pool_config config);
154
155 /*
156 * Destroys buffer pool.
157 * Does nothing if buffer_pool is NULL.
158 */
159 AWS_S3_API void aws_s3_default_buffer_pool_destroy(struct aws_s3_buffer_pool *buffer_pool);
160
161 /*
162 * Reserves memory from the pool for later use.
163 * Best effort and can potentially reserve memory slightly over the limit.
164 * Reservation takes some memory out of the available pool, but does not
165 * allocate it right away.
166 * On success ticket will be returned.
167 * On failure NULL is returned, error is raised and reservation hold is placed
168 * on the buffer. Any further reservations while hold is active will fail.
169 * Remove reservation hold to unblock reservations.
170 *
171 * If you MUST acquire a buffer now (waiting to reserve a ticket would risk deadlock),
172 * use aws_s3_buffer_pool_acquire_forced_buffer() instead.
173 */
174 AWS_S3_API struct aws_future_s3_buffer_ticket *aws_s3_default_buffer_pool_reserve(
175 struct aws_s3_buffer_pool *buffer_pool,
176 struct aws_s3_buffer_pool_reserve_meta meta);
177
178 /*
179 * Trades in the ticket for a buffer.
180 * Cannot fail and can over allocate above mem limit if reservation was not accurate.
181 * Using the same ticket twice will return the same buffer.
182 * Buffer is only valid until the ticket is released.
183 */
184 AWS_S3_API struct aws_byte_buf aws_s3_default_buffer_pool_acquire_buffer(
185 struct aws_s3_buffer_pool *buffer_pool,
186 struct aws_s3_default_buffer_ticket *ticket);
187
188 /*
189 * Get pool memory usage stats.
190 */
191 AWS_S3_API struct aws_s3_default_buffer_pool_usage_stats aws_s3_default_buffer_pool_get_usage(
192 struct aws_s3_buffer_pool *buffer_pool);
193
194 enum aws_s3_default_buffer_pool_reserved_from {
195 AWS_S3_BUFFER_POOL_RESERVED_FROM_SECONDARY = 0,
196 AWS_S3_BUFFER_POOL_RESERVED_FROM_PRIMARY,
197 AWS_S3_BUFFER_POOL_RESERVED_FROM_SPECIAL,
198 };
199
200 /*
201 * Helper to determine which area of default buffer pool ticket was reserved from.
202 */
203 AWS_S3_API enum aws_s3_default_buffer_pool_reserved_from aws_s3_default_buffer_pool_get_ticket_reserved_from(
204 struct aws_s3_default_buffer_ticket *ticket);
205
206 /*
207 * Trims all unused mem from the pool.
208 * Warning: fairly slow operation, do not use in critical path.
209 * TODO: partial trimming? ex. only trim down to 50% of max?
210 */
211 AWS_S3_API void aws_s3_default_buffer_pool_trim(struct aws_s3_buffer_pool *buffer_pool);
212
213 AWS_EXTERN_C_END
214
215 #endif /* AWS_S3_BUFFER_ALLOCATOR_H */