pulse-server: implement stats

This commit is contained in:
Wim Taymans 2021-01-19 10:51:23 +01:00
parent d82066e49f
commit 7e5441fbb3
2 changed files with 33 additions and 14 deletions

View file

@ -118,6 +118,7 @@ enum {
struct message { struct message {
struct spa_list link; struct spa_list link;
struct stats *stat;
uint32_t extra[4]; uint32_t extra[4];
uint32_t channel; uint32_t channel;
uint32_t allocated; uint32_t allocated;
@ -441,13 +442,19 @@ static int message_get(struct message *m, ...)
static int ensure_size(struct message *m, uint32_t size) static int ensure_size(struct message *m, uint32_t size)
{ {
uint32_t alloc; uint32_t alloc, diff;
void *data;
if (m->length + size <= m->allocated) if (m->length + size <= m->allocated)
return size; return size;
alloc = SPA_ROUND_UP_N(SPA_MAX(m->allocated + size, 4096u), 4096u); alloc = SPA_ROUND_UP_N(SPA_MAX(m->allocated + size, 4096u), 4096u);
if ((m->data = realloc(m->data, alloc)) == NULL) diff = alloc - m->allocated;
if ((data = realloc(m->data, alloc)) == NULL)
return -errno; return -errno;
m->stat->allocated += diff;
m->stat->accumulated += diff;
m->data = data;
m->allocated = alloc; m->allocated = alloc;
return size; return size;
} }

View file

@ -70,6 +70,14 @@
#include "defs.h" #include "defs.h"
#include "json.h" #include "json.h"
struct stats {
uint32_t n_allocated;
uint32_t allocated;
uint32_t n_accumulated;
uint32_t accumulated;
uint32_t sample_cache;
};
#include "format.c" #include "format.c"
#include "volume.c" #include "volume.c"
#include "message.c" #include "message.c"
@ -86,13 +94,6 @@ struct client;
#include "sample.c" #include "sample.c"
struct operation {
struct spa_list link;
struct client *client;
uint32_t tag;
void (*callback) (struct operation *op);
};
struct client { struct client {
struct spa_list link; struct spa_list link;
struct impl *impl; struct impl *impl;
@ -229,6 +230,8 @@ struct impl {
struct spa_list servers; struct spa_list servers;
struct pw_map samples; struct pw_map samples;
struct stats stat;
}; };
#include "collect.c" #include "collect.c"
@ -240,6 +243,8 @@ static void sample_free(struct sample *sample)
pw_log_info("free sample id:%u name:%s", sample->index, sample->name); pw_log_info("free sample id:%u name:%s", sample->index, sample->name);
impl->stat.sample_cache -= sample->length;
if (sample->index != SPA_ID_INVALID) if (sample->index != SPA_ID_INVALID)
pw_map_remove(&impl->samples, sample->index); pw_map_remove(&impl->samples, sample->index);
if (sample->props) if (sample->props)
@ -276,6 +281,8 @@ static void message_free(struct client *client, struct message *msg, bool dequeu
spa_list_remove(&msg->link); spa_list_remove(&msg->link);
if (destroy) { if (destroy) {
pw_log_trace("destroy message %p", msg); pw_log_trace("destroy message %p", msg);
msg->stat->n_allocated--;
msg->stat->allocated -= msg->allocated;
free(msg->data); free(msg->data);
free(msg); free(msg);
} else { } else {
@ -296,6 +303,9 @@ static struct message *message_alloc(struct client *client, uint32_t channel, ui
if (msg == NULL) { if (msg == NULL) {
msg = calloc(1, sizeof(struct message)); msg = calloc(1, sizeof(struct message));
pw_log_trace("new message %p", msg); pw_log_trace("new message %p", msg);
msg->stat = &client->impl->stat;
msg->stat->n_allocated++;
msg->stat->n_accumulated++;
} }
if (msg == NULL) if (msg == NULL)
return NULL; return NULL;
@ -2278,6 +2288,8 @@ static int do_finish_upload_stream(struct client *client, uint32_t command, uint
sample->buffer = stream->buffer; sample->buffer = stream->buffer;
sample->length = stream->attr.maxlength; sample->length = stream->attr.maxlength;
impl->stat.sample_cache += sample->length;
stream->props = NULL; stream->props = NULL;
stream->buffer = NULL; stream->buffer = NULL;
stream_free(stream); stream_free(stream);
@ -3206,11 +3218,11 @@ static int do_stat(struct client *client, uint32_t command, uint32_t tag, struct
reply = reply_new(client, tag); reply = reply_new(client, tag);
message_put(reply, message_put(reply,
TAG_U32, 0, /* n_allocated */ TAG_U32, impl->stat.n_allocated, /* n_allocated */
TAG_U32, 0, /* allocated size */ TAG_U32, impl->stat.allocated, /* allocated size */
TAG_U32, 0, /* n_accumulated */ TAG_U32, impl->stat.n_accumulated, /* n_accumulated */
TAG_U32, 0, /* accumulated_size */ TAG_U32, impl->stat.accumulated, /* accumulated_size */
TAG_U32, 0, /* sample cache size */ TAG_U32, impl->stat.sample_cache, /* sample cache size */
TAG_INVALID); TAG_INVALID);
return send_message(client, reply); return send_message(client, reply);