From patchwork Wed Apr 5 13:25:00 2017 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: 96860 Delivered-To: patch@linaro.org Received: by 10.182.246.10 with SMTP id xs10csp262162obc; Wed, 5 Apr 2017 06:33:47 -0700 (PDT) X-Received: by 10.55.139.1 with SMTP id n1mr26802484qkd.269.1491399227473; Wed, 05 Apr 2017 06:33:47 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id w76si17796919qka.93.2017.04.05.06.33.47 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 05 Apr 2017 06:33:47 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:40585 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cvl4E-0003AU-TW for patch@linaro.org; Wed, 05 Apr 2017 09:33:46 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54633) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cvkvz-0004hZ-7O for qemu-devel@nongnu.org; Wed, 05 Apr 2017 09:25:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cvkvw-0000Kb-3D for qemu-devel@nongnu.org; Wed, 05 Apr 2017 09:25:15 -0400 Received: from mail-wr0-x22d.google.com ([2a00:1450:400c:c0c::22d]:35475) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cvkvv-0000K4-Tp for qemu-devel@nongnu.org; Wed, 05 Apr 2017 09:25:12 -0400 Received: by mail-wr0-x22d.google.com with SMTP id k6so14523718wre.2 for ; Wed, 05 Apr 2017 06:25:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UP+AFiBfHOY8/mlkST5aMGha+kgWptX2T3X1O1+gBek=; b=Lqlx3RgtM/P15ix7MrsqKOR4tccNfq4A0Ks9mmaqEvcRZkleBDYK96jydJ/hTQ6r1K vJ12bEmqARrH0WsClRpJ52uzhgMSpCv438cHZ2uP7uVDwvYGse1Kc3kihUVx0spilvGc hwnmTOv32tWzbQ39RCCPDSxe/cEPp7OgO1S9k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UP+AFiBfHOY8/mlkST5aMGha+kgWptX2T3X1O1+gBek=; b=dSSohDZlytwZtIUwQaUrVEsEQWJrglpd3bxV4fzM2PZDZlYkmz5+4QIzRhdMy8iCZU 0SMYGZsb0vZIYn8HkCue9c2X5Bs9W4km54JpWLfEkuN4SwJ/NJIf86a4scP1zJUib4dk K7d5FKYM0RrE8l+WSuJ+rfNCXrgMUMYTsTPWTiROO5Q7VjB/d3nAN9p1sYa7jXJUCo4U tYj4BykS0F+hQtcbwTIG4Etx6ifF9wWi9FNFFUMpImDEN+VG6rCLhv5XjLQSwA8ZZ1X9 /xskXPNGHtwwDel3eU7Q2HhBWCpzgCWblynyfk7iqpaV39Y+zACEhjzYoa6n70owVhQZ sw2Q== X-Gm-Message-State: AFeK/H320F2rc6KOzbIdJKn/1+wI3BtIKX3ek0FCG0GiDXep+9IgvANrO11teHsM+4Ckpz5W X-Received: by 10.223.139.80 with SMTP id v16mr23868973wra.133.1491398710485; Wed, 05 Apr 2017 06:25:10 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id z40sm26287568wrz.67.2017.04.05.06.25.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Apr 2017 06:25:07 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id BB28E3E102C; Wed, 5 Apr 2017 14:25:03 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: dovgaluk@ispras.ru, rth@twiddle.net, pbonzini@redhat.com Date: Wed, 5 Apr 2017 14:25:00 +0100 Message-Id: <20170405132503.32125-10-alex.bennee@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170405132503.32125-1-alex.bennee@linaro.org> References: <20170405132503.32125-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::22d Subject: [Qemu-devel] [PATCH v2 09/12] cpus: introduce cpu_update_icount helper X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mttcg@listserver.greensocs.com, peter.maydell@linaro.org, nikunj@linux.vnet.ibm.com, Peter Crosthwaite , a.rigo@virtualopensystems.com, qemu-devel@nongnu.org, cota@braap.org, bobby.prani@gmail.com, =?utf-8?q?Alex_Benn=C3=A9e?= , fred.konrad@greensocs.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" By holding off updates to timer_state.qemu_icount we can run into trouble when the non-vCPU thread needs to know the time. This helper ensures we atomically update timers_state.qemu_icount based on what has been currently executed. Signed-off-by: Alex Bennée --- cpus.c | 16 ++++++++++++++-- include/qemu/timer.h | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) -- 2.11.0 diff --git a/cpus.c b/cpus.c index 88eabdc19f..71c3baba87 100644 --- a/cpus.c +++ b/cpus.c @@ -232,12 +232,24 @@ static int64_t cpu_get_icount_executed(CPUState *cpu) return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra); } +/* + * Update the global shared timer_state.qemu_icount to take into + * account executed instructions. This is done by the TCG vCPU + * thread so the main-loop can see time has moved forward. + */ +void cpu_update_icount(CPUState *cpu) +{ + int64_t executed = cpu_get_icount_executed(cpu); + cpu->icount_budget -= executed; + atomic_add(&timers_state.qemu_icount, executed); +} + int64_t cpu_get_icount_raw(void) { int64_t icount; CPUState *cpu = current_cpu; - icount = timers_state.qemu_icount; + icount = atomic_read(&timers_state.qemu_icount); if (cpu && cpu->running) { if (!cpu->can_do_io) { fprintf(stderr, "Bad icount read\n"); @@ -1220,7 +1232,7 @@ static void process_icount_data(CPUState *cpu) { if (use_icount) { /* Account for executed instructions */ - timers_state.qemu_icount += cpu_get_icount_executed(cpu); + cpu_update_icount(cpu); /* Reset the counters */ cpu->icount_decr.u16.low = 0; diff --git a/include/qemu/timer.h b/include/qemu/timer.h index e1742f2f3d..8a1eb74839 100644 --- a/include/qemu/timer.h +++ b/include/qemu/timer.h @@ -869,6 +869,7 @@ int64_t cpu_get_icount_raw(void); int64_t cpu_get_icount(void); int64_t cpu_get_clock(void); int64_t cpu_icount_to_ns(int64_t icount); +void cpu_update_icount(CPUState *cpu); /*******************************************/ /* host CPU ticks (if available) */