From patchwork Thu May 16 22:20:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Pierrick Bouvier X-Patchwork-Id: 797267 Delivered-To: patch@linaro.org Received: by 2002:adf:e68e:0:b0:351:d90a:5487 with SMTP id r14csp500952wrm; Thu, 16 May 2024 15:21:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWUd8no27bb7G+UUjUnfIGDIbH/FRN05H43gzI715YPjIRR+RjyfY1j671Be90rjOXQpnlWGxF2V4YSdiElAwcU X-Google-Smtp-Source: AGHT+IEHE4YOJlJPTvWciWNzBB0o+RE5qDJ4/YhRDQCCnvyJqjUCfPxBs70pqpdyGHgiGq0Tfqbm X-Received: by 2002:a05:6808:1794:b0:3c9:6510:5908 with SMTP id 5614622812f47-3c99707221cmr26730352b6e.28.1715898112460; Thu, 16 May 2024 15:21:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1715898112; cv=none; d=google.com; s=arc-20160816; b=fCa4PzV/l69KxZEsFTPHnPHUiSHtVWcnMVS7IRPBV2ekM7IpIiKahTm86OadW7YTar 9rIXofiKjZkta7Yz6AC2NLYTeII/EwQawhxWqjA2f3lDZ8OspJVMvRg/0TbttIybF9p2 HO+G6813DptyMJBxmN+80WGcjOQpBjttvOMMda1crqcrHiCxDUFyX9T4MY8zeUbmwPol Il4rp14tS6Xu8w8b0t8QCKf2VVKizKSVkLinaiNgQtqYJwgAmapiqYmaOl1Q+NmHOwxc KQPwnXiIzQRCPQhVGsh/tLe2HYsMUai/ivG8lJiwlZiDWMcw5cOHqZmpJLLFR8vdDcIO /TeA== 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=Y0Mdqs4rLLstx/rEbobrQf1vWZp7tESBgee9yE2s3+g=; fh=g/MPAHpMYE0ua/lb96iWG5PoNHzHglLQcSfHbMEd7k0=; b=cj19Cf7cpz7jtloFxEszOS9KSC3Hhdp1gkgQsKyYZGd/mM6uXPMJUp2c9EIqCo1P7h scin6jqJXEqVzA5s7UdKEas6eQ8ZwDSZFYOsppOSNHO5njdIsSlqqQkL1ObceaUMT6F2 +LclqRbsJylDCIi71u/iKZPH0NBUaXvypiD3c0ovYpkpmIlqu+kutm9p4H+8ae1QCKze qxDo5fLB+4TAhsb5yp8b+/ZDaNHuqctWHCHuZ6c6zjF0WQKQ9Iqg/sk5cKqfQ72ddVky DQ+mUROyMDrwqns6CG0OjR2wBkEmFipEkvaA6Fofg3S7iat0atdTEjiC3untjzris15E 7C3w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=e4QLgFr0; 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 af79cd13be357-792bf33b561si1662141385a.477.2024.05.16.15.21.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 16 May 2024 15:21:52 -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=e4QLgFr0; 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 1s7jTS-0006iW-JV; Thu, 16 May 2024 18:21:19 -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 1s7jTK-0006bt-9D for qemu-devel@nongnu.org; Thu, 16 May 2024 18:21:13 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s7jTG-0003BS-8h for qemu-devel@nongnu.org; Thu, 16 May 2024 18:21:09 -0400 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-6f44bcbaae7so774079b3a.2 for ; Thu, 16 May 2024 15:21:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1715898063; x=1716502863; 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=Y0Mdqs4rLLstx/rEbobrQf1vWZp7tESBgee9yE2s3+g=; b=e4QLgFr0umY45MnXcK1dB//KIVNXr/4J54footaJ9HL2SOP0Mmr4YpYGaQwjjrgH3o FgEMPW0Yxl0FV74b0VjCfRIWypN0l2uuzbC9Y3h9jj3lET7MvsANdaZl+O4UnsX7+XZN KOctTQAnBl+7mi5OdVdDzywpb/TSIGCzmaigWVjj1H9Dt5N1XdWjzkU6kXr97EhvBxVT NoLavgyBtqgDMzu+ozLZJ0sZ17KZW2OHs5OdTexfeLOiTO54TfjEUKKIA0sO+P68zAq4 tIOWZEobqTH41xsdG0EwLSnWrhwnDiXMCM3uBGrOmRiO0C7v0jttvrCVXHN0A9L16+qo Nu2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715898063; x=1716502863; 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=Y0Mdqs4rLLstx/rEbobrQf1vWZp7tESBgee9yE2s3+g=; b=e+PKu4I/Fb6rlTH71ynUitL4UqQCK6bDXbwgj8kTQAbLsJweXhxmu0SY2Uw1lbLhet 66GGEzoAc8ibRtBhXRSrBaQ0X6qh4gHHxyytqm8nXMwTCj6ux5WWfPPcSnsc3SY6vbuK kjU+wwjGEcDoLz0PyZAna+0VVyclmfHngIbMs1CRHl55GTqJsEznfby/G6k+qPNBcXhB fYlDUhh3YkmevvSQiV4YpueceMM7iSPmouuKNkCuTJMBYgP+u0XJlrMxtudZgBAd97M/ rrUulmJ15ojqlEs60T8dZoGj+D9VIa8TbsH1lHIc3QUtajhkLbi9gGEjY3SopkcErWSH 7T0A== X-Gm-Message-State: AOJu0YyPXG6N9ZWR/L8dThA60R3KL292vMQ9tFhpvTWF7RhldjqUd3oc VKf66yc34j9UUzcoluwleOCrfTgLuFzwFKYQIfcWDh2/AiHobgiy9Nz8dn4Y6KRm9T8y8MsCHQM mb80= X-Received: by 2002:a05:6a00:3d07:b0:6ea:d740:62a4 with SMTP id d2e1a72fcca58-6f4e03466d2mr23816737b3a.25.1715898063609; Thu, 16 May 2024 15:21:03 -0700 (PDT) Received: from linaro.vn.shawcable.net ([2604:3d08:9384:1d00::e697]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66476sm13589219b3a.28.2024.05.16.15.21.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 15:21:02 -0700 (PDT) From: Pierrick Bouvier To: qemu-devel@nongnu.org Cc: Thomas Huth , Laurent Vivier , Mahmoud Mandour , =?utf-8?q?Alex_Benn=C3=A9e?= , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , Paolo Bonzini , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH 1/5] sysemu: add set_virtual_time to accel ops Date: Thu, 16 May 2024 15:20:43 -0700 Message-Id: <20240516222047.1853459-2-pierrick.bouvier@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240516222047.1853459-1-pierrick.bouvier@linaro.org> References: <20240516222047.1853459-1-pierrick.bouvier@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=pierrick.bouvier@linaro.org; helo=mail-pf1-x42e.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 From: Alex Bennée We are about to remove direct calls to individual accelerators for this information and will need a central point for plugins to hook into time changes. From: Alex Bennée Signed-off-by: Alex Bennée Reviewed-by: Philippe Mathieu-Daudé --- include/sysemu/accel-ops.h | 18 +++++++++++++++++- include/sysemu/cpu-timers.h | 3 ++- ...et-virtual-clock.c => cpus-virtual-clock.c} | 5 +++++ system/cpus.c | 11 +++++++++++ stubs/meson.build | 6 +++++- 5 files changed, 40 insertions(+), 3 deletions(-) rename stubs/{cpus-get-virtual-clock.c => cpus-virtual-clock.c} (68%) diff --git a/include/sysemu/accel-ops.h b/include/sysemu/accel-ops.h index ef91fc28bbd..a0886722305 100644 --- a/include/sysemu/accel-ops.h +++ b/include/sysemu/accel-ops.h @@ -20,7 +20,12 @@ typedef struct AccelOpsClass AccelOpsClass; DECLARE_CLASS_CHECKERS(AccelOpsClass, ACCEL_OPS, TYPE_ACCEL_OPS) -/* cpus.c operations interface */ +/** + * struct AccelOpsClass - accelerator interfaces + * + * This structure is used to abstract accelerator differences from the + * core CPU code. Not all have to be implemented. + */ struct AccelOpsClass { /*< private >*/ ObjectClass parent_class; @@ -44,7 +49,18 @@ struct AccelOpsClass { void (*handle_interrupt)(CPUState *cpu, int mask); + /** + * @get_virtual_clock: fetch virtual clock + * @set_virtual_clock: set virtual clock + * + * These allow the timer subsystem to defer to the accelerator to + * fetch time. The set function is needed if the accelerator wants + * to track the changes to time as the timer is warped through + * various timer events. + */ int64_t (*get_virtual_clock)(void); + void (*set_virtual_clock)(int64_t time); + int64_t (*get_elapsed_ticks)(void); /* gdbstub hooks */ diff --git a/include/sysemu/cpu-timers.h b/include/sysemu/cpu-timers.h index d86738a378d..7bfa960fbd6 100644 --- a/include/sysemu/cpu-timers.h +++ b/include/sysemu/cpu-timers.h @@ -96,8 +96,9 @@ int64_t cpu_get_clock(void); void qemu_timer_notify_cb(void *opaque, QEMUClockType type); -/* get the VIRTUAL clock and VM elapsed ticks via the cpus accel interface */ +/* get/set VIRTUAL clock and VM elapsed ticks via the cpus accel interface */ int64_t cpus_get_virtual_clock(void); +void cpus_set_virtual_clock(int64_t new_time); int64_t cpus_get_elapsed_ticks(void); #endif /* SYSEMU_CPU_TIMERS_H */ diff --git a/stubs/cpus-get-virtual-clock.c b/stubs/cpus-virtual-clock.c similarity index 68% rename from stubs/cpus-get-virtual-clock.c rename to stubs/cpus-virtual-clock.c index fd447d53f3c..af7c1a1d403 100644 --- a/stubs/cpus-get-virtual-clock.c +++ b/stubs/cpus-virtual-clock.c @@ -6,3 +6,8 @@ int64_t cpus_get_virtual_clock(void) { return cpu_get_clock(); } + +void cpus_set_virtual_clock(int64_t new_time) +{ + /* do nothing */ +} diff --git a/system/cpus.c b/system/cpus.c index 68d161d96b7..03ba026667c 100644 --- a/system/cpus.c +++ b/system/cpus.c @@ -229,6 +229,17 @@ int64_t cpus_get_virtual_clock(void) return cpu_get_clock(); } +/* + * Signal the new virtual time to the accelerator. This is only needed + * by accelerators that need to track the changes as we warp time. + */ +void cpus_set_virtual_clock(int64_t new_time) +{ + if (cpus_accel && cpus_accel->set_virtual_clock) { + cpus_accel->set_virtual_clock(new_time); + } +} + /* * return the time elapsed in VM between vm_start and vm_stop. Unless * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle diff --git a/stubs/meson.build b/stubs/meson.build index 3b9d42023cb..672213b7482 100644 --- a/stubs/meson.build +++ b/stubs/meson.build @@ -3,6 +3,11 @@ # below, so that it is clear who needs the stubbed functionality. stub_ss.add(files('cpu-get-clock.c')) +stub_ss.add(files('cpus-virtual-clock.c')) +stub_ss.add(files('qemu-timer-notify-cb.c')) +stub_ss.add(files('icount.c')) +stub_ss.add(files('dump.c')) +stub_ss.add(files('error-printf.c')) stub_ss.add(files('fdset.c')) stub_ss.add(files('iothread-lock.c')) stub_ss.add(files('is-daemonized.c')) @@ -28,7 +33,6 @@ endif if have_block or have_ga stub_ss.add(files('replay-tools.c')) # stubs for hooks in util/main-loop.c, util/async.c etc. - stub_ss.add(files('cpus-get-virtual-clock.c')) stub_ss.add(files('icount.c')) stub_ss.add(files('graph-lock.c')) if linux_io_uring.found() From patchwork Thu May 16 22:20:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Pierrick Bouvier X-Patchwork-Id: 797266 Delivered-To: patch@linaro.org Received: by 2002:adf:e68e:0:b0:351:d90a:5487 with SMTP id r14csp500951wrm; Thu, 16 May 2024 15:21:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWnUAUQUlGPeXXXVL0hqR9xU+PUfpBhL0nqK4dvy9fzu153WoejMYpBIQ56FjOC8LbA4TuZVgd5e+2NtjroE5fa X-Google-Smtp-Source: AGHT+IFfdRIwMCNaioQxgxa5VDldTKCJUxcZ/QJVMsG/ZViqBJtV1a9oyYuoxy6N2l71qsEY1+BI X-Received: by 2002:a05:620a:2a08:b0:793:c53:8ee5 with SMTP id af79cd13be357-7930c539303mr347238585a.26.1715898112295; Thu, 16 May 2024 15:21:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1715898112; cv=none; d=google.com; s=arc-20160816; b=x7xTOKVJT7kmaKkGKicpMlufB9LX9jorJle/+BZvseth/CODRY4HuE2Ay2d4PEHeuZ VkSRTfVOO8FBE4sJ+BUhV5KiiKPHs8OgHAksBLRpixFTCJ3CjvstEBeudnOKwBvSSrZ5 Jey9qykM5gCfZKP5U2siz+OvR1eoetP+7Iz6IF7YzcGYM/DC8bK9a4iOBFQpl0Nj0PTN 5nuvJf7GnbceBWN/MuaA020gEKq+jzZE9sHA0eR8hXeQ/EX3Ug8zeTNmYg5fdLNUA1N0 HkUoi9BAB+D0kAtLjbBdo3hjwRcsEfT1ofRu1yGGTF+O890XHPv0DqSmt7s+F4L6dt/D ZoIQ== 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=K9/6iNsaG2WmKJUo8RO+PQUPavM6bWCYZObihw+51LY=; fh=dnKm9ic91IyE2znXvQafF3QZhC6YRQTIuif9qSthU6U=; b=tg+4+344Ex9KcUOieMWmR2wZVGKQilKxBYFmy77rIPI7lbPLB7MctIESFeCG0t9N/j BqGEZ5jL0XAt3jw3GKcsY8yMDZKvMnNPO2bkTe/HrkvireCr6btIY3reu2uUh78CqhSC RADTHxvWQd7l87xeAiIv8t5hPKGRX1S/mKX3zhUAIzgEariPCucGHbaL36NR2gRPHxee M0Sz5r9+ChWfdi+AVrq0kKm1PGODIOY9CI0k87wIFdCzO11bGe17G+XzasNuxeUuanWR J3kSC44iP4uhuD9IymcOTRT6oLBF/5FMmDtOdKRTU/dazfwtPmZ66CwWorHLhMFUiLzt HLtg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bM1XhnVZ; 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 af79cd13be357-79301186c5esi377830485a.731.2024.05.16.15.21.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 16 May 2024 15:21:52 -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=bM1XhnVZ; 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 1s7jTV-0006m8-Ex; Thu, 16 May 2024 18:21:21 -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 1s7jTK-0006bs-90 for qemu-devel@nongnu.org; Thu, 16 May 2024 18:21:13 -0400 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s7jTH-0003Bd-Lp for qemu-devel@nongnu.org; Thu, 16 May 2024 18:21:09 -0400 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-6f4302187c0so539314b3a.1 for ; Thu, 16 May 2024 15:21:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1715898065; x=1716502865; 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=K9/6iNsaG2WmKJUo8RO+PQUPavM6bWCYZObihw+51LY=; b=bM1XhnVZMu4DzAv3fuvdrU6BxX3bDE3KRJNkpJRkH0CH+xfcEjaXnxzu2RXljndi+v bhYbel0LFlrJDaFWejvhEL+eyE2Tmv6os3k3lbb3yOFVUgT/H6Nd7xI0cRK+wb+1SK+c VUDKxqh3Fhm5GPvNn7cf5ZMlXBrhU/apv1U1ONn30XlJ1EsLjRiOQomJ3yvrD/uRlFoD OV+e0DIQCb9K+r3xlgzU1yuau9szazxm2vJt0R977LiQVPa/0Gn+0b7Do5xeM4wgkwL3 VyB0ikUvrg6JOzmVCNRhhkakuWaW+oJjOI44RbRV4GW/la7VQei/c0TnBEDHGeS753xQ 3Xcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715898065; x=1716502865; 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=K9/6iNsaG2WmKJUo8RO+PQUPavM6bWCYZObihw+51LY=; b=GcIAToTlQaIVzc/MgjOxQvt4qR9VEYYH3p0S6ocq0IJQeRq6M0AG2LBf0L694LlXxl gAfVO/Ohz7H8qrJlE0hrBnumeDcddyoxNbLFl/vM/NeylnGs1ykZr23nCrHd1Z3CCc0Q n9K9TCLhM6PKm2abPrct83hMsdHeprLivlz0SADyS0/QjbVnldBfC7Bujxx2U8wpwX4o q3DmrRelWVFalk2wVRUs1tmjnwjtFrBYQ3dxM6JNragtVujLDlVqbT6ZKfdSXiBPc8FV jAeRfZ9QwxVKv9qa2+paLqgXhZbgUSUFs6gz8Ear523OploQX0oheCAYNoIWd9JTz7PA K9xA== X-Gm-Message-State: AOJu0YwIq60eVXnbM8f/LwlgrEeALqwfZlt37vQzynesccz0qswq70Tb 8Hvw00naltA2vH5y+lvIFmf8Fv1eqKZieb1JaWznDddpONzrZT+o5JOTim5p2q0VQTmIflEu5+z j148= X-Received: by 2002:a05:6a20:914f:b0:1a8:2cd1:e493 with SMTP id adf61e73a8af0-1afde1c576amr30444273637.29.1715898065079; Thu, 16 May 2024 15:21:05 -0700 (PDT) Received: from linaro.vn.shawcable.net ([2604:3d08:9384:1d00::e697]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66476sm13589219b3a.28.2024.05.16.15.21.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 15:21:04 -0700 (PDT) From: Pierrick Bouvier To: qemu-devel@nongnu.org Cc: Thomas Huth , Laurent Vivier , Mahmoud Mandour , =?utf-8?q?Alex_Benn=C3=A9e?= , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , Paolo Bonzini Subject: [PATCH 2/5] qtest: use cpu interface in qtest_clock_warp Date: Thu, 16 May 2024 15:20:44 -0700 Message-Id: <20240516222047.1853459-3-pierrick.bouvier@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240516222047.1853459-1-pierrick.bouvier@linaro.org> References: <20240516222047.1853459-1-pierrick.bouvier@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::432; envelope-from=pierrick.bouvier@linaro.org; helo=mail-pf1-x432.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 From: Alex Bennée This generalises the qtest_clock_warp code to use the AccelOps handlers for updating its own sense of time. This will make the next patch which moves the warp code closer to pure code motion. From: Alex Bennée Signed-off-by: Alex Bennée Acked-by: Thomas Huth --- include/sysemu/qtest.h | 1 + accel/qtest/qtest.c | 1 + system/qtest.c | 6 +++--- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/sysemu/qtest.h b/include/sysemu/qtest.h index b5d5fd34637..45f3b7e1df5 100644 --- a/include/sysemu/qtest.h +++ b/include/sysemu/qtest.h @@ -36,6 +36,7 @@ void qtest_server_set_send_handler(void (*send)(void *, const char *), void qtest_server_inproc_recv(void *opaque, const char *buf); int64_t qtest_get_virtual_clock(void); +void qtest_set_virtual_clock(int64_t count); #endif #endif diff --git a/accel/qtest/qtest.c b/accel/qtest/qtest.c index f6056ac8361..53182e6c2ae 100644 --- a/accel/qtest/qtest.c +++ b/accel/qtest/qtest.c @@ -52,6 +52,7 @@ static void qtest_accel_ops_class_init(ObjectClass *oc, void *data) ops->create_vcpu_thread = dummy_start_vcpu_thread; ops->get_virtual_clock = qtest_get_virtual_clock; + ops->set_virtual_clock = qtest_set_virtual_clock; }; static const TypeInfo qtest_accel_ops_type = { diff --git a/system/qtest.c b/system/qtest.c index 6da58b3874e..ee8b139e982 100644 --- a/system/qtest.c +++ b/system/qtest.c @@ -332,14 +332,14 @@ int64_t qtest_get_virtual_clock(void) return qatomic_read_i64(&qtest_clock_counter); } -static void qtest_set_virtual_clock(int64_t count) +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 = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + int64_t clock = cpus_get_virtual_clock(); AioContext *aio_context; assert(qtest_enabled()); aio_context = qemu_get_aio_context(); @@ -348,7 +348,7 @@ static void qtest_clock_warp(int64_t dest) QEMU_TIMER_ATTR_ALL); int64_t warp = qemu_soonest_timeout(dest - clock, deadline); - qtest_set_virtual_clock(qtest_get_virtual_clock() + warp); + 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]); From patchwork Thu May 16 22:20:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Pierrick Bouvier X-Patchwork-Id: 797268 Delivered-To: patch@linaro.org Received: by 2002:adf:e68e:0:b0:351:d90a:5487 with SMTP id r14csp501013wrm; Thu, 16 May 2024 15:22:06 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXBDham+1G7RVLciBvNpdClg8jzeGB7jebfX9M8+haE/H9RfmreFnMH5L5x5CnUrSNyl/Io543u07Oszj0Eswzp X-Google-Smtp-Source: AGHT+IEltHxo2Cmk030ivxiUQhAiwX0YIG4xKadijWsL2l9XGLAIheQiEvITKQJD2Yj/uDQOWECT X-Received: by 2002:a05:6871:6001:b0:23c:c9bf:8316 with SMTP id 586e51a60fabf-24172a79c26mr23772544fac.23.1715898125926; Thu, 16 May 2024 15:22:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1715898125; cv=none; d=google.com; s=arc-20160816; b=qqGJE1R1ekOmI9bwTdemBU9ksuxpXjKb9IE5n17r6tgvhhQBhiMXLbJ37/SNLt39B3 PI/OXPeEf4MIMbqrKfXzrwlC8NbWJcFG4OAiDVYpVtIC0avaAx5Bf84ACYDkjY+ln+yf ruEtFYI42nrWZGSOiI5LYNrY1HX7ubVBP6v1+a94qy4MfAiAGXWUdPv/YAloKtcBgxAq 1lMZWfv6JFAREhC7XoLfEifiREC7kte3M5eSYvcOoTAch7fVQ6HtGZ+5/YWADL+MMOYt NlFQiJpRnialI4fQ6qAuX40/mK0YuM/wB6rVZ1tPr0xJaE9W29V+GcUlDtKXkcrfhN5+ 1S7Q== 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=i4r8M+Ho2ethkxiN2yuJrvpiAjkoen/RLNiEY1Q1RNE=; fh=dnKm9ic91IyE2znXvQafF3QZhC6YRQTIuif9qSthU6U=; b=TBCBr6TVNMXTLnat/Zsg7awWQgVy0o9G9oB1LNGUZZX96JkaJ6ezu+DwHa4QIv0iBB q0Lqx8u4gMTRYuuzEEXTr3+oF1dwaMVR9q7UEoeOrAr44AbOxs4Y1nxZHBfZt082Gwjn +cbBG1jYjq/HB1rJi/CoCUo529LhfzPOs8lulqXvUictmtgOFm5MYKeQX8pJrduC3GFm fDDYOX0ISrtUkqrvm0HeYapzPqukGzIfc9HT1Rw9xXHpdrqcOm3ND3LHE05ZfRyGaBAm eSseIPU//qlgSeGwEo2gs4tq3CkWHvewof6zEQ3AzRzbkSBFAMFVTyZfprPvH8dhsPsh RMNw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yllkz4Z3; 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 af79cd13be357-7930b43aaf2si201077285a.391.2024.05.16.15.22.05 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 16 May 2024 15:22:05 -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=yllkz4Z3; 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 1s7jTS-0006ij-Kl; Thu, 16 May 2024 18:21:19 -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 1s7jTL-0006dM-Lv for qemu-devel@nongnu.org; Thu, 16 May 2024 18:21:13 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s7jTI-0003Bk-KE for qemu-devel@nongnu.org; Thu, 16 May 2024 18:21:11 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6f44b5e7f07so654318b3a.2 for ; Thu, 16 May 2024 15:21:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1715898067; x=1716502867; 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=i4r8M+Ho2ethkxiN2yuJrvpiAjkoen/RLNiEY1Q1RNE=; b=yllkz4Z3t/5N/UvDrAdPY03ObjGaXzGszBhGqxguy3F8cctC9fZnvuYrpC9/VfYX7A 8zuzYSapUEGzN66ctyo7QEuC1xCiftxXmfoRqBuX36qCA5c//dpLOSIRLq3EQkWN9cE1 b3k4IRBesAURKIYYf6uXxcXWfEBJ0siQWOdw50wmZU1oN1CQWKXP1jCNnkOhKJrBgvwh 6vWpwLPBfbGIX1uhTZ05oXNbwz8FhfMQIKsB7rxiKMsctrk+UC/dpxfr20+utxc+MXTz FIDe9peYLo0LVD5MDhEL79fb4FfR7HcTVxubzz7mEQQnLpc3+25pNMaiYppzGSfOx/Z7 3cMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715898067; x=1716502867; 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=i4r8M+Ho2ethkxiN2yuJrvpiAjkoen/RLNiEY1Q1RNE=; b=DqkMuq6/hSTFLjpKb41CWyJ4F1eqbD7KvwVMayBTnNR6ScsHUfvtYRMCQOnvCR4Jkf NZO/g54B0MXkEbcv401TxcY2/XjHL6ug80gDeW/Zz+Giw2c7bLS16/InlsrNyakGQvLw sMrMOmRZ9yw4K9Sp9LHMqZJSCZNTK5wX0u62ZQWiUMmFtZjo8AhxD8mn0G0qOKOIfQun VZ2H1XFIJsscWA10/7JrEXAFUXlanlAtqeDe80LTDRKJ//9ukHvG/h0rGhgUqSNo31Ms KMOodb29+H/X1C5p5lXKgYub1TI63J1/izB3FCXkBBi/0NStCEgtWXRUGECGVPejgu+c krcg== X-Gm-Message-State: AOJu0Yz2raHN3WWWT9LDac2tEhUFXEgy2a8evQXV6/pxZ1fN1HewOVyf 3QF9292DdMd4X1tytZjp3b4eK4uueh9xeZP0BE3czcKpIyJcUtdGoy91WXlZXBqQ8Ru+DLBjEsL Nkzc= X-Received: by 2002:a05:6a00:1a8f:b0:6f0:be31:8577 with SMTP id d2e1a72fcca58-6f4e036b56dmr21338731b3a.22.1715898066762; Thu, 16 May 2024 15:21:06 -0700 (PDT) Received: from linaro.vn.shawcable.net ([2604:3d08:9384:1d00::e697]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66476sm13589219b3a.28.2024.05.16.15.21.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 15:21:05 -0700 (PDT) From: Pierrick Bouvier To: qemu-devel@nongnu.org Cc: Thomas Huth , Laurent Vivier , Mahmoud Mandour , =?utf-8?q?Alex_Benn=C3=A9e?= , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , Paolo Bonzini Subject: [PATCH 3/5] sysemu: generalise qtest_warp_clock as qemu_clock_advance_virtual_time Date: Thu, 16 May 2024 15:20:45 -0700 Message-Id: <20240516222047.1853459-4-pierrick.bouvier@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240516222047.1853459-1-pierrick.bouvier@linaro.org> References: <20240516222047.1853459-1-pierrick.bouvier@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::430; envelope-from=pierrick.bouvier@linaro.org; helo=mail-pf1-x430.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 From: Alex Bennée 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. From: Alex Bennée Signed-off-by: Alex Bennée --- 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 9a366e551fb..910587d8293 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: 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 dest); /* * QEMUTimerList diff --git a/system/qtest.c b/system/qtest.c index ee8b139e982..e6f6b4e62d5 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)) @@ -755,7 +735,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)); @@ -781,7 +762,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 6a0de33dd2b..213114be68c 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; +} From patchwork Thu May 16 22:20:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Pierrick Bouvier X-Patchwork-Id: 797270 Delivered-To: patch@linaro.org Received: by 2002:adf:e68e:0:b0:351:d90a:5487 with SMTP id r14csp501209wrm; Thu, 16 May 2024 15:22:46 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXITH8ibxX+HvLGGNHOfemcpAhFM9/ZUNFxxISPlVPe7lCbVRlVTWoxkF9TT1fzfhk87jKWWl9zW8QusTCAwdAX X-Google-Smtp-Source: AGHT+IGCxi6GfVmrIBQEobllgY2AuRxyOPAjsSqNquwZ24BQLlSRfUF3dVVul079LwLFtW3fZsWK X-Received: by 2002:a05:6358:9987:b0:183:f7cb:af75 with SMTP id e5c5f4694b2df-193bd00710cmr2030193155d.32.1715898165725; Thu, 16 May 2024 15:22:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1715898165; cv=none; d=google.com; s=arc-20160816; b=eoH5cy2l0MZWAT+PsvJwSRQHwHGFSbfxwgd/PtbFvtmdQvTH8aKoxNHrbZVzp5KL41 WejpPvt+tYy8EuKbic7NVcx9Cg08bKNK7KQ8PdzEWRo6j1Wrvajn3bQtJTvWratuW3e0 iAwC+HHkN1sXQkjV8/EkeZWHg06FSHGq/jG+kAcWJfWEyHPJapsr8rNf9q6r7tBAqPub yjM/aCOjNpwaI9S5b7p6yznQM5T3xSSjsQBsi1Hd0BMRZXIONNIEfvbFysq4NsDtsII/ 7xsk6ivYeq5b621VtlFLIKh9tA75z/ZwHSnWJJ/pV7Bvf7DEXd/TguU9pN0Uvl4GNkcH l3eg== 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=wCrQq/arpKfIJalWxUJZGuwFUrWxkEQgahovvZswQxw=; fh=dnKm9ic91IyE2znXvQafF3QZhC6YRQTIuif9qSthU6U=; b=uhRcV3F0lFxEJ978hYzQs5uEKfnvFsN6+yl8+Dnhzy10gARkhOE5F59wZb4LAJmCUV NYd4sfL96l9jYOckhko6AAm8LJqvigUXBv4U8Pnx/Qlli0tU4uHK40m40dYDkQXVMQnj p5303N/OnWzIVgsp25Q08Y8Y1XFahLdYggDQy+A7v05KyoyKqI3f6W1O5fH4BS7/xQ8V 7NjnaBhkW3A4WoHAhcPcrQfKfu8nMS3Qc3a7qptf1Jj1qwaH9qAmn1e1++PC8AM9EtAT Mnswea+CrI0H/JtFIz/m7snUSA/vEk2bG982ULDU4T5Us6n7PIJUIcz9xsxYOpCAXSbk Ko2A==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fKdBHwAt; 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-43df56d487asi153214561cf.678.2024.05.16.15.22.45 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 16 May 2024 15:22:45 -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=fKdBHwAt; 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 1s7jTV-0006m4-Bx; Thu, 16 May 2024 18:21:21 -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 1s7jTN-0006f1-GG for qemu-devel@nongnu.org; Thu, 16 May 2024 18:21:14 -0400 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s7jTK-0003Bv-1R for qemu-devel@nongnu.org; Thu, 16 May 2024 18:21:12 -0400 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-6f447260f9dso693821b3a.0 for ; Thu, 16 May 2024 15:21:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1715898068; x=1716502868; 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=wCrQq/arpKfIJalWxUJZGuwFUrWxkEQgahovvZswQxw=; b=fKdBHwAtYslUzBxgFKDJlGt7y/j2X69XpYqipFOJ2oZC4SnPOOZ58rcb5TxTq7I5AK YiAtyNjNRKX/omkZgXpTzWvzg5xteOdkYQeu1SpqAOKUBOPprv3YGB9BXuCaeLgK5a0Q mupTrltfeUUvQFFGf7y0ZztVqR/nXizFgqpnCxP5b88GweuZSD0kkWE9bVvt/NJH284I QT9FVl60DfzVf1CwOpx3xcgilHlLqwpzdvU3uoddsF3zwIOivYYxkOE6DO208j1k5q1T KVPVOu22tWLY3aMNsvaFxZSdCk+xh/LErgZ0s7UnrXhFDXpAZuaNmz4jLYSXorsaBDgd eXLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715898068; x=1716502868; 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=wCrQq/arpKfIJalWxUJZGuwFUrWxkEQgahovvZswQxw=; b=xA0FuEvrKwVkn3QkK2dLAuLCsVlftrMVYJGpMdwnFpIVzABSW+2b0pfvCrzWi5JA8U RsCcYPKgfRGXH51xnybDqhnAWFzytwH9BgG/HrS3Rj9irDUXVHzgV+k/lYYwLs1Jlhqi mlG8fNydioROchQbfk0fx5decmUbv2jga89HgQd5MkYW66Yo5lJqotfXSEKuCZyoj3Xu 3nRqSixvg3S57fQ5PJp9/s+HqqikZD8rVciasxJO9+1sNjkJdhROeccFevDsGMefpnTV TyHdcVodWPpK1DrJV069Mbg2T5yWvp0zHe9rthJxeQ84iQWAZlMqCo8uKdYwjGarzFHa zcDw== X-Gm-Message-State: AOJu0Yz0Ym8terK7se9btU7zNYPUMV15oiGuzqxqyPG2APcEpYH0OmHz wP1PDmOnskEZUVXbHTN+olXRLTh7cBYx/e/IKgkKZQe7BB3nDM7SLlrZTAPuYq8psjFvNCcz5M8 TInM= X-Received: by 2002:a05:6a20:d70b:b0:1af:d9a3:f38d with SMTP id adf61e73a8af0-1afde202083mr19482104637.62.1715898067830; Thu, 16 May 2024 15:21:07 -0700 (PDT) Received: from linaro.vn.shawcable.net ([2604:3d08:9384:1d00::e697]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66476sm13589219b3a.28.2024.05.16.15.21.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 15:21:07 -0700 (PDT) From: Pierrick Bouvier To: qemu-devel@nongnu.org Cc: Thomas Huth , Laurent Vivier , Mahmoud Mandour , =?utf-8?q?Alex_Benn=C3=A9e?= , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , Paolo Bonzini Subject: [PATCH 4/5] plugins: add time control API Date: Thu, 16 May 2024 15:20:46 -0700 Message-Id: <20240516222047.1853459-5-pierrick.bouvier@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240516222047.1853459-1-pierrick.bouvier@linaro.org> References: <20240516222047.1853459-1-pierrick.bouvier@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42b; envelope-from=pierrick.bouvier@linaro.org; helo=mail-pf1-x42b.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 From: Alex Bennée Expose the ability to control time through the plugin API. Only one plugin can control time so it has to request control when loaded. There are probably more corner cases to catch here. From: Alex Bennée Signed-off-by: Alex Bennée --- include/qemu/qemu-plugin.h | 23 +++++++++++++++++++++++ plugins/api.c | 31 +++++++++++++++++++++++++++++++ plugins/qemu-plugins.symbols | 2 ++ 3 files changed, 56 insertions(+) diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index 95703d8fec1..80b1637cede 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -661,6 +661,29 @@ void qemu_plugin_register_vcpu_mem_inline_per_vcpu( qemu_plugin_u64 entry, uint64_t imm); +/** + * qemu_plugin_request_time_control() - request the ability to control time + * + * This grants the plugin the ability to control system time. Only one + * plugin can control time so if multiple plugins request the ability + * all but the first will fail. + * + * Returns an opaque handle or NULL if fails + */ +const void *qemu_plugin_request_time_control(void); + +/** + * qemu_plugin_update_ns() - update system emulation time + * @handle: opaque handle returned by qemu_plugin_request_time_control() + * @time: time in nanoseconds + * + * This allows an appropriately authorised plugin (i.e. holding the + * time control handle) to move system time forward to @time. + * + * Start time is 0. + */ +void qemu_plugin_update_ns(const void *handle, int64_t time); + typedef void (*qemu_plugin_vcpu_syscall_cb_t)(qemu_plugin_id_t id, unsigned int vcpu_index, int64_t num, uint64_t a1, uint64_t a2, diff --git a/plugins/api.c b/plugins/api.c index 5a0a7f8c712..26822b69ea2 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -39,6 +39,7 @@ #include "qemu/main-loop.h" #include "qemu/plugin.h" #include "qemu/log.h" +#include "qemu/timer.h" #include "tcg/tcg.h" #include "exec/exec-all.h" #include "exec/gdbstub.h" @@ -583,3 +584,33 @@ uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry) } return total; } + +/* + * Time control + */ +static bool has_control; + +const void *qemu_plugin_request_time_control(void) +{ + if (!has_control) { + has_control = true; + return &has_control; + } + return NULL; +} + +static void advance_virtual_time__async(CPUState *cpu, run_on_cpu_data data) +{ + int64_t new_time = data.host_ulong; + qemu_clock_advance_virtual_time(new_time); +} + +void qemu_plugin_update_ns(const void *handle, int64_t new_time) +{ + if (handle == &has_control) { + /* Need to execute out of cpu_exec, so bql can be locked. */ + async_run_on_cpu(current_cpu, + advance_virtual_time__async, + RUN_ON_CPU_HOST_ULONG(new_time)); + } +} diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols index aa0a77a319f..ca773d8d9fe 100644 --- a/plugins/qemu-plugins.symbols +++ b/plugins/qemu-plugins.symbols @@ -38,6 +38,7 @@ qemu_plugin_register_vcpu_tb_exec_cond_cb; qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu; qemu_plugin_register_vcpu_tb_trans_cb; + qemu_plugin_request_time_control; qemu_plugin_reset; qemu_plugin_scoreboard_free; qemu_plugin_scoreboard_find; @@ -51,5 +52,6 @@ qemu_plugin_u64_set; qemu_plugin_u64_sum; qemu_plugin_uninstall; + qemu_plugin_update_ns; qemu_plugin_vcpu_for_each; }; From patchwork Thu May 16 22:20:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierrick Bouvier X-Patchwork-Id: 797271 Delivered-To: patch@linaro.org Received: by 2002:adf:e68e:0:b0:351:d90a:5487 with SMTP id r14csp501215wrm; Thu, 16 May 2024 15:22:48 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUYyCwC1IgiTpvNIZ7ivDTyB1KAdzQC7SpdTJ3VFucTKGffv/rh/7EiKAALBGbsVFL0AtwgeEmJPIBfWoj3+sCl X-Google-Smtp-Source: AGHT+IFQm4HGT0WJ/G82I583E88ISgz7K0Qk9PZQtLaGKJKgWciZ1h/Icl1PFFAp44UWFy8hs0QM X-Received: by 2002:ac8:5850:0:b0:43e:39a3:7520 with SMTP id d75a77b69052e-43e39a37837mr61742411cf.34.1715898168192; Thu, 16 May 2024 15:22:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1715898168; cv=none; d=google.com; s=arc-20160816; b=1KvtR4bSqBu9x0dJw2xRTLZjJT7WpiZ4pa9Mqu3yelXv+bC54ABch9DN/3ZkayFS9N 9qBe2yUff3iOywoYt8R6aX04ucuohQyc6J/N8/vm4MZXbCrGJXLrGqBFpyEtOeBuw7NZ YCkorC+Fa8yHMWLknVobNUHwFsy+1d8gXduVwjTeqJHr82ld09i0AeuAbzRl4vlIqPyr IwzphrPId4Y0pK0162DtUdh5lynMYQF+gxPBvodAv9ORRWFPmd/iMuo8WZfDHQoL8FZQ bR2/dc8rPKuUtpt0M9MKT/P4NUz/OJxUg8dnccGRBhPJl5hKMMyyuIhAaEVDys93sqGQ 1WBQ== 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=a9mfgVW6aKcKwSz1phkXMBstK1m6Ri0Rr6frZF0Wh5I=; fh=dnKm9ic91IyE2znXvQafF3QZhC6YRQTIuif9qSthU6U=; b=H+8V4Nbhub7DYPG68zvWc7GXTy2K6WhD3YtVs4fkhr1oiY9aCveTb9k9gDOIEdhW94 VgSL7DS6QuTGTXvk1CWrbQBSmmAS5QNHBkUCkwGf8vL0PPVF4rYYyMKPKmE3MJ64IkIG MEK5EpfDfupsWzlHOvYlFgO7G7rVxi1tbUoFz1UO0XK3pSWpSeDRo77MuoShGM+IE8Ev UAj1cH7e8pTE0auLGaF+mcQyMPO5DAdjNWQVP+wF22t/0qsOyMsm/kg0i5VNIONq1/1p 21Lgpf/WNPQphk6X6+/cpggr298gc4BzdkN+6F5e4sndByggb3IUbWMzHKFNXwftemtf vTew==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WEQ4CcCn; 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-43df549ca73si176411961cf.3.2024.05.16.15.22.48 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 16 May 2024 15:22:48 -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=WEQ4CcCn; 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 1s7jTX-0006o9-2x; Thu, 16 May 2024 18:21:23 -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 1s7jTN-0006f2-GN for qemu-devel@nongnu.org; Thu, 16 May 2024 18:21:14 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s7jTL-0003CH-8H for qemu-devel@nongnu.org; Thu, 16 May 2024 18:21:13 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6f4e59191a1so727342b3a.1 for ; Thu, 16 May 2024 15:21:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1715898069; x=1716502869; 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=a9mfgVW6aKcKwSz1phkXMBstK1m6Ri0Rr6frZF0Wh5I=; b=WEQ4CcCnDBNgXA9Og31IcQfcLC4oskZrp+XOJYldKj4bj439OkQPq4d0hE5Yf+WEP8 OBbU97E60MHPdX4h/1GeX8w383xMf4KSNF4uRXbrc0C0CFZ1d3IlGrtztaYzVfeOeOuY 2YV9hXt2tS9ud2S4Vp5hv7JTmPHKJkwzBIVybfZ7B7IS6PWURtAHQpo4mp6+nmOIZ23G u5rKjyA1TDR39129gaIXxoE4sX7z7csgWCrh7HEymXLm/DwNK5MCbspgTZi/5HZnuNXL Aq4FsBeCxFjymduRuJPS/SuffImOayeRT8KsGG382er7NSfmdU+I3uVAspSf2SVryneT /+pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715898069; x=1716502869; 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=a9mfgVW6aKcKwSz1phkXMBstK1m6Ri0Rr6frZF0Wh5I=; b=dxjYf7KLsKloHmwmzoibEcpAO+JykyPuJfAFN7NKxYd8OimgC4yj5jqfp81bo8/Ra/ RiyOvgbbNRFhWE11cCCw0md22vzxGj7m8ZffG5cwAndeeW1Q4X0ehmfrAFpQteVDwyF3 1PZWKa8CjyeOksSqS/zszr0LCuXnKvNSa4HHewJpD21UrH8if7z39umtDk3/aOQxFIs2 5+CPEtdPFVnABqwTiv6R6iqf2xvepm4a2ce1U/ZWHNWhbbkKeRUnQ+0QyN4KbK5N0LuA XWe8EJFt0zpPRwcSVO1SeeQWvsAic7J/GSjPfYXttL0/MkE1oy2ibPiLcLxE5J8Z0U+n fOJA== X-Gm-Message-State: AOJu0Yztw6AYB/blNh/tEAhOd+ouKAvgFPqAsoqSJj2JBZLmOgroVU1T GfTxTZ9X4AZnlyy5vrs4gdsLOPtzWzOONLlyzO7TriJ603kz7WqJj2qQQG2+KPT8YN8dhj0FtBQ Goic= X-Received: by 2002:a05:6a20:2d26:b0:1b0:14ee:3b29 with SMTP id adf61e73a8af0-1b014ee3b88mr8875271637.10.1715898069046; Thu, 16 May 2024 15:21:09 -0700 (PDT) Received: from linaro.vn.shawcable.net ([2604:3d08:9384:1d00::e697]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66476sm13589219b3a.28.2024.05.16.15.21.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 15:21:08 -0700 (PDT) From: Pierrick Bouvier To: qemu-devel@nongnu.org Cc: Thomas Huth , Laurent Vivier , Mahmoud Mandour , =?utf-8?q?Alex_Benn=C3=A9e?= , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , Paolo Bonzini Subject: [PATCH 5/5] contrib/plugins: add ips plugin example for cost modeling Date: Thu, 16 May 2024 15:20:47 -0700 Message-Id: <20240516222047.1853459-6-pierrick.bouvier@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240516222047.1853459-1-pierrick.bouvier@linaro.org> References: <20240516222047.1853459-1-pierrick.bouvier@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::430; envelope-from=pierrick.bouvier@linaro.org; helo=mail-pf1-x430.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 This plugin uses the new time control interface to make decisions about the state of time during the emulation. The algorithm is currently very simple. The user specifies an ips rate which applies per core. If the core runs ahead of its allocated execution time the plugin sleeps for a bit to let real time catch up. Either way time is updated for the emulation as a function of total executed instructions with some adjustments for cores that idle. Examples -------- Slow down execution of /bin/true: $ num_insn=$(./build/qemu-x86_64 -plugin ./build/tests/plugin/libinsn.so -d plugin /bin/true |& grep total | sed -e 's/.*: //') $ time ./build/qemu-x86_64 -plugin ./build/contrib/plugins/libips.so,ips=$(($num_insn/4)) /bin/true real 4.000s Boot a Linux kernel simulating a 250MHz cpu: $ /build/qemu-system-x86_64 -kernel /boot/vmlinuz-6.1.0-21-amd64 -append "console=ttyS0" -plugin ./build/contrib/plugins/libips.so,ips=$((250*1000*1000)) -smp 1 -m 512 check time until kernel panic on serial0 Signed-off-by: Pierrick Bouvier --- contrib/plugins/ips.c | 239 +++++++++++++++++++++++++++++++++++++++ contrib/plugins/Makefile | 1 + 2 files changed, 240 insertions(+) create mode 100644 contrib/plugins/ips.c diff --git a/contrib/plugins/ips.c b/contrib/plugins/ips.c new file mode 100644 index 00000000000..cf3159df391 --- /dev/null +++ b/contrib/plugins/ips.c @@ -0,0 +1,239 @@ +/* + * ips rate limiting plugin. + * + * This plugin can be used to restrict the execution of a system to a + * particular number of Instructions Per Second (ips). This controls + * time as seen by the guest so while wall-clock time may be longer + * from the guests point of view time will pass at the normal rate. + * + * This uses the new plugin API which allows the plugin to control + * system time. + * + * Copyright (c) 2023 Linaro Ltd + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include +#include +#include + +QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION; + +/* how many times do we update time per sec */ +#define NUM_TIME_UPDATE_PER_SEC 10 +#define NSEC_IN_ONE_SEC (1000 * 1000 * 1000) + +static GMutex global_state_lock; + +static uint64_t insn_per_second = 1000 * 1000; /* ips per core, per second */ +static uint64_t insn_quantum; /* trap every N instructions */ +static bool precise_execution; /* count every instruction */ +static int64_t start_time_ns; /* time (ns since epoch) first vCPU started */ +static int64_t virtual_time_ns; /* last set virtual time */ + +static const void *time_handle; + +typedef enum { + UNKNOWN = 0, + EXECUTING, + IDLE, + FINISHED +} vCPUState; + +typedef struct { + uint64_t counter; + uint64_t track_insn; + vCPUState state; + /* timestamp when vCPU entered state */ + int64_t last_state_time; +} vCPUTime; + +struct qemu_plugin_scoreboard *vcpus; + +/* return epoch time in ns */ +static int64_t now_ns(void) +{ + return g_get_real_time() * 1000; +} + +static uint64_t num_insn_during(int64_t elapsed_ns) +{ + double num_secs = elapsed_ns / (double) NSEC_IN_ONE_SEC; + return num_secs * (double) insn_per_second; +} + +static int64_t time_for_insn(uint64_t num_insn) +{ + double num_secs = (double) num_insn / (double) insn_per_second; + return num_secs * (double) NSEC_IN_ONE_SEC; +} + +static int64_t uptime_ns(void) +{ + int64_t now = now_ns(); + g_assert(now >= start_time_ns); + return now - start_time_ns; +} + +static void vcpu_set_state(vCPUTime *vcpu, vCPUState new_state) +{ + vcpu->last_state_time = now_ns(); + vcpu->state = new_state; +} + +static void update_system_time(vCPUTime *vcpu) +{ + /* flush remaining instructions */ + vcpu->counter += vcpu->track_insn; + vcpu->track_insn = 0; + + int64_t uptime = uptime_ns(); + uint64_t expected_insn = num_insn_during(uptime); + + if (vcpu->counter >= expected_insn) { + /* this vcpu ran faster than expected, so it has to sleep */ + uint64_t insn_advance = vcpu->counter - expected_insn; + uint64_t time_advance_ns = time_for_insn(insn_advance); + int64_t sleep_us = time_advance_ns / 1000; + g_usleep(sleep_us); + } + + /* based on number of instructions, what should be the new time? */ + int64_t new_virtual_time = time_for_insn(vcpu->counter); + + g_mutex_lock(&global_state_lock); + + /* Time only moves forward. Another vcpu might have updated it already. */ + if (new_virtual_time > virtual_time_ns) { + qemu_plugin_update_ns(time_handle, new_virtual_time); + virtual_time_ns = new_virtual_time; + } + + g_mutex_unlock(&global_state_lock); +} + +static void set_start_time() +{ + g_mutex_lock(&global_state_lock); + if (!start_time_ns) { + start_time_ns = now_ns(); + } + g_mutex_unlock(&global_state_lock); +} + +static void vcpu_init(qemu_plugin_id_t id, unsigned int cpu_index) +{ + vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index); + /* ensure start time is set first */ + set_start_time(); + /* start counter from absolute time reference */ + vcpu->counter = num_insn_during(uptime_ns()); + vcpu_set_state(vcpu, EXECUTING); +} + +static void vcpu_idle(qemu_plugin_id_t id, unsigned int cpu_index) +{ + vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index); + vcpu_set_state(vcpu, IDLE); +} + +static void vcpu_resume(qemu_plugin_id_t id, unsigned int cpu_index) +{ + vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index); + g_assert(vcpu->state == IDLE); + int64_t idle_time = now_ns() - vcpu->last_state_time; + /* accumulate expected number of instructions */ + vcpu->counter += num_insn_during(idle_time); + vcpu_set_state(vcpu, EXECUTING); +} + +static void vcpu_exit(qemu_plugin_id_t id, unsigned int cpu_index) +{ + vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index); + vcpu_set_state(vcpu, FINISHED); + update_system_time(vcpu); + vcpu->counter = 0; +} + +static void every_insn_quantum(unsigned int cpu_index, void *udata) +{ + vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index); + g_assert(vcpu->track_insn >= insn_quantum); + update_system_time(vcpu); +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + size_t n_insns = qemu_plugin_tb_n_insns(tb); + qemu_plugin_u64 track_insn = + qemu_plugin_scoreboard_u64_in_struct(vcpus, vCPUTime, track_insn); + if (precise_execution) { + /* count (and eventually trap) on every instruction */ + for (int idx = 0; idx < qemu_plugin_tb_n_insns(tb); ++idx) { + struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, idx); + qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu( + insn, QEMU_PLUGIN_INLINE_ADD_U64, track_insn, 1); + qemu_plugin_register_vcpu_insn_exec_cond_cb( + insn, every_insn_quantum, + QEMU_PLUGIN_CB_NO_REGS, QEMU_PLUGIN_COND_GE, + track_insn, insn_quantum, NULL); + } + } else { + /* count (and eventually trap) once per tb */ + qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu( + tb, QEMU_PLUGIN_INLINE_ADD_U64, track_insn, n_insns); + qemu_plugin_register_vcpu_tb_exec_cond_cb( + tb, every_insn_quantum, + QEMU_PLUGIN_CB_NO_REGS, QEMU_PLUGIN_COND_GE, + track_insn, insn_quantum, NULL); + } +} + +static void plugin_exit(qemu_plugin_id_t id, void *udata) +{ + qemu_plugin_scoreboard_free(vcpus); +} + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, + const qemu_info_t *info, int argc, + char **argv) +{ + for (int i = 0; i < argc; i++) { + char *opt = argv[i]; + g_auto(GStrv) tokens = g_strsplit(opt, "=", 2); + if (g_strcmp0(tokens[0], "ips") == 0) { + insn_per_second = g_ascii_strtoull(tokens[1], NULL, 10); + if (!insn_per_second && errno) { + fprintf(stderr, "%s: couldn't parse %s (%s)\n", + __func__, tokens[1], g_strerror(errno)); + return -1; + } + + } else if (g_strcmp0(tokens[0], "precise") == 0) { + if (!qemu_plugin_bool_parse(tokens[0], tokens[1], + &precise_execution)) { + fprintf(stderr, "boolean argument parsing failed: %s\n", opt); + return -1; + } + } else { + fprintf(stderr, "option parsing failed: %s\n", opt); + return -1; + } + } + + vcpus = qemu_plugin_scoreboard_new(sizeof(vCPUTime)); + insn_quantum = insn_per_second / NUM_TIME_UPDATE_PER_SEC; + + time_handle = qemu_plugin_request_time_control(); + g_assert(time_handle); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_vcpu_init_cb(id, vcpu_init); + qemu_plugin_register_vcpu_idle_cb(id, vcpu_idle); + qemu_plugin_register_vcpu_resume_cb(id, vcpu_resume); + qemu_plugin_register_vcpu_exit_cb(id, vcpu_exit); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + + return 0; +} diff --git a/contrib/plugins/Makefile b/contrib/plugins/Makefile index 0b64d2c1e3a..449ead11305 100644 --- a/contrib/plugins/Makefile +++ b/contrib/plugins/Makefile @@ -27,6 +27,7 @@ endif NAMES += hwprofile NAMES += cache NAMES += drcov +NAMES += ips ifeq ($(CONFIG_WIN32),y) SO_SUFFIX := .dll