From patchwork Thu Jun 30 07:37:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Lieven X-Patchwork-Id: 9206839 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 ECF5260752 for ; Thu, 30 Jun 2016 07:51:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD58A28594 for ; Thu, 30 Jun 2016 07:51:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CD703285BA; Thu, 30 Jun 2016 07:51:13 +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.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3020528594 for ; Thu, 30 Jun 2016 07:51:13 +0000 (UTC) Received: from localhost ([::1]:47553 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bIWki-0006Ah-6k for patchwork-qemu-devel@patchwork.kernel.org; Thu, 30 Jun 2016 03:51:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58679) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bIWXf-00080h-1x for qemu-devel@nongnu.org; Thu, 30 Jun 2016 03:37:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bIWXd-0007vq-Lt for qemu-devel@nongnu.org; Thu, 30 Jun 2016 03:37:42 -0400 Received: from mx-v6.kamp.de ([2a02:248:0:51::16]:33566 helo=mx01.kamp.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bIWXd-0007vf-Bu for qemu-devel@nongnu.org; Thu, 30 Jun 2016 03:37:41 -0400 Received: (qmail 23015 invoked by uid 89); 30 Jun 2016 07:37:40 -0000 Received: from [195.62.97.28] by client-16-kamp (envelope-from , uid 89) with qmail-scanner-2010/03/19-MF (clamdscan: 0.99.2/21815. hbedv: 8.3.40.44/7.12.99.34. avast: 1.2.2/16062901. spamassassin: 3.4.1. Clear:RC:1(195.62.97.28):. Processed in 0.228186 secs); 30 Jun 2016 07:37:40 -0000 Received: from smtp.kamp.de (HELO submission.kamp.de) ([195.62.97.28]) by mx01.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted); 30 Jun 2016 07:37:37 -0000 X-GL_Whitelist: yes Received: (qmail 6048 invoked from network); 30 Jun 2016 07:37:27 -0000 Received: from lieven-pc.kamp-intra.net (HELO lieven-pc) (relay@kamp.de@::ffff:172.21.12.60) by submission.kamp.de with ESMTPS (DHE-RSA-AES256-GCM-SHA384 encrypted) ESMTPA; 30 Jun 2016 07:37:27 -0000 Received: by lieven-pc (Postfix, from userid 1000) id B21992084D; Thu, 30 Jun 2016 09:37:23 +0200 (CEST) From: Peter Lieven To: qemu-devel@nongnu.org Date: Thu, 30 Jun 2016 09:37:19 +0200 Message-Id: <1467272240-32123-6-git-send-email-pl@kamp.de> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1467272240-32123-1-git-send-email-pl@kamp.de> References: <1467272240-32123-1-git-send-email-pl@kamp.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a02:248:0:51::16 Subject: [Qemu-devel] [PATCH 5/6] oslib-posix: add a configure switch to debug stack usage 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: kwolf@redhat.com, peter.maydell@linaro.org, mst@redhat.com, Peter Lieven , dgilbert@redhat.com, mreitz@redhat.com, pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP this adds a knob to track the maximum stack usage of stacks created by qemu_alloc_stack. Signed-off-by: Peter Lieven --- configure | 15 +++++++++++++++ util/oslib-posix.c | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/configure b/configure index 5929aba..ba66916 100755 --- a/configure +++ b/configure @@ -296,6 +296,7 @@ libiscsi="" libnfs="" coroutine="" coroutine_pool="" +debug_stack_usage="no" seccomp="" glusterfs="" glusterfs_xlator_opt="no" @@ -1004,6 +1005,8 @@ for opt do ;; --enable-coroutine-pool) coroutine_pool="yes" ;; + --enable-debug-stack-usage) debug_stack_usage="yes" + ;; --disable-docs) docs="no" ;; --enable-docs) docs="yes" @@ -4298,6 +4301,13 @@ fi if test "$coroutine" = "gthread" -a "$coroutine_pool" = "yes"; then error_exit "'gthread' coroutine backend does not support pool (use --disable-coroutine-pool)" fi +if test "$debug_stack_usage" = "yes"; then + if test "$coroutine_pool" = "yes"; then + echo "WARN: disabling coroutine pool for stack usage debugging" + coroutine_pool=no + fi +fi + ########################################## # check if we have open_by_handle_at @@ -4866,6 +4876,7 @@ echo "QGA MSI support $guest_agent_msi" echo "seccomp support $seccomp" echo "coroutine backend $coroutine" echo "coroutine pool $coroutine_pool" +echo "debug stack usage $debug_stack_usage" echo "GlusterFS support $glusterfs" echo "Archipelago support $archipelago" echo "gcov $gcov_tool" @@ -5336,6 +5347,10 @@ else echo "CONFIG_COROUTINE_POOL=0" >> $config_host_mak fi +if test "$debug_stack_usage" = "yes" ; then + echo "CONFIG_DEBUG_STACK_USAGE=y" >> $config_host_mak +fi + if test "$open_by_handle_at" = "yes" ; then echo "CONFIG_OPEN_BY_HANDLE=y" >> $config_host_mak fi diff --git a/util/oslib-posix.c b/util/oslib-posix.c index a567a7d..0a9f2b2 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -50,6 +50,10 @@ #include +#ifdef CONFIG_DEBUG_STACK_USAGE +#include "qemu/error-report.h" +#endif + int qemu_get_thread_id(void) { #if defined(__linux__) @@ -500,6 +504,9 @@ pid_t qemu_fork(Error **errp) void *qemu_alloc_stack(size_t sz) { +#ifdef CONFIG_DEBUG_STACK_USAGE + void *ptr2; +#endif /* allocate sz bytes plus one extra page for a guard * page at the bottom of the stack */ void *ptr = mmap(NULL, sz + getpagesize(), PROT_NONE, @@ -511,11 +518,37 @@ void *qemu_alloc_stack(size_t sz) MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) == MAP_FAILED) { abort(); } - return ptr + getpagesize(); + ptr += getpagesize(); +#ifdef CONFIG_DEBUG_STACK_USAGE + for (ptr2 = ptr; ptr2 < ptr + sz; ptr2 += sizeof(u_int32_t)) { + *(u_int32_t *)ptr2 = 0xdeadbeaf; + } +#endif + return ptr; } +#ifdef CONFIG_DEBUG_STACK_USAGE +static __thread unsigned int max_stack_usage; +#endif + void qemu_free_stack(void *stack, size_t sz) { +#ifdef CONFIG_DEBUG_STACK_USAGE + void *ptr; + unsigned int usage; + for (ptr = stack; ptr < stack + sz; ptr += sizeof(u_int32_t)) { + if (*(u_int32_t *)ptr != 0xdeadbeaf) { + break; + } + } + usage = sz - (uintptr_t) (ptr - stack); + if (usage > max_stack_usage) { + error_report("thread %d max stack usage increased from %u to %u", + qemu_get_thread_id(), max_stack_usage, usage); + max_stack_usage = usage; + } +#endif + /* unmap the stack plus the extra guard page */ munmap(stack - getpagesize(), sz + getpagesize()); }