From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id AF68C1FF183
	for <inbox@lore.proxmox.com>; Wed, 18 Jun 2025 15:05:42 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 9A3B313021;
	Wed, 18 Jun 2025 15:03:00 +0200 (CEST)
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Wed, 18 Jun 2025 15:02:06 +0200
Message-Id: <20250618130209.90649-30-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250618130209.90649-1-f.ebner@proxmox.com>
References: <20250618130209.90649-1-f.ebner@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.031 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
Subject: [pve-devel] [PATCH qemu-server v2 29/32] drive: create a throttle
 group for each drive starting with machine version 10.0
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

The throttle group will be referenced via the 'blockdev' schema.

Co-developed-by: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Drop unused get_drive_id() call.
* Fix quoting ID string for drivedel, I had accidentally used single
  quotes there leading to variable not getting expanded.
* Comment that the version guard is for the switch to -blockdev.

 src/PVE/QemuServer.pm          | 52 ++++++++++++++++++++++++++++++++++
 src/PVE/QemuServer/Blockdev.pm | 44 ++++++++++++++++++++++++++++
 src/PVE/QemuServer/Makefile    |  1 +
 3 files changed, 97 insertions(+)
 create mode 100644 src/PVE/QemuServer/Blockdev.pm

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 4eccfb62..1b8d42a3 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -51,6 +51,7 @@ use PVE::Tools
 use PVE::QMPClient;
 use PVE::QemuConfig;
 use PVE::QemuConfig::NoWrite;
+use PVE::QemuServer::Blockdev;
 use PVE::QemuServer::Helpers qw(config_aware_timeout min_version kvm_user_version windows_version);
 use PVE::QemuServer::Cloudinit;
 use PVE::QemuServer::CGroup;
@@ -4100,6 +4101,11 @@ sub config_to_command {
                 push @$devices, '-blockdev', $live_restore->{blockdev};
             }
 
+            if (min_version($machine_version, 10, 0)) { # for the switch to -blockdev
+                my $throttle_group = PVE::QemuServer::Blockdev::generate_throttle_group($drive);
+                push @$cmd, '-object', to_json($throttle_group, { canonical => 1 });
+            }
+
             my $drive_cmd =
                 print_drive_commandline_full($storecfg, $vmid, $drive, $live_blockdev_name);
 
@@ -4510,6 +4516,14 @@ sub qemu_iothread_del {
 sub qemu_driveadd {
     my ($storecfg, $vmid, $device) = @_;
 
+    my $machine_type = PVE::QemuServer::Machine::get_current_qemu_machine($vmid);
+
+    # for the switch to -blockdev
+    if (PVE::QemuServer::Machine::is_machine_version_at_least($machine_type, 10, 0)) {
+        my $throttle_group = PVE::QemuServer::Blockdev::generate_throttle_group($device);
+        mon_cmd($vmid, 'object-add', %$throttle_group);
+    }
+
     my $drive = print_drive_commandline_full($storecfg, $vmid, $device, undef);
     $drive =~ s/\\/\\\\/g;
     my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_add auto \"$drive\"", 60);
@@ -4523,6 +4537,13 @@ sub qemu_driveadd {
 sub qemu_drivedel {
     my ($vmid, $deviceid) = @_;
 
+    my $machine_type = PVE::QemuServer::Machine::get_current_qemu_machine($vmid);
+
+    # for the switch to -blockdev
+    if (PVE::QemuServer::Machine::is_machine_version_at_least($machine_type, 10, 0)) {
+        mon_cmd($vmid, 'object-del', id => "throttle-drive-$deviceid");
+    }
+
     my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_del drive-$deviceid", 10 * 60);
     $ret =~ s/^\s+//;
 
@@ -4788,6 +4809,37 @@ sub qemu_block_set_io_throttle {
 
     return if !check_running($vmid);
 
+    my $machine_type = PVE::QemuServer::Machine::get_current_qemu_machine($vmid);
+    # for the switch to -blockdev
+    if (PVE::QemuServer::Machine::is_machine_version_at_least($machine_type, 10, 0)) {
+        mon_cmd(
+            $vmid,
+            'qom-set',
+            path => "throttle-$deviceid",
+            property => "limits",
+            value => {
+                'bps-total' => int($bps),
+                'bps-read' => int($bps_rd),
+                'bps-write' => int($bps_wr),
+                'iops-total' => int($iops),
+                'iops-read' => int($iops_rd),
+                'iops-write' => int($iops_wr),
+                'bps-total-max' => int($bps_max),
+                'bps-read-max' => int($bps_rd_max),
+                'bps-write-max' => int($bps_wr_max),
+                'iops-total-max' => int($iops_max),
+                'iops-read-max' => int($iops_rd_max),
+                'iops-write-max' => int($iops_wr_max),
+                'bps-total-max-length' => int($bps_max_length),
+                'bps-read-max-length' => int($bps_rd_max_length),
+                'bps-write-max-length' => int($bps_wr_max_length),
+                'iops-total-max-length' => int($iops_max_length),
+                'iops-read-max-length' => int($iops_rd_max_length),
+                'iops-write-max-length' => int($iops_wr_max_length),
+            },
+        );
+    }
+
     mon_cmd(
         $vmid, "block_set_io_throttle",
         device => $deviceid,
diff --git a/src/PVE/QemuServer/Blockdev.pm b/src/PVE/QemuServer/Blockdev.pm
new file mode 100644
index 00000000..4d1266b8
--- /dev/null
+++ b/src/PVE/QemuServer/Blockdev.pm
@@ -0,0 +1,44 @@
+package PVE::QemuServer::Blockdev;
+
+use strict;
+use warnings;
+
+use PVE::QemuServer::Drive;
+
+sub generate_throttle_group {
+    my ($drive) = @_;
+
+    my $drive_id = PVE::QemuServer::Drive::get_drive_id($drive);
+
+    my $limits = {};
+
+    for my $type (['', '-total'], [_rd => '-read'], [_wr => '-write']) {
+        my ($dir, $qmpname) = @$type;
+        if (my $v = $drive->{"mbps$dir"}) {
+            $limits->{"bps$qmpname"} = int($v * 1024 * 1024);
+        }
+        if (my $v = $drive->{"mbps${dir}_max"}) {
+            $limits->{"bps$qmpname-max"} = int($v * 1024 * 1024);
+        }
+        if (my $v = $drive->{"bps${dir}_max_length"}) {
+            $limits->{"bps$qmpname-max-length"} = int($v);
+        }
+        if (my $v = $drive->{"iops${dir}"}) {
+            $limits->{"iops$qmpname"} = int($v);
+        }
+        if (my $v = $drive->{"iops${dir}_max"}) {
+            $limits->{"iops$qmpname-max"} = int($v);
+        }
+        if (my $v = $drive->{"iops${dir}_max_length"}) {
+            $limits->{"iops$qmpname-max-length"} = int($v);
+        }
+    }
+
+    return {
+        id => "throttle-drive-$drive_id",
+        limits => $limits,
+        'qom-type' => 'throttle-group',
+    };
+}
+
+1;
diff --git a/src/PVE/QemuServer/Makefile b/src/PVE/QemuServer/Makefile
index 0d09b763..7d3830de 100644
--- a/src/PVE/QemuServer/Makefile
+++ b/src/PVE/QemuServer/Makefile
@@ -3,6 +3,7 @@ PREFIX=/usr
 PERLDIR=$(PREFIX)/share/perl5
 
 SOURCES=Agent.pm	\
+	Blockdev.pm	\
 	CGroup.pm	\
 	Cloudinit.pm	\
 	CPUConfig.pm	\
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel