profiler: Decrease memory usage

Make flush buffer initially smaller and increase it when needed.
This commit is contained in:
Jonas Holmberg 2024-05-29 17:05:00 +02:00 committed by Wim Taymans
parent e4432190f7
commit 1deffe757c

View file

@ -58,7 +58,7 @@ PW_LOG_TOPIC(mod_topic, "mod." NAME);
#define TMP_BUFFER (16 * 1024) #define TMP_BUFFER (16 * 1024)
#define DATA_BUFFER (32 * 1024) #define DATA_BUFFER (32 * 1024)
#define FLUSH_BUFFER (8 * 1024 * 1024) #define FLUSH_BUFFER (8 * 1024)
int pw_protocol_native_ext_profiler_init(struct pw_context *context); int pw_protocol_native_ext_profiler_init(struct pw_context *context);
@ -107,12 +107,8 @@ struct impl {
struct spa_source *flush_event; struct spa_source *flush_event;
unsigned int listening:1; unsigned int listening:1;
#ifdef max_align_t uint8_t *flush;
alignas(max_align_t) size_t flush_size;
#else
alignas(64)
#endif
uint8_t flush[FLUSH_BUFFER + sizeof(struct spa_pod_struct)];
}; };
struct resource_data { struct resource_data {
@ -138,16 +134,27 @@ static void do_flush_event(void *data, uint64_t count)
avail = spa_ringbuffer_get_read_index(&n->buffer, &idx); avail = spa_ringbuffer_get_read_index(&n->buffer, &idx);
pw_log_trace("%p avail %d", impl, avail); pw_log_trace("%p: avail %d", impl, avail);
if (avail > 0) { if (avail > 0) {
if (total + avail < FLUSH_BUFFER) { size_t size = total + avail + sizeof(struct spa_pod_struct);
spa_ringbuffer_read_data(&n->buffer, n->data, DATA_BUFFER, if (size > impl->flush_size) {
idx % DATA_BUFFER, uint8_t *flush;
SPA_PTROFF(p, sizeof(struct spa_pod_struct) + total, void), flush = realloc(impl->flush, size);
avail); if (flush == NULL) {
total += avail; pw_log_warn("%p: failed to realloc flush size %zu", impl, impl->flush_size);
continue;
}
impl->flush = flush;
impl->flush_size = size;
pw_log_debug("%p: new flush buffer size %zu", impl, impl->flush_size);
p = (struct spa_pod_struct *)impl->flush;
} }
spa_ringbuffer_read_data(&n->buffer, n->data, DATA_BUFFER,
idx % DATA_BUFFER,
SPA_PTROFF(p, sizeof(struct spa_pod_struct) + total, void),
avail);
total += avail;
spa_ringbuffer_read_update(&n->buffer, idx + avail); spa_ringbuffer_read_update(&n->buffer, idx + avail);
} }
} }
@ -411,6 +418,7 @@ static void module_destroy(void *data)
pw_loop_destroy_source(impl->main_loop, impl->flush_event); pw_loop_destroy_source(impl->main_loop, impl->flush_event);
free(impl->flush);
free(impl); free(impl);
} }
@ -446,6 +454,12 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
impl = calloc(1, sizeof(struct impl)); impl = calloc(1, sizeof(struct impl));
if (impl == NULL) if (impl == NULL)
return -errno; return -errno;
impl->flush_size = FLUSH_BUFFER + sizeof(struct spa_pod_struct);
impl->flush = malloc(impl->flush_size);
if (impl->flush == NULL) {
free(impl);
return -errno;
}
spa_list_init(&impl->node_list); spa_list_init(&impl->node_list);
pw_protocol_native_ext_profiler_init(context); pw_protocol_native_ext_profiler_init(context);
@ -468,6 +482,7 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
pw_properties_copy(props), pw_properties_copy(props),
global_bind, impl); global_bind, impl);
if (impl->global == NULL) { if (impl->global == NULL) {
free(impl->flush);
free(impl); free(impl);
return -errno; return -errno;
} }