From d8bf5b9fff2be75eea08b874c3d97754fc646845 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 10 Apr 2019 14:50:34 -0700 Subject: [PATCH] ircd::ctx: Add a syscall_usage_warning to suite. --- include/ircd/ctx/ctx.h | 1 + include/ircd/ctx/syscall_usage_warning.h | 47 ++++++++++++++++++++++++ ircd/ctx.cc | 41 +++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 include/ircd/ctx/syscall_usage_warning.h diff --git a/include/ircd/ctx/ctx.h b/include/ircd/ctx/ctx.h index 4f42fe3a6..11a150200 100644 --- a/include/ircd/ctx/ctx.h +++ b/include/ircd/ctx/ctx.h @@ -74,6 +74,7 @@ namespace ircd::ctx #include "this_ctx.h" #include "stack_usage_assertion.h" #include "slice_usage_warning.h" +#include "syscall_usage_warning.h" #include "critical_assertion.h" #include "critical_indicator.h" #include "exception_handler.h" diff --git a/include/ircd/ctx/syscall_usage_warning.h b/include/ircd/ctx/syscall_usage_warning.h new file mode 100644 index 000000000..6e7c3cbcc --- /dev/null +++ b/include/ircd/ctx/syscall_usage_warning.h @@ -0,0 +1,47 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2019 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_CTX_SYSCALL_USAGE_WARNING_H + +namespace ircd::ctx::this_ctx +{ + struct syscall_usage_warning; +} + +#ifndef NDEBUG +struct ircd::ctx::this_ctx::syscall_usage_warning +{ + const string_view fmt; + const va_rtti ap; + ircd::prof::syscall_timer timer; + + template + syscall_usage_warning(const string_view &fmt, args&&... a) + :syscall_usage_warning{fmt, va_rtti{std::forward(a)...}} + {} + + explicit syscall_usage_warning(const string_view &fmt, va_rtti &&ap) + :fmt{fmt} + ,ap{std::move(ap)} + {} + + ~syscall_usage_warning() noexcept; +}; +#else +struct ircd::ctx::this_ctx::syscall_usage_warning +{ + syscall_usage_warning(const string_view &fmt, ...) + { + // In at least gcc 6.3.0 20170519, template param packs are not DCE'ed + // so legacy varargs are used here. + } +}; +#endif diff --git a/ircd/ctx.cc b/ircd/ctx.cc index a214e6e81..60521b548 100644 --- a/ircd/ctx.cc +++ b/ircd/ctx.cc @@ -854,6 +854,47 @@ noexcept } #endif +/////////////////////////////////////////////////////////////////////////////// +// +// ctx/syscall_usage_warning.h +// + +#ifndef NDEBUG +ircd::ctx::this_ctx::syscall_usage_warning::~syscall_usage_warning() +noexcept +{ + const uint64_t total + { + timer.stopped? + timer.at(): + timer.stop() + }; + + if(likely(!total)) + return; + + thread_local char buf[512]; + const string_view reason + { + fmt::vsprintf{buf, fmt, ap} + }; + + thread_local char tmbuf[64]; + log::dwarning + { + log, "context '%s' id:%lu watchdog: system call took %s :%s", + current? + name(cur()): + ios::handler::current? + name(*ios::handler::current): + "*"_sv, + current? id(cur()) : 0, + pretty_nanoseconds(tmbuf, total, true), + reason + }; +} +#endif + /////////////////////////////////////////////////////////////////////////////// // // ctx/slice_usage_warning.h