Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2359014lqp; Sun, 24 Mar 2024 15:36:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWRpzirvEFA63KZC/UQiINP4aExFUuFhiea92Lmy7Z29omwRn/5tu1wpSKYmPwB6pKR9npl1ier68kPKhxbjOZBjNO6R5lOR8BJq/Df4g== X-Google-Smtp-Source: AGHT+IHUtxNClmSV1l4okP2WwnT0YmtF0eykw5kobuXTcxRJPQ/Y6QxlGi+EwQf0lIl6+woPX2ji X-Received: by 2002:a92:de0c:0:b0:366:27f8:bbd0 with SMTP id x12-20020a92de0c000000b0036627f8bbd0mr5772438ilm.20.1711319767997; Sun, 24 Mar 2024 15:36:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711319767; cv=pass; d=google.com; s=arc-20160816; b=YSbjhecNVEH77wPLQmEYsKbEPNB3+ZujClxI6hHIzP0WOvHDwfvTABpouXm+sS83px s8UKTJCZ8Jk7tWP/ySp+NTXOlIIAVcpEd1YKPFAzs0pNMTl3fsuNKzGg+Y+FJN3TFCND wIE1OxqEZowvO82iWggBhomH8TjynTKCq5FUscBjuiK7JsGnsLtRNfLowVGG9NIIuiOa SNNiM9amlF9b4qTYkSmGd5jzV+Y5pjXlG9ZekY+SC2V7HmxxOzikd44Yhgn3uUlh3QsM Z6IV2eGsNN4YDUTi1U0QWHA9LxNEFe6Lp1WBmObA7wOoNN6XMO3wNTbctaQx4qwEIgIo jnmQ== 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:references:in-reply-to:message-id :date:subject:cc:to:from:feedback-id:dkim-signature; bh=ZEa8g/haIngtd9EpZwnZ7Ys0qyF84K/G4/YzTdjByEo=; fh=g4XnT82zPtCWyPP5oqZh76q1Z5+ypWfhL5+vhanoYEA=; b=aCb0xXKDLw6mvkDXg+0M+wfPb+DwTKkWn3rZAbc7R9rhoM2sbwYv+IBDkyETOZ/yyh nl8bn6XMa0XEey5b9pObG3dLfXGJ6wPBDUL0r62yQ7jxTxbLo4zO2pn51aRoiUdQ5hiA FTDesp2N6zGmtzyuWT4YgZyr0/yXV19Kk6zrWvDYTJJHkn+cZt5k9HDkbhIYb9AXPoaG Q1wu4FkkjLSj0hwvWiAjYpMtbFtebMRBfBRdFEmhK0x24tXrnGLAyyuXBJnnPEPpczWV f9QSkCUaVxAy/ZHmKkqZAt05Dvbh/nI4XjUzSktEF9fLpvEMcN6m/RW4Ff8NH1TRji12 aMWw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KnMexYax; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-112886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112886-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 15-20020a63000f000000b005dc507e8d15si6382847pga.820.2024.03.24.15.36.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 15:36:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-112886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KnMexYax; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-112886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112886-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 1FA2EB21281 for ; Sun, 24 Mar 2024 22:35:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5E39246B9B; Sun, 24 Mar 2024 22:34:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KnMexYax" Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D666145027; Sun, 24 Mar 2024 22:34:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319666; cv=none; b=okneP21+0SEIkBYrJlKFIvuSyTVJ7y6rIfZOJCkbGO6DWv6fWgzUQjhS30suEDyXwEaDXhPhaw4qt/9NLYMsW7GHwjUye5CBbvZH6x+SOTMHcOQSGlNISE6nEI83pcmXpV1whIo8cwzLwXMvTltitpq81jFw/r1oofqCFA37kQ8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319666; c=relaxed/simple; bh=lTwVdmQCrScr7m5mI/A+ln2I0q5l4JBsQMRgmXUoJ3I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pyLRWzQmWC/Bk97TlZTnbmC9uJ2mn+eEHRs8g+rIMAWFVsKYX45Ap2LfGdeoDfTgyHdpKULXZHcK4xVhnZ+qEhThDMrkqqYMDuohMMoI4qVi/PUEiesmsiI55vz0+8AYsNMuaUu8/PTFld7oG8QOZHJzbW8i1C+uVPra23hH9U0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KnMexYax; arc=none smtp.client-ip=209.85.160.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qt1-f170.google.com with SMTP id d75a77b69052e-42a9c21f9ecso16186301cf.0; Sun, 24 Mar 2024 15:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711319664; x=1711924464; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=ZEa8g/haIngtd9EpZwnZ7Ys0qyF84K/G4/YzTdjByEo=; b=KnMexYaxRUU0FeoWJcoYitFZWyJ1dMQ88xehbhuXXND3KT6/krUGhm7R+I5ZrKKz5C c50Sa7EBBe7S8HalDnDE6bLEtc9mWJ24LzsDQl1vaYIqDyFAMQAP5hj8ZFijQoEqi0iB zzZXCxH/eFsPaoVYkuVkvCEjE98PIBiDc/Rg5oipcsRFa1FQDYzSBAQsQ3Gc0bSnBN7b xuj8yM9Gh8R/vrUwhB0Zh10FBuHHMJTPF0F4lKGNzKcP84nfPMzXlBT0LxXv8jwkfHAe PQmWS3XucQNjrmGNoFutJaTXCLeKE8kvINsinu5BTppGcWsVZakwrjDETQuQlAXltgPf /+VQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711319664; x=1711924464; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ZEa8g/haIngtd9EpZwnZ7Ys0qyF84K/G4/YzTdjByEo=; b=Ah1uAFrddukajt6zuAN7ECjRsmhOUE1KjwtK9x00Rxh6fhupnpiAI+U2kzIlRIcE1Z clbBx522wmRFi1NS8f1b2ukxMfp69dXT/mEMYkA0Ad7pEvN3hPXaRkSF0a29WTdXsggp UD2IZVu66RmZfOuvq0IxaDrUwjWu9WJYStgpavw7bFInGXDN5L+Zy2j741G181wACgIs 7qhCaEk8bE8k9ApX1xc8eZb7prfWk8GOuMsR9/4y3aX2GP9fcsiNEA6s9Sq/0yfVMHl7 8r70mRvE0z6YE2ORzCNG9X1TomWpRo6ZA31SUNw0Q0ex9ieI1plJGzLAtj6LsJ2aStxT K7Pg== X-Forwarded-Encrypted: i=1; AJvYcCVeUH+RVmNSOyMkSsj6Qv8tPpZHDVsdKJMPgaW3wCgMPOVYhVW/dX54VkzKofj/T/gcUdwshA5xAchW4XH3tyajdrpPe+aoLtuCkDEM X-Gm-Message-State: AOJu0YwLoJpKleGOazJu2PTAaobedEjv/EwVxnWmN8waS5GULl8ConEU 1ymrDQREbtemKePP+A3Jlz1TMnudcOCLGSC860SSbtQHpGhO1rwH X-Received: by 2002:a05:622a:4e85:b0:431:3df7:6fd4 with SMTP id dj5-20020a05622a4e8500b004313df76fd4mr6891381qtb.2.1711319663869; Sun, 24 Mar 2024 15:34:23 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id hg1-20020a05622a610100b00430b5dcac34sm1980354qtb.8.2024.03.24.15.34.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 15:34:23 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfauth.nyi.internal (Postfix) with ESMTP id 052F21200043; Sun, 24 Mar 2024 18:34:23 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Sun, 24 Mar 2024 18:34:23 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddtjedgudeihecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnheptdehvdeivdfhieffgeegleetfefgjeefgfevvdfhteeljeehhfdtvdfg tdehfeetnecuffhomhgrihhnpegvlhgrphhsvggurdhtohenucevlhhushhtvghrufhiii gvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhht hhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquh hnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 24 Mar 2024 18:34:22 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner Cc: Alice Ryhl , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , benno.lossin@proton.me, Andreas Hindborg , John Stultz , Stephen Boyd , Valentin Obst , Heghedus Razvan , Asahi Lina Subject: [PATCH 5/5] rust: time: Add Instant::elapsed() for monotonic clocks Date: Sun, 24 Mar 2024 15:33:39 -0700 Message-ID: <20240324223339.971934-6-boqun.feng@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240324223339.971934-1-boqun.feng@gmail.com> References: <20240324223339.971934-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This is a convenient way to do: t1 = Clock::now(); ... delta = Clock::now() - t1; Hence add it. 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(+) 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 { } } +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 time of `&self`, and this + /// could happen if `&self` is a timestamp generated by a [`Instant`] + [`Duration`]. + /// + /// But for typical usages, it should always return non-negative [`Duration`]: + /// + /// # Examples + /// + /// ``` + /// use kernel::time::{Clock, clock::KernelTime}; + /// + /// let ts = KernelTime::now(); + /// + /// // `KernelTime` is monotonic. + /// assert!(ts.elapsed().to_ns() >= 0); + /// ``` + 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