From: Peter Lieven <pl@kamp.de>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, mreitz@redhat.com, pbonzini@redhat.com,
mst@redhat.com, dgilbert@redhat.com, peter.maydell@linaro.org,
eblake@redhat.com, rth@twiddle.net, Peter Lieven <pl@kamp.de>
Subject: [Qemu-devel] [PATCH V2 5/6] oslib-posix: add a configure switch to debug stack usage
Date: Mon, 4 Jul 2016 14:31:26 +0200 [thread overview]
Message-ID: <1467635487-9329-6-git-send-email-pl@kamp.de> (raw)
In-Reply-To: <1467635487-9329-1-git-send-email-pl@kamp.de>
this adds a knob to track the maximum stack usage of stacks
created by qemu_alloc_stack.
Signed-off-by: Peter Lieven <pl@kamp.de>
---
configure | 19 +++++++++++++++++++
util/oslib-posix.c | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+)
diff --git a/configure b/configure
index 93e4c95..d13ad1d 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"
@@ -4307,6 +4310,17 @@ 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 "$cpu" = "ia64" -o "$cpu" = "hppa"; then
+ error_exit "stack usage debugging is not supported for $cpu"
+ fi
+ 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
@@ -4884,6 +4898,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"
@@ -5354,6 +5369,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 1ce35ef..4298465 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -50,6 +50,10 @@
#include <qemu/mmap-alloc.h>
+#ifdef CONFIG_DEBUG_STACK_USAGE
+#include "qemu/error-report.h"
+#endif
+
int qemu_get_thread_id(void)
{
#if defined(__linux__)
@@ -501,6 +505,9 @@ pid_t qemu_fork(Error **errp)
void *qemu_alloc_stack(size_t sz)
{
void *ptr, *guardpage;
+#ifdef CONFIG_DEBUG_STACK_USAGE
+ void *ptr2;
+#endif
size_t pagesz = getpagesize();
/* avoid stacks smaller than _SC_THREAD_STACK_MIN */
@@ -528,11 +535,41 @@ void *qemu_alloc_stack(size_t sz)
abort();
}
+#ifdef CONFIG_DEBUG_STACK_USAGE
+ for (ptr2 = ptr + pagesz; ptr2 < ptr + sz; ptr2 += sizeof(uint32_t)) {
+ *(uint32_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
+ unsigned int usage;
+ void *ptr;
+#endif
sz = MAX(sz, sysconf(_SC_THREAD_STACK_MIN));
+
+#ifdef CONFIG_DEBUG_STACK_USAGE
+ for (ptr = stack + getpagesize(); ptr < stack + sz;
+ ptr += sizeof(uint32_t)) {
+ if (*(uint32_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
+
munmap(stack, sz);
}
--
1.9.1
next prev parent reply other threads:[~2016-07-04 12:31 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-04 12:31 [Qemu-devel] [PATCH V2 0/6] coroutine: mmap stack memory and stack size Peter Lieven
2016-07-04 12:31 ` [Qemu-devel] [PATCH V2 1/6] oslib-posix: add helpers for stack alloc and free Peter Lieven
2016-07-06 8:12 ` Markus Armbruster
2016-07-08 17:18 ` Richard Henderson
2016-07-04 12:31 ` [Qemu-devel] [PATCH V2 2/6] coroutine: add a macro for the coroutine stack size Peter Lieven
2016-07-04 12:31 ` [Qemu-devel] [PATCH V2 3/6] coroutine-ucontext: use helper for allocating stack memory Peter Lieven
2016-07-04 12:31 ` [Qemu-devel] [PATCH V2 4/6] coroutine-sigaltstack: " Peter Lieven
2016-07-04 12:31 ` Peter Lieven [this message]
2016-07-04 12:31 ` [Qemu-devel] [PATCH V2 6/6] coroutine: reduce stack size to 64kB Peter Lieven
2016-07-06 8:13 ` Markus Armbruster
2016-07-04 14:54 ` [Qemu-devel] [PATCH V2 0/6] coroutine: mmap stack memory and stack size Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1467635487-9329-6-git-send-email-pl@kamp.de \
--to=pl@kamp.de \
--cc=dgilbert@redhat.com \
--cc=eblake@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=rth@twiddle.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.