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 06E6E6139E for ; Fri, 4 Feb 2022 12:19:31 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id F1EF330E9D for ; Fri, 4 Feb 2022 12:19:00 +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 id 11A3930E78 for ; Fri, 4 Feb 2022 12:18:59 +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 C80384567C for ; Fri, 4 Feb 2022 12:18:52 +0100 (CET) From: Hannes Laimer To: pbs-devel@lists.proxmox.com Date: Fri, 4 Feb 2022 11:17:28 +0000 Message-Id: <20220204111729.22107-6-h.laimer@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220204111729.22107-1-h.laimer@proxmox.com> References: <20220204111729.22107-1-h.laimer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.045 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% 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: [pbs-devel] [PATCH proxmox-backup v7 5/6] api: add get_active_operations endpoint X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Feb 2022 11:19:31 -0000 Signed-off-by: Hannes Laimer --- pbs-datastore/src/task_tracking.rs | 61 ++++++++++++++++++++++-------- src/api2/admin/datastore.rs | 31 ++++++++++++++- 2 files changed, 75 insertions(+), 17 deletions(-) diff --git a/pbs-datastore/src/task_tracking.rs b/pbs-datastore/src/task_tracking.rs index a02d9a17..c0a419e4 100644 --- a/pbs-datastore/src/task_tracking.rs +++ b/pbs-datastore/src/task_tracking.rs @@ -1,6 +1,7 @@ use anyhow::Error; use libc::pid_t; use nix::unistd::Pid; +use std::iter::Sum; use std::path::PathBuf; use pbs_api_types::Operation; @@ -8,12 +9,46 @@ use proxmox_sys::fs::{file_read_optional_string, open_file_locked, replace_file, use proxmox_sys::linux::procfs; use serde::{Deserialize, Serialize}; +#[derive(Deserialize, Serialize, Clone, Copy, Default)] +pub struct ActiveOperationStats { + pub read: i64, + pub write: i64, +} + +impl Sum for ActiveOperationStats { + fn sum(iter: I) -> Self + where + I: Iterator, + { + iter.fold(Self::default(), |a, b| Self { + read: a.read + b.read, + write: a.write + b.write, + }) + } +} + #[derive(Deserialize, Serialize, Clone)] struct TaskOperations { pid: u32, starttime: u64, - reading_operations: i64, - writing_operations: i64, + active_operations: ActiveOperationStats, +} + +pub fn get_active_operations(name: &str) -> Result { + let path = PathBuf::from(format!("{}/{}", crate::ACTIVE_OPERATIONS_DIR, name)); + + Ok(match file_read_optional_string(&path)? { + Some(data) => serde_json::from_str::>(&data)? + .iter() + .filter_map( + |task| match procfs::check_process_running(task.pid as pid_t) { + Some(stat) if task.starttime == stat.starttime => Some(task.active_operations), + _ => None, + }, + ) + .sum(), + None => ActiveOperationStats::default(), + }) } pub fn update_active_operations(name: &str, operation: Operation, count: i64) -> Result<(), Error> { @@ -43,8 +78,8 @@ pub fn update_active_operations(name: &str, operation: Operation, count: i64) -> if pid == task.pid { updated = true; match operation { - Operation::Read => task.reading_operations += count, - Operation::Write => task.writing_operations += count, + Operation::Read => task.active_operations.read += count, + Operation::Write => task.active_operations.write += count, }; } Some(task.clone()) @@ -57,18 +92,12 @@ pub fn update_active_operations(name: &str, operation: Operation, count: i64) -> }; if !updated { - updated_tasks.push(match operation { - Operation::Read => TaskOperations { - pid, - starttime, - reading_operations: 1, - writing_operations: 0, - }, - Operation::Write => TaskOperations { - pid, - starttime, - reading_operations: 0, - writing_operations: 1, + updated_tasks.push(TaskOperations { + pid, + starttime, + active_operations: match operation { + Operation::Read => ActiveOperationStats { read: 1, write: 0 }, + Operation::Write => ActiveOperationStats { read: 0, write: 1 }, }, }) } diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index ce710938..aa7c818d 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -43,7 +43,7 @@ use pbs_api_types::{ Authid, BackupContent, Counts, CryptMode, use pbs_client::pxar::create_zip; use pbs_datastore::{ check_backup_owner, DataStore, BackupDir, BackupGroup, StoreProgress, LocalChunkReader, - CATALOG_NAME, + CATALOG_NAME, task_tracking }; use pbs_datastore::backup_info::BackupInfo; use pbs_datastore::cached_chunk_reader::CachedChunkReader; @@ -1590,6 +1590,30 @@ pub fn get_rrd_stats( ) } +#[api( + input: { + properties: { + store: { + schema: DATASTORE_SCHEMA, + }, + }, + }, + access: { + permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT, true), + }, +)] +/// Read datastore stats +pub fn get_active_operations( + store: String, + _param: Value, +) -> Result { + let active_operations = task_tracking::get_active_operations(&store)?; + Ok(json!({ + "read": active_operations.read, + "write": active_operations.write, + })) +} + #[api( input: { properties: { @@ -1947,6 +1971,11 @@ pub fn set_backup_owner( #[sortable] const DATASTORE_INFO_SUBDIRS: SubdirMap = &[ + ( + "active-operations", + &Router::new() + .get(&API_METHOD_GET_ACTIVE_OPERATIONS) + ), ( "catalog", &Router::new() -- 2.30.2