Skip to content
Snippets Groups Projects
Commit cd748405 authored by Peter W. Draper's avatar Peter W. Draper
Browse files

Process the memory logger dump in SWIFT

Now keeps a sum across all dumps and matches frees to previous dumps as well.

Note needs the hashmap() function coming with the FOF code
parent e2d88c94
Branches
Tags
1 merge request!838Speed up memory use reports
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <unistd.h> #include <unistd.h>
/* Local defines. */ /* Local defines. */
#include "c_hashmap/hashmap.h"
#include "memuse.h" #include "memuse.h"
/* Local includes. */ /* Local includes. */
...@@ -70,6 +71,9 @@ struct memuse_log_entry { ...@@ -70,6 +71,9 @@ struct memuse_log_entry {
/* Label associated with the memory. */ /* Label associated with the memory. */
char label[MEMUSE_MAXLAB + 1]; char label[MEMUSE_MAXLAB + 1];
/* Key for hashmap. Formatted address of memory. */
char key[MEMUSE_MAXLAB + 1];
}; };
/* The log of allocations and frees. */ /* The log of allocations and frees. */
...@@ -78,6 +82,12 @@ static volatile size_t memuse_log_size = 0; ...@@ -78,6 +82,12 @@ static volatile size_t memuse_log_size = 0;
static volatile size_t memuse_log_count = 0; static volatile size_t memuse_log_count = 0;
static volatile size_t memuse_log_done = 0; static volatile size_t memuse_log_done = 0;
/* Hashmap for matching frees to allocations. */
static map_t memuse_hashmap;
/* Current sum of memory in use. Only used in dumping. */
static size_t memuse_sum = 0;
#define MEMUSE_INITLOG 1000000 #define MEMUSE_INITLOG 1000000
static void memuse_log_reallocate(size_t ind) { static void memuse_log_reallocate(size_t ind) {
...@@ -88,6 +98,9 @@ static void memuse_log_reallocate(size_t ind) { ...@@ -88,6 +98,9 @@ static void memuse_log_reallocate(size_t ind) {
sizeof(struct memuse_log_entry) * MEMUSE_INITLOG)) == NULL) sizeof(struct memuse_log_entry) * MEMUSE_INITLOG)) == NULL)
error("Failed to allocate memuse log."); error("Failed to allocate memuse log.");
/* Create the hashmap. */
memuse_hashmap = hashmap_new();
/* Last action. */ /* Last action. */
memuse_log_size = MEMUSE_INITLOG; memuse_log_size = MEMUSE_INITLOG;
...@@ -162,15 +175,61 @@ void memuse_log_dump(const char *filename) { ...@@ -162,15 +175,61 @@ void memuse_log_dump(const char *filename) {
/* Write a header. */ /* Write a header. */
fprintf(fd, "# Current use: %s\n", memuse_process(1)); fprintf(fd, "# Current use: %s\n", memuse_process(1));
fprintf(fd, "# cpufreq: %lld\n", clocks_get_cpufreq()); fprintf(fd, "# cpufreq: %lld\n", clocks_get_cpufreq());
fprintf(fd, "# dtic adr rank step allocated label size\n"); fprintf(fd, "# dtic rank step label size sum\n");
for (size_t k = 0; k < memuse_log_count; k++) { for (size_t k = 0; k < memuse_log_count; k++) {
fprintf(fd, "%lld %p %d %d %d %s %zd\n", memuse_log[k].dtic,
memuse_log[k].ptr, memuse_log[k].rank, memuse_log[k].step, /* Check if this address has already been used. */
memuse_log[k].allocated, memuse_log[k].label, memuse_log[k].size); struct memuse_log_entry *stored_log;
sprintf(memuse_log[k].key, "%p", memuse_log[k].ptr);
int error = hashmap_get(memuse_hashmap, memuse_log[k].key,
(void **)(&stored_log));
if (error == MAP_OK) {
/* Found the allocation, this should be the free. */
if (memuse_log[k].allocated) {
/* Allocated twice, this is an error, but we cannot abort as that will
* attempt another memory dump, so just complain. */
#if SWIFT_DEBUG_CHECKS
message("Allocated the same address twice (%s: %zd)\n",
memuse_log[k].key, memuse_log[k].size);
#endif
continue;
}
/* Free, so we can update the size to remove the allocation. */
memuse_log[k].size = -stored_log->size;
/* And remove this key. */
hashmap_remove(memuse_hashmap, memuse_log[k].key);
} else if (memuse_log[k].allocated) {
/* Not found, so new allocation which we store. */
hashmap_put(memuse_hashmap, memuse_log[k].key, &memuse_log[k]);
} else if (!memuse_log[k].allocated) {
/* Unmatched free, OK if NULL. */
#if SWIFT_DEBUG_CHECKS
if (memuse_log[k].ptr != NULL)
fprintf(stderr, "Unmatched non-NULL free: %s\n", memuse_log[k].label);
#endif
continue;
}
memuse_sum += memuse_log[k].size;
/* And output. */
fprintf(fd, "%lld %d %d %s %zd %zd\n", memuse_log[k].dtic,
memuse_log[k].rank, memuse_log[k].step, memuse_log[k].label,
memuse_log[k].size, memuse_sum);
} }
/* Clear the log. */ /* Clear the log. OK as records are dumped, but note active memory is still
* in the hashmap. */
memuse_log_count = 0; memuse_log_count = 0;
/* Close the file. */ /* Close the file. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment