From nobody Wed Feb 11 03:41:59 2026 Received: from out-175.mta0.migadu.com (out-175.mta0.migadu.com [91.218.175.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8670312BF24 for ; Mon, 6 Jan 2025 11:22:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736162532; cv=none; b=IDzVKKhyZOxMK5LisRzmVSz/h9OqwpV4vYGu/8JyHKBBUb4KLbw3acZfuYy4/eBnI1bDgdxUwJllHhhI4jt4aXpytW5oVASI0AT7Awnwgewq70+pqcd6xCmF7q5Kh5YhGBXU/dwb5fyrSMWVkTM9Zi7MnFgZQUXdfdwDVukfixw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736162532; c=relaxed/simple; bh=tASTzFAo37QtwEdqoKbMe4Cfk48zzO1WBrRnBs5JlS0=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=GJAHcZ6eYXItiN43pW6ihKcKYw/Sa3EEKMHqpoguXM0ifhg4CQHOo0g5Qjto34ymUlX1KB/egWzdXy7J85rBj/LXmP/4K12H6rsFQN/6d7oYPHAUJJfBXSnUwQ+joPjoCSQft8J2496AWihOvRkTjE6zAmV2bQGyRecE+etpp68= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=qAG7W4jj; arc=none smtp.client-ip=91.218.175.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="qAG7W4jj" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1736162522; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=0Xw8ufFZe9gNpfTNOnMpE79QJyb64gfXitJNrGdbwSo=; b=qAG7W4jjoLSbP56vUwZ3H6toPhiNgezZcIHFdixKOpNVLEVWTqrkdhghSR+DR+jOhjjKfq nfCqr+gfPJz6P2Vlf3pPTvU12NZBAPvBkMpShNNMBA234Lpk/+GRZAAPBV9KG64J7/c6Ic eK4Rc0wJomvdLKD0no7Qll72BvrXMgM= From: Hao Ge To: akpm@linux-foundation.org, surenb@google.com, kent.overstreet@linux.dev Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, hao.ge@linux.dev, Hao Ge Subject: [PATCH] tools/mm: Introduce a tool to handle entries in allocinfo Date: Mon, 6 Jan 2025 19:21:03 +0800 Message-Id: <20250106112103.25401-1-hao.ge@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Hao Ge Some users always say that the information provided by /proc/allocinfo is too extensive or bulky. However, from allocinfo's own perspective, it is not at fault as it needs to provide comprehensive information. Users can then select the information that is useful to them based on their own needs. For commonly used scenarios, we can develop a tool to facilitate users in filtering and utilizing relevant information from allocinfo. Currently, there is only one filtering feature available, which is to filter out entries where the 'bytes' field is 0 (since it is often used to understand the current memory allocation status, as previously mentioned by David Wang, who noticed that 2/3 of the lines have an accumulative counter of 0, indicating no memory activities,it will fill up the entire screen,by using the "-s" parameter, a lot of unnecessary information for this scenario can be filtered out.) In subsequent phases, we will continue to add more features to this tool, with the goal of making it convenient for most people to use the memory allocation profiling tool. Signed-off-by: Hao Ge --- MAINTAINERS | 1 + tools/mm/Makefile | 4 +- tools/mm/allocinfo_tool.c | 150 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 tools/mm/allocinfo_tool.c diff --git a/MAINTAINERS b/MAINTAINERS index 910305c11e8a..cfc3f9f0c046 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -15000,6 +15000,7 @@ F: Documentation/mm/allocation-profiling.rst F: include/linux/alloc_tag.h F: include/linux/pgalloc_tag.h F: lib/alloc_tag.c +F: tools/mm/allocinfo_tool.c =20 MEMORY CONTROLLER DRIVERS M: Krzysztof Kozlowski diff --git a/tools/mm/Makefile b/tools/mm/Makefile index f5725b5c23aa..f669d534a82b 100644 --- a/tools/mm/Makefile +++ b/tools/mm/Makefile @@ -3,7 +3,7 @@ # include ../scripts/Makefile.include =20 -BUILD_TARGETS=3Dpage-types slabinfo page_owner_sort thp_swap_allocator_test +BUILD_TARGETS=3Dpage-types slabinfo page_owner_sort thp_swap_allocator_tes= t allocinfo_tool INSTALL_TARGETS =3D $(BUILD_TARGETS) thpmaps =20 LIB_DIR =3D ../lib/api @@ -23,7 +23,7 @@ $(LIBS): $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) =20 clean: - $(RM) page-types slabinfo page_owner_sort thp_swap_allocator_test + $(RM) page-types slabinfo page_owner_sort thp_swap_allocator_test allocin= fo_tool make -C $(LIB_DIR) clean =20 sbindir ?=3D /usr/sbin diff --git a/tools/mm/allocinfo_tool.c b/tools/mm/allocinfo_tool.c new file mode 100644 index 000000000000..817f46d07a50 --- /dev/null +++ b/tools/mm/allocinfo_tool.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * allocinfo_tool: Tool to parse allocinfo + * + * Authors: Hao Ge + * + * Compile with: + * gcc -o allocinfo_tool allocinfo_tool.c + */ + +#include +#include +#include +#include +#include + +#define ALLOCINFO_FILE "/proc/allocinfo" +#define BUF_SIZE 1024 +#define NAME_MAX_LENTH 64 + +struct alloc_tag_counters { + signed long long bytes; + unsigned long long calls; +}; + +struct codetag { + unsigned int lineno; + char modname[NAME_MAX_LENTH]; + char function[NAME_MAX_LENTH]; + char filename[NAME_MAX_LENTH]; +}; + +struct alloc_info { + struct alloc_tag_counters counters; + struct codetag tag; + int has_modname; +}; + +static int arg_opt; + +enum OPT_BIT { + SKIP_ZERO =3D 1 << 0, +}; + +void usage(void) +{ + printf("Usage: ./allocinfo_tool [OPTIONS]\n" + "-s\t\t\tskip bytes for 0 allocinfo entries\n" + ); +} + +int parse_alloc_info(char *line, struct alloc_info *info) +{ + if (sscanf(line, "%12lli %8llu %[^:]:%d [%[^]]] func:%s", + &info->counters.bytes, &info->counters.calls, + info->tag.filename, &info->tag.lineno, + info->tag.modname, info->tag.function) =3D=3D 6){ + info->has_modname =3D 1; + return 1; + }; + + if (sscanf(line, "%12llu %8llu %[^:]:%u func:%s", + &info->counters.bytes, &info->counters.calls, + info->tag.filename, &info->tag.lineno, + info->tag.function) =3D=3D 5){ + info->has_modname =3D 0; + return 1; + } + + return 0; +} + +int read_alloc_info(void) +{ + FILE *file =3D fopen(ALLOCINFO_FILE, "r"); + + if (!file) { + perror("Failed to open /proc/allocinfo"); + return EXIT_FAILURE; + } + + int line =3D 0, i =3D 0; + char *buffer =3D malloc(BUF_SIZE); + struct alloc_info *info; + + while (fgets(buffer, BUF_SIZE, file)) { + + /* + * allocinfo - version: 1.0 + * # + */ + if (line < 2) { + printf("%s", buffer); + line++; + continue; + } + + info =3D realloc(info, sizeof(struct alloc_info) * (i + 1)); + + if (parse_alloc_info(buffer, info + i) =3D=3D 0) { + printf("Mismatch with the format of /proc/allocinfo"); + return 0; + } + + if ((arg_opt & SKIP_ZERO) && (info[i].counters.bytes =3D=3D 0)) + continue; + + printf("%12lli %8llu ", info[i].counters.bytes, info[i].counters.calls); + + if (info[i].has_modname) + printf("%s:%u [%s] func:%s", + info[i].tag.filename, info[i].tag.lineno, + info[i].tag.modname, info[i].tag.function); + else + printf("%s:%u func:%s", + info[i].tag.filename, info[i].tag.lineno, + info[i].tag.function); + printf(" "); + printf("\n"); + i++; + } + + free(info); + free(buffer); + fclose(file); +} + +int main(int argc, char *argv[]) +{ + + int opt; + struct option longopts[] =3D { + { "s", 0, NULL, 1}, + { 0, 0, 0, 0}, + }; + + while ((opt =3D getopt_long(argc, argv, "s", longopts, NULL)) !=3D -1) { + switch (opt) { + case 's': + arg_opt =3D arg_opt | SKIP_ZERO; + break; + default: + usage(); + exit(1); + } + } + + read_alloc_info(); + +} --=20 2.25.1