From patchwork Sun Jan 3 20:50:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 356155 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp14471985jai; Sun, 3 Jan 2021 13:06:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJztu8McfhCqtzj6HZNh36QHPgXwJMqLoguI9gtbmpvgIZ4/qQyXwquaiMFTHgpXfkdyAbAy X-Received: by 2002:a25:25c2:: with SMTP id l185mr99438870ybl.140.1609707973515; Sun, 03 Jan 2021 13:06:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609707973; cv=none; d=google.com; s=arc-20160816; b=IdniH4tObS+7oXpjoyTxEYtU6aQEWBi8/5t/kqEnc9jpaLHORWQbQXNgm0DJE5sYA3 Akg8ypaFGYiBjwxdwWAJLmhx3J2OPjzxFdTngv7g5lbDKy7FNuZdj44omg9VxNcEWP26 PC23bWEwCq7pT3m9kVd+x4KN+h8f7uXWogWgSuhpiga8383Q1BGihvKaTgsuf3yabuLw EYNAfG8CfltW4R3WHSb1dzJbh52KUpGIbb0+ylnxMWiRAGU10uC/hXenUdTpCP5fQylc mgjmPuP+CmD63t1HlR1BRDlxDnaoaQVSCQUw/VOH7Ch1E6sP6ooIwRp4G20nSklXA6es Z/1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=IdHQJwCqOdYJyKNmtS/fZyUmMRQdfAB5wRMdzdWKipA=; b=dk32P2w+c2pBdt9K+kj3rmN4lt/edK9tXWZB34NtEuIZuWAAZiss5zf6LQbLinxK5q 0cF3R7DxhVr6ESDr4pVI4+r2qsjyAGwZEaRxTusSHVQURHz6TvaaWmgM3RCvnICzpKzf XJsQbXQ1JnCvUL4+EcPhSGcZbH5Dq4dY/VOxctHKh4VVHTz/RaTjMD/WOrSB7ReywoJY oEMGnd1y3C3nIEmxUqMH/VdwiX/pD/x0OYxtjWbpczgbJlZw4sAuMfUaki4slcdaU8Cq ObT0ekc0QlaWiwF8stk74KoWWwJvshwXNal4oR1+RHERNt2ycIkNVj3K0W/NOtqlp/NE 5JJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=HwYHJYDT; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=amsat.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id m18si46707038ybk.129.2021.01.03.13.06.13 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 03 Jan 2021 13:06:13 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=HwYHJYDT; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=amsat.org Received: from localhost ([::1]:50948 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kwAZp-0003iu-0P for patch@linaro.org; Sun, 03 Jan 2021 16:06:13 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:37754) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kwAM5-0003kg-8s for qemu-devel@nongnu.org; Sun, 03 Jan 2021 15:52:01 -0500 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]:43022) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kwAM2-00068e-BG for qemu-devel@nongnu.org; Sun, 03 Jan 2021 15:52:01 -0500 Received: by mail-wr1-x42f.google.com with SMTP id y17so29462909wrr.10 for ; Sun, 03 Jan 2021 12:51:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IdHQJwCqOdYJyKNmtS/fZyUmMRQdfAB5wRMdzdWKipA=; b=HwYHJYDTTT9fz3lggP7dIYTMzsuwzpdQPoIhTRpZt8Odep4aqgeq1M1vCcSGk6BMxN wjpPuYtsgM4u8gSYof+X0cYw045wEGSesiyNvidjKXS1kRLgKCOQXpnVtQUEulVs3d9U yS+FuOoPFm5QtsSTH6m3sKw1ic5mNmv+ngB/d5tPgVctJIEIGubmHvKeuHDkWbPVm4xG MPIVV+E3jZZ/eVjoO+BzDzMo++p+yWgZo8sDEHtQeUpjq8MQrkhQ1hEArvje38I7v3uO 3m3XB27y4gjsgMJcTGmxXI3DYxXwqqpsTvoAkj+NRkIHDJKkmGdeuAl0FluyrlFXNgO5 a5Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=IdHQJwCqOdYJyKNmtS/fZyUmMRQdfAB5wRMdzdWKipA=; b=rU3e55NGqYdP7U1ipc/3ARpkIzmXZ9HJ21ksK/pCY4d4ogTFuuk6D+mnsCRXKed6kl dKzwnIShP/5Bym414frFenXe5bXwFB+soolS+kg6FxkFVyoO8oRkze5JQbH8uRSkyg4T UASKAmpxl/N/wNLXFVjZ+F50L+NXTvtXbON7WbzNhzdmiJHIVzNO1ZYtggoqZZCe+NI2 eBzFGJq41cphe3YJufYa0sCX3CNDZpjr9gO/u/kYkc4TqFWxCZNCWpj1PYG9QrzGZKJb nHFi05jxccO9zNxo7JPOatXookAnuZYxTvEvr1XLkaHIpkw0SwLRo/7z3Im3JuzrmUon vhrA== X-Gm-Message-State: AOAM530/5HYSPENcUPvD0Br2nJnriTNd8u9M6LmPGmZnZplUonUfA6kT hFNCaEnEL28eKiQ0HCBFlJ2Fk9yZPFE= X-Received: by 2002:adf:c387:: with SMTP id p7mr76731410wrf.95.1609707115943; Sun, 03 Jan 2021 12:51:55 -0800 (PST) Received: from localhost.localdomain (194.red-83-57-172.dynamicip.rima-tde.net. [83.57.172.194]) by smtp.gmail.com with ESMTPSA id b200sm30290701wmb.10.2021.01.03.12.51.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Jan 2021 12:51:55 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Subject: [PULL 19/35] clock: Introduce clock_ticks_to_ns() Date: Sun, 3 Jan 2021 21:50:05 +0100 Message-Id: <20210103205021.2837760-20-f4bug@amsat.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210103205021.2837760-1-f4bug@amsat.org> References: <20210103205021.2837760-1-f4bug@amsat.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42f; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-wr1-x42f.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.248, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: Peter Maydell , Luc Michel , Huacai Chen , Richard Henderson , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Aurelien Jarno Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Peter Maydell 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 Tested-by: Philippe Mathieu-Daudé Reviewed-by: Luc Michel Reviewed-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20201215150929.30311-2-peter.maydell@linaro.org> Signed-off-by: Philippe Mathieu-Daudé --- docs/devel/clocks.rst | 29 +++++++++++++++++++++++++++++ include/hw/clock.h | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) -- 2.26.2 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)); } +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 ----------------------- 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 @@ #include "qom/object.h" #include "qemu/queue.h" +#include "qemu/host-utils.h" +#include "qemu/bitops.h" #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)); } +/** + * 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 From patchwork Sun Jan 3 20:50:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 356156 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp14472067jai; Sun, 3 Jan 2021 13:06:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJxYSZzsbw6E29qHvdr7EIl39fM9jKEX52/mtkq/dr2TEa5gaA5XyaA2I/KoJkt8Pe2F8Y2S X-Received: by 2002:a25:383:: with SMTP id 125mr97489519ybd.120.1609707983308; Sun, 03 Jan 2021 13:06:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609707983; cv=none; d=google.com; s=arc-20160816; b=Gj/pc7ohK+UIQvxDHkHWsg83do8XcA1+9gTkUKvT8oqRwjRSpzQ1SNzUN/4FihWwXq GLLmWal+TAkZ+T9FnxbBG2aQJqFtvw9t//Eiei9xbXsaeIIRGw/of0H7ho6G52IJXWQb cvIVMzF2CfuQEh3srHnRyfqx4CwOwCPAZ2L/V5f8ULfgGsMDrmPM3VM2PF+k3v9pPG2F KA9kSYiy6VP/DDcSyFXs+F4GEPovgcHwL/UPmeWF6wiRdf7wdn25DP/tXiwaMemAdbti 7au6Zk1UwXo8j3Ni/fmj+sYsRgsEFPRstwQgkWZ2h9CtkuI6F5dAtRtcFYtfRs3hs3bC 0uWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=ONmR7DvST/KHwvIMlpQESaWh+hvboxmRkL03nu+T/uM=; b=mgOAZEWZj2LlyUG22RBlt9q4bECulmjTe/JGcEhzNiP89aZrX6pGl4VThg6seD42At +lGpOy+CJ4h8mskuE7qiUcH4Wk5q1/NWeJWLeLxPyOrrj+ZRkvZpwx2GHbSHJ7KxNrMR Ap0Gqd7WNZ0L11KwrrV/C+v4DtQ86mWPTiZb1eBReuAX4D+iP3dXeHVKxB7PhvxllVY2 w4YGcuKMGjh0tDuYuO2G2ZWLjyIJRv1IlUKUr1oJvoWVvlKtpCQ61K4XEtDA6HMdB5+w MRFe5bBr4Owtu593avgX8kYQHj9E832Vhk9HJloBxaZYt3m+rJcInOgE1gJ9/ibGKpZm MrUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=HEzjlMV5; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=amsat.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id k65si33531888ybf.156.2021.01.03.13.06.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 03 Jan 2021 13:06:23 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=HEzjlMV5; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=amsat.org Received: from localhost ([::1]:51782 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kwAZy-00043c-RC for patch@linaro.org; Sun, 03 Jan 2021 16:06:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:37770) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kwAM7-0003qm-GC for qemu-devel@nongnu.org; Sun, 03 Jan 2021 15:52:03 -0500 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]:54898) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kwAM6-0006Av-0k for qemu-devel@nongnu.org; Sun, 03 Jan 2021 15:52:03 -0500 Received: by mail-wm1-x334.google.com with SMTP id c133so15945104wme.4 for ; Sun, 03 Jan 2021 12:52:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ONmR7DvST/KHwvIMlpQESaWh+hvboxmRkL03nu+T/uM=; b=HEzjlMV5Y+TSG3Oz+s7DHMSoie7zxoE+rmiD6vVzX8Sjr/g2KwEUVjmXFLtt3JWG5Y H/HCUC7sRl7fkS05Rpt/aYf5sWridbt/Y3Ddqfln5eYvVTLYDfh2jGAi5LwH2nt53thq 6LfSkwIKUC5Ze1sSQ63zY9+9+fXlBwlX2l/123ad1Vy9mWW1xdnHR6CG2o0AIoAxVHNN r44p7HuA+UQnIEw1qtwhjeIFziqoko/2w8S9cDXQAhOvqLgTiUHtbvRDLMZx4DckrQYF LqPL/ljscRyBcibzVx20bGEkDLj4GQ82RPJU6kWZj6Ad7y9dhn4/7KEQ0ksGf9/ri7Hn gAIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=ONmR7DvST/KHwvIMlpQESaWh+hvboxmRkL03nu+T/uM=; b=PwhOSOzqmB/S7H9eJffp//NiEANf/mJCGJ/3Jv4XErnsY43TS0HYluoNLXIS3fk+5H Bx55xDcPKYeK2Q1F3xht5RiE/P/xfNC+uHQG9MRxVcqaMKh27ejDcaPXFDmFC91U66ob 4+eSNmPN4ATbysBLUUOq9sg+WWrrVAeUdHh3DsoXCkQukP6E103v6Q3nlbil9Eko8gJM 1FBBqz84abnnqaKhyl9DpyeEXyd70RfVthcwwO0lX+tiaVkRIHpIpIclAugBC9iEH1Sn 1ldBz1/P4/af9xzxZF2o3+uUDzGqBOPVhvrvfkJxxOXi9QPkGPuBYOlgrhKudAwGcVoB qEoQ== X-Gm-Message-State: AOAM533txzc0uv3yjOtSxYoVoVW1uHid7kRYa++MROtpJT6jWNdTb7ey ZSxLXoo0t1RlcdhNHiyIPpTQ7Dbuoc0= X-Received: by 2002:a1c:9949:: with SMTP id b70mr23702785wme.72.1609707120644; Sun, 03 Jan 2021 12:52:00 -0800 (PST) Received: from localhost.localdomain (194.red-83-57-172.dynamicip.rima-tde.net. [83.57.172.194]) by smtp.gmail.com with ESMTPSA id r1sm90034783wrl.95.2021.01.03.12.51.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Jan 2021 12:52:00 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Subject: [PULL 20/35] target/mips: Don't use clock_get_ns() in clock period calculation Date: Sun, 3 Jan 2021 21:50:06 +0100 Message-Id: <20210103205021.2837760-21-f4bug@amsat.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210103205021.2837760-1-f4bug@amsat.org> References: <20210103205021.2837760-1-f4bug@amsat.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-wm1-x334.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.248, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: Peter Maydell , Luc Michel , Huacai Chen , Richard Henderson , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Aurelien Jarno Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Peter Maydell 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 Tested-by: Philippe Mathieu-Daudé Reviewed-by: Richard Henderson Reviewed-by: Luc Michel Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20201215150929.30311-3-peter.maydell@linaro.org> Signed-off-by: Philippe Mathieu-Daudé --- target/mips/cpu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) -- 2.26.2 diff --git a/target/mips/cpu.c b/target/mips/cpu.c index b2cd69ff7f9..2283214c879 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 = &cpu->env; - env->cp0_count_ns = cpu->cp0_count_rate - * clock_get_ns(MIPS_CPU(cpu)->clock); + env->cp0_count_ns = clock_ticks_to_ns(MIPS_CPU(cpu)->clock, + cpu->cp0_count_rate); assert(env->cp0_count_ns); } From patchwork Sun Jan 3 20:50:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 356157 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp14472841jai; Sun, 3 Jan 2021 13:08:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJybt3tEw1xUg/MF3yQX0wbukpdl5zBqWZjP/4zJxqi7VdLUzl0vxaJoeqg3Bao+ja+Ns5GI X-Received: by 2002:a25:e443:: with SMTP id b64mr95717847ybh.89.1609708104239; Sun, 03 Jan 2021 13:08:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609708104; cv=none; d=google.com; s=arc-20160816; b=kCpmLCLZqIkpj88fF5UNgwR/K2d7/BLajiu6dEAkR+pIj6xvU+z9z2HIfNH4EiP3NG dU2+uEmHJ7R6Mv1moi2ZJpnXPPyoTy5fwt4oAZGxNgrg5TlH6dYizc4cxjEXv1XMDdHC Le/B1QhCk6GRAm+sjvDt7N22F3KpV7cEVd0Niw/WpWe/1m4Ol+bkCYT70xn1KD/bjIqY Wxqip/7NrW53qkk0kWptyuxGWUFeYUl9nQkNSqFPGXuUfz4kKeKliBZeyoP/Ok/zBEm1 L7vo6YvhCQZ6OFaoSrgMYGI1ZoWMRAgfkfFK5ihzfJy+S8Vep6GstfygnbCev2ilOe9T DY3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=DdsNLET2BpxdZf8VQjwnKxo9S8h71JuQHMRBO6qs7tI=; b=us0gY6qV6L87eFzVWZGGXZkuDn9MS5RUvwzT3IRWsRvyn0N7lxfwZ3xRGj+Il3nA/u 8f92oFWvGtchBAwvqandm7YOQxBxNZCxAiENpzdwTw+gkDTqOag/m6f2ckCUPmm4IUp1 S4d9ZVNDHxxWLVZL7fhy5Ru7TgNwBX6HjhlUnP4p061zNGbYMMgtE7umKiZ6l/VCPf5V Y4n9rJ7Khm92k9jRnK5LyPFK4VvTHdOSbl1cO99x/pq1gXvjQvPpzLiEnL0IMdmRd3V7 BskE74PwKpXpJEd7zsCV8ZTLLf0pnHATvbVFmcejxwLeit1CZKBH5R8fnPbZ4cnf9qqx RHbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=rBSEI6xm; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=amsat.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id p13si53821156ybl.262.2021.01.03.13.08.24 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 03 Jan 2021 13:08:24 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=rBSEI6xm; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=amsat.org Received: from localhost ([::1]:57426 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kwAbv-0006QF-LG for patch@linaro.org; Sun, 03 Jan 2021 16:08:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:37798) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kwAMC-000449-I7 for qemu-devel@nongnu.org; Sun, 03 Jan 2021 15:52:08 -0500 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]:42578) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kwAMA-0006By-Ty for qemu-devel@nongnu.org; Sun, 03 Jan 2021 15:52:08 -0500 Received: by mail-wr1-x429.google.com with SMTP id m5so29428791wrx.9 for ; Sun, 03 Jan 2021 12:52:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DdsNLET2BpxdZf8VQjwnKxo9S8h71JuQHMRBO6qs7tI=; b=rBSEI6xmzmEpa53fx2l9KF0RZUTeHaXoiSr1fzEnbG+9ZgJe3fOihKUjOjqA9OdJ5j +lAvPncHXgRryST94UdsBHD5f+10TgjJlP0mU+KPwQg8J3w8M+f19sym51AHhU+yla3A ZIad6XT26v5k+dtoqBth2UKRhjBCupZkhikCaTRbD6BV8caBiy9nXDWtQVTrAWqMahr3 WH6YKZV/Puum4R5yo31Ezv7XShU+Lc/HCjMmonhok9SFIef91KzmQ5wF4Sqa1UJEDsf0 BPYi4XxzUa4EpohXLrUhDRBdHnEgZCF3wZkH73Od0xrqKziLUsjrU7Q7E5aqu4oMQv7N ZE4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=DdsNLET2BpxdZf8VQjwnKxo9S8h71JuQHMRBO6qs7tI=; b=dfXisdwxDvXC/18Kgltqg4HBN6rXfimtliYqGGNDere9kPBG5FKV1eDYa1d9qdrN+c 6yd8XHiSXYWQP4u2nCnJ6zsmXPfRMN3LDfbm2IB1QwmuTEw9fFWJcHe0yyjyVdh41GEQ M3+NAOJj2Kpv+aca+45HONNHUKFfbNj38XzwW3/nUnmmjxhfad9TVBV5fPdBQbKb+Bpj kOZrCc34+mGIO0S5EZNL3ICSvwZL2o/g0fM08RLJn2zRX9HKL9/ytFeKdT6TD/JmKNAU Pk7nsnQhucg1mD3Sorxmd7MNeR3xKl66o4gM7WnFwiw6xbYhbDNVP2GbjxF834/NuOdp NEdA== X-Gm-Message-State: AOAM531cn9ocLGu7ii6BNwb3hwujKf74UtfkRagTamZ6GpHlI51NojLk O6bTa6S3ryzd0dwxpftbicCm86H3bEE= X-Received: by 2002:a5d:528a:: with SMTP id c10mr69994916wrv.117.1609707125359; Sun, 03 Jan 2021 12:52:05 -0800 (PST) Received: from localhost.localdomain (194.red-83-57-172.dynamicip.rima-tde.net. [83.57.172.194]) by smtp.gmail.com with ESMTPSA id r1sm90035030wrl.95.2021.01.03.12.52.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Jan 2021 12:52:04 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Subject: [PULL 21/35] clock: Remove clock_get_ns() Date: Sun, 3 Jan 2021 21:50:07 +0100 Message-Id: <20210103205021.2837760-22-f4bug@amsat.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210103205021.2837760-1-f4bug@amsat.org> References: <20210103205021.2837760-1-f4bug@amsat.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-wr1-x429.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.248, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: Peter Maydell , Luc Michel , Huacai Chen , Richard Henderson , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Aurelien Jarno Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Peter Maydell 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 Tested-by: Philippe Mathieu-Daudé Reviewed-by: Richard Henderson Reviewed-by: Luc Michel Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20201215150929.30311-4-peter.maydell@linaro.org> Signed-off-by: Philippe Mathieu-Daudé --- docs/devel/clocks.rst | 17 +++++++++++++---- include/hw/clock.h | 6 ------ 2 files changed, 13 insertions(+), 10 deletions(-) -- 2.26.2 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 ------------------------------- -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. It is also possible to register a callback on clock frequency changes. Here is an example: @@ -254,8 +263,8 @@ Here is an example: */ /* 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)); } 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 / 1000000000llu)) -#define CLOCK_PERIOD_TO_NS(per) ((per) / (CLOCK_PERIOD_1SEC / 1000000000llu)) #define CLOCK_PERIOD_FROM_HZ(hz) (((hz) != 0) ? CLOCK_PERIOD_1SEC / (hz) : 0u) #define CLOCK_PERIOD_TO_HZ(per) (((per) != 0) ? CLOCK_PERIOD_1SEC / (per) : 0u) @@ -215,11 +214,6 @@ static inline unsigned clock_get_hz(Clock *clk) return CLOCK_PERIOD_TO_HZ(clock_get(clk)); } -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 From patchwork Sun Jan 3 20:50:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 356158 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp14472925jai; Sun, 3 Jan 2021 13:08:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJwvkAxn1q+bERRidx/Pq3S50S0J+oR9OhXP3r9TCOh88XDxR4guSa3GnabFZqyuz5AarqJp X-Received: by 2002:a05:6902:728:: with SMTP id l8mr101198434ybt.399.1609708116669; Sun, 03 Jan 2021 13:08:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609708116; cv=none; d=google.com; s=arc-20160816; b=uIwjdj07HvBw2Bbrx7oRp7wVOdkEtmwjR/cNdpptUIKe8KviX2pamPHUulpQjwwxux x9MbgyagH+nR2IFphZpGz8H38D6mAXTJO/z8UEGPncxZJDhI/TExphS0iJ4gsJlvlCos CcYNTIw4Pxejw0km489gdC+PZjXAxoQXD9brcGUPnfcnzNOGNMTuevrd3DQqhRnM1IC8 FNb0i8jowbjutwUZB6xuVQXTJ/FbeDoKjwMkmKn3PBtHjUCZE9/NF6reRYBJN3FdCyIV 6yiNjZU+KsjXwy92pfR0vflQrz/OpDlAqCn0nT1ZM7PTTdJrerky/TWmotYGcneU/QWJ hV1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=S5HsQlYCUdheC9WapV12I7REW2HWT9Eel8KOz7tCI0c=; b=DwvVQlKgASHgxfhnn7UA62ulb/MnYovyTt+mFWS0iLfgyKcN78QZKDu+wdgNQvWpeJ EOGbM0waSyOT78evAHFYJ1KA73a28UPxm84RyhGReQwRXFbr+BS+WD1kdm6AttUq4lTp BlyrcGYXdXRJaZCGWG8COdr+q5xrTqO6n8SumTG0APf7dE4EYNnCmL5EJPOOOn+kGQ0r FtRBO4zaNBbXXtKusVkYShfbJrZyGLzpndVOSdBC2IPOlAwA79CC9bnIaWBhpG9SZjvn WXL8PPioTQRLuPhzZUIBaXiRdzh2pr0bG1RVCK9rKU4RCZrV8f99f33DgFAVlZNG+KGa JngA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Q9sViTLF; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=amsat.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id f200si47221724yba.49.2021.01.03.13.08.36 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 03 Jan 2021 13:08:36 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Q9sViTLF; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=amsat.org Received: from localhost ([::1]:58160 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kwAc8-0006jN-5R for patch@linaro.org; Sun, 03 Jan 2021 16:08:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:37810) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kwAMH-00048o-6R for qemu-devel@nongnu.org; Sun, 03 Jan 2021 15:52:13 -0500 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]:35653) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kwAMF-0006D6-M5 for qemu-devel@nongnu.org; Sun, 03 Jan 2021 15:52:12 -0500 Received: by mail-wm1-x32f.google.com with SMTP id e25so16678418wme.0 for ; Sun, 03 Jan 2021 12:52:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S5HsQlYCUdheC9WapV12I7REW2HWT9Eel8KOz7tCI0c=; b=Q9sViTLFYRhYMAAbN8Cn4mDn+uY5yzyxxGNl/LjYddodG4eLWGGsu6tEaaiHgaUxKV CaAWQHJPTbzzWRaNZsOBCZ1ERF7txS8dedr3q0d4RZwVLOZiVYR6MEDtdUMmIn4zAJGB eRWKuFGGIzTzKUt768P2SjYrI861/L7rr4tbzzz4Po1YYCANHwi2v210K52gHoVHWSkp bJIBOjLOrVJCCiOS8SaIVVJase97JURDngN6bF5faaj20E6Ks2eDB3KTuQpNIwBj1ksk rKEho9y4hZfE90rQwuV42/9n++/dGQ+Ae96JxiGw1856HiXjPbS1qTAwJ8FsjsOHGrHL 9ADg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=S5HsQlYCUdheC9WapV12I7REW2HWT9Eel8KOz7tCI0c=; b=sAe4NT1TSeQQPk2T1wpy2IkkP3uVek/pm+9ilaVPwtWcDP7NpNlmfoAZxtDAC90T06 GdN8Y2axFEllERwpnlxtwzmLoDQIrxSkOzWC3gJKYAq890kdxj/A3VRME2T+0Ih+p//W CHStK3nRGXdJKdER+UlMgdeOqDQJisCBKBJyF4RH/Mv3V+dFeWYkZMmH8mX1EsovqTfh VxN97t51mlRLj8QTW7sQHCc/8Ec8jsi4RYsKAx2n9Uk1ob05VNq1F+pWrYbFqhjwwOyj Cx8HwYpqJ9Kf/QOJLuJiNxpY1bW+LLcVehXKV5ouR+Yi5CUdKU/uAVBccXqFKvRBv2VB 624Q== X-Gm-Message-State: AOAM531hVh4kWf0Dsq3oege29vlhyhwMbCRQ5hIWHpA7mFF8zJpoQbiJ JyRR/aoA6WEkRuBwpgGxoSnla/HwRJc= X-Received: by 2002:a1c:2b05:: with SMTP id r5mr23732194wmr.179.1609707130251; Sun, 03 Jan 2021 12:52:10 -0800 (PST) Received: from localhost.localdomain (194.red-83-57-172.dynamicip.rima-tde.net. [83.57.172.194]) by smtp.gmail.com with ESMTPSA id g5sm85018363wro.60.2021.01.03.12.52.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Jan 2021 12:52:09 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Subject: [PULL 22/35] clock: Define and use new clock_display_freq() Date: Sun, 3 Jan 2021 21:50:08 +0100 Message-Id: <20210103205021.2837760-23-f4bug@amsat.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210103205021.2837760-1-f4bug@amsat.org> References: <20210103205021.2837760-1-f4bug@amsat.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32f; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-wm1-x32f.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.248, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: Peter Maydell , Luc Michel , Huacai Chen , Richard Henderson , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Aurelien Jarno Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Peter Maydell 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=3.333333e+07 mmio 00000000f8000000/0000000000001000 After: dev: xilinx,zynq_slcr, id "" clock-in "ps_clk" freq_hz=33.3 MHz mmio 00000000f8000000/0000000000001000 Signed-off-by: Peter Maydell Tested-by: Philippe Mathieu-Daudé Reviewed-by: Richard Henderson Reviewed-by: Luc Michel Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20201215150929.30311-5-peter.maydell@linaro.org> Signed-off-by: Philippe Mathieu-Daudé --- 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(-) -- 2.26.2 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)); } +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 ---------------------------- 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) != 0; } +/** + * clock_display_freq: return human-readable representation of clock frequency + * @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 @@ */ #include "qemu/osdep.h" +#include "qemu/cutils.h" #include "hw/clock.h" #include "trace.h" @@ -111,6 +112,11 @@ static void clock_disconnect(Clock *clk) QLIST_REMOVE(clk, sibling); } +char *clock_display_freq(Clock *clk) +{ + return freq_to_str(clock_get_hz(clk)); +} + static void clock_initfn(Object *obj) { Clock *clk = CLOCK(obj); diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c index 2c57e36c9ae..8dc656becca 100644 --- a/softmmu/qdev-monitor.c +++ b/softmmu/qdev-monitor.c @@ -736,11 +736,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=%e\n", + g_autofree char *freq_str = clock_display_freq(ncl->clock); + qdev_printf("clock-%s%s \"%s\" freq_hz=%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 = object_get_class(OBJECT(dev)); do {