/*-
 * Copyright (c) 2005 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/sysctl.h>

#include <err.h>
#include <errno.h>
#include <memstat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void
usage(void)
{

	fprintf(stderr, "memstat -amz [type list ...]\n");
	exit(-1);
}

static void
print_type(struct memory_type *mtp)
{
	int first, i;

	printf("%16s %8lld %7lldK %8lld  ",
	    memstat_get_name(mtp), memstat_get_count(mtp),
	    ((int64_t)memstat_get_bytes(mtp) + 1023) / 1024,
	    memstat_get_numallocs(mtp));

	switch (memstat_get_allocator(mtp)) {
	case ALLOCATOR_UMA:
		printf("%llu", memstat_get_size(mtp));
		break;
	case ALLOCATOR_MALLOC:
		first = 1;
		for (i = 0; i < 32; i++) {
			if (memstat_get_sizemask(mtp) & (2 << i)) {
				if (!first)
					printf(",");
				printf("%d", 2 << (i + 4));
				first = 0;
			}
		}
		break;
	}
	printf("\n");
}

int
main(int argc, char *argv[])
{
	int (*query_function)(struct memory_type_list *, int) = NULL;
	struct memory_type_list *mtlp;
	struct memory_type *mtp;
	int i;

	if (argc < 2)
		usage();

	if (strcmp(argv[1], "-a") == 0)
		query_function = memstat_sysctl_all;
	else if (strcmp(argv[1], "-z") == 0)
		query_function = memstat_sysctl_uma;
	else if (strcmp(argv[1], "-m") == 0)
		query_function = memstat_sysctl_malloc;
	else
		usage();

	mtlp = memstat_mtl_alloc();
	if (mtlp == NULL)
		err(-1, "memstat_mtl_alloc");

	if (query_function(mtlp, 0) < 0) {
		warnx("memstat_sysctl: %s",
		    memstat_strerror(memstat_mtl_geterror(mtlp)));
		memstat_mtl_free(mtlp);
		exit(-1);
	}

	printf("%16s %8s %8s %8s  Sizes\n", "Type", "InUse", "MemUse",
	    "Requests");
	if (argc > 2) {
		for (i = 2; i < argc; i++) {
			mtp = memstat_mtl_find(mtlp, ALLOCATOR_ANY, argv[i]);
			if (mtp == NULL) {
				memstat_mtl_free(mtlp);
				errx(-1, "memstat_mtl_find: %s: no match\n",
				    argv[i]);
			}
			print_type(mtp);
		}
		return (0);
	}

	for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
	    mtp = memstat_mtl_next(mtp)) {
		if (memstat_get_numallocs(mtp) == 0 &&
		    memstat_get_count(mtp) == 0)
			continue;
		print_type(mtp);
	}

	memstat_mtl_free(mtlp);

	return (0);
}
