From: Jonathan Austin Subject: [PATCH v2 2/3] kvm tools: remove periodic tick in favour of a polling thread Date: Wed, 4 Sep 2013 14:25:47 +0100 Message-ID: <1378301148-18823-3-git-send-email-jonathan.austin@arm.com> References: <1378301148-18823-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, Matt.Evans@arm.com, Jonathan Austin To: kvm@vger.kernel.org Return-path: Received: from service87.mimecast.com ([91.220.42.44]:49724 "EHLO service87.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1762611Ab3IDN0c (ORCPT ); Wed, 4 Sep 2013 09:26:32 -0400 In-Reply-To: <1378301148-18823-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'. Signed-off-by: Jonathan Austin Acked-by: Marc Zyngier --- tools/kvm/arm/kvm.c | 2 +- tools/kvm/builtin-run.c | 13 ----------- 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 +- 7 files changed, 35 insertions(+), 67 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/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