Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1maCFP-00079G-HF for mharc-grub-devel@gnu.org; Tue, 12 Oct 2021 03:30:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42576) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maCFM-00076K-Vt for grub-devel@gnu.org; Tue, 12 Oct 2021 03:30:49 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]:46957) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1maCFK-0006XC-U6 for grub-devel@gnu.org; Tue, 12 Oct 2021 03:30:48 -0400 Received: by mail-pj1-x1032.google.com with SMTP id pi19-20020a17090b1e5300b0019fdd3557d3so1860168pjb.5 for ; Tue, 12 Oct 2021 00:30:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+n6fHRYNo4P5frkjRPanfgfIpkDi0Bu7IFk2PvZyG6Y=; b=fXTJbpz8A3pkE1fV6toFj/wCs826YTCe8oxMj5l1VXZ/4DCHuL0fezllMKKeQGkAgg UJUHkOjJI2UM4otruh8CyghLxDZABZAkE8FQm9ALLj7mr0DmCVnjGT4dXLw1Mo6uOkaZ Ev/Hjh317QcHuWYcOaPRsotspSs8Apt/0muxc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+n6fHRYNo4P5frkjRPanfgfIpkDi0Bu7IFk2PvZyG6Y=; b=GF+Kwx+sfFALZIvtZQ3CV6BNp38Z/FWxeM5EGQ84684B8im42STAk9Ym2EWl1iC0ZS LXQ36nW7LEwyL5ZuHouTpAHOZP8HmJrDW1H/1Ms449E+TaEqhDIY2Q3jXeroIRfQhl1r wvIif3s/7cbxlbo+1RbXR8O8D2+q85XPxWo1U6SWwDlWWk7D2txCzMiT4y0E95zc8/Qz ZMP+aWuyt0exHDnXKAF1Mxw20au0h1KA6u2X4lokZV6F5W/08h9+lc3huoTqUsP6GRkN NvyLGbD7AiMpe/yw7XSg3NWUelCDMxGYHvhVBqC/JsyMvxGwMiLkObOUPrAGgfdE/oYV Xe0w== X-Gm-Message-State: AOAM531eQ5c3MQO+0lRp7KW0JR2II/eSfeFnY+lfXkOLjW5Y6WeE7ino oS+ORu57CWR8dGxHEwPqjLi4naeaeUi2AA== X-Google-Smtp-Source: ABdhPJwgrD6UsJEnNIpfysOBdRdKlqehqA/vdqHmyRVsU1npnWh+ztKu6HZQSL85R+RdAW5Lj0vK1w== X-Received: by 2002:a17:902:e0d5:b0:13f:25a0:d26b with SMTP id e21-20020a170902e0d500b0013f25a0d26bmr16226931pla.53.1634023845348; Tue, 12 Oct 2021 00:30:45 -0700 (PDT) Received: from localhost ([2001:4479:e300:600:41fe:d15:934f:4029]) by smtp.gmail.com with ESMTPSA id x20sm1646269pjp.48.2021.10.12.00.30.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Oct 2021 00:30:45 -0700 (PDT) From: Daniel Axtens To: grub-devel@gnu.org Cc: leif@nuviainc.com, stefanb@linux.ibm.com, ps@pks.im, dkiper@net-space.pl, Daniel Axtens Subject: [PATCH 07/19] Add memtool module with memory allocation stress-test Date: Tue, 12 Oct 2021 18:29:56 +1100 Message-Id: <20211012073008.4059133-8-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211012073008.4059133-1-dja@axtens.net> References: <20211012073008.4059133-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=dja@axtens.net; helo=mail-pj1-x1032.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Oct 2021 07:30:49 -0000 When working on memory, it's nice to be able to test your work. Add a memtest module. When compiled with --enable-mm-debug, it exposes 3 commands: * lsmem - print all allocations and free space in all regions * lsfreemem - print free space in all regions * stress_big_allocs - stress test large allocations: - how much memory can we allocate in one chunk? - how many 1MB chunks can we allocate? - check that gap-filling works with a 1MB aligned 900kB alloc + a 100kB alloc. Signed-off-by: Daniel Axtens --- I've put this as copyright IBM for now - hopefully we can conclude on whether we're still doing FSF copyright assignments? --- grub-core/Makefile.core.def | 5 ++ grub-core/commands/memtools.c | 157 ++++++++++++++++++++++++++++++++++ grub-core/kern/mm.c | 4 + include/grub/mm.h | 4 +- 4 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 grub-core/commands/memtools.c diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 8022e1c0a794..0cc3a4a500ec 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -2527,3 +2527,8 @@ module = { common = commands/i386/wrmsr.c; enable = x86; }; + +module = { + name = memtools; + common = commands/memtools.c; +}; diff --git a/grub-core/commands/memtools.c b/grub-core/commands/memtools.c new file mode 100644 index 000000000000..6d5778f4a1b0 --- /dev/null +++ b/grub-core/commands/memtools.c @@ -0,0 +1,157 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2021 IBM Corporation + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +#ifdef MM_DEBUG + +static grub_err_t +grub_cmd_lsmem (grub_command_t cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) + +{ +#ifndef GRUB_MACHINE_EMU + grub_mm_dump(0); +#endif + + return 0; +} + +static grub_err_t +grub_cmd_lsfreemem (grub_command_t cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) + +{ +#ifndef GRUB_MACHINE_EMU + grub_mm_dump_free(); +#endif + + return 0; +} + + +#define BIG_ALLOC (64 * 1024 * 1024) +#define SMALL_ALLOC 32 + +static grub_err_t +grub_cmd_stress_big_allocs (grub_command_t cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) +{ + int i, max_mb, blocks_alloced; + void *mem; + void **blocklist; + + grub_printf ("Test 1: increasingly sized allocs to 1GB block\n"); + for (i = 1; i < 1024; i++) { + grub_printf ("%d MB . ", i); + mem = grub_malloc (i * 1024 * 1024); + if (mem == NULL) + { + grub_printf ("failed\n"); + break; + } + else + grub_free (mem); + + if (i % 10 == 0) + grub_printf ("\n"); + } + + max_mb = i - 1; + grub_printf ("Max sized allocation we did was %d MB\n", max_mb); + + grub_printf ("Test 2: 1MB at a time, max 4GB\n"); + blocklist = grub_calloc (4096, sizeof (void *)); + for (i = 0; i < 4096; i++) + { + blocklist[i] = grub_malloc (1024 * 1024); + if (!blocklist[i]) + { + grub_printf ("Ran out of memory at iteration %d\n", i); + break; + } + } + blocks_alloced = i; + for (i = 0; i < blocks_alloced; i++) + grub_free (blocklist[i]); + + grub_printf ("\nTest 3: 1MB aligned 900kB + 100kB\n"); + //grub_mm_debug=1; + for (i = 0; i < 4096; i += 2) + { + blocklist[i] = grub_memalign (1024 * 1024, 900 * 1024); + if (!blocklist[i]) + { + grub_printf ("Failed big allocation, iteration %d\n", i); + blocks_alloced = i; + break; + } + + blocklist[i + 1] = grub_malloc (100 * 1024); + if (!blocklist[i + 1]) + { + grub_printf ("Failed small allocation, iteration %d\n", i); + blocks_alloced = i + 1; + break; + } + grub_printf ("."); + } + for (i = 0; i < blocks_alloced; i++) + grub_free (blocklist[i]); + + grub_free (blocklist); + + grub_errno = GRUB_ERR_NONE; + return GRUB_ERR_NONE; +} + +static grub_command_t cmd_lsmem, cmd_lsfreemem, cmd_sba; + +#endif /* MM_DEBUG */ + +GRUB_MOD_INIT(memtools) +{ +#ifdef MM_DEBUG + cmd_lsmem = grub_register_command ("lsmem", grub_cmd_lsmem, + 0, N_("List free and allocated memory blocks.")); + cmd_lsfreemem = grub_register_command ("lsfreemem", grub_cmd_lsfreemem, + 0, N_("List free memory blocks.")); + cmd_sba = grub_register_command ("stress_big_allocs", grub_cmd_stress_big_allocs, + 0, N_("Stress test large allocations.")); +#endif +} + +GRUB_MOD_FINI(memtools) +{ +#ifdef MM_DEBUG + grub_unregister_command (cmd_lsmem); + grub_unregister_command (cmd_lsfreemem); + grub_unregister_command (cmd_sba); +#endif +} diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index 835ed8a8f6f9..032c8f71aed2 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -556,6 +556,8 @@ grub_mm_dump_free (void) { grub_mm_header_t p; + grub_printf ("Region %p (size %" PRIuGRUB_SIZE ")\n\n", r, r->size); + /* Follow the free list. */ p = r->first; do @@ -583,6 +585,8 @@ grub_mm_dump (unsigned lineno) { grub_mm_header_t p; + grub_printf ("Region %p (size %" PRIuGRUB_SIZE ")\n\n", r, r->size); + for (p = (grub_mm_header_t) ALIGN_UP ((grub_addr_t) (r + 1), GRUB_MM_ALIGN); (grub_addr_t) p < (grub_addr_t) (r+1) + r->size; diff --git a/include/grub/mm.h b/include/grub/mm.h index 9c38dd3ca5d2..44fde7cb9033 100644 --- a/include/grub/mm.h +++ b/include/grub/mm.h @@ -46,8 +46,8 @@ void grub_mm_check_real (const char *file, int line); /* Set this variable to 1 when you want to trace all memory function calls. */ extern int EXPORT_VAR(grub_mm_debug); -void grub_mm_dump_free (void); -void grub_mm_dump (unsigned lineno); +void EXPORT_FUNC(grub_mm_dump_free) (void); +void EXPORT_FUNC(grub_mm_dump) (unsigned lineno); #define grub_calloc(nmemb, size) \ grub_debug_calloc (GRUB_FILE, __LINE__, nmemb, size) -- 2.30.2