Skip to content

Commit

Permalink
Improve memuse by providing total in use and in the malloc arena
Browse files Browse the repository at this point in the history
  • Loading branch information
carlgsmith committed Sep 17, 2024
1 parent 1460d3d commit 309449d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
23 changes: 15 additions & 8 deletions apteryx.c
Original file line number Diff line number Diff line change
Expand Up @@ -2262,15 +2262,22 @@ apteryx_memuse (const char *path)
DEBUG ("MEMUSE: %s\n", path ?: "NULL");

/* Check path */
path = validate_path (path, &url);
/* if path is empty, or path ends in '/' but is not the root db path (ie "/") */
if (!path ||
((path[strlen(path)-1] == '/') && strlen(path) > 1))
if (path[0] == '.')
{
ERROR ("MEMUSE: invalid path (%s)!\n", path ?: "NULL");
free (url);
assert (!apteryx_debug || path);
return 0;
url = strdup(default_url);
}
else
{
path = validate_path (path, &url);
/* if path is empty, or path ends in '/' but is not the root db path (ie "/") */
if (!path ||
((path[strlen(path)-1] == '/') && strlen(path) > 1))
{
ERROR ("MEMUSE: invalid path (%s)!\n", path ?: "NULL");
free (url);
assert (!apteryx_debug || path);
return 0;
}
}

/* IPC */
Expand Down
2 changes: 2 additions & 0 deletions apteryx.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ uint64_t apteryx_timestamp (const char *path);
/**
* Get the memory usage in bytes of a given path
* @param path path to get the memory usage for
* '.' = total memory in use (mi.uordblks + mi.hblkhd)
* '..' = total memory allocated (mi.arena + mi.hblkhd)
* @return 0 if the path doesn't exist, memory usage in bytes otherwise
*/
uint64_t apteryx_memuse (const char *path);
Expand Down
10 changes: 8 additions & 2 deletions apteryxc.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,7 @@ main (int argc, char **argv)
{
if (!path || param)
{
usage ();
return 0;
path = "/";
}
apteryx_init (apteryx_debug);
if (path[strlen(path) - 1] != '/')
Expand All @@ -479,6 +478,13 @@ main (int argc, char **argv)
printf ("%10"PRIu64" %s\n", size, (char *) _iter->data);
}
g_list_free_full (paths, free);
if (g_strcmp0 (path, "/") == 0)
{
uint64_t total = apteryx_memuse ("..");
uint64_t used = apteryx_memuse (".");
uint64_t db = apteryx_memuse ("/");
printf ("%"PRIu64"/%"PRIu64" total bytes used/arena (%"PRIu64" in database)\n", used, total, db);
}
apteryx_shutdown ();
g_free (path);
break;
Expand Down
16 changes: 15 additions & 1 deletion apteryxd.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sys/poll.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <malloc.h>
#include "apteryx.h"
#include "internal.h"

Expand Down Expand Up @@ -2436,7 +2437,20 @@ handle_memuse (rpc_message msg)
INC_COUNTER (counters.memuse);

/* Lookup value */
value = db_memuse (path);
if (path[0] == '.' && path[1] == '\0')
{
/* Total memory in use */
struct mallinfo2 mi = mallinfo2 ();
value = (unsigned int) (mi.uordblks) + (unsigned int) (mi.hblkhd);
}
else if (path[0] == '.' && path[1] == '.' && path[2] == '\0')
{
/* Total memory allocated */
struct mallinfo2 mi = mallinfo2 ();
value = (unsigned int) (mi.arena) + (unsigned int) (mi.hblkhd);
}
else
value = db_memuse (path);

/* Send result */
DEBUG (" = %"PRIu64"\n", value);
Expand Down

0 comments on commit 309449d

Please sign in to comment.