Browse Source

Add some memory statistics.

jeroen
Jeroen Vreeken 5 years ago
parent
commit
57f7d6fc0e
3 changed files with 40 additions and 18 deletions
  1. +26
    -18
      controller/controller/controller_mem.c
  2. +2
    -0
      controller/controller/controller_mem.h
  3. +12
    -0
      controller/dt_ctrl.c

+ 26
- 18
controller/controller/controller_mem.c View File

@ -22,10 +22,6 @@
#include <stdbool.h>
#include <string.h>
static size_t total;
static size_t total_w;
static size_t total_r;
#define BLOCK_SIZE (256*1024)
struct controller_mem_block {
@ -38,7 +34,7 @@ struct controller_mem_chunk {
struct controller_mem_chunk *next;
size_t off;
size_t len;
bool used;
int flags;
};
static struct controller_mem_chunk *new_chunk(void)
@ -105,17 +101,11 @@ void *controller_mem_calloc(int flags, size_t nmemb, size_t size)
}
#endif
total += sz;
if (flags & CONTROLLER_MEM_PERIODIC_WRITE)
total_w += sz;
if (flags & CONTROLLER_MEM_PERIODIC_READ)
total_r += sz;
for (block = blocks; block; block = block->next) {
for (chunk = &block->chunks; *chunk; chunk = &(*chunk)->next) {
apad = (align - ((*chunk)->off & (align-1))) & (align-1);
if (!(*chunk)->used && (*chunk)->len >= sz + apad) {
if (!(*chunk)->flags && (*chunk)->len >= sz + apad) {
break;
}
}
@ -154,7 +144,7 @@ void *controller_mem_calloc(int flags, size_t nmemb, size_t size)
e_chunk->len = r_chunk->len - sz;
r_chunk->len -= e_chunk->len;
}
r_chunk->used = true;
r_chunk->flags = flags;
ptr = (char *)block->base + r_chunk->off;
memset(ptr, 0, r_chunk->len);
@ -183,7 +173,7 @@ void *controller_mem_realloc(int flags, void *ptr, size_t size)
if (size == chunk->len)
return ptr;
if (size < chunk->len) {
if (!chunk->next || chunk->next->used) {
if (!chunk->next || chunk->next->flags) {
struct controller_mem_chunk *next;
next = calloc(1, sizeof(*next));
@ -201,7 +191,7 @@ void *controller_mem_realloc(int flags, void *ptr, size_t size)
return ptr;
}
if (size > chunk->len &&
chunk->next && !chunk->next->used &&
chunk->next && !chunk->next->flags &&
chunk->next->len + chunk->len >= size) {
size_t nnew = size - chunk->len;
@ -245,7 +235,7 @@ void controller_mem_free(void *ptr)
for (block = blocks; block; block = block->next) {
for (chunk = block->chunks; chunk; chunk = chunk->next) {
if ((char *)block->base + chunk->off == ptr) {
chunk->used = false;
chunk->flags = 0;
ptr = NULL;
break;
}
@ -263,9 +253,9 @@ void controller_mem_free(void *ptr)
/* cleanup free chunks */
for (block = blocks; block; block = block->next) {
for (chunk = block->chunks; chunk; chunk = chunk->next) {
if (chunk->used)
if (chunk->flags)
continue;
while (chunk->next && !chunk->next->used) {
while (chunk->next && !chunk->next->flags) {
struct controller_mem_chunk *next;
next = chunk->next;
@ -277,3 +267,21 @@ void controller_mem_free(void *ptr)
}
}
int controller_mem_usage(int flags, size_t *size)
{
struct controller_mem_block *block;
struct controller_mem_chunk *chunk;
*size = 0;
for (block = blocks; block; block = block->next) {
for (chunk = block->chunks; chunk; chunk = chunk->next) {
if (chunk->flags == flags) {
*size += chunk->len;
}
}
}
return 0;
}

+ 2
- 0
controller/controller/controller_mem.h View File

@ -27,4 +27,6 @@ void *controller_mem_calloc(int flags, size_t nmemb, size_t size);
void *controller_mem_realloc(int flags, void *ptr, size_t size);
void controller_mem_free(void *);
int controller_mem_usage(int flags, size_t *size);
#endif /* _INCLUDE_CONTROLLER_MEM_H_ */

+ 12
- 0
controller/dt_ctrl.c View File

@ -42,6 +42,7 @@
#include <controller/controller_dumpdot.h>
#include <controller/controller_load.h>
#include <controller/controller_time.h>
#include <controller/controller_mem.h>
#include <shell/shell.h>
#include <ec/ec.h>
#include <log/log.h>
@ -58,6 +59,7 @@ int main(int argc, char **argv)
int blocks;
int outputs;
int i;
size_t usage_0, usage_r, usage_w, usage_rw;
sigemptyset (&sigset);
sigaddset(&sigset, SIGALRM);
@ -124,6 +126,16 @@ int main(int argc, char **argv)
controller_sample_start();
controller_mem_usage(0, &usage_0);
controller_mem_usage(CONTROLLER_MEM_PERIODIC_WRITE, &usage_w);
controller_mem_usage(CONTROLLER_MEM_PERIODIC_READ, &usage_r);
controller_mem_usage(CONTROLLER_MEM_PERIODIC_WRITE | CONTROLLER_MEM_PERIODIC_READ, &usage_rw);
log_send(LOG_T_DEBUG, "Memory usage: %zd bytes unused", usage_0);
log_send(LOG_T_DEBUG, "Memory usage: %zd bytes periodic read", usage_r);
log_send(LOG_T_DEBUG, "Memory usage: %zd bytes periodic write", usage_w);
log_send(LOG_T_DEBUG, "Memory usage: %zd bytes periodic read/write", usage_rw);
log_send(LOG_T_DEBUG, "Entering state polling loop");
int oks_p = 0, errors_p = 0, recoverables_p = 0;
while (1) {


Loading…
Cancel
Save