From: Patrick Steinhardt <ps@pks.im>
To: Ramsay Jones <ramsay@ramsayjones.plus.com>
Cc: GIT Mailing-list <git@vger.kernel.org>,
Junio C Hamano <gitster@pobox.com>,
Adam Dinwoodie <git@dinwoodie.org>
Subject: Re: [PATCH v2 10/13] builtin/gc.c: correct RAM calculation when using sysinfo
Date: Mon, 14 Apr 2025 09:55:08 +0200 [thread overview]
Message-ID: <Z_y_XL2C_Za5SB5m@pks.im> (raw)
In-Reply-To: <a2eb9ab117c9a7ea8723c166739b30243388ea77.1743859985.git.ramsay@ramsayjones.plus.com>
On Sun, Apr 06, 2025 at 08:38:36PM +0100, Ramsay Jones wrote:
> The man page for sysinfo(2) on Linux states that (from v2.3.48) the
> sizes of the memory and swap fields, of the returned structure, are
> given as multiples of 'mem_unit' bytes. In earlier versions (prior to
> v2.3.23 on i386 in particular), the 'mem_unit' field was not part of
> the structure, and all sizes were measured in bytes. The man page does
> not discuss the motivation for this change, but it is possible that the
> change was intended for the, relatively rare, 32-bit platform with more
> than 4GB of memory.
>
> The total_ram() function makes the assumption that the 'totalram' field
> of the 'struct sysinfo' is measured in bytes, or alternatively that the
> 'mem_unit' field is always equal to one. Having writen a program to call
> the sysinfo() function and print the structure fields, it seems that, on
> Linux x84_64 and i686 anyway, the 'mem_unit' field is indeed set to one
> (note that the 32-bit system had only 2GB ram). However, cygwin also has
> an sysinfo() implementation, which gives the following values:
>
> $ ./sysinfo
> uptime: 21381
> loads: 0, 0, 0
> total ram: 2074637
> free ram: 843237
> shared ram: 0
> buffer ram: 0
> total swap: 327680
> free swap: 306932
> procs: 15
> total high: 0
> free high: 0
> mem_unit: 4096
>
> total ram: 8497713152
> $
>
> [This laptop has 8GB ram, so a little bit seems to be missing. ;) ]
Interesting. I can confirm that `mem_unit` is 1 on my system, so this
does not make a difference here. But my tests on Cygwin show the same
behaviour as on your system, so the patch looks reasonable to me.
> Modify the total_ram() function to allow for the possibility that the
> memory size is not specified in bytes (ie 'mem_unit' is greater than
> one).
>
> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
> ---
> builtin/gc.c | 9 +++++++--
> 1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/gc.c b/builtin/gc.c
> index 99431fd467..cdcf1dc6e7 100644
> --- a/builtin/gc.c
> +++ b/builtin/gc.c
> @@ -373,8 +373,13 @@ static uint64_t total_ram(void)
> #if defined(HAVE_SYSINFO)
> struct sysinfo si;
>
> - if (!sysinfo(&si))
> - return si.totalram;
> + if (!sysinfo(&si)) {
> + uint64_t total = si.totalram;
> +
> + if (si.mem_unit > 1)
> + total *= (uint64_t)si.mem_unit;
> + return total;
> + }
I expect that all systems have a proper value for `si.mem_unit` set so
that we could unconditionally multiplicate the fields with one another.
But it doesn't hurt either, so I don't mind the guarding clause.
Patrick
next prev parent reply other threads:[~2025-04-14 7:55 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-15 2:46 [PATCH 00/12] miscellaneous build mods (part 1) Ramsay Jones
2025-04-06 19:38 ` [PATCH v2 00/13] " Ramsay Jones
2025-04-06 19:38 ` [PATCH v2 01/13] meson.build: remove -DCURL_DISABLE_TYPECHECK Ramsay Jones
2025-04-06 19:38 ` [PATCH v2 02/13] Makefile: only set some BASIC_CFLAGS when RUNTIME_PREFIX is set Ramsay Jones
2025-04-14 7:54 ` Patrick Steinhardt
2025-04-06 19:38 ` [PATCH v2 03/13] meson.build: only set build variables for non-default values Ramsay Jones
2025-04-06 19:49 ` Ramsay Jones
2025-04-14 7:54 ` Patrick Steinhardt
2025-04-14 19:19 ` [-SPAM-] " Ramsay Jones
2025-04-15 5:59 ` Patrick Steinhardt
2025-04-06 19:38 ` [PATCH v2 04/13] meson.build: set default help format to html on windows Ramsay Jones
2025-04-06 20:16 ` Ramsay Jones
2025-04-14 7:54 ` Patrick Steinhardt
2025-04-06 19:38 ` [PATCH v2 05/13] Makefile: remove NEEDS_LIBRT build variable Ramsay Jones
2025-04-06 19:38 ` [PATCH v2 06/13] config.mak.uname: add a note about NO_STRLCPY for Linux Ramsay Jones
2025-04-06 19:38 ` [PATCH v2 07/13] config.mak.uname: only set NO_REGEX on cygwin for v1.7 Ramsay Jones
2025-04-14 7:55 ` Patrick Steinhardt
2025-04-14 20:03 ` [-SPAM-] " Ramsay Jones
2025-04-15 5:59 ` Patrick Steinhardt
2025-04-15 15:05 ` Junio C Hamano
2025-04-06 19:38 ` [PATCH v2 08/13] config.mak.uname: add HAVE_GETDELIM to the cygwin section Ramsay Jones
2025-04-06 19:38 ` [PATCH v2 09/13] config.mak.uname: add clock_gettime() to the cygwin build Ramsay Jones
2025-04-14 7:55 ` Patrick Steinhardt
2025-04-14 20:05 ` [-SPAM-] " Ramsay Jones
2025-04-06 19:38 ` [PATCH v2 10/13] builtin/gc.c: correct RAM calculation when using sysinfo Ramsay Jones
2025-04-14 7:55 ` Patrick Steinhardt [this message]
2025-04-14 20:11 ` [-SPAM-] " Ramsay Jones
2025-04-06 19:38 ` [PATCH v2 11/13] config.mak.uname: add sysinfo() configuration for cygwin Ramsay Jones
2025-04-14 7:55 ` Patrick Steinhardt
2025-04-06 19:38 ` [PATCH v2 12/13] config.mak.uname: add arc4random to the cygwin build Ramsay Jones
2025-04-06 19:38 ` [PATCH v2 13/13] config.mak.uname: set CSPRNG_METHOD to getrandom on Linux Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 00/13] miscellaneous build mods (part 1) Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 01/13] meson.build: remove -DCURL_DISABLE_TYPECHECK Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 02/13] Makefile: only set some BASIC_CFLAGS when RUNTIME_PREFIX is set Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 03/13] meson.build: only set build variables for non-default values Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 04/13] meson.build: set default help format to html on windows Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 05/13] Makefile: remove NEEDS_LIBRT build variable Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 06/13] config.mak.uname: add a note about NO_STRLCPY for Linux Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 07/13] config.mak.uname: only set NO_REGEX on cygwin for v1.7 Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 08/13] config.mak.uname: add HAVE_GETDELIM to the cygwin section Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 09/13] config.mak.uname: add clock_gettime() to the cygwin build Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 10/13] builtin/gc.c: correct RAM calculation when using sysinfo Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 11/13] config.mak.uname: add sysinfo() configuration for cygwin Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 12/13] config.mak.uname: add arc4random to the cygwin build Ramsay Jones
2025-04-16 23:18 ` [PATCH v3 13/13] config.mak.uname: set CSPRNG_METHOD to getrandom on Linux Ramsay Jones
2025-04-17 13:55 ` Junio C Hamano
2025-04-17 18:27 ` Ramsay Jones
2025-04-17 20:13 ` Junio C Hamano
2025-04-17 22:06 ` Ramsay Jones
2025-04-17 3:45 ` [PATCH v3 00/13] miscellaneous build mods (part 1) Junio C Hamano
2025-04-17 8:36 ` Patrick Steinhardt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z_y_XL2C_Za5SB5m@pks.im \
--to=ps@pks.im \
--cc=git@dinwoodie.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=ramsay@ramsayjones.plus.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).