From patchwork Thu Jun 20 15:22:14 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: 805981 Delivered-To: patch@linaro.org Received: by 2002:a5d:508d:0:b0:362:4979:7f74 with SMTP id a13csp345737wrt; Thu, 20 Jun 2024 08:23:34 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXJK99TY6TANiMJJ4iMrBmiL36+grpPB5+Vt4BJ8fie/k3T7vqAQIj7N0k7Xo/gLMxy2mZ8KKRrGFIyI1nZm2Lt X-Google-Smtp-Source: AGHT+IHYv37yaZYZYZeTozPEK3EkpUjRbjT/N9Z138PCVonx4XyNqvkQEcuTT5+y8Z7jl+5nkHIP X-Received: by 2002:ac8:5ac5:0:b0:444:9b62:7304 with SMTP id d75a77b69052e-444a79a9cb0mr73242521cf.11.1718897014611; Thu, 20 Jun 2024 08:23:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718897014; cv=none; d=google.com; s=arc-20160816; b=FFRfXOI+l4qIsCEGuCSZRRHqz8Y39+NRRd3f7354SXoeO3jqodjQb7A1vmhBgFYg4z 2VWVsXtYBu1UU2ca4nEHY3hhWuNJ1tG4kyHjekFUfSve8ZwwXRYoc+H354zwajAkUXup aV2wYkhyDmSFp4i/XjbkPv5mXCfRYyn9kE+EOyiQau0otyBfwvtqTFKla4lxEOyJivBR ZhFDTE8hfPUkrINkP0bI4cJV5P1mkkAB5LiqdFTIfSdYg7a1Vd3cCTtUgN5wOdw/jZAq FAH89/4OgfgLKnsvMgjpjjQ71YSKR4izHL5ns0SW3f+an9yau+533EY6QomLnOi1Rili xPTg== 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=oUsmThce/MAXtT1TzjuV9l3/1dXmfT3EJ/fU+1YA9l4=; fh=qYUyA+SSE+X3fQR5X1u+SwltJ/ArWqMeAeFp+rGIm4A=; b=zVm+IevnFDxwFLmRL+Or4+9W3JgSkniwWnUmCLa0BDMtf4dMNnvUjWJZJHRscDdG+1 uwVn2YWPrAs342NtxOO6RNZ7bFYgMJl0d8+Qme8Q6nP9ceqMkOy/oD3thiPYjwzfBsFg AGJEbkWBKFOtQt+y724C10IJ2a973e5Sdla60qsl5UMVnjUgWgDJRC4qRrGGPxNvxAY3 d1oU9se+wX/iQnvkvNkbMA1yW7kInhpJRniQy9B3DL2ZR4ddHzrU3b0gKtyYXfiN1dzy Ff/h1/y3stWmAW1gCfDh56cHhvhyR8hgqVgQ1xUqfQSBoGyflyP+LSvj9yRgm+f3HASw lHyA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RFAuZ3eW; 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-441f2ea8a74si167398661cf.327.2024.06.20.08.23.34 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 20 Jun 2024 08:23:34 -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=RFAuZ3eW; 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 1sKJcU-0005uJ-Kg; Thu, 20 Jun 2024 11:22:38 -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 1sKJcS-0005s6-Ae for qemu-devel@nongnu.org; Thu, 20 Jun 2024 11:22:36 -0400 Received: from mail-ed1-x530.google.com ([2a00:1450:4864:20::530]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sKJcP-0007yi-It for qemu-devel@nongnu.org; Thu, 20 Jun 2024 11:22:36 -0400 Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-57d1679ee6eso2487694a12.1 for ; Thu, 20 Jun 2024 08:22:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718896951; x=1719501751; 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=oUsmThce/MAXtT1TzjuV9l3/1dXmfT3EJ/fU+1YA9l4=; b=RFAuZ3eWKqjn4fiw2U7sdeS3BZEAGFXH83pDLo1prJFt9wgbTctkCDRgkzQhXbOf8i 8R+SrFIk433e7IlZDaQaxFjJJPD8wFMEvysdggm/PdpNZgLle+T/Qv/nCs9yyeXUiHCr jrZF/mWrUyvRmgdaHtebHOixXWGCqyCSuU0Uk3sCEe4AMVWLwIkkBO5BwW9OUC+eKJon N8WQg5S5uRNsi17dtOLmBmlfJRaIdXiXmq7iLaOe9jftcUzjalinMEHwxWC+TrnGOMDH g5QbHnqyG68XkRm7KflCBxHTtaSQz4jof/GGIuwIxNmvn23y72WhV31yCHukjdwrTok/ DO0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718896951; x=1719501751; 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=oUsmThce/MAXtT1TzjuV9l3/1dXmfT3EJ/fU+1YA9l4=; b=QdZ0KoHykGWeXamgYxBNM429xKyDFqeTxXmICOr8SBMQD2tYI7/UdqT7qv/6oEoIUe pN0kn+uOF16LODltJ+3HC2DJqf3ZVR9CqG8qF3XFNiIAjofAJTZWbe8zBmWNwi5gC6ic VNTScnEVToj4m9G16DVZZZYNDqZ/jq5KJTcSZ/XMs3Yb8LxYzN17d02B/1j2JQXlWxys D0QCq/rYbRGKJ1vUbaDYmGHTgFDtHMETpsSjbyNSjq96u/ts27PiEB6WuxBjQO3pTG1o 49z2CkSIWJO7ttNkRRkCulcsc8Kx41EheX95mdvFzxf0C8DsfdIkh3t0GVi2AEsXtZ3t IFnw== X-Gm-Message-State: AOJu0YxZoUJYpPlxfC+1+bbv4AdGG2U6x3zo2DONLf4fJLNPOSSRvUfE +7am9T5wvtU/QiUZFJOsIn1i2LG1k3JommtLB3uE4EAx9btZ7HAJBeGHoqqlfnk= X-Received: by 2002:a17:907:d042:b0:a6f:9550:c0ee with SMTP id a640c23a62f3a-a6fa430f3f1mr452004166b.18.1718896950979; Thu, 20 Jun 2024 08:22:30 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a6f56f416dfsm775943366b.164.2024.06.20.08.22.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jun 2024 08:22:28 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id 9CFFA5FA01; Thu, 20 Jun 2024 16:22:21 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Peter Maydell , kvm@vger.kernel.org, qemu-ppc@nongnu.org, Christian Borntraeger , Paolo Bonzini , Jamie Iles , David Hildenbrand , =?utf-8?q?Alex_Benn=C3=A9e?= , Pierrick Bouvier , Mark Burton , Daniel Henrique Barboza , qemu-arm@nongnu.org, Laurent Vivier , Alexander Graf , Ilya Leoshkevich , Richard Henderson , Marco Liebel , Halil Pasic , Thomas Huth , qemu-s390x@nongnu.org, Cameron Esfahani , Alexandre Iooss , Nicholas Piggin , Roman Bolshakov , "Dr. David Alan Gilbert" , Marcelo Tosatti , Mahmoud Mandour , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v2 06/12] sysemu: generalise qtest_warp_clock as qemu_clock_advance_virtual_time Date: Thu, 20 Jun 2024 16:22:14 +0100 Message-Id: <20240620152220.2192768-7-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240620152220.2192768-1-alex.bennee@linaro.org> References: <20240620152220.2192768-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::530; envelope-from=alex.bennee@linaro.org; helo=mail-ed1-x530.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, T_SCC_BODY_TEXT_LINE=-0.01 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: <20240530220610.1245424-4-pierrick.bouvier@linaro.org> --- v2 - use target_ns in docs and signature --- include/qemu/timer.h | 15 +++++++++++++++ system/qtest.c | 25 +++---------------------- util/qemu-timer.c | 26 ++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 22 deletions(-) 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; +}