Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3510451lqp; Tue, 26 Mar 2024 11:01:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUOuqEchIFtCPuEDOeaHFVUM8V/ciR8vu0GKL7rxpUqwZVTTRGy043A3FSP+gBNc0JI1n1QbBAnj91M2bSEPff3K0xV0rb9HBBQTBw2Sg== X-Google-Smtp-Source: AGHT+IGESqix82xScMT+7M+YDENQwq77tmjbb3rEOppDxytG04gaq2B0HAWNjCpF1cjJd3T+9EVZ X-Received: by 2002:a17:907:6d0f:b0:a48:7cbd:8b13 with SMTP id sa15-20020a1709076d0f00b00a487cbd8b13mr6923531ejc.52.1711476106422; Tue, 26 Mar 2024 11:01:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711476106; cv=pass; d=google.com; s=arc-20160816; b=ABj4/91jtyl+SB2G2SgkgQY1C5c34XrytM7/1jclUk7RdlJqeOoFELon2u1M9A5NJb JZbkyFMLxeZKLJkaytqJfRA6jjtYcA4LSHXtXojCj28FUFdHlrb1sCpepMgTMJy/8VTI Xif1VYyOHw5lr7H4o/d3xZ+Q0MC0qfFa5hsgdTLWH0n3OTvp4ti8MMm3faDFhIAyeMY4 5N5Otn2hA9GIhSenmFp/k1OOnw/gggR34E7/SEl7n2CneZDfhGnFIJexCUrmP3+zb33v btUrZgiIQDPosYhehzlnVg5MWnQjx7zpEiBxG7PuPm5ohFib1fKICVmSKIFX+LWre+CG qexA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=a6lW0gueo5dxaCOjmKynC7GoF7+rNyRSk1GMnrRg1Rc=; fh=h9Aj+v2p+MW8Gj9f+2vDxzIHXtXpmUzEbLOAJPP2U4g=; b=jLb9pHJjgDd10G2wpBZTOlL1e4tCdTaWE0+DMddsH2FbtwtmETJE412YqcRu8zwRzB jehj5WnZo7rd4z5kKoqldvRxdsa8+i71wopzTaRCNB7sclgzGKdRXho5MkrOh3s90hcg pkhkqrSU9IRbDUmpnqRCGI37BB5p8sbf0IRJ7g1kgyxV825TZdD6aMv329gY9mSCN+Mh 4SoB4Zs7BoXp/cn7P63wc2PXbuViSD6XoHy8dPdlGli7LpZuqGSpgDSa7H78U8YvXm0j 46F+j+ky6Xjr2dkSn2V2yXwDLyMadFYkomqcDoQlHLQ/V22RNstjypdUwFSRWEwWjR9B WFew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=PBKF62TL; 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-119641-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119641-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id lz14-20020a170906fb0e00b00a4751926e3esi2644171ejb.26.2024.03.26.11.01.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 11:01:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-119641-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=PBKF62TL; 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-119641-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119641-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 01B121F82A45 for ; Tue, 26 Mar 2024 18:01:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0B4BD8527A; Tue, 26 Mar 2024 18:01:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PBKF62TL" Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) (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 F2312208A7; Tue, 26 Mar 2024 18:01:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711476092; cv=none; b=p8UHFL2Tr5WYv2Ym78CDE11/A1eBn/h0S8o/TvH9RBknf3rnDTQUbGs8bk7GPBjawu5ZSFWiiDj8YW5iQbNpP3rrculsGXMeb2uA5WTZ/57Bl27XfrAqXe8aDRXe7pVk3GKBSXM6hTNFELNV74aCUw5a8MXuXLF5JxVEcsrNlZY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711476092; c=relaxed/simple; bh=a3mtfSC9jF8u/tEtEzqSEIjsTQ+XBc8DW/4oluPcibI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cTxAqWGCwwGC3gqJK01Z5WVJtoLzkQtys+kWvAWSZAM2Uc5RTxH/odhyPSyXeSaW47xYpE0pZj9vqP9gr9UFyGpV7nUudb1tSrDFnc6KGLRRWxZ506ZhIX1Wgif0BAY1d60NASOKOEezT7CZskrpsRXF/23qL5bO12FcN5ZCl7w= 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=PBKF62TL; arc=none smtp.client-ip=209.85.222.175 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-qk1-f175.google.com with SMTP id af79cd13be357-78a5f7269e3so5839485a.1; Tue, 26 Mar 2024 11:01:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711476088; x=1712080888; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=a6lW0gueo5dxaCOjmKynC7GoF7+rNyRSk1GMnrRg1Rc=; b=PBKF62TLMkkWPe6puq3TP2J2z9NseLOl1abHLfBu14F1QGBgKQANIPUW5AOpc34X9x DDx+pDj/IWob4D5qXNbf/TUnP8oDECCIjNcJ+9h8F3nq35x2FCRVtYphS5xi/9Bvx7xn sY3XV4zzI1pP+XPBxQmT9ASLi+ErSs+syOjqwLYrlAZqWQNZHVrOP8li8uWp22u/+VjS 06DXZihnBvRiEYJNEWwimUYIIew/rgtrMHIj2dtEVsaf+LXqujwn+N7UykFmeQi1YYcx jP4ngYN3Af5NaYO9ylUV+Enua0oNU/G46X3mPzyRvyiWO2XNuInyaHbFsRztpKDl123M tzKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711476088; x=1712080888; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a6lW0gueo5dxaCOjmKynC7GoF7+rNyRSk1GMnrRg1Rc=; b=BB4lQ2DPpC0Ww/klLF2r3AfmeHb9PSSbX7XnCdP0B2wTbAKoJaPnxKfwEkQlXjywM7 dl+W6NBflNK+8MgBpVs0tZipbZVqy6pnyn2S6gQpxCakuMBWjcLgwU6a6LkBeLT90CVk BLwnuvnuS4GTkl60nKNwNE+vu2W6s5ggNSP7G3gcXb6Ky1vkZSWIy6iZN+bc3jpHFKHT Vu6OI0RZz3NsP6x7KbLgn6Z+jDBKAsvezmYu3jp5Hxy8apBmTwlzRRV1/vJnpjnnw0tx HxVy6NlR3ebK2GjMybMkeyBSmfoelYgRHr+fhvVBXQAXH4ZM8m25Zl7Yr3jAcqc65GJU nK4A== X-Forwarded-Encrypted: i=1; AJvYcCX0yxkaH+27zjTNRNPSRtwfaipU/3x+zpTIM6P+lQx6h6KWAt19a43XQo+4+0IWOswF6MUOcZRKpHvOYuXw5cwOSsoMdVKdvSmdk7B+ X-Gm-Message-State: AOJu0YzTuKSQuW2K4nLyz7BT+0FvpQqZ0Q56NAWhiXQy3W8xpCUaDVc/ EaG0yw0CrxAHXwEz/AR9I+Qu+3PJ+y8HUwYFnNFBBqakwV2iqKEx X-Received: by 2002:ae9:ea0a:0:b0:78a:29cc:69dc with SMTP id f10-20020ae9ea0a000000b0078a29cc69dcmr3040022qkg.23.1711476087719; Tue, 26 Mar 2024 11:01:27 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 2-20020a05620a04c200b0078874069a89sm3206269qks.9.2024.03.26.11.01.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 11:01:27 -0700 (PDT) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfauth.nyi.internal (Postfix) with ESMTP id 8348B1200032; Tue, 26 Mar 2024 14:01:26 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Tue, 26 Mar 2024 14:01:26 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddufedguddtkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepjeeludevuefhieffieegffeuffeghefhvddvueehgfevfffhvddvgfeh veekkeegnecuffhomhgrihhnpegvlhgrphhsvggurdhtohenucevlhhushhtvghrufhiii gvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhht hhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquh hnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 26 Mar 2024 14:01:25 -0400 (EDT) Date: Tue, 26 Mar 2024 11:00:52 -0700 From: Boqun Feng To: 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 , =?iso-8859-1?Q?Bj=F6rn?= 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: References: <20240324223339.971934-1-boqun.feng@gmail.com> <20240324223339.971934-6-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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Tue, Mar 26, 2024 at 05:13:38PM +0000, Benno Lossin wrote: > On 24.03.24 23:33, Boqun Feng wrote: > > 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 there currently is no way to add an `Instant` to a `Duration`. > This is kinda the disadvantages of "upstreaming the bits you only need", we know for sure there will be a way to generate an `Instant` with an addition of a `Duration`. I can of course provide that function in this series. But let's settle down on "negative durations" first. > > + /// > > + /// 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); > > Now that I thought a bit more about the design, I think allowing > negative durations is a bad idea. > Do you disagree? > So yes, I don't think allowing negative duration is really good design. But as I mentioned in the cover letter, I hope to support cases where: d = ts2 - ts1; ts = ts3 + d; (where ts1, ts2, ts3 is Instant, and d is of course Duration) without any branch instruction in the asm code. It's useful in the case where ts1 is a old time base, and ts3 is the new one, and you want to "remain" the delta between ts2 and t1 and apply that on ts3. To me there are three options to achieve that: 1) allow negative durations (this also mirrors what `ktime_t` represents for timedelta AKAIU), 2) have a timedelta type that differs from Duration, and it can be negative, 3) provide a function to do the above calculation for `Instant`. I choose the first one because it's quick and simple (also easy to map to `ktime_t`). But I don't have my own preference on these three options. Regards, Boqun > 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`. > > -- > 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 > >