From: Jonathan Austin Subject: [PATCH v3 2/3] kvm tools: remove periodic tick in favour of a polling thread Date: Wed, 11 Sep 2013 18:32:48 +0100 Message-ID: <1378920769-24258-3-git-send-email-jonathan.austin@arm.com> References: <1378920769-24258-1-git-send-email-jonathan.austin@arm.com> Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Cc: penberg@kernel.org, marc.zyngier@arm.com, will.deacon@arm.com, Jonathan Austin To: kvm@vger.kernel.org Return-path: Received: from service87.mimecast.com ([91.220.42.44]:41018 "EHLO service87.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755045Ab3IKRdD (ORCPT ); Wed, 11 Sep 2013 13:33:03 -0400 In-Reply-To: <1378920769-24258-1-git-send-email-jonathan.austin@arm.com> Sender: kvm-owner@vger.kernel.org List-ID: Currently the only use of the periodic timer tick in kvmtool is to handle reading from stdin. Though functional, this periodic tick can be problematic on slow (eg FPGA) platforms and can cause low interactivity or even stop the execution from progressing at all. This patch removes the periodic tick in favour of a dedicated thread blocke= d waiting for input from the console. In order to reflect the new behaviour, the old 'kvm__arch_periodic_tick' function is renamed to 'kvm__arch_read_te= rm'. In making this change it is necessary to actively flush the emulated serial console's output buffer after the guest writes to it, as otherwise flushing only happens with terminal input. Similarly, it is no longer necessary to flush the buffer when we process input. Signed-off-by: Jonathan Austin Acked-by: Marc Zyngier --- tools/kvm/arm/kvm.c | 2 +- tools/kvm/builtin-run.c | 13 ----------- tools/kvm/hw/serial.c | 10 +-------- tools/kvm/include/kvm/kvm.h | 2 +- tools/kvm/kvm.c | 50 ---------------------------------------= ---- tools/kvm/powerpc/kvm.c | 2 +- tools/kvm/term.c | 31 +++++++++++++++++++++++++++ tools/kvm/x86/kvm.c | 2 +- 8 files changed, 36 insertions(+), 76 deletions(-) diff --git a/tools/kvm/arm/kvm.c b/tools/kvm/arm/kvm.c index 27e6cf4..008b7fe 100644 --- a/tools/kvm/arm/kvm.c +++ b/tools/kvm/arm/kvm.c @@ -46,7 +46,7 @@ void kvm__arch_delete_ram(struct kvm *kvm) =09munmap(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size); } =20 -void kvm__arch_periodic_poll(struct kvm *kvm) +void kvm__arch_read_term(struct kvm *kvm) { =09if (term_readable(0)) { =09=09serial8250__update_consoles(kvm); diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c index 4d7fbf9d..da95d71 100644 --- a/tools/kvm/builtin-run.c +++ b/tools/kvm/builtin-run.c @@ -165,13 +165,6 @@ void kvm_run_set_wrapper_sandbox(void) =09OPT_END()=09=09=09=09=09=09=09\ =09}; =20 -static void handle_sigalrm(int sig, siginfo_t *si, void *uc) -{ -=09struct kvm *kvm =3D si->si_value.sival_ptr; - -=09kvm__arch_periodic_poll(kvm); -} - static void *kvm_cpu_thread(void *arg) { =09char name[16]; @@ -487,17 +480,11 @@ static struct kvm *kvm_cmd_run_init(int argc, const c= har **argv) { =09static char real_cmdline[2048], default_name[20]; =09unsigned int nr_online_cpus; -=09struct sigaction sa; =09struct kvm *kvm =3D kvm__new(); =20 =09if (IS_ERR(kvm)) =09=09return kvm; =20 -=09sa.sa_flags =3D SA_SIGINFO; -=09sa.sa_sigaction =3D handle_sigalrm; -=09sigemptyset(&sa.sa_mask); -=09sigaction(SIGALRM, &sa, NULL); - =09nr_online_cpus =3D sysconf(_SC_NPROCESSORS_ONLN); =09kvm->cfg.custom_rootfs_name =3D "default"; =20 diff --git a/tools/kvm/hw/serial.c b/tools/kvm/hw/serial.c index 931067f..3b5540c 100644 --- a/tools/kvm/hw/serial.c +++ b/tools/kvm/hw/serial.c @@ -169,15 +169,6 @@ static void serial8250__receive(struct kvm *kvm, struc= t serial8250_device *dev, { =09int c; =20 -=09/* -=09 * If the guest transmitted a full fifo, we clear the -=09 * TEMT/THRE bits to let the kernel escape from the 8250 -=09 * interrupt handler. We come here only once a ms, so that -=09 * should give the kernel the desired pause. That also flushes -=09 * the tx fifo to the terminal. -=09 */ -=09serial8250_flush_tx(kvm, dev); - =09if (dev->mcr & UART_MCR_LOOP) =09=09return; =20 @@ -260,6 +251,7 @@ static bool serial8250_out(struct ioport *ioport, struc= t kvm *kvm, u16 port, =09=09=09dev->lsr &=3D ~UART_LSR_TEMT; =09=09=09if (dev->txcnt =3D=3D FIFO_LEN / 2) =09=09=09=09dev->lsr &=3D ~UART_LSR_THRE; +=09=09=09serial8250_flush_tx(kvm, dev); =09=09} else { =09=09=09/* Should never happpen */ =09=09=09dev->lsr &=3D ~(UART_LSR_TEMT | UART_LSR_THRE); diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h index ad53ca7..d05b936 100644 --- a/tools/kvm/include/kvm/kvm.h +++ b/tools/kvm/include/kvm/kvm.h @@ -103,7 +103,7 @@ void kvm__arch_delete_ram(struct kvm *kvm); int kvm__arch_setup_firmware(struct kvm *kvm); int kvm__arch_free_firmware(struct kvm *kvm); bool kvm__arch_cpu_supports_vm(void); -void kvm__arch_periodic_poll(struct kvm *kvm); +void kvm__arch_read_term(struct kvm *kvm); =20 void *guest_flat_to_host(struct kvm *kvm, u64 offset); u64 host_to_guest_flat(struct kvm *kvm, void *ptr); diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c index cfd30dd..d7d2e84 100644 --- a/tools/kvm/kvm.c +++ b/tools/kvm/kvm.c @@ -393,56 +393,6 @@ found_kernel: =09return ret; } =20 -#define TIMER_INTERVAL_NS 1000000=09/* 1 msec */ - -/* - * This function sets up a timer that's used to inject interrupts from the - * userspace hypervisor into the guest at periodical intervals. Please not= e - * that clock interrupt, for example, is not handled here. - */ -int kvm_timer__init(struct kvm *kvm) -{ -=09struct itimerspec its; -=09struct sigevent sev; -=09int r; - -=09memset(&sev, 0, sizeof(struct sigevent)); -=09sev.sigev_value.sival_int=09=3D 0; -=09sev.sigev_notify=09=09=3D SIGEV_THREAD_ID; -=09sev.sigev_signo=09=09=09=3D SIGALRM; -=09sev.sigev_value.sival_ptr=09=3D kvm; -=09sev._sigev_un._tid=09=09=3D syscall(__NR_gettid); - -=09r =3D timer_create(CLOCK_REALTIME, &sev, &kvm->timerid); -=09if (r < 0) -=09=09return r; - -=09its.it_value.tv_sec=09=09=3D TIMER_INTERVAL_NS / 1000000000; -=09its.it_value.tv_nsec=09=09=3D TIMER_INTERVAL_NS % 1000000000; -=09its.it_interval.tv_sec=09=09=3D its.it_value.tv_sec; -=09its.it_interval.tv_nsec=09=09=3D its.it_value.tv_nsec; - -=09r =3D timer_settime(kvm->timerid, 0, &its, NULL); -=09if (r < 0) { -=09=09timer_delete(kvm->timerid); -=09=09return r; -=09} - -=09return 0; -} -firmware_init(kvm_timer__init); - -int kvm_timer__exit(struct kvm *kvm) -{ -=09if (kvm->timerid) -=09=09if (timer_delete(kvm->timerid) < 0) -=09=09=09die("timer_delete()"); - -=09kvm->timerid =3D 0; - -=09return 0; -} -firmware_exit(kvm_timer__exit); =20 void kvm__dump_mem(struct kvm *kvm, unsigned long addr, unsigned long size= , int debug_fd) { diff --git a/tools/kvm/powerpc/kvm.c b/tools/kvm/powerpc/kvm.c index b4b9f82..c1712cf 100644 --- a/tools/kvm/powerpc/kvm.c +++ b/tools/kvm/powerpc/kvm.c @@ -151,7 +151,7 @@ void kvm__irq_trigger(struct kvm *kvm, int irq) =09kvm__irq_line(kvm, irq, 0); } =20 -void kvm__arch_periodic_poll(struct kvm *kvm) +void kvm__arch_read_term(struct kvm *kvm) { =09/* FIXME: Should register callbacks to platform-specific polls */ =09spapr_hvcons_poll(kvm); diff --git a/tools/kvm/term.c b/tools/kvm/term.c index ac9c7cc..5c3e543 100644 --- a/tools/kvm/term.c +++ b/tools/kvm/term.c @@ -25,6 +25,8 @@ bool term_got_escape=09=3D false; =20 int term_fds[TERM_MAX_DEVS][2]; =20 +static pthread_t term_poll_thread; + int term_getc(struct kvm *kvm, int term) { =09unsigned char c; @@ -91,6 +93,30 @@ bool term_readable(int term) =09return poll(&pollfd, 1, 0) > 0; } =20 +static void *term_poll_thread_loop(void *param) +{ +=09struct pollfd fds[TERM_MAX_DEVS]; +=09struct kvm *kvm =3D (struct kvm *) param; + +=09int i; + +=09for (i =3D 0; i < TERM_MAX_DEVS; i++) { +=09=09fds[i].fd =3D term_fds[i][TERM_FD_IN]; +=09=09fds[i].events =3D POLLIN; +=09=09fds[i].revents =3D 0; +=09} + +=09while (1) { +=09=09/* Poll with infinite timeout */ +=09=09if(poll(fds, TERM_MAX_DEVS, -1) < 1) +=09=09=09break; +=09=09kvm__arch_read_term(kvm); +=09} + +=09die("term_poll_thread_loop: error polling device fds %d\n", errno); +=09return NULL; +} + static void term_cleanup(void) { =09int i; @@ -161,6 +187,11 @@ int term_init(struct kvm *kvm) =09term.c_lflag &=3D ~(ICANON | ECHO | ISIG); =09tcsetattr(STDIN_FILENO, TCSANOW, &term); =20 + +=09/* Use our own blocking thread to read stdin, don't require a tick */ +=09if(pthread_create(&term_poll_thread, NULL, term_poll_thread_loop,kvm)) +=09=09die("Unable to create console input poll thread\n"); + =09signal(SIGTERM, term_sig_cleanup); =09atexit(term_cleanup); =20 diff --git a/tools/kvm/x86/kvm.c b/tools/kvm/x86/kvm.c index 9b46772..d285d1d 100644 --- a/tools/kvm/x86/kvm.c +++ b/tools/kvm/x86/kvm.c @@ -374,7 +374,7 @@ int kvm__arch_free_firmware(struct kvm *kvm) =09return 0; } =20 -void kvm__arch_periodic_poll(struct kvm *kvm) +void kvm__arch_read_term(struct kvm *kvm) { =09serial8250__update_consoles(kvm); =09virtio_console__inject_interrupt(kvm); --=20 1.7.9.5