From patchwork Mon Jun 24 10:18:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 807015 Delivered-To: patch@linaro.org Received: by 2002:a5d:508d:0:b0:362:4979:7f74 with SMTP id a13csp1811178wrt; Mon, 24 Jun 2024 03:20:40 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVtQTxa2iZ/Sq7EltdJ+RlBdnZDVRSUBFghoPpb4/EtMCaFP5kSgM7LV/ZWBW1mItTmpncHyigK2+ToMQgfloLF X-Google-Smtp-Source: AGHT+IEk67P3XhVoYcDT6u+cP7TlkcoNDHp3Ma/QbrCQKIvlARp8Fs8IJi9AurjeXYADMYNwQ6Vy X-Received: by 2002:ac8:5784:0:b0:441:1f35:404f with SMTP id d75a77b69052e-444d9216fa9mr50265811cf.44.1719224440481; Mon, 24 Jun 2024 03:20:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1719224440; cv=none; d=google.com; s=arc-20160816; b=V6z+V4Nyiu10rdxdeN9hD0XRSQfwpeWKihpx66hMVnbyGOAy5NiJV+GVanxbWBNr2U SKlHZaI2demjRYzJYVA7zhpKMggJO8MaPa5fxzv8mE2nWJC5moTKCzxUdnuRPVr2PGP1 ZwLyUoUZw/UOc40ye3pKKY+dzN5YLrdCub9MDlXxceGX2CUHxzaQyPzGCmGfkP2/l1MR q2r53j2Ba6yVu0j+XWvecF9mhhiboFOwawlCTzr5jGtBmrmrstryBczgto+N2fYzfPmq BewG0HfmTnwzO1UGuK0Oz7jrRZnilp8lPGkG/SQgBBMEqqYABq8abilNutJEquAgzYka c2zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to :from:dkim-signature; bh=ZgJtc3lx/g2nwJr+x/MfCvRRoPneDciPWhATdKfcPaw=; fh=FsSelxnBxrcg3TiUMIBxM5FQURdmmznA0Ss+6SzJyZ8=; b=L6485OHrlkid48waO7kNY6a4aaGTMmyZmDxJq0/fhxZluvS0NcMh4i9MT07ukV/Aft vRJXdquzgaKkTmtxhs6LzK9fUYxorfuNFb6s1H0ixXQqZ+Gxg10lniXU1RNhccFgKkiX Z2nZ9yn66/kpFxtrc76RcrXwMYUnXW6sNjpTeuerwW6PO1V2Mp6M9+saspnjPBAw0u6i hgeSBUUwgdDiJB4SSOO7Gor2q+al0kkaKxcb8rbkhxnmsM7s3SxQNV2SBCv/WP8payPW nOr2TlAao7Jsht03WvXOeOyaeEgmJtyxKaovexewYFns4YDEZtGS09TD0s0yLQAV/KG/ ZKNw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=hbGu1h3w; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d75a77b69052e-444c2c62326si78485701cf.560.2024.06.24.03.20.40 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 24 Jun 2024 03:20:40 -0700 (PDT) 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=pass header.i=@linaro.org header.s=google header.b=hbGu1h3w; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sLgmj-0006pq-7r; Mon, 24 Jun 2024 06:18:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sLgmf-0006nU-Bk for qemu-devel@nongnu.org; Mon, 24 Jun 2024 06:18:49 -0400 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sLgma-0007gn-9X for qemu-devel@nongnu.org; Mon, 24 Jun 2024 06:18:49 -0400 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2ec50a5e230so26750021fa.0 for ; Mon, 24 Jun 2024 03:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1719224321; x=1719829121; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZgJtc3lx/g2nwJr+x/MfCvRRoPneDciPWhATdKfcPaw=; b=hbGu1h3w7vCxvSdAXv28enYgsBs5s1JP09QVqCYbcZhNoxeui6JDl42CWbIF34q5/A XsLK0puK1qbT52QcV+Ope9bXb3wWBQB9E5Vzn5QzukF6zO4NLRj/ffgS+beQCMTGwCCB zldGtRsESLO98t32s+g28okLvjFRSkQYPzkp7iTMje7WOzZ4Ec8h/vpY1YrraIDEuGiH 2VyBEII4vOe6robLQLwQOGXxc9C2VXTsvc8panlr7tgXv4pIq1fwHXfujRYKm7SBg826 gpAe0VTHzgUdPETDbby4BYF3sZ0qcyS5rhEPZmioWUz9bOW9WPSjA+SgHF2oi0DTVkRr ZTWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719224321; x=1719829121; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZgJtc3lx/g2nwJr+x/MfCvRRoPneDciPWhATdKfcPaw=; b=LWfKiwVb2grFbrLOtYYTFGbWz2g0Txm/Bc3vwueF9SjYSB9OltGos7TwZGhSvIiWEX dtQz7UNIlodPQwxDfROziZOR5p7/IbpkhnIDeVJi5lBAUrhP+iiID7ZyiQ2oTG1Zj/CV R0/X5lFOK/3J1h7Pg/csOu7kSsnBOFZml78LmbU5ekZrueoStbXu/8g3/kiylb8Ij1ta JQQ+/XIQTJyAcyAJEEUck7POhg/2WxX7aB7HataPaz+8M5OnT0CM+s1UKcaU2FSS0SkE CjqkJFLegvUKjorm15qRVq4iocy1LdW5DZeKLXu5HdE+rPtUMRnOHrtgAnc8fqJz0E4Y lRjQ== X-Gm-Message-State: AOJu0Yx2HLT0VGfWsxCpQPie1W6/aVh+vOnkq0rC/Tww1ibwTcJDrGyY 4mGcx2VAVdLdAk4GoOeCWlvb1DkZse7DpKcZ+riAbkflYZs75IiA6XhAyclERg8= X-Received: by 2002:a2e:b0f5:0:b0:2ec:4086:ea6d with SMTP id 38308e7fff4ca-2ec5b2695d9mr26484241fa.4.1719224321326; Mon, 24 Jun 2024 03:18:41 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57d3042d8b1sm4542750a12.45.2024.06.24.03.18.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jun 2024 03:18:38 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id E3F485FA28; Mon, 24 Jun 2024 11:18:36 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= , Pierrick Bouvier , Thomas Huth , Laurent Vivier , Paolo Bonzini Subject: [PULL 06/12] sysemu: generalise qtest_warp_clock as qemu_clock_advance_virtual_time Date: Mon, 24 Jun 2024 11:18:30 +0100 Message-Id: <20240624101836.193761-7-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240624101836.193761-1-alex.bennee@linaro.org> References: <20240624101836.193761-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::232; envelope-from=alex.bennee@linaro.org; helo=mail-lj1-x232.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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Move the key functionality of moving time forward into the clock sub-system itself. This will allow us to plumb in time control into plugins. Signed-off-by: Pierrick Bouvier Signed-off-by: Alex Bennée Message-Id: <20240620152220.2192768-7-alex.bennee@linaro.org> diff --git a/include/qemu/timer.h b/include/qemu/timer.h index 9a366e551f..5ce83c7911 100644 --- a/include/qemu/timer.h +++ b/include/qemu/timer.h @@ -245,6 +245,21 @@ bool qemu_clock_run_timers(QEMUClockType type); */ bool qemu_clock_run_all_timers(void); +/** + * qemu_clock_advance_virtual_time(): advance the virtual time tick + * @target_ns: target time in nanoseconds + * + * This function is used where the control of the flow of time has + * been delegated to outside the clock subsystem (be it qtest, icount + * or some other external source). You can ask the clock system to + * return @early at the first expired timer. + * + * Time can only move forward, attempts to reverse time would lead to + * an error. + * + * Returns: new virtual time. + */ +int64_t qemu_clock_advance_virtual_time(int64_t target_ns); /* * QEMUTimerList diff --git a/system/qtest.c b/system/qtest.c index 5be66b0140..8cb98966b4 100644 --- a/system/qtest.c +++ b/system/qtest.c @@ -337,26 +337,6 @@ void qtest_set_virtual_clock(int64_t count) qatomic_set_i64(&qtest_clock_counter, count); } -static void qtest_clock_warp(int64_t dest) -{ - int64_t clock = cpus_get_virtual_clock(); - AioContext *aio_context; - assert(qtest_enabled()); - aio_context = qemu_get_aio_context(); - while (clock < dest) { - int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, - QEMU_TIMER_ATTR_ALL); - int64_t warp = qemu_soonest_timeout(dest - clock, deadline); - - cpus_set_virtual_clock(cpus_get_virtual_clock() + warp); - - qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); - timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]); - clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - } - qemu_clock_notify(QEMU_CLOCK_VIRTUAL); -} - static bool (*process_command_cb)(CharBackend *chr, gchar **words); void qtest_set_command_cb(bool (*pc_cb)(CharBackend *chr, gchar **words)) @@ -751,7 +731,8 @@ static void qtest_process_command(CharBackend *chr, gchar **words) ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, QEMU_TIMER_ATTR_ALL); } - qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns); + qemu_clock_advance_virtual_time( + qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns); qtest_send_prefix(chr); qtest_sendf(chr, "OK %"PRIi64"\n", (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)); @@ -777,7 +758,7 @@ static void qtest_process_command(CharBackend *chr, gchar **words) g_assert(words[1]); ret = qemu_strtoi64(words[1], NULL, 0, &ns); g_assert(ret == 0); - qtest_clock_warp(ns); + qemu_clock_advance_virtual_time(ns); qtest_send_prefix(chr); qtest_sendf(chr, "OK %"PRIi64"\n", (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)); diff --git a/util/qemu-timer.c b/util/qemu-timer.c index 6a0de33dd2..213114be68 100644 --- a/util/qemu-timer.c +++ b/util/qemu-timer.c @@ -645,6 +645,11 @@ int64_t qemu_clock_get_ns(QEMUClockType type) } } +static void qemu_virtual_clock_set_ns(int64_t time) +{ + return cpus_set_virtual_clock(time); +} + void init_clocks(QEMUTimerListNotifyCB *notify_cb) { QEMUClockType type; @@ -675,3 +680,24 @@ bool qemu_clock_run_all_timers(void) return progress; } + +int64_t qemu_clock_advance_virtual_time(int64_t dest) +{ + int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + AioContext *aio_context; + aio_context = qemu_get_aio_context(); + while (clock < dest) { + int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, + QEMU_TIMER_ATTR_ALL); + int64_t warp = qemu_soonest_timeout(dest - clock, deadline); + + qemu_virtual_clock_set_ns(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + warp); + + qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); + timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]); + clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + } + qemu_clock_notify(QEMU_CLOCK_VIRTUAL); + + return clock; +}