Skip to content

Commit

Permalink
Fixes #1107 - Use a tiered list of read-grant amounts based on percen…
Browse files Browse the repository at this point in the history
…tage of buffer capacity that's in use.
  • Loading branch information
ted-ross committed Jun 2, 2023
1 parent 66f3668 commit f305f72
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
3 changes: 2 additions & 1 deletion include/qpid/dispatch/alloc_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ static inline void *qd_alloc_deref_safe_ptr(const qd_alloc_safe_ptr_t *sp)
void free_##T(T *p); \
typedef qd_alloc_safe_ptr_t T##_sp; \
void set_safe_ptr_##T(T *p, T##_sp *sp); \
T *safe_deref_##T(T##_sp sp)
T *safe_deref_##T(T##_sp sp); \
qd_alloc_stats_t *alloc_stats_##T(void)

/**
* Define allocator configuration.
Expand Down
62 changes: 57 additions & 5 deletions src/adaptors/adaptor_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/
#include "adaptor_common.h"
#include "adaptor_buffer.h"

#include "qpid/dispatch/amqp.h"
#include "qpid/dispatch/connection_manager.h"
Expand All @@ -29,6 +30,11 @@

ALLOC_DEFINE(qd_adaptor_config_t);

static uint64_t buffer_ceiling = 0;
static uint64_t buffer_threshold_50;
static uint64_t buffer_threshold_75;
static uint64_t buffer_threshold_85;

void qd_free_adaptor_config(qd_adaptor_config_t *config)
{
if (!config)
Expand Down Expand Up @@ -72,24 +78,70 @@ qd_error_t qd_load_adaptor_config(qd_adaptor_config_t *config, qd_entity_t *enti
return qd_error_code();
}

void qd_adaptor_common_init(void)
{
if (buffer_ceiling > 0) {
return;
}

char *ceiling_string = getenv("SKUPPER_MEMORY_CEILING");
uint64_t memory_ceiling = (uint64_t) 4 * (uint64_t) 1024 * (uint64_t) 1024 * (uint64_t) 1024; // 4 Gig default

if (!!ceiling_string) {
long long convert = atoll(ceiling_string);
if (convert > 0) {
memory_ceiling = (uint64_t) convert;
}
}

buffer_ceiling = MAX(memory_ceiling / QD_ADAPTOR_MAX_BUFFER_SIZE, 100);
buffer_threshold_50 = buffer_ceiling / 2;
buffer_threshold_75 = (buffer_ceiling / 20) * 15;
buffer_threshold_85 = (buffer_ceiling / 20) * 17;
}

int qd_raw_connection_grant_read_buffers(pn_raw_connection_t *pn_raw_conn)
{
#define TIER_1 8 // [0% .. 50%)
#define TIER_2 4 // [50% .. 75%)
#define TIER_3 2 // [75% .. 85%)
#define TIER_4 2 // [85% .. 100%]

assert(pn_raw_conn);
pn_raw_buffer_t raw_buffers[RAW_BUFFER_BATCH];
size_t desired = pn_raw_connection_read_buffers_capacity(pn_raw_conn);
const size_t granted = desired;
size_t desired = TIER_4;
size_t capacity = MIN(pn_raw_connection_read_buffers_capacity(pn_raw_conn), TIER_1);

if (capacity == 0) {
return 0;
}

qd_alloc_stats_t *stats = alloc_stats_qd_adaptor_buffer_t();
uint64_t buffers_in_use = !!stats ? stats->held_by_threads : 0;

if (buffers_in_use < buffer_threshold_50) {
desired = TIER_1;
} else if (buffers_in_use < buffer_threshold_75) {
desired = TIER_2;
} else if (buffers_in_use < buffer_threshold_85) {
desired = TIER_3;
}

size_t already_granted = TIER_1 - capacity;
const size_t granted = desired > already_granted ? desired - already_granted : 0;
size_t count = granted;

while (desired) {
while (count) {
int i;
for (i = 0; i < desired && i < RAW_BUFFER_BATCH; ++i) {
for (i = 0; i < count && i < RAW_BUFFER_BATCH; ++i) {
qd_adaptor_buffer_t *buf = qd_adaptor_buffer();
raw_buffers[i].bytes = (char *) qd_adaptor_buffer_base(buf);
raw_buffers[i].capacity = qd_adaptor_buffer_capacity(buf);
raw_buffers[i].size = 0;
raw_buffers[i].offset = 0;
raw_buffers[i].context = (uintptr_t) buf;
}
desired -= i;
count -= i;
pn_raw_connection_give_read_buffers(pn_raw_conn, raw_buffers, i);
}

Expand Down
5 changes: 5 additions & 0 deletions src/adaptors/adaptor_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ ALLOC_DECLARE(qd_adaptor_config_t);
qd_error_t qd_load_adaptor_config(qd_adaptor_config_t *config, qd_entity_t *entity);
void qd_free_adaptor_config(qd_adaptor_config_t *config);

/**
* Perform router-startup actions used in the common module.
*/
void qd_adaptor_common_init(void);

/**
* Grants as many read qd_adaptor buffers as returned by pn_raw_connection_read_buffers_capacity().
* Maximum read capacity is set to 16 in proton raw api.
Expand Down
1 change: 1 addition & 0 deletions src/adaptors/tcp/tcp_adaptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2234,6 +2234,7 @@ static void qdr_tcp_activate_CT(void *notused, qdr_connection_t *c)
*/
static void qdr_tcp_adaptor_init(qdr_core_t *core, void **adaptor_context)
{
qd_adaptor_common_init();
qdr_tcp_adaptor_t *adaptor = NEW(qdr_tcp_adaptor_t);
adaptor->core = core;
adaptor->adaptor = qdr_protocol_adaptor(core,
Expand Down

0 comments on commit f305f72

Please sign in to comment.