From patchwork Mon Oct 17 08:28:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 9378693 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C9C5F6075E for ; Mon, 17 Oct 2016 08:28:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BC20428C6F for ; Mon, 17 Oct 2016 08:28:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B09D328DA0; Mon, 17 Oct 2016 08:28:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 39FAD28C6F for ; Mon, 17 Oct 2016 08:28:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758253AbcJQI2v (ORCPT ); Mon, 17 Oct 2016 04:28:51 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:36020 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757612AbcJQI2n (ORCPT ); Mon, 17 Oct 2016 04:28:43 -0400 Received: by mail-pf0-f193.google.com with SMTP id r16so12328606pfg.3 for ; Mon, 17 Oct 2016 01:28:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=RJCmE0dWblMAaTUEHv7Xnsa8y68Vman4GpnOrbHkfvQ=; b=cRTYpeyskzrZPvwjqXIWDxp/0T2yDpPu23qMQ+GM2IEIoNUIdzM+14Lfn6k7HDFSWw 9ZeJZiMJL/bvR01ZEfU2CUIJdVdvUCoj4vQqqtpVP9tQ4poMggEHfJCzG6MSEfbzEuIl QN7eu7tKx5GxB1vtk+RePYH0JDcUwuKrOYH+efS4ras1JdDltoPvITuhGkOGpPON7Ux5 14r3lu1UsyjF2It2sd3Zj3E1JET0p8SBOn577K5gZI9XMkWxRQfpCZCvAPy1IFzTYBUo J47n7LDT/WUE6HJfQ/x0VVNBqYFr6vTCgPa4wa2cEi5JTnHgOMt7VGyJanqdi1LwTRho AN2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=RJCmE0dWblMAaTUEHv7Xnsa8y68Vman4GpnOrbHkfvQ=; b=WtzxJnqtBXjOfFJUXE/iM6n+z20xYUv4lD/m5utbmkZzIT0sVj8o7IcXwYwPR5CXeu H9JZM8+niaW0Q02sCSuIH2pg80MZ8Ua2G49NgSa7KNROAHpiL5qMTugGanejJuV38jXn Tf+bDHjUglllq7YWqUZOvF/YOtD1iONvwCTGTkmL6R34dt1emqDMk+aU82EjYxZiXP1Z UUlq+luQ1+9mB8ZfYo8v2bXD8eG+Q90MEP/dubJHqR4h5xh9lj47m04sp7eTb861cbUS xK8QVQL1HTVSM9jFXcfpF7Nt1R1TDWJezy8uLT2lIVUeJP87jarOLSUPWFEzQZ7TvM2n fZ6w== X-Gm-Message-State: AA6/9RmsmkmMR2CGUn5lzKrTwEPldyOgCKOvIqoYAgm0391nw3Kii7LlKyYKkW2i16nIcw== X-Received: by 10.98.75.152 with SMTP id d24mr3172366pfj.97.1476692922119; Mon, 17 Oct 2016 01:28:42 -0700 (PDT) Received: from kernel.kingsoft.cn ([114.255.44.132]) by smtp.gmail.com with ESMTPSA id d89sm23533398pfe.32.2016.10.17.01.28.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 17 Oct 2016 01:28:41 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: kvm@vger.kernel.org Cc: Wanpeng Li , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Subject: [PATCH] x86: apic: add APIC Timer periodic/oneshot mode latency test Date: Mon, 17 Oct 2016 16:28:37 +0800 Message-Id: <1476692917-3554-1-git-send-email-wanpeng.li@hotmail.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wanpeng Li Add APIC Timer periodic/oneshot mode latency test. Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- x86/Makefile.x86_64 | 1 + x86/apic_timer_latency.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++ x86/unittests.cfg | 6 +++ 3 files changed, 132 insertions(+) create mode 100644 x86/apic_timer_latency.c diff --git a/x86/Makefile.x86_64 b/x86/Makefile.x86_64 index e166911..c9eda46 100644 --- a/x86/Makefile.x86_64 +++ b/x86/Makefile.x86_64 @@ -14,6 +14,7 @@ tests = $(TEST_DIR)/access.flat $(TEST_DIR)/apic.flat \ tests += $(TEST_DIR)/svm.flat tests += $(TEST_DIR)/vmx.flat tests += $(TEST_DIR)/tscdeadline_latency.flat +tests += $(TEST_DIR)/apic_timer_latency.flat include $(TEST_DIR)/Makefile.common diff --git a/x86/apic_timer_latency.c b/x86/apic_timer_latency.c new file mode 100644 index 0000000..4dd285f --- /dev/null +++ b/x86/apic_timer_latency.c @@ -0,0 +1,125 @@ +/* + * qemu command line | grep latency | cut -f 2 -d ":" > latency + * + * In octave: + * load latency + * min(latency) + * max(latency) + * mean(latency) + * hist(latency, 50) + */ + +/* + * for host tracing of breakmax option: + * + * # cd /sys/kernel/debug/tracing/ + * # echo x86-tsc > trace_clock + * # echo "kvm_exit kvm_entry kvm_msr" > set_event + * # echo "sched_switch $extratracepoints" >> set_event + * # echo apic_timer_fn > set_ftrace_filter + * # echo "function" > current_tracer + */ + +#include "libcflat.h" +#include "apic.h" +#include "vm.h" +#include "smp.h" +#include "desc.h" +#include "isr.h" +#include "msr.h" + +static void test_lapic_existence(void) +{ + u32 lvr; + + lvr = apic_read(APIC_LVR); + printf("apic version: %x\n", lvr); + report("apic existence", (u16)lvr == 0x14); +} + +static int tdt_count; +u64 exptime; +int delta; +int mode; +#define TABLE_SIZE 100000 +u64 table[TABLE_SIZE]; +volatile int table_idx; +volatile int hitmax = 0; +int breakmax = 0; +#define APIC_LVT_TIMER_ONE_SHOT (0) +#define APIC_LVT_TIMER_VECTOR (0xee) + +static void apic_timer_isr(isr_regs_t *regs) +{ + u64 now = rdtsc(); + ++tdt_count; + + if (table_idx < TABLE_SIZE && tdt_count > 1) + table[table_idx++] = now - exptime; + + if (breakmax && tdt_count > 1 && (now - exptime) > breakmax) { + hitmax = 1; + apic_write(APIC_EOI, 0); + return; + } + + exptime = now+delta; + + if (mode == APIC_LVT_TIMER_ONE_SHOT) + /* Set "Initial Counter Register", which starts the timer */ + apic_write(APIC_TMICT, delta); + + apic_write(APIC_EOI, 0); +} + +static void test_apic_timer(int mode) +{ + handle_irq(APIC_LVT_TIMER_VECTOR, apic_timer_isr); + irq_enable(); + + /* Periodic mode */ + apic_write(APIC_LVTT, mode | APIC_LVT_TIMER_VECTOR); + /* Divider == 1 */ + apic_write(APIC_TDCR, 0x0000000b); + apic_write(APIC_TMICT, delta); +} + +int main(int argc, char **argv) +{ + int i, size; + u64 count = 0; + + setup_vm(); + smp_init(); + setup_idt(); + + test_lapic_existence(); + + mask_pic_interrupts(); + + mode = argc <= 1 ? APIC_LVT_TIMER_PERIODIC : atol(argv[1]); + delta = argc <= 2 ? 0x100000 : atol(argv[2]); + size = argc <= 3 ? TABLE_SIZE : atol(argv[3]); + breakmax = argc <= 4 ? 0 : atol(argv[4]); + printf("breakmax=%d\n", breakmax); + if (mode == APIC_LVT_TIMER_PERIODIC) + printf("APIC Timer Periodic Mode\n"); + else if (mode == APIC_LVT_TIMER_ONE_SHOT) + printf("APIC Timer Oneshot Mode\n"); + test_apic_timer(mode); + irq_enable(); + + do { + asm volatile("hlt"); + } while (!hitmax && table_idx < size); + + for (i = 0; i < table_idx; i++) { + if (hitmax && i == table_idx-1) + printf("hit max: %d < ", breakmax); + count += table[i]; + printf("latency: %" PRId64 "\n", table[i]); + } + + printf("average latency = %lu\n", count/size); + return report_summary(); +} diff --git a/x86/unittests.cfg b/x86/unittests.cfg index 7242517..e055ce1 100644 --- a/x86/unittests.cfg +++ b/x86/unittests.cfg @@ -32,6 +32,12 @@ extra_params = -cpu qemu64,+x2apic,+tsc-deadline arch = x86_64 timeout = 30 +[apic_timer_latency] +file = apic_timer_latency.flat +smp = 1 +arch = x86_64 +timeout = 30 + [ioapic] file = ioapic.flat extra_params = -cpu qemu64