From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id A41D59E462 for ; Tue, 31 Oct 2023 13:11:16 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8C9D11B0B0 for ; Tue, 31 Oct 2023 13:11:16 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Tue, 31 Oct 2023 13:11:15 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id EDAF842DF3 for ; Tue, 31 Oct 2023 13:11:14 +0100 (CET) From: Christoph Heiss To: pve-devel@lists.proxmox.com Date: Tue, 31 Oct 2023 13:10:56 +0100 Message-ID: <20231031121108.1130299-5-c.heiss@proxmox.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231031121108.1130299-1-c.heiss@proxmox.com> References: <20231031121108.1130299-1-c.heiss@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.015 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [PATCH installer v3 4/8] fix #4829: install: add new ZFS `arc_max` setup option X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 Oct 2023 12:11:16 -0000 Signed-off-by: Christoph Heiss --- Changes v1 -> v2: * no changes Changes v2 -> v3: * better documented calculation and renamed some variables to reflect its unit (thanks Thomas!) * moved modprobe config setup into separate sub Proxmox/Install.pm | 14 ++++++++++++ Proxmox/Install/Config.pm | 1 + Proxmox/Install/RunEnv.pm | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/Proxmox/Install.pm b/Proxmox/Install.pm index 1117fc4..f371716 100644 --- a/Proxmox/Install.pm +++ b/Proxmox/Install.pm @@ -291,6 +291,19 @@ sub get_zfs_raid_setup { return ($devlist, $cmd); } +# If the maximum ARC size for ZFS was explicitly changed by the user, applies +# it to the new system by setting the `zfs_arc_max` module parameter in /etc/modprobe.d/zfs.conf +my sub zfs_setup_module_conf { + my ($targetdir) = @_; + + my $arc_max = Proxmox::Install::Config::get_zfs_opt('arc_max'); + my $arc_max_mib = Proxmox::Install::RunEnv::clamp_zfs_arc_max($arc_max) * 1024 * 1024; + + if ($arc_max > 0) { + file_write_all("$targetdir/etc/modprobe.d/zfs.conf", "options zfs zfs_arc_max=$arc_max\n") + } +} + sub get_btrfs_raid_setup { my $filesys = Proxmox::Install::Config::get_filesys(); @@ -1141,6 +1154,7 @@ _EOD file_write_all("$targetdir/etc/kernel/cmdline", "root=ZFS=$zfs_pool_name/ROOT/$zfs_root_volume_name boot=zfs\n"); + zfs_setup_module_conf($targetdir); } diversion_remove($targetdir, "/usr/sbin/update-grub"); diff --git a/Proxmox/Install/Config.pm b/Proxmox/Install/Config.pm index 024f62a..f496d61 100644 --- a/Proxmox/Install/Config.pm +++ b/Proxmox/Install/Config.pm @@ -72,6 +72,7 @@ my sub init_cfg { compress => 'on', checksum => 'on', copies => 1, + arc_max => Proxmox::Install::RunEnv::default_zfs_arc_max(), # in MiB }, # TODO: single disk selection config target_hd => undef, diff --git a/Proxmox/Install/RunEnv.pm b/Proxmox/Install/RunEnv.pm index 3e810b2..9116397 100644 --- a/Proxmox/Install/RunEnv.pm +++ b/Proxmox/Install/RunEnv.pm @@ -303,6 +303,51 @@ sub query_installation_environment : prototype() { return $output; } +# OpenZFS specifies 64 MiB as the absolute minimum: +# https://openzfs.github.io/openzfs-docs/Performance%20and%20Tuning/Module%20Parameters.html#zfs-arc-max +our $ZFS_ARC_MIN_SIZE_MIB = 64; # MiB + +# See https://bugzilla.proxmox.com/show_bug.cgi?id=4829 +our $ZFS_ARC_MAX_SIZE_MIB = 16 * 1024; # 16384 MiB = 16 GiB +our $ZFS_ARC_SYSMEM_PERCENTAGE = 0.1; # use 10% of available system memory by default + +# Calculates the default upper limit for the ZFS ARC size. +# Returns the default ZFS maximum ARC size in MiB. +sub default_zfs_arc_max { + # Use ZFS default on non-PVE + return 0 if Proxmox::Install::ISOEnv::get('product') ne 'pve'; + + my $default_mib = get('total_memory') * $ZFS_ARC_SYSMEM_PERCENTAGE; + my $rounded_mib = int(sprintf('%.0f', $default_mib)); + print "total_memory:" . get('total_memory') . " mib_rounded:$rounded_mib\n"; + + if ($rounded_mib > $ZFS_ARC_MAX_SIZE_MIB) { + return $ZFS_ARC_MAX_SIZE_MIB; + } elsif ($rounded_mib < $ZFS_ARC_MIN_SIZE_MIB) { + return $ZFS_ARC_MIN_SIZE_MIB; + } + + return $rounded_mib; +} + +# Clamps the provided ZFS arc_max value (in MiB) to the accepted bounds. The +# lower is specified by `$ZFS_ARC_MIN_SIZE_MIB`, the upper by the available system memory. +# Returns the clamped value in MiB. +sub clamp_zfs_arc_max { + my ($mib) = @_; + + return $mib if $mib == 0; + + my $total_mem_mib = get('total_memory'); + if ($mib > $total_mem_mib) { + return $total_mem_mib; + } elsif ($mib < $ZFS_ARC_MIN_SIZE_MIB) { + return $ZFS_ARC_MIN_SIZE_MIB; + } + + return $mib; +} + my $_env = undef; sub get { my ($k) = @_; -- 2.42.0