stats.c 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdarg.h>
#include <malloc.h>
#include <errno.h>
#include <string.h>
#include "pthread_impl.h"
#include "malloc_impl.h"

#ifdef MUSL_ITERATE_AND_STATS_API
#define STAT_PRINTF_MAX_LEN 255
#define ALLOCATOR_VERSION 1
#define SEPARATOR_REPEATS 7

typedef void (write_cb_fun)(void *, const char *);

typedef enum {
	TABLE, XML
} print_mode;

typedef struct {
	size_t mmapped_regions;
	size_t total_mmapped_memory;
	size_t total_allocated_memory;
	size_t total_allocated_heap_space;
} malloc_stats_t;

static void stat_printf(write_cb_fun *write_cb, void *write_cb_arg, const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	char buf[STAT_PRINTF_MAX_LEN + 1];
	if (vsnprintf(buf, STAT_PRINTF_MAX_LEN, fmt, args)) {
		if (write_cb != NULL) {
			write_cb(write_cb_arg, buf);
		} else {
			printf(buf);
		}
	} else {
		fprintf(stderr, "Error writing to buffer");
	}
	va_end(args);
}

static void print_thread_stats_table(
	write_cb_fun *write_cb,
	void *write_cb_arg,
	int tid,
	malloc_stats_t *stats
)
{
	stat_printf(
		write_cb,
		write_cb_arg,
		"%-11d %-23zu %-20zu %-20zu\n",
		tid,
		stats->total_allocated_memory,
		stats->total_mmapped_memory,
		stats->mmapped_regions
	);
}

static void print_amount_xml(write_cb_fun *write_cb, void *write_cb_arg, const char *name, size_t value)
{
	stat_printf(write_cb, write_cb_arg, "<%s>%zu</%s>\n", name, value, name);
}

static void print_thread_specific_amounts_xml(write_cb_fun *write_cb, void *write_cb_arg, malloc_stats_t *stats)
{
	print_amount_xml(write_cb, write_cb_arg, "total_allocated_memory", stats->total_allocated_memory);
	print_amount_xml(write_cb, write_cb_arg, "total_mmapped_memory", stats->total_mmapped_memory);
	print_amount_xml(write_cb, write_cb_arg, "mmapped_regions", stats->mmapped_regions);
}

static void print_thread_stats_xml(
	write_cb_fun *write_cb,
	void *write_cb_arg,
	int tid,
	malloc_stats_t *stats
)
{
	stat_printf(write_cb, write_cb_arg, "<thread id=\"%d\">\n", tid);
	print_thread_specific_amounts_xml(write_cb, write_cb_arg, stats);
	stat_printf(write_cb, write_cb_arg, "</thread>\n");
}

static malloc_stats_t add_up_chunks(occupied_bin_t *occupied_bin)
{
	malloc_stats_t stats = {0, 0, 0, 0};
	for (struct chunk *c = occupied_bin->head; c != NULL; c = c->next_occupied) {
		size_t chunk_memory = CHUNK_SIZE(c) - OVERHEAD;
		stats.total_allocated_memory += chunk_memory;
		if (IS_MMAPPED(c)) {
			stats.mmapped_regions++;
			stats.total_mmapped_memory += chunk_memory;
		} else {
			stats.total_allocated_heap_space += chunk_memory;
		}
	}
	return stats;
}

static malloc_stats_t add_up_chunks_by_threads(occupied_bin_t *occupied_bin, int tid)
{
	malloc_stats_t stats = {0, 0, 0, 0};
	for (struct chunk *c = occupied_bin->head; c != NULL; c = c->next_occupied) {
		if (c->thread_id == tid) {
			size_t chunk_memory = CHUNK_SIZE(c) - OVERHEAD;
			stats.total_allocated_memory += chunk_memory;
			if (IS_MMAPPED(c)) {
				stats.mmapped_regions++;
				stats.total_mmapped_memory += chunk_memory;
			} else {
				stats.total_allocated_heap_space += chunk_memory;
			}
		}
	}
	return stats;
}

static size_t print_threads(write_cb_fun *write_cb, void *write_cb_arg, print_mode mode)
{
	size_t total_allocated_heap_space = 0;

	for (size_t i = 0; i < OCCUPIED_BIN_COUNT; ++i) {
		occupied_bin_t *occupied_bin = __get_occupied_bin_by_idx(i);
		int min_id = 0;
		int found;
		do {
			found = 0;
			for (struct chunk *c = occupied_bin->head; c != NULL; c = c->next_occupied) {
				if (c->thread_id > min_id) {
					min_id = c->thread_id;
					found = 1;
				}
			}
			if (found) {
				malloc_stats_t stats = add_up_chunks_by_threads(occupied_bin, min_id);
				total_allocated_heap_space += stats.total_allocated_heap_space;

				if (mode == TABLE) {
					print_thread_stats_table(write_cb, write_cb_arg, min_id, &stats);
				} else {
					print_thread_stats_xml(write_cb, write_cb_arg, min_id, &stats);
				}
			}
		} while (found);
	}

	return total_allocated_heap_space;
}

static void print_total_free_heap_space(
	write_cb_fun *write_cb,
	void *write_cb_arg,
	size_t total_allocated_heap_space,
	print_mode mode
)
{
	if (mode == TABLE) {
		stat_printf(write_cb, write_cb_arg, "\n");
		for (size_t i = 0; i < SEPARATOR_REPEATS; i++) {
			stat_printf(
				write_cb,
				write_cb_arg,
				"-----------"
			);
		}
		stat_printf(
			write_cb,
			write_cb_arg,
			"\ntotal free heap space: %zu\n",
			__get_total_heap_space() - total_allocated_heap_space
		);
	} else {
		print_amount_xml(
			write_cb,
			write_cb_arg,
			"total_free_heap_space",
			__get_total_heap_space() - total_allocated_heap_space
		);
	}
}

static void print_to_file(void *fp, const char *s)
{
	if (fputs(s, fp) == EOF) {
		fprintf(stderr, "Error writing to file stream: %s", strerror(errno));
	}
}

static void add_stats(malloc_stats_t *destination, const malloc_stats_t *source)
{
	destination->total_allocated_memory += source->total_allocated_memory;
	destination->total_mmapped_memory += source->total_mmapped_memory;
	destination->mmapped_regions += source->mmapped_regions;
	destination->total_allocated_heap_space += source->total_allocated_heap_space;
}
#endif

int malloc_info(int options, FILE* fp)
{
#ifdef MUSL_ITERATE_AND_STATS_API
	if (options != 0) {
		errno = EINVAL;
		return -1;
	}
	malloc_disable();
	stat_printf(print_to_file, fp, "<?xml version=\"1.0\"?>\n");
	stat_printf(print_to_file, fp, "<malloc version=\"%d\">\n", ALLOCATOR_VERSION);
	stat_printf(print_to_file, fp, "<threads>\n");
	size_t total_allocated_heap_space = print_threads(print_to_file, fp, XML);
	stat_printf(print_to_file, fp, "</threads>\n");
	print_total_free_heap_space(print_to_file, fp, total_allocated_heap_space, XML);
	stat_printf(print_to_file, fp, "</malloc>\n");
	malloc_enable();
#endif
	return 0;
}

void malloc_stats_print(void (*write_cb) (void *, const char *), void *cbopaque, const char *opts)
{
#ifdef MUSL_ITERATE_AND_STATS_API
	malloc_disable();
	stat_printf(
		write_cb,
		cbopaque,
		"%-11s %-23s %-20s %-20s\n",
		"thread_id",
		"total_allocated_memory",
		"total_mmapped_memory",
		"mmapped_regions"
	);
	size_t total_allocated_heap_space = print_threads(write_cb, cbopaque, TABLE);
	print_total_free_heap_space(write_cb, cbopaque, total_allocated_heap_space, TABLE);
	malloc_enable();
#endif
}

struct mallinfo2 mallinfo2(void)
{
#ifdef MUSL_ITERATE_AND_STATS_API
	malloc_disable();
	malloc_stats_t shared_stats = {0, 0, 0, 0};
	for (size_t i = 0; i < OCCUPIED_BIN_COUNT; ++i) {
		malloc_stats_t stats = add_up_chunks(__get_occupied_bin_by_idx(i));
		add_stats(&shared_stats, &stats);
	}

	struct mallinfo2 res = {
		.hblks = shared_stats.mmapped_regions,
		.hblkhd = shared_stats.total_mmapped_memory,
		.uordblks = shared_stats.total_allocated_memory,
		.fordblks = __get_total_heap_space() - shared_stats.total_allocated_heap_space
	};
	malloc_enable();
	return res;
#endif
	return (struct mallinfo2){};
}

struct mallinfo mallinfo(void)
{
	struct mallinfo2 mallinfo2_res = mallinfo2();
	return (struct mallinfo) {
		.hblks = mallinfo2_res.hblks,
		.hblkhd = mallinfo2_res.hblkhd,
		.uordblks = mallinfo2_res.uordblks,
		.fordblks = mallinfo2_res.fordblks,
	};
}