//------------------------------------------------------------------------------ /* This file is part of rippled: https://github.com/ripple/rippled Copyright (c) 2012, 2013 Ripple Labs Inc. 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 appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #ifndef RIPPLE_BASICS_LOGGEDTIMINGS_H_INCLUDED #define RIPPLE_BASICS_LOGGEDTIMINGS_H_INCLUDED #include #include #include #include namespace ripple { namespace detail { /** Template class that performs destruction of an object. Default implementation simply calls delete */ template struct Destroyer; /** Specialization for std::shared_ptr. */ template struct Destroyer > { static void destroy (std::shared_ptr & p) { p.reset (); } }; /** Specialization for std::unordered_map */ template struct Destroyer > { static void destroy (std::unordered_map & v) { v.clear (); } }; /** Cleans up an elaspsed time so it prints nicely */ inline double cleanElapsed (double seconds) noexcept { if (seconds >= 10) return std::floor (seconds + 0.5); return static_cast ((seconds * 10 + 0.5) / 10); } } // detail //------------------------------------------------------------------------------ /** Measure the time required to destroy an object. */ template double timedDestroy (Object& object) { std::int64_t const startTime (beast::Time::getHighResolutionTicks ()); detail::Destroyer ::destroy (object); return beast::Time::highResolutionTicksToSeconds ( beast::Time::getHighResolutionTicks () - startTime); } /** Log the timed destruction of an object if the time exceeds a threshold. */ template void logTimedDestroy ( Object& object, std::string const& objectDescription, double thresholdSeconds = 1) { double const seconds = timedDestroy (object); if (seconds > thresholdSeconds) { deprecatedLogs().journal("LoggedTimings").warning << objectDescription << " took " << detail::cleanElapsed (seconds) << " seconds to destroy"; } } //------------------------------------------------------------------------------ /** Log a timed function call if the time exceeds a threshold. */ template void logTimedCall ( beast::Journal::Stream stream, std::string const& description, char const* fileName, int lineNumber, Function f, double thresholdSeconds = 1) { double const seconds = beast::measureFunctionCallTime (f); if (seconds > thresholdSeconds) { stream << description << " took "<< detail::cleanElapsed (seconds) << " seconds to execute at " << beast::Debug::getSourceLocation (fileName, lineNumber); } } } // ripple #endif