Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nYin6-0006FL-B0 for mharc-grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:48 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50430) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nYimz-0006C2-N4 for grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:44 -0400 Received: from [2607:f8b0:4864:20::529] (port=39731 helo=mail-pg1-x529.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nYimx-0005JD-Pa for grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:41 -0400 Received: by mail-pg1-x529.google.com with SMTP id q19so11486801pgm.6 for ; Sun, 27 Mar 2022 23:23:39 -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=LikPF7uDmD92NFddJfsgtHaBoeWJzdLOVzlQ2iKjQnc=; b=S5KkFt6xASymESz5voyr51UnK83a+XKVHpRKOTazEmDsy7T8f+IlcWe2pxSDzjhbgM Ubpvsig9E5lMBTdQcs2zXLgHX853L1rW29NNrZEtoHra4XEz2Ft0pYC3qL2gQviRRDkJ BOzP87HwH7tI9ymKOCrkMbg0OV73zXZUg/epo= 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=LikPF7uDmD92NFddJfsgtHaBoeWJzdLOVzlQ2iKjQnc=; b=I8Qsocm8lkll/X+Fumz+tQC49eiUq56E+HXV5qpzcSJC3Mw+fgZQ1y6BuvFlMUkhXO UxfJfARTuj1X1CZ0Y1dfHctSK0Sin5+M0lN6qOwR8qI0B1B8sdrHPBZaegWZkWUOq07d et18P5pcAJ1jty24/JKA1JMF+iu3BJ3/2uu0kZBnrl4JJSQccJbfjIH+EzkA0fzbjUdh 8KUpyxqDhGOoQCIMD+0mVCv+0QUm1kh9aluAHN+mJG0++th2/leWV7A/XyTWK/7Lc3D6 OZ4ysYVCC4qjTtkD4AIhGTLl5mVPNWf2h17CBR1mHIXFXTRSYyXoqv0dc4SuDhB4d2xb A4Kg== X-Gm-Message-State: AOAM530mSFbyhuJ4jeAceeCVtXaxd9U+agVTbrtsmpxwc1Klw0xAN1BX P48cq+JDmM7JyLLLNP1GEylxANpZzFsFpA== X-Google-Smtp-Source: ABdhPJzhOT+2t3/8TbTeVUAXRX2Qcx/aD6eWyckHI5H9JP0dCB034/bMDLhXqW/fVqLRQVZgisG55w== X-Received: by 2002:a05:6a00:2186:b0:4f7:5544:1cc9 with SMTP id h6-20020a056a00218600b004f755441cc9mr22067911pfi.62.1648448618224; Sun, 27 Mar 2022 23:23:38 -0700 (PDT) Received: from localhost ([2001:4479:e000:e400:743e:bc3e:ec72:bf20]) by smtp.gmail.com with ESMTPSA id h13-20020a056a00230d00b004f427ffd485sm16172784pfh.143.2022.03.27.23.23.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Mar 2022 23:23:37 -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 v2 14/15] [RFC] Add memtool module with memory allocation stress-test Date: Mon, 28 Mar 2022 17:22:39 +1100 Message-Id: <20220328062240.878781-15-dja@axtens.net> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220328062240.878781-1-dja@axtens.net> References: <20220328062240.878781-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::529 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::529; envelope-from=dja@axtens.net; helo=mail-pg1-x529.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Mar 2022 06:23:44 -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 haven't addressed most of the change requests yet. This will need lots of work to get to a mergable state, especially the ability to check MM_DEBUG in template files so that it can only be built if --enable-mm-debug is set, ('enable = mm_debug' or something) and that involves a lot of slow and painful build system hacking. Perhaps later on. But I have sorted out the copyright assignment so at least that is clear if anyone else wants to hack on it. --- grub-core/Makefile.core.def | 5 ++ grub-core/commands/memtools.c | 155 ++++++++++++++++++++++++++++++++++ grub-core/kern/mm.c | 4 + 3 files changed, 164 insertions(+) create mode 100644 grub-core/commands/memtools.c diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index cada67b667ff..ce7d79d38e5b 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -2530,3 +2530,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..bb4ad359e013 --- /dev/null +++ b/grub-core/commands/memtools.c @@ -0,0 +1,155 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2021 Free Software Foundation, Inc. + * 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; +} + + +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 c3bf4646f55e..1e93d0d6cdb4 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -657,6 +657,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 @@ -684,6 +686,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; -- 2.32.0