Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3481996lqp; Tue, 26 Mar 2024 10:12:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUairO5wy55G+yCqIoDB6zu3wkRNRuf9Rj5m/VIWANs5u0nsEiyfI7kTbBMe3TrL0uJql8niD11WjsTEhyowEHYWA3DV3ts2uDxTbWkkQ== X-Google-Smtp-Source: AGHT+IFRQb9r8t2xhYVW02qtBC6Wg6tRSHkgM0TARIPf0daRhI0rLw1MsUUx/nikk7VzLuSghgGZ X-Received: by 2002:a05:622a:529b:b0:431:75a3:b2e6 with SMTP id dr27-20020a05622a529b00b0043175a3b2e6mr1534371qtb.41.1711473154729; Tue, 26 Mar 2024 10:12:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711473154; cv=pass; d=google.com; s=arc-20160816; b=hqt6r+TpWIFFU7/n1qAH0Cx5FkP8aueAysq24H/d6p4243YaXG0tnIAjpftPtb4l4/ Oy3lMhI59gXLH0a0iASe9KcklfKuNxFAkjkI6W3iskX/udUhSE1RVwTiPqoDhnKldHiB ULl6WY+cHC+JgngQth3bc9OXpaRFWGfRqLUlw4vG5QoxE4Yv+cU9lLbfoqi3emBPWSx5 auxpl2oPIPA2b1G/5M7fLvg+m894k0AlJ7VMmveQ6G4vw1mUl2q5wGYnaYytaXoqV7Mt RofgozLGnTu8t8qSan+B1cqJB4dAal1Z4pqFePq4qWiZ4IgMHuFQ1goFuIBJqe2rp8Wc Zl6Q== 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=qgy6KTgPoHVTqXlGJ95HB7f9RHaWGhN9Lg4BvWlkQqs=; fh=l34Pu6iR2IKlhsZKCSCLnn5cDDtrPfjZQw5oAgTb7Cg=; b=SHAHN8W46G94ykBVqLZzrlT7oTULezRFt2RBTaLAEKm7SbRiQfJsromwQbadbzOBHr AWCQUOIUG0sW196FImr62t34tMJOFiEnA+rQ3aKzDNEKXOguDza1PLT7+mI8PdZqdo3h 0rkyqOZFT6g8rv/+tuZRRczZZI6arE7KQJaWlLrNebXzwjUqzvs1PVaEKpuXVAe6XbTJ zGmexKmWIZvgXxy2WGSsht4KBtT7YxBYn5+5UiFprTxINLgUJXhKWujdVce/xwJVcMr4 8aoxqCfQ96n6Te8pNI4bQZfPkEY2mROATBjbqgicoWmzWgoQnx0PnzSc9PCW4r4BYPG4 9bow==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=fX6A0oE3; 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-119559-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119559-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id y18-20020a05622a165200b0043171b20d16si1287252qtj.171.2024.03.26.10.12.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 10:12:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-119559-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=@gmail.com header.s=20230601 header.b=fX6A0oE3; 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-119559-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119559-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id A2A081C63801 for ; Tue, 26 Mar 2024 17:12:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CF6EE171BB; Tue, 26 Mar 2024 17:11:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fX6A0oE3" Received: from mail-qt1-f180.google.com (mail-qt1-f180.google.com [209.85.160.180]) (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 302391CAAD; Tue, 26 Mar 2024 17:11:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711473105; cv=none; b=CsIadOWxdQ6CKBV464l3Rji7sECErCIQQkxQKNk3T/6136GNwhLg1RrSb19lBlrFcFaBPmvVU9Spc8zFZ2+06kfvN051dj1rGYlPPjg7czupKuQuQVE7uQ5E4JWZVq2DaUCrnvDAvp/UKBkOWWfrFXAGlN+rsBg1G5funLx//kk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711473105; c=relaxed/simple; bh=2H4seo235TtcNfGay7H2sbkZqoRkvg1ndzz5RqCoHR0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=AZM4Waw/K0t4KwYHRUcRnVnm6LFlMJ9I03+CkyxT0YJsvr+nheKkx+XxbTIzPYJU/g/TH57exmPO+1uMnXtCBcxHZ3ncoE5/fewSqice3Y6IQi2TGOeWxm5Mtvkwj/Ra4/Jlz9Ucam1aOlss0xaZcQKN30rRx83HjTj7zKVi5hg= 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=fX6A0oE3; arc=none smtp.client-ip=209.85.160.180 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-f180.google.com with SMTP id d75a77b69052e-430c45ab240so33688611cf.0; Tue, 26 Mar 2024 10:11:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711473103; x=1712077903; 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=qgy6KTgPoHVTqXlGJ95HB7f9RHaWGhN9Lg4BvWlkQqs=; b=fX6A0oE3G6pUfmEBQs3ydtyTudGP6CF9ZkYk011fNUFnLttWW0Mm57yeAUVg0tS5Df tr5M43ghc4NijvgKtb7H5fG/dBaPd/Krjt7ZAmyPNdiHgOotWIJU9WtejKE4aeiA1wWv CEPea9DbXUz3pUYtmFtdqrKs9+lhL9i0MYNa5FsK4VQDpKP69eLi4qzaS6lqBBpkR+CB qICeb0MKQKq8U7348dMZjMjvBml+2kzy5icFK8tt/fLRDnlLmplr3sQU0GLSt+0KsLbN 3V6/XYU5akCzZ4s/ZNfBVqSBbdcZrqdxbIGfqSj3KzkXslnK/NMgZAZ78TNat/s6tDar K4wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711473103; x=1712077903; 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=qgy6KTgPoHVTqXlGJ95HB7f9RHaWGhN9Lg4BvWlkQqs=; b=auxj3Cx/kwPcL9wfhW2wuqUNQ9nDgaulYZ0essgnIHEcmHPf3nr47qi1xX6+BQLQeq 0ZZPUuWsjdyBCKYSGUEmxG8NZeCFgWEQdXVzWU02MgbMizf/qNEMzEjHK5v7thJOc57C yaWOo8WuCxHEkXM+wfUKgI30Spw+ON1uOUkH+MVdletVhLEoyxKiKcWZ4nPoZjZdiRnB KWNnhvTpU5mViYAHx41p4D3n7862Uq5cm6UA4L4gL9JB0Lxu+4f23+UhvjYij5WA+g5m yLgttdkXETa44WdUcggelwUvh5v7RRCcFCGjE8UYGBkS1wSxaaqYBSVzMkbt/Sa4775Z kaZw== X-Forwarded-Encrypted: i=1; AJvYcCWmQXTr+Tfw1aOvU903nuCpZoEWQ4MUe0kEvbB+15eeECR/Wczc2QogGjd9k+sbhuRFzpgaIWfbwxmyrgtfba2u/FR3RtD30P45JkuX X-Gm-Message-State: AOJu0Yzg9L5E5YkTQZira5D+wUPrn1tugkIB+Gz2J4akgz/YbZR24e+5 yWskNdPTLAwKjYV20aUZyMM2Gca9q7zLWTTXhwAFxhARuwrSfr43 X-Received: by 2002:a05:6214:262f:b0:696:987d:7922 with SMTP id gv15-20020a056214262f00b00696987d7922mr1412880qvb.9.1711473102734; Tue, 26 Mar 2024 10:11:42 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id x11-20020ad4458b000000b00696a47179a1sm418788qvu.14.2024.03.26.10.11.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 10:11:42 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfauth.nyi.internal (Postfix) with ESMTP id 7A67F1200069; Tue, 26 Mar 2024 13:11:41 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Tue, 26 Mar 2024 13:11:41 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddufedgleekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeehudfgudffffetuedtvdehueevledvhfelleeivedtgeeuhfegueeviedu ffeivdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 26 Mar 2024 13:11:40 -0400 (EDT) Date: Tue, 26 Mar 2024 10:11:07 -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 Subject: Re: [PATCH 2/5] rust: time: Introduce Duration type Message-ID: References: <20240324223339.971934-1-boqun.feng@gmail.com> <20240324223339.971934-3-boqun.feng@gmail.com> <4hYJbftgOk1JOPbJ6CfKZell6ngp8GljwIUIB1vOQvIf-7jiogG5xDtCvcMlF7cIJAdy9fO5HLQh_8ohnWNB3MAaj0xjAGeyyDowemgunOU=@proton.me> 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: <4hYJbftgOk1JOPbJ6CfKZell6ngp8GljwIUIB1vOQvIf-7jiogG5xDtCvcMlF7cIJAdy9fO5HLQh_8ohnWNB3MAaj0xjAGeyyDowemgunOU=@proton.me> On Tue, Mar 26, 2024 at 04:50:02PM +0000, Benno Lossin wrote: > On 24.03.24 23:33, Boqun Feng wrote:> From: Alice Ryhl > > > > Introduce a type representing time duration. Define our own type instead > > of using `core::time::Duration` because in kernel C code, an i64 > > (ktime_t) is used for representing time durations, an i64 backed > > duration type is more efficient when interacting with time APIs in C. > > > > Signed-off-by: Alice Ryhl > > [boqun: Rename `Ktime` to `Duration`, and make it a type of durations] > > Signed-off-by: Boqun Feng > > --- > > rust/kernel/time.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 44 insertions(+) > > > > diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs > > index bbb666e64dd7..b238b3a4e899 100644 > > --- a/rust/kernel/time.rs > > +++ b/rust/kernel/time.rs > > @@ -7,6 +7,9 @@ > > //! > > //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). > > > > +/// The number of nanoseconds per millisecond. > > +pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64; > > + > > /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. > > pub type Jiffies = core::ffi::c_ulong; > > > > @@ -20,3 +23,44 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { > > // matter what the argument is. > > unsafe { bindings::__msecs_to_jiffies(msecs) } > > } > > + > > +/// A time duration. > > +/// > > +/// # Examples > > +/// > > +/// ``` > > +/// let one_second = kernel::time::Duration::new(1000_000_000); > > +/// > > +/// // 1 second is 1000 milliseconds. > > +/// assert_eq!(one_second.to_ms(), 1000); > > +/// ``` > > +#[repr(transparent)] > > +#[derive(Copy, Clone, Debug)] > > +pub struct Duration { > > + inner: i64, > > Why not use the name `ns` or `nanos`? > Good point, I will rename it in next version. > > +} > > + > > +impl Duration { > > + /// Creates a new duration of `ns` nanoseconds. > > + pub const fn new(ns: i64) -> Self { > > + Self { inner: ns } > > + } > > + > > + /// Divides the number of nanoseconds by a compile-time constant. > > + #[inline] > > + fn divns_constant(self) -> i64 { > > + self.to_ns() / DIV > > + } > > I am a bit confused, why is this better than writing > `self.to_ns() / DIV` at the callsite? > Hmm.. you're right, there should be no difference I think. If there is nothing I'm missing from Alice, I will drop this function in the next version. Regards, Boqun > -- > Cheers, > Benno > > > + > > + /// Returns the number of milliseconds. > > + #[inline] > > + pub fn to_ms(self) -> i64 { > > + self.divns_constant::() > > + } > > + > > + /// Returns the number of nanoseconds. > > + #[inline] > > + pub fn to_ns(self) -> i64 { > > + self.inner > > + } > > +} > > -- > > 2.44.0 > >