From nobody Tue May 21 10:47:23 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1608064193; cv=none; d=zohomail.com; s=zohoarc; b=Fo3kx7KHvv08gZqE4ZIF7E4CNZ1bZ1erG9oQmrGqZoVpkn34ptZdOSFk3GbBYgh/9A7Qu5VpLYHv9UXqnbQb4ZJH2kpcGsPms6MklcUP/hsX/Gpbt9u7oLY2YMPFa2D1K032HBxlxjsBgXu6eiFKR6jiEuCJTWnlMgMdPL4CT/E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608064193; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=IGI78ySIqdwEDouakruXPezW0i6YbGzlKC8KIhhFa68=; b=YtEsOZYEl4fZs0+1q80GiyE42JVVWU9gAxdCeVpwHNcXva430eIxZPZlZUYnlpZ5GtVRj4fmej2pwLQ7SGwoIfDVEkLugjc2xQWhDgT3Ikg8c1psHgybcGHRiNuvvCyF/SgNF9rZkEhGRTNhqAiQjr2zGSwvGu/NQUVsgrnjHx8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1608064193314671.3877746865321; Tue, 15 Dec 2020 12:29:53 -0800 (PST) Received: from localhost ([::1]:36724 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kpBzm-0008AL-5B for importer@patchew.org; Tue, 15 Dec 2020 10:12:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:48014) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kpBxJ-0005Io-6S for qemu-devel@nongnu.org; Tue, 15 Dec 2020 10:09:37 -0500 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]:37553) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kpBxG-0000kf-QS for qemu-devel@nongnu.org; Tue, 15 Dec 2020 10:09:36 -0500 Received: by mail-wm1-x333.google.com with SMTP id q75so18816990wme.2 for ; Tue, 15 Dec 2020 07:09:34 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id l1sm36930593wmi.15.2020.12.15.07.09.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Dec 2020 07:09:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IGI78ySIqdwEDouakruXPezW0i6YbGzlKC8KIhhFa68=; b=lmFEZTxa+CGVUHKWJ77errqhhZyO9Cum19x/XWXsKq/O/+7DHdKdHiw+MV3E92Dinp 2Mv4QBztfX7xGVPkRsFyXT4uQlwLsGzWqg/TGtUv1pBfeVHIvZfo9jfj+wlM7ycSCHDf C67Sa0YKM3ug0Cw7d2yM/G3YQ2iBqtICStUlMcrcuOkIoLp9Ih27I2uNdqFkrY6Amxq1 /smUskKyyLpCGIrGnRL8rlI4xiPRMsWZitn2zbZbzFysdteXwViH2ilUcqWK2SfLYsUG 4g/Gw1zmDhNAcy5YMZaIJ27L+rYo4DIGmd+2QgEROLRAjXE7j5QF6UlmBebHIiW8jc54 iOqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IGI78ySIqdwEDouakruXPezW0i6YbGzlKC8KIhhFa68=; b=F3/T/qdwE1NlAMR9dwNkz7kPea5FlXkiilrL/deBI4E1VRLEYBQWLCLPOcNx1fFPq7 H96dJoBe1Tpx06JOjUc1Rja7b0pqqFeYQM5H+DAecdHzS+0coVt82Yyp8OsDSmuMsVNs KreOOujq8xaBNbFTvlf0JprCLi9Cxf1FTpqzVuTvi/zqIUZHutaRrqynnk5gbNaG9FDC zL7mINj+mykmaRvFQk46CnRZqTGySEdIoys2mGhpQDLFwtkAXyRVoWo6fOSDhs6RNQaP xSaw3OjSlc/Bml0SdxIao/xGnjfbVDneA1sjqkPdf/B4paw/e0s7ITLtD9sSxdr0l+wa Gy1Q== X-Gm-Message-State: AOAM5305FvkEHesRhFMwaxvhBILGi5Ho4mJl6/IxT2dQW0llAEI9eevN zuYb6pHkzGkFvXYK4ldQ7fUSlYu+gfwKvg== X-Google-Smtp-Source: ABdhPJwHWQv2EXwQrLdTDml+6/zmvhQ85RDnf/hyd+ICynrrvq5KjyczTrFJMF3bornBLOZIe6YaIw== X-Received: by 2002:a05:600c:258:: with SMTP id 24mr33996982wmj.16.1608044972839; Tue, 15 Dec 2020 07:09:32 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH v2 1/4] clock: Introduce clock_ticks_to_ns() Date: Tue, 15 Dec 2020 15:09:26 +0000 Message-Id: <20201215150929.30311-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201215150929.30311-1-peter.maydell@linaro.org> References: <20201215150929.30311-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::333; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x333.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Aleksandar Rikalo , Richard Henderson , Luc Michel , Jiaxun Yang , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The clock_get_ns() API claims to return the period of a clock in nanoseconds. Unfortunately since it returns an integer and a clock's period is represented in units of 2^-32 nanoseconds, the result is often an approximation, and calculating a clock expiry deadline by multiplying clock_get_ns() by a number-of-ticks is unacceptably inaccurate. Introduce a new API clock_ticks_to_ns() which returns the number of nanoseconds it takes the clock to make a given number of ticks. This function can do the complete calculation internally and will thus give a more accurate result. Signed-off-by: Peter Maydell Reviewed-by: Luc Michel Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Richard Henderson Tested-by: Philippe Mathieu-Daud=C3=A9 --- The 64x64->128 multiply is a bit painful for 32-bit and I guess in theory since we know we only want bits [95:32] of the result we could special-case it, but TBH I don't think 32-bit hosts merit much optimization effort these days. Changes in v2: saturate the result to INT64_MAX. --- docs/devel/clocks.rst | 29 +++++++++++++++++++++++++++++ include/hw/clock.h | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/docs/devel/clocks.rst b/docs/devel/clocks.rst index e5da28e2111..c2e70e64db1 100644 --- a/docs/devel/clocks.rst +++ b/docs/devel/clocks.rst @@ -258,6 +258,35 @@ Here is an example: clock_get_ns(dev->my_clk_input)); } =20 +Calculating expiry deadlines +---------------------------- + +A commonly required operation for a clock is to calculate how long +it will take for the clock to tick N times; this can then be used +to set a timer expiry deadline. Use the function ``clock_ticks_to_ns()``, +which takes an unsigned 64-bit count of ticks and returns the length +of time in nanoseconds required for the clock to tick that many times. + +It is important not to try to calculate expiry deadlines using a +shortcut like multiplying a "period of clock in nanoseconds" value +by the tick count, because clocks can have periods which are not a +whole number of nanoseconds, and the accumulated error in the +multiplication can be significant. + +For a clock with a very long period and a large number of ticks, +the result of this function could in theory be too large to fit in +a 64-bit value. To avoid overflow in this case, ``clock_ticks_to_ns()`` +saturates the result to INT64_MAX (because this is the largest valid +input to the QEMUTimer APIs). Since INT64_MAX nanoseconds is almost +300 years, anything with an expiry later than that is in the "will +never happen" category. Callers of ``clock_ticks_to_ns()`` should +therefore generally not special-case the possibility of a saturated +result but just allow the timer to be set to that far-future value. +(If you are performing further calculations on the returned value +rather than simply passing it to a QEMUTimer function like +``timer_mod_ns()`` then you should be careful to avoid overflow +in those calculations, of course.) + Changing a clock period ----------------------- =20 diff --git a/include/hw/clock.h b/include/hw/clock.h index 81bcf3e505a..b5fff6ded83 100644 --- a/include/hw/clock.h +++ b/include/hw/clock.h @@ -16,6 +16,8 @@ =20 #include "qom/object.h" #include "qemu/queue.h" +#include "qemu/host-utils.h" +#include "qemu/bitops.h" =20 #define TYPE_CLOCK "clock" OBJECT_DECLARE_SIMPLE_TYPE(Clock, CLOCK) @@ -218,6 +220,45 @@ static inline unsigned clock_get_ns(Clock *clk) return CLOCK_PERIOD_TO_NS(clock_get(clk)); } =20 +/** + * clock_ticks_to_ns: + * @clk: the clock to query + * @ticks: number of ticks + * + * Returns the length of time in nanoseconds for this clock + * to tick @ticks times. Because a clock can have a period + * which is not a whole number of nanoseconds, it is important + * to use this function when calculating things like timer + * expiry deadlines, rather than attempting to obtain a "period + * in nanoseconds" value and then multiplying that by a number + * of ticks. + * + * The result could in theory be too large to fit in a 64-bit + * value if the number of ticks and the clock period are both + * large; to avoid overflow the result will be saturated to INT64_MAX + * (because this is the largest valid input to the QEMUTimer APIs). + * Since INT64_MAX nanoseconds is almost 300 years, anything with + * an expiry later than that is in the "will never happen" category + * and callers can reasonably not special-case the saturated result. + */ +static inline uint64_t clock_ticks_to_ns(const Clock *clk, uint64_t ticks) +{ + uint64_t ns_low, ns_high; + + /* + * clk->period is the period in units of 2^-32 ns, so + * (clk->period * ticks) is the required length of time in those + * units, and we can convert to nanoseconds by multiplying by + * 2^32, which is the same as shifting the 128-bit multiplication + * result right by 32. + */ + mulu64(&ns_low, &ns_high, clk->period, ticks); + if (ns_high & MAKE_64BIT_MASK(31, 33)) { + return INT64_MAX; + } + return ns_low >> 32 | ns_high << 32; +} + /** * clock_is_enabled: * @clk: a clock --=20 2.20.1 From nobody Tue May 21 10:47:23 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1608045044; cv=none; d=zohomail.com; s=zohoarc; b=d5LktK3Fh6kwcul0eY2UvBz/WOVP7JwMJgywQPrfjyw7GAeNxzEjH8ZDGlSHrA/1LPVE2mAHnr6J6cFurGEWgvbYozYEfxv6grinv5m0+ilNJcm7mpi9C6zZhKu77xk6NPDcBK5C8XGqAyrVl0dOP8o/5FYrhJPKeWB0NlSRSpY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608045044; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=a+b1GhhvLrJkl2Vn8twSVpP5pnoCxjLycXJoM3JnXDI=; b=ahzDO1ryb8Qed6ME4znT4GYGUiXutuXA6UB9NauuVBJM4BgslUinM16i+L7HjCLjX4ZwlsazDjlV15aiOrwngyC5qYSsoFlYUVsi8qLqTbUdnOrdQkGMr1y6dbJ94lBevq6e4b9tIXblCfHfoeMd4kCouhjX5gEHq16uusAownQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1608045044515345.8595368022076; Tue, 15 Dec 2020 07:10:44 -0800 (PST) Received: from localhost ([::1]:60112 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kpByK-0006AC-Nj for importer@patchew.org; Tue, 15 Dec 2020 10:10:40 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:48020) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kpBxJ-0005J2-L4 for qemu-devel@nongnu.org; Tue, 15 Dec 2020 10:09:37 -0500 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]:46086) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kpBxH-0000kk-Va for qemu-devel@nongnu.org; Tue, 15 Dec 2020 10:09:37 -0500 Received: by mail-wr1-x443.google.com with SMTP id d13so1930724wrc.13 for ; Tue, 15 Dec 2020 07:09:35 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id l1sm36930593wmi.15.2020.12.15.07.09.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Dec 2020 07:09:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=a+b1GhhvLrJkl2Vn8twSVpP5pnoCxjLycXJoM3JnXDI=; b=vXod0hlRtxicw20rtXnuoOemidOlUPRkiR/tvzmwg/ezxSLvMCOld985Rb6jDK+WcU iuat5mh2r68+5zf887w/lY5ypFUeSYni2fIwDyNniK3LPoSVO86/vkI0Qpf5+iUdHYrf Fp5dnS/2WEVWuWWiR5uq0INpMcyiKMChYaauAvnA/2GLp0CMMgO6O+awD3IcfgPO0doW Z1eWaotURoPyn8zshWiZEEfa1/FVSg9bQqPbdq8Bl8y93R2Y68MO5bg7sj+QxcViAEj2 QLaXlQIOCDa6vn1jiDdmz486lF8rZFW7BhUnojkaDvUyI3JBpwRnrlaTFdsAmHi0crlP 6M7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=a+b1GhhvLrJkl2Vn8twSVpP5pnoCxjLycXJoM3JnXDI=; b=XrhskQESeBIgU5clX/W+xT/bjolsw3R4LWsXum329SZaoktYVrz7d6NdyhX0blaCSh 2zaKOMTPRNMebxQtAwXaz05nJ2efs6A4Xg2+68Ls/h//k4OJZMdd/zoTQk/wYAq6fWS/ DdeQtWCC/VKaCapMOp17cjE+hspO78Vd6w7MyovpnOvW8h+B7TZXRVKNz6McumgNNB39 akfQ+XhmHETCoqo0KrezH3nbK1nnVDA5sJconptcCO8fqgsYv170xNwxXmWNLC/vm/94 tFWQ+Cz0bzKbVPBLyZbjyUOdEoxlthqlY3suaLVrc9tuIQTQJ/RiMTzZ/mEOISrfgu5i UReQ== X-Gm-Message-State: AOAM5316GgtD2r9ZhvlfnGRuErClEWz+lKXu0SU1O/DxRSCOZxoDcm9B lQwuJt8UG38vKJ+bGmN92sPUw+IXqgXfjw== X-Google-Smtp-Source: ABdhPJyPLZQEW38E3wxggCl/nNZzX+hiM7eD872pQkgvOCbC4dRBMNPXGBpZSl/WUyxkXKKdCr5Uww== X-Received: by 2002:a5d:4cca:: with SMTP id c10mr33881136wrt.176.1608044974039; Tue, 15 Dec 2020 07:09:34 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH v2 2/4] target/mips: Don't use clock_get_ns() in clock period calculation Date: Tue, 15 Dec 2020 15:09:27 +0000 Message-Id: <20201215150929.30311-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201215150929.30311-1-peter.maydell@linaro.org> References: <20201215150929.30311-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::443; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x443.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Aleksandar Rikalo , Richard Henderson , Luc Michel , Jiaxun Yang , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Currently the MIPS code uses the old clock_get_ns() API to calculate a time length in nanoseconds: cpu->cp0_count_rate * clock_get_ns(MIPS_CPU(cpu)->clock) This relies on the clock having a period which is an exact number of nanoseconds. Switch to the new clock_ticks_to_ns() function, which does the multiplication internally at a higher precision. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Reviewed-by: Luc Michel Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Philippe Mathieu-Daud=C3=A9 --- target/mips/cpu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/target/mips/cpu.c b/target/mips/cpu.c index aadc6f8e74d..eea14f3b52f 100644 --- a/target/mips/cpu.c +++ b/target/mips/cpu.c @@ -380,8 +380,8 @@ static void mips_cp0_period_set(MIPSCPU *cpu) { CPUMIPSState *env =3D &cpu->env; =20 - env->cp0_count_ns =3D cpu->cp0_count_rate - * clock_get_ns(MIPS_CPU(cpu)->clock); + env->cp0_count_ns =3D clock_ticks_to_ns(MIPS_CPU(cpu)->clock, + cpu->cp0_count_rate); assert(env->cp0_count_ns); } =20 --=20 2.20.1 From nobody Tue May 21 10:47:23 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1608064176; cv=none; d=zohomail.com; s=zohoarc; b=Lt/WQj96BBsMfpLWFrMFgSmkPIYSBGuNOFLnqn/czJH0SrAllZI9lHnMmbXBia5bUZk5Z831C/u1icAIIoXcPwVNFykiIyYhOQWj9t2hYTyI4e9wkBRjVEZUR7Neh9lioJahK9sMqP4DY6l0w47Z5GfBhSOcHVSvRehPihVsy7k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608064176; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=qSKE1KtRg1W0oTZN5C+68UidN1xnNarWeSnPqZ3lXqU=; b=I5qEy3hUJamrCABMONRwmFYuHSwnD2zlYBw4YtI0vILqKQIBKbbkJQLvQozqysIqNHE6v+WmH/UuOesV6CZWusaxlrZuIeMhisPewMzEfgBuauxI1RFr2rWhoV+gx1YunfIuQTbW69vMysyuz5YEl0VhOFBMjlFvNlZTIVDllIk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1608064176784840.2709343382367; Tue, 15 Dec 2020 12:29:36 -0800 (PST) Received: from localhost ([::1]:36744 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kpBzm-0008Ak-6o for importer@patchew.org; Tue, 15 Dec 2020 10:12:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:48048) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kpBxK-0005KC-W4 for qemu-devel@nongnu.org; Tue, 15 Dec 2020 10:09:39 -0500 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]:32968) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kpBxI-0000kw-VF for qemu-devel@nongnu.org; Tue, 15 Dec 2020 10:09:38 -0500 Received: by mail-wm1-x341.google.com with SMTP id n16so98294wmc.0 for ; Tue, 15 Dec 2020 07:09:36 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id l1sm36930593wmi.15.2020.12.15.07.09.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Dec 2020 07:09:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qSKE1KtRg1W0oTZN5C+68UidN1xnNarWeSnPqZ3lXqU=; b=eI52u3Xh752eohMFk8DYyQL6qDGig5gGrTUa/IhsRymE3ZCep3ZwmCE7JhsCPAO85v s3QThh3dASpctNqY9/XV6BjiWQixoykZ6YNV24ReuO4fcjgXkafMaOpRy2+4IS3Gt5tx lpREaxd9PmPeKOMYCu6xH8cNeAE2FNTESbjNt4BS6R94PVrWjZKaq6XggrLDjXUl3sCg O2waeUJj05vCiTwNfbsWkGrchaf+/HMZsOCGT23KiRj/RXXjEZhv5sfkseICxE7h/+D2 K4bQ8dsT1nvVeFlB/3sOjG/TUti2eGyM1Zkzgpp59ktyVBsu1yKiNfDuw+lBXMCY6YTc LYuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qSKE1KtRg1W0oTZN5C+68UidN1xnNarWeSnPqZ3lXqU=; b=dGg44I3SwWEgTOAfIzLvBc8zS9crn75QIn1+SO5w43FH/DSOIYjOZzd5Cs/o6vTz4G J3j2AzoKEkbfFUmICeYOyDvZiuao/GNh0KvoxWrxc3AePclQN78xtCgofyVXAMjvRxy6 fFK/6D1xHfZF0vaNl7GmVmlMgB24zZzP0YzzfEMwSlB7jG1PdsilGvt4cc7gfwK3dDHp 7gayJhzAPMhi3CrJIWdRV7y1foPBUxKsMv9/bf3rB7kqOnN+shcOhCQ8kK/1ZwqDRcEV 8ZCVjfov5PXPyZcmBTTpHMpP/2kI3Mb5mAjKaS445BhLrWH4Z6eY/xOFoFcGYZX9nxCk mHzw== X-Gm-Message-State: AOAM532r9hKeon8pZdYtnb3kTxAoxnS0W8rUSjFhFc0Fk8TIZboif8JD nHNk1LOdcLzg0WuoIC5KOooY3GK4KCZAbA== X-Google-Smtp-Source: ABdhPJxNFBdoUbhVO63HSwU4kpivSsRqVGeMaafh8/1itKxTvP8AZjsmgg5TIPiJxsx8fhJBXjPiKw== X-Received: by 2002:a1c:63d4:: with SMTP id x203mr32264777wmb.28.1608044975299; Tue, 15 Dec 2020 07:09:35 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH v2 3/4] clock: Remove clock_get_ns() Date: Tue, 15 Dec 2020 15:09:28 +0000 Message-Id: <20201215150929.30311-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201215150929.30311-1-peter.maydell@linaro.org> References: <20201215150929.30311-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::341; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x341.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Aleksandar Rikalo , Richard Henderson , Luc Michel , Jiaxun Yang , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Remove the now-unused clock_get_ns() API and the CLOCK_PERIOD_TO_NS() macro that only it was using. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Reviewed-by: Luc Michel Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Philippe Mathieu-Daud=C3=A9 --- docs/devel/clocks.rst | 17 +++++++++++++---- include/hw/clock.h | 6 ------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/devel/clocks.rst b/docs/devel/clocks.rst index c2e70e64db1..2d317ff32f2 100644 --- a/docs/devel/clocks.rst +++ b/docs/devel/clocks.rst @@ -238,8 +238,17 @@ object during device instance init. For example: Fetching clock frequency/period ------------------------------- =20 -To get the current state of a clock, use the functions ``clock_get()``, -``clock_get_ns()`` or ``clock_get_hz()``. +To get the current state of a clock, use the functions ``clock_get()`` +or ``clock_get_hz()``. + +``clock_get()`` returns the period of the clock in its fully precise +internal representation, as an unsigned 64-bit integer in units of +2^-32 nanoseconds. (For many purposes ``clock_ticks_to_ns()`` will +be more convenient; see the section below on expiry deadlines.) + +``clock_get_hz()`` returns the frequency of the clock, rounded to the +next lowest integer. This implies some inaccuracy due to the rounding, +so be cautious about using it in calculations. =20 It is also possible to register a callback on clock frequency changes. Here is an example: @@ -254,8 +263,8 @@ Here is an example: */ =20 /* do something with the new period */ - fprintf(stdout, "device new period is %" PRIu64 "ns\n", - clock_get_ns(dev->my_clk_input)); + fprintf(stdout, "device new period is %" PRIu64 "* 2^-32 ns\n", + clock_get(dev->my_clk_input)); } =20 Calculating expiry deadlines diff --git a/include/hw/clock.h b/include/hw/clock.h index b5fff6ded83..852c636961e 100644 --- a/include/hw/clock.h +++ b/include/hw/clock.h @@ -40,7 +40,6 @@ typedef void ClockCallback(void *opaque); * macro helpers to convert to hertz / nanosecond */ #define CLOCK_PERIOD_FROM_NS(ns) ((ns) * (CLOCK_PERIOD_1SEC / 1000000000ll= u)) -#define CLOCK_PERIOD_TO_NS(per) ((per) / (CLOCK_PERIOD_1SEC / 1000000000ll= u)) #define CLOCK_PERIOD_FROM_HZ(hz) (((hz) !=3D 0) ? CLOCK_PERIOD_1SEC / (hz)= : 0u) #define CLOCK_PERIOD_TO_HZ(per) (((per) !=3D 0) ? CLOCK_PERIOD_1SEC / (per= ) : 0u) =20 @@ -215,11 +214,6 @@ static inline unsigned clock_get_hz(Clock *clk) return CLOCK_PERIOD_TO_HZ(clock_get(clk)); } =20 -static inline unsigned clock_get_ns(Clock *clk) -{ - return CLOCK_PERIOD_TO_NS(clock_get(clk)); -} - /** * clock_ticks_to_ns: * @clk: the clock to query --=20 2.20.1 From nobody Tue May 21 10:47:23 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1608071743; cv=none; d=zohomail.com; s=zohoarc; b=PaPvT6JK64cgt+i1Rz3pnuPx7hFmmZtxHOBoPSGtT6DC+vJ+1pMsH4/hWxQW+BkBagV8F4c0jGnMdda1/qgeCbVEBVanOW53z7z4KFkpT+Ux0bQQcP8cGbhN1KJUoUZf7d59taoQpJIwuGtrXjxxrsgl6RcLndMWrpGl26ERPWg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608071743; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Nkq5zKbaVAWjdXkuGKd7o/1m+2dYVJpKL4l20M54fCI=; b=bejRqFXlBfzdxTjEAWPtRdU5TMAT3XUEWiCM8y7tpXCjfRWYdIRGR/VxX/4/NvXcmufcmtHmKhCYHDpF/3ZEoXOY2p6LukJIKScLEcMZx32iO/7skD67zCgCOa898s/yJvgkH75jYR5tYvvFevnBMyek/+4RhiAlsMserAVHxgg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1608071743025112.93567124793367; Tue, 15 Dec 2020 14:35:43 -0800 (PST) Received: from localhost ([::1]:44120 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kpC2O-0002kP-FK for importer@patchew.org; Tue, 15 Dec 2020 10:14:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:48058) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kpBxM-0005Lt-0y for qemu-devel@nongnu.org; Tue, 15 Dec 2020 10:09:40 -0500 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]:37241) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kpBxK-0000lE-81 for qemu-devel@nongnu.org; Tue, 15 Dec 2020 10:09:39 -0500 Received: by mail-wr1-x42d.google.com with SMTP id i9so20194236wrc.4 for ; Tue, 15 Dec 2020 07:09:37 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id l1sm36930593wmi.15.2020.12.15.07.09.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Dec 2020 07:09:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Nkq5zKbaVAWjdXkuGKd7o/1m+2dYVJpKL4l20M54fCI=; b=dbJJQvv6APgPeCzkFQjdX53UBN1sPUaSi4nLH9VM9xaed/iXHk9KqLZXLDOuuH9RQK EW77eIiQX27KD9RCe0f0DRcZe2k5FtdlmjZpDeMKf13Ivl9ZscDTLxaeQigac1E0OPD0 JJjeernv3BbTX2qxfxnHunqadt/M28VcMHorQHJjZHcJTISOPKvM3HCqB0a6JjASiYBn /VVkMWo46OLG6KwqQW23Zcmo6Lj23/Jk2Q8p3mzMG1Zk9dEr9pJV40KhzhFCFNMGMVLp lMs3nIN/x03qHKRJTbYk/+K7IsCpVjx3FSrP+dMe78TslvPSaa9tn/NQaIrl2zTOushe Ed4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Nkq5zKbaVAWjdXkuGKd7o/1m+2dYVJpKL4l20M54fCI=; b=LQTIrI8g+Zsap/hth6B6geIg8L6Z+MV3lz0mJdMCJWn5iW6thD4pmHKxEhevD0w5Ws ryU1kvuW/WgF+M/DFSoCfwMejwRM/p/2nBDSCXckzkds26gM13qRjYzKQjRcZllXFbu8 qOeFPLjNhURIQ385XlkbRDWGLmoCYnEh4+E8jfUflPtXMWePRRbK4V8tBW4zZ472PVwG ybU91BgGGMNO+nUbK5z/KwrE8PVl4PxMM7pmXvFdQwsVVLhOtciJ3L0/Kyi4EnQJTe7K ADQzhuPBTNZlrKEdMe1jL5Oe6nJOMGAfuNMgpdX4g2+yrDc/rr0clxNA03J6L4Wx0tkO 23vA== X-Gm-Message-State: AOAM533Cu0VvDhDQCGZ19+QyV1eu2CCwF7pxzxzl/YNvmRVLAVWP9U1Z 76guaJYJkVKF8h0GirCOSBEgzoUmODTc3Q== X-Google-Smtp-Source: ABdhPJzJMnxtyvc8pMSU8z5t08Ox2EdT2GbV4wpcj9g5DWvaOESIsWVa+KYe9+CZXynKNXwhsJeLxw== X-Received: by 2002:a5d:6a05:: with SMTP id m5mr3205573wru.96.1608044976547; Tue, 15 Dec 2020 07:09:36 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH v2 4/4] clock: Define and use new clock_display_freq() Date: Tue, 15 Dec 2020 15:09:29 +0000 Message-Id: <20201215150929.30311-5-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201215150929.30311-1-peter.maydell@linaro.org> References: <20201215150929.30311-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::42d; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x42d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Aleksandar Rikalo , Richard Henderson , Luc Michel , Jiaxun Yang , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" It's common to want to print a human-readable indication of a clock's frequency. Provide a utility function in the clock API to return a string which is a displayable representation of the frequency, and use it in qdev-monitor.c. Before: (qemu) info qtree [...] dev: xilinx,zynq_slcr, id "" clock-in "ps_clk" freq_hz=3D3.333333e+07 mmio 00000000f8000000/0000000000001000 After: dev: xilinx,zynq_slcr, id "" clock-in "ps_clk" freq_hz=3D33.3 MHz mmio 00000000f8000000/0000000000001000 Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Reviewed-by: Luc Michel Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Philippe Mathieu-Daud=C3=A9 --- This is based on Philippe's patch "qdev-monitor: Display frequencies scaled to SI unit" but I have abstracted out the "prettified string" into the clock API. --- docs/devel/clocks.rst | 5 +++++ include/hw/clock.h | 12 ++++++++++++ hw/core/clock.c | 6 ++++++ softmmu/qdev-monitor.c | 6 +++--- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/docs/devel/clocks.rst b/docs/devel/clocks.rst index 2d317ff32f2..2548d842322 100644 --- a/docs/devel/clocks.rst +++ b/docs/devel/clocks.rst @@ -267,6 +267,11 @@ Here is an example: clock_get(dev->my_clk_input)); } =20 +If you are only interested in the frequency for displaying it to +humans (for instance in debugging), use ``clock_display_freq()``, +which returns a prettified string-representation, e.g. "33.3 MHz". +The caller must free the string with g_free() after use. + Calculating expiry deadlines ---------------------------- =20 diff --git a/include/hw/clock.h b/include/hw/clock.h index 852c636961e..6382f346569 100644 --- a/include/hw/clock.h +++ b/include/hw/clock.h @@ -264,4 +264,16 @@ static inline bool clock_is_enabled(const Clock *clk) return clock_get(clk) !=3D 0; } =20 +/** + * clock_display_freq: return human-readable representation of clock frequ= ency + * @clk: clock + * + * Return a string which has a human-readable representation of the + * clock's frequency, e.g. "33.3 MHz". This is intended for debug + * and display purposes. + * + * The caller is responsible for freeing the string with g_free(). + */ +char *clock_display_freq(Clock *clk); + #endif /* QEMU_HW_CLOCK_H */ diff --git a/hw/core/clock.c b/hw/core/clock.c index 8c6af223e7c..76b5f468b6e 100644 --- a/hw/core/clock.c +++ b/hw/core/clock.c @@ -12,6 +12,7 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/cutils.h" #include "hw/clock.h" #include "trace.h" =20 @@ -111,6 +112,11 @@ static void clock_disconnect(Clock *clk) QLIST_REMOVE(clk, sibling); } =20 +char *clock_display_freq(Clock *clk) +{ + return freq_to_str(clock_get_hz(clk)); +} + static void clock_initfn(Object *obj) { Clock *clk =3D CLOCK(obj); diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c index 832e2548424..fb6c94eaeaf 100644 --- a/softmmu/qdev-monitor.c +++ b/softmmu/qdev-monitor.c @@ -732,11 +732,11 @@ static void qdev_print(Monitor *mon, DeviceState *dev= , int indent) } } QLIST_FOREACH(ncl, &dev->clocks, node) { - qdev_printf("clock-%s%s \"%s\" freq_hz=3D%e\n", + g_autofree char *freq_str =3D clock_display_freq(ncl->clock); + qdev_printf("clock-%s%s \"%s\" freq_hz=3D%s\n", ncl->output ? "out" : "in", ncl->alias ? " (alias)" : "", - ncl->name, - CLOCK_PERIOD_TO_HZ(1.0 * clock_get(ncl->clock))); + ncl->name, freq_str); } class =3D object_get_class(OBJECT(dev)); do { --=20 2.20.1