Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3483161lqp; Tue, 26 Mar 2024 10:14:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW0C4Lfrgz4kqq+3hOptn+RYV6sP38PcHoffU+Qqtz7x8o3EHmDKO66SQoPc/4HOX8AETZ6Y7nydrg7MenuXvZzW9jV3gJPrJ2rB6whtg== X-Google-Smtp-Source: AGHT+IEYvWQ70zLhCsX8TSmzf11mCZN27NzPNjabgMc/xuOJd4grYLL6R0Cx3bd60V74zO5n0NXy X-Received: by 2002:a05:6214:e65:b0:696:8505:1945 with SMTP id jz5-20020a0562140e6500b0069685051945mr300355qvb.36.1711473252911; Tue, 26 Mar 2024 10:14:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711473252; cv=pass; d=google.com; s=arc-20160816; b=NRbbrISNPLmoTIGvHbZJP2s7O5q1PBCaeYVBtS3IDQDLL+ehUlkHNqE3qauI8nZ+Rx tHcGSriHE3LYwqB9X8oLMmkTT+e9SqHtaW7j+Kr5nnz/SIXx829PDikrjMII6i1BJqtH Kr7RSwf7OcNHO4EKrmqGl211tMC7u2H+0TtXAnzDjEZCuELFkpjbt9BJqvC7uLbLgfoB tLv1zgEyGtgbz5LirNwp0bBmi0/25X1dkqvP2RS23R8uNpInr4S8XLz5hU+B7Kz3w88+ YaCZn8RNS+Lzld/ORZEW3kmlok1JYm0IxMz6AZ+xQBvLC25DZkBm29wNvPJKpxXtCjrE sRHA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=2SMAljw4XINFd821G0mOj/hS2BbJyOq2IwtofbwqjYM=; fh=6w4G9LgDrc0wGmF4gz6L02UDi66UhJm+aeDXAs5EOUU=; b=0xzZQa2r2SFw+SfyC/h3kUbUU2wBxcSysFS68MY0XiiaUJ+STKK/ILyWTuF7nYgA2+ Z8RQeKZmjXQ57isha3/goxvORz5vN1dOFzYJ1YrIeCCr5GfdQZT5r8ZCVxYBUctjiLvT dSg86kkJeYMX9KxsVDHOctPMZ+SugXY6FsUTxV3mSMvIlfCXdOtJrg9Q3exUYiY0x6K2 dNYp2dxni0+JBnW/yTa95vcZPZW9VSjbkQ6A0NjeQTA2jtoIY4kHg9rnQi9s+Ct3mvXT FRc/vzm2rcYtANapy4tHtGVrPtYenVg79VcD7HjdxMpMCdULceR5rTPVwzCoZGrGez4A zxsw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b="AY/oPqhF"; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-119566-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119566-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id f13-20020a0562141d2d00b006916aa8ecc9si10509286qvd.148.2024.03.26.10.14.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 10:14:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-119566-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b="AY/oPqhF"; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-119566-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119566-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id DB4FC1C65F3C for ; Tue, 26 Mar 2024 17:14:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A04211CA9E; Tue, 26 Mar 2024 17:13:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="AY/oPqhF" Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BC8C1C69C for ; Tue, 26 Mar 2024 17:13:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711473231; cv=none; b=LlT3fSxKOuaIcbf8Jts7dhWLU8d7UXTIO5aSKtq4qKKh2cHkSgOChl1a2BoU6QUMxvYlt6njFZhuONL5aKybnPIFsRuiOHHBSgDkUbfmri1DKC4ZJxRJDOYdJUQHknsW71GP0rZdNTIrps89zyLMzeJIX/6sNHhbd/Ay1TIDlRE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711473231; c=relaxed/simple; bh=FrW4urb3kaGNnmCDOoJ/pJREbGJJEOcKrsesprEishU=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lLzCMJWu+l2U63EBcs32gnMWVG8YFufiKz75jlEdN+vEtqBHTBEATnBlYZ/l3YpCm2Z2xlBjgKOXGAVbOlJ/DJMvFnJawYWuSRsgrnhZ9KN+jxFnJxWxS2gSAT0VINeQL6aL7nPtfSJxqhjok2Oabuj+Wxo6FJSCJZENyRXxK40= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=AY/oPqhF; arc=none smtp.client-ip=185.70.43.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1711473227; x=1711732427; bh=2SMAljw4XINFd821G0mOj/hS2BbJyOq2IwtofbwqjYM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=AY/oPqhFT+U/f9Jb34/v0EuZ9Y6G7v5QpAIgG/qKhmEwTwMQUg7LVx9edtm/wvfAL BuqY0+sQf8DmK2UUU+uVyDSyq6pLg3e4KJuKvcBXjqUxc8aNbyRr+QiWOX08grkAhX JyNDw4gxpYj+SRlWsyCK7i3yWoQG8Z5KEVqCGEQzKASxoubipZoicMK4lWlv5acRub 6ynza26Kypc8FdmGPTTI+sSkCYSV+/5bUSaHEDV9wW2nbhgnvAR6z8qNcyENN+CtG7 9d4zQNfcpM15EZtUJgE2ww478Un+/QI3FVMe0jIcdqskzR9Wuzc2Z1zoP3cwxX05N1 AS2FY5L160fug== Date: Tue, 26 Mar 2024 17:13:38 +0000 To: Boqun Feng From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Alice Ryhl , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , John Stultz , Stephen Boyd , Valentin Obst , Heghedus Razvan , Asahi Lina Subject: Re: [PATCH 5/5] rust: time: Add Instant::elapsed() for monotonic clocks Message-ID: In-Reply-To: <20240324223339.971934-6-boqun.feng@gmail.com> References: <20240324223339.971934-1-boqun.feng@gmail.com> <20240324223339.971934-6-boqun.feng@gmail.com> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 24.03.24 23:33, Boqun Feng wrote: > This is a convenient way to do: >=20 > =09t1 =3D Clock::now(); > =09... > =09delta =3D Clock::now() - t1; >=20 > Hence add it. >=20 > Co-developed-by: Heghedus Razvan > Signed-off-by: Heghedus Razvan > Co-developed-by: Asahi Lina > Signed-off-by: Asahi Lina > Signed-off-by: Boqun Feng > --- > rust/kernel/time.rs | 25 +++++++++++++++++++++++++ > 1 file changed, 25 insertions(+) >=20 > diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs > index 5cd669cbea01..cd1e45169517 100644 > --- a/rust/kernel/time.rs > +++ b/rust/kernel/time.rs > @@ -114,6 +114,31 @@ fn sub(self, other: Self) -> Self::Output { > } > } >=20 > +impl Instant { > + /// Returns the time elapsed since this [`Instant`]. > + /// > + /// This provides a convenient way to calculate time elapsed since a= previous [`Clock::now`]. > + /// Note even though the function only exists for monotonic clocks, = it could still return > + /// negative [`Duration`] if the current time is earlier than the ti= me of `&self`, and this > + /// could happen if `&self` is a timestamp generated by a [`Instant`= ] + [`Duration`]. But there currently is no way to add an `Instant` to a `Duration`. > + /// > + /// But for typical usages, it should always return non-negative [`D= uration`]: > + /// > + /// # Examples > + /// > + /// ``` > + /// use kernel::time::{Clock, clock::KernelTime}; > + /// > + /// let ts =3D KernelTime::now(); > + /// > + /// // `KernelTime` is monotonic. > + /// assert!(ts.elapsed().to_ns() >=3D 0); Now that I thought a bit more about the design, I think allowing negative durations is a bad idea. Do you disagree? If there is a case where you have a non-monotonic clock, or you are not sure if two timestamps are in the correct relation, we could have a function that returns a `Option` or `Result`. --=20 Cheers, Benno > + /// ``` > + pub fn elapsed(&self) -> Duration { > + T::now() - *self > + } > +} > + > /// Contains the various clock source types available to the kernel. > pub mod clock { > use super::*; > -- > 2.44.0 >