Skip to content

Commit e2cee6a

Browse files
leonardo-albertovichedsiper
authored andcommitted
in_storage_backlog: improved dummy route mask allocation strategy
This change is meant to allocate the dummy route mask once to avoid heap fragmentation. Additionally it fixes a non issue with the superfluous memset (calloc already zeroes the memory) where it was only zeoring 64 bits of the mask regardless of its size due to the leftover sizeof(dummy_routes_mask). Signed-off-by: Leonardo Alminana <[email protected]>
1 parent 9d6c3cb commit e2cee6a

File tree

1 file changed

+28
-23
lines changed
  • plugins/in_storage_backlog

1 file changed

+28
-23
lines changed

plugins/in_storage_backlog/sb.c

+28-23
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct flb_sb {
5151
struct flb_input_instance *ins; /* input instance */
5252
struct cio_ctx *cio; /* chunk i/o instance */
5353
struct mk_list backlogs; /* list of all pending chunks segregated by output plugin */
54+
flb_route_mask_element *dummy_routes_mask; /* dummy route mask used when segregating events */
5455
};
5556

5657

@@ -280,43 +281,33 @@ static int sb_append_chunk_to_segregated_backlogs(struct cio_chunk *target_chun
280281
int tag_len;
281282
const char * tag_buf;
282283
int result;
283-
flb_route_mask_element *dummy_routes_mask;
284-
285-
dummy_routes_mask = flb_calloc(context->ins->config->route_mask_slots,
286-
sizeof(flb_route_mask_element));
287-
288-
if (dummy_routes_mask == NULL) {
289-
flb_error("[storage backlog] could not allocate route mask elements %s/%s",
290-
stream->name, target_chunk->name);
291-
292-
return -1;
293-
}
294284

295285
memset(&dummy_input_chunk, 0, sizeof(struct flb_input_chunk));
296-
memset(dummy_routes_mask, 0, sizeof(dummy_routes_mask));
286+
287+
memset(context->dummy_routes_mask,
288+
0,
289+
context->ins->config->route_mask_slots * sizeof(flb_route_mask_element));
297290

298291
dummy_input_chunk.in = context->ins;
299292
dummy_input_chunk.chunk = target_chunk;
300-
dummy_input_chunk.routes_mask = dummy_routes_mask;
293+
dummy_input_chunk.routes_mask = context->dummy_routes_mask;
301294

302295
chunk_size = cio_chunk_get_real_size(target_chunk);
303296

304297
if (chunk_size < 0) {
305298
flb_warn("[storage backlog] could not get real size of chunk %s/%s",
306299
stream->name, target_chunk->name);
307-
flb_free(dummy_routes_mask);
308300

309-
return -2;
301+
return -1;
310302
}
311303

312304
result = flb_input_chunk_get_tag(&dummy_input_chunk, &tag_buf, &tag_len);
313305
if (result == -1) {
314306
flb_error("[storage backlog] could not retrieve chunk tag from %s/%s, "
315307
"removing it from the queue",
316308
stream->name, target_chunk->name);
317-
flb_free(dummy_routes_mask);
318309

319-
return -3;
310+
return -2;
320311
}
321312

322313
flb_routes_mask_set_by_tag(dummy_input_chunk.routes_mask, tag_buf, tag_len,
@@ -330,15 +321,11 @@ static int sb_append_chunk_to_segregated_backlogs(struct cio_chunk *target_chun
330321
result = sb_append_chunk_to_segregated_backlog(target_chunk, stream,
331322
chunk_size, backlog);
332323
if (result) {
333-
flb_free(dummy_routes_mask);
334-
335-
return -4;
324+
return -3;
336325
}
337326
}
338327
}
339328

340-
flb_free(dummy_routes_mask);
341-
342329
return 0;
343330
}
344331

@@ -670,12 +657,25 @@ static int cb_sb_init(struct flb_input_instance *in,
670657
char mem[32];
671658
struct flb_sb *ctx;
672659

673-
ctx = flb_malloc(sizeof(struct flb_sb));
660+
ctx = flb_calloc(1, sizeof(struct flb_sb));
661+
674662
if (!ctx) {
675663
flb_errno();
676664
return -1;
677665
}
678666

667+
ctx->dummy_routes_mask = flb_calloc(in->config->route_mask_slots,
668+
sizeof(flb_route_mask_element));
669+
670+
if (ctx->dummy_routes_mask == NULL) {
671+
flb_errno();
672+
flb_free(ctx);
673+
674+
flb_error("[storage backlog] could not allocate route mask elements");
675+
676+
return -1;
677+
}
678+
679679
ctx->cio = data;
680680
ctx->ins = in;
681681
ctx->mem_limit = flb_utils_size_to_bytes(config->storage_bl_mem_limit);
@@ -692,6 +692,7 @@ static int cb_sb_init(struct flb_input_instance *in,
692692
ret = flb_input_set_collector_time(in, cb_queue_chunks, 1, 0, config);
693693
if (ret < 0) {
694694
flb_plg_error(ctx->ins, "could not create collector");
695+
flb_free(ctx->dummy_routes_mask);
695696
flb_free(ctx);
696697
return -1;
697698
}
@@ -720,6 +721,10 @@ static int cb_sb_exit(void *data, struct flb_config *config)
720721

721722
sb_destroy_backlogs(ctx);
722723

724+
if (ctx->dummy_routes_mask != NULL) {
725+
flb_free(ctx->dummy_routes_mask);
726+
}
727+
723728
flb_free(ctx);
724729

725730
return 0;

0 commit comments

Comments
 (0)