public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Robert Obkircher <r.obkircher@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox-backup v2 08/13] task tracking: use parameter for initial count and refactor updates
Date: Thu,  2 Jul 2026 16:59:05 +0200	[thread overview]
Message-ID: <20260702145916.360488-9-r.obkircher@proxmox.com> (raw)
In-Reply-To: <20260702145916.360488-1-r.obkircher@proxmox.com>

Don't hard-code the initial count to 1 and bail out if it is negative.
Replace the ActiveOperationStats initializers with the update logic
because they would be reformatted to multiple lines after adding a
field and an operation.

Signed-off-by: Robert Obkircher <r.obkircher@proxmox.com>
---
 pbs-datastore/src/task_tracking.rs | 49 ++++++++++++++++++------------
 1 file changed, 29 insertions(+), 20 deletions(-)

diff --git a/pbs-datastore/src/task_tracking.rs b/pbs-datastore/src/task_tracking.rs
index 528ac8c18..a67b77221 100644
--- a/pbs-datastore/src/task_tracking.rs
+++ b/pbs-datastore/src/task_tracking.rs
@@ -1,4 +1,4 @@
-use anyhow::Error;
+use anyhow::{Error, bail};
 use libc::pid_t;
 use nix::unistd::Pid;
 use std::iter::Sum;
@@ -15,6 +15,16 @@ pub struct ActiveOperationStats {
     pub write: i64,
 }
 
+impl ActiveOperationStats {
+    fn add(&mut self, count: i64, operation: Operation) {
+        match operation {
+            Operation::Read => self.read += count,
+            Operation::Write => self.write += count,
+            Operation::Lookup => (), // no IO must happen there
+        }
+    }
+}
+
 impl Sum<Self> for ActiveOperationStats {
     fn sum<I>(iter: I) -> Self
     where
@@ -102,12 +112,7 @@ pub fn update_active_operations(
 
     let pid = std::process::id();
 
-    let mut updated_active_operations = match operation {
-        Operation::Read => ActiveOperationStats { read: 1, write: 0 },
-        Operation::Write => ActiveOperationStats { read: 0, write: 1 },
-        Operation::Lookup => ActiveOperationStats { read: 0, write: 0 },
-    };
-    let mut found_entry = false;
+    let mut updated = None;
     let mut updated_tasks: Vec<TaskOperations> = match file_read_optional_string(&path)? {
         Some(data) => serde_json::from_str::<Vec<TaskOperations>>(&data)?
             .iter_mut()
@@ -116,13 +121,8 @@ pub fn update_active_operations(
                     Some(stat) if pid == task.pid && stat.starttime != task.starttime => None,
                     Some(_) => {
                         if pid == task.pid {
-                            found_entry = true;
-                            match operation {
-                                Operation::Read => task.active_operations.read += count,
-                                Operation::Write => task.active_operations.write += count,
-                                Operation::Lookup => (), // no IO must happen there
-                            };
-                            updated_active_operations = task.active_operations;
+                            task.active_operations.add(count, operation);
+                            updated = Some(task.active_operations);
                         }
                         Some(task.clone())
                     }
@@ -133,18 +133,27 @@ pub fn update_active_operations(
         None => Vec::new(),
     };
 
-    if !found_entry {
+    let result = if let Some(updated) = updated {
+        updated
+    } else {
+        if count < 0 {
+            bail!("unexpected initial active operation count: {count} {operation:?}")
+        }
+        let mut new = ActiveOperationStats::default();
+        new.add(count, operation);
         updated_tasks.push(TaskOperations {
             pid,
             starttime: procfs::PidStat::read_from_pid(Pid::from_raw(pid as pid_t))?.starttime,
-            active_operations: updated_active_operations,
-        })
-    }
+            active_operations: new,
+        });
+        new
+    };
+
     replace_file(
         &path,
         serde_json::to_string(&updated_tasks)?.as_bytes(),
         options,
         false,
-    )
-    .map(|_| updated_active_operations)
+    )?;
+    Ok(result)
 }
-- 
2.47.3





  parent reply	other threads:[~2026-07-02 15:01 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-02 14:58 [PATCH proxmox{,-backup} v2 00/13] add GarbageCollection maintenance mode Robert Obkircher
2026-07-02 14:58 ` [PATCH proxmox v2 01/13] pbs-api-types: propagate maintenance mode parse errors Robert Obkircher
2026-07-02 14:58 ` [PATCH proxmox v2 02/13] pbs-api-types: use match statement for maintenance mode check Robert Obkircher
2026-07-02 14:59 ` [PATCH proxmox v2 03/13] pbs-api-types: deny non-lookup operations for unknown modes Robert Obkircher
2026-07-02 14:59 ` [PATCH proxmox v2 04/13] pbs-api-types: add WriteNonExpanding datastore operation Robert Obkircher
2026-07-02 14:59 ` [PATCH proxmox v2 05/13] pbs-api-types: add GarbageCollection maintenance mode Robert Obkircher
2026-07-02 14:59 ` [PATCH proxmox-backup v2 06/13] datastore: propagate maintenance mode parse errors Robert Obkircher
2026-07-02 14:59 ` [PATCH proxmox-backup v2 07/13] task tracking: only read starttime when needed Robert Obkircher
2026-07-02 14:59 ` Robert Obkircher [this message]
2026-07-02 14:59 ` [PATCH proxmox-backup v2 09/13] www: access active operation fields by name instead of index Robert Obkircher
2026-07-02 14:59 ` [PATCH proxmox-backup v2 10/13] task tracking: count WriteNonExpanding datastore operations Robert Obkircher
2026-07-02 14:59 ` [PATCH proxmox-backup v2 11/13] datastore: open datastores with WriteNonExpanding instead of Write Robert Obkircher
2026-07-02 14:59 ` [PATCH proxmox-backup v2 12/13] fix #5797: www: display new GarbageCollection maintenance mode Robert Obkircher
2026-07-02 14:59 ` [PATCH proxmox-backup v2 13/13] docs: document garbage-collection " Robert Obkircher

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=20260702145916.360488-9-r.obkircher@proxmox.com \
    --to=r.obkircher@proxmox.com \
    --cc=pbs-devel@lists.proxmox.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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal