diff --git a/Debug/CMakeLists.txt b/Debug/CMakeLists.txt index d7669501d615f90b86801987188c6b365dc3ed58..8066c78463fc4b4de3012890f878f5238370018a 100644 --- a/Debug/CMakeLists.txt +++ b/Debug/CMakeLists.txt @@ -1,8 +1,8 @@ set(my_headers ${CMAKE_CURRENT_SOURCE_DIR}/DebOut.hh ${CMAKE_CURRENT_SOURCE_DIR}/DebShow.hh + ${CMAKE_CURRENT_SOURCE_DIR}/DebTime.hh ${CMAKE_CURRENT_SOURCE_DIR}/DebUtils.hh - PARENT_SCOPE ) diff --git a/Debug/DebTime.hh b/Debug/DebTime.hh new file mode 100644 index 0000000000000000000000000000000000000000..001be33e3e7cacf35eb2a0d38bc84f17a9ebd423 --- /dev/null +++ b/Debug/DebTime.hh @@ -0,0 +1,65 @@ +// (C) Copyright 2015 by Autodesk, Inc. +// +// The information contained herein is confidential, proprietary +// to Autodesk, Inc., and considered a trade secret as defined +// in section 499C of the penal code of the State of California. +// Use of this information by anyone other than authorized +// employees of Autodesk, Inc. is granted only under a written +// non-disclosure agreement, expressly prescribing the scope +// and manner of such use. + +#ifndef BASE_DEBTIME_HH_INCLUDED +#define BASE_DEBTIME_HH_INCLUDED + +#include <Base/Utils/StopWatch.hh> +#include <Base/Debug/DebOut.hh> + +namespace Base { + +class StopWatchSession +{ +public: + StopWatchSession( + ReForm::DebEnter& _deb, + const char* _sssn_name = nullptr, + const int _deb_lvl = 2) + : deb_(_deb), sssn_name_(_sssn_name), deb_lvl_(_deb_lvl) + { + sw_.start(); + } + + ~StopWatchSession() + { + // TODO: implement "prettier" DEB out if seconds turn into minutes/hours/etc + if (!deb_.permission(deb_lvl_)) + return; + deb_.stream() << sssn_name_ << " took " << sw_.stop()/1000.0 << " s.\n" + << deb_.end(); + } + +private: + ReForm::DebEnter& deb_; + const char* sssn_name_; + const int deb_lvl_; + StopWatch sw_; + +private: + // disable copy and assignment + StopWatchSession(const StopWatchSession&); + StopWatchSession& operator=(const StopWatchSession&); +}; + +} //namespace Base + +#define DEB_time_session(SSSN, LL) \ + Base::StopWatchSession __sw_sssn(deb, SSSN, LL); + +#define DEB_time_session_def(SSSN) \ + Base::StopWatchSession __sw_sssn(deb, SSSN, 2); + +#define DEB_time_func(LL) DEB_enter_func \ + Base::StopWatchSession __sw_func(deb, __FUNCTION__, LL); + +#define DEB_time_func_def DEB_time_func(2) + +#endif//BASE_DEBTIME_HH_INCLUDED diff --git a/Utils/CMakeLists.txt b/Utils/CMakeLists.txt index b5ba2ec3203a9435dca9a0e98707fdffecb57a57..da78b37c8addf6686cf03b9c7d74819647ff8fef 100644 --- a/Utils/CMakeLists.txt +++ b/Utils/CMakeLists.txt @@ -1,9 +1,10 @@ set(my_headers ${CMAKE_CURRENT_SOURCE_DIR}/OutcomeUtils.hh - + ${CMAKE_CURRENT_SOURCE_DIR}/StopWatch.hh PARENT_SCOPE ) set(my_sources + ${CMAKE_CURRENT_SOURCE_DIR}/StopWatch.cc PARENT_SCOPE ) diff --git a/Utils/StopWatch.cc b/Utils/StopWatch.cc new file mode 100644 index 0000000000000000000000000000000000000000..8804452c8c1c59a3d002b21f593e13275791a6ed --- /dev/null +++ b/Utils/StopWatch.cc @@ -0,0 +1,143 @@ +// (C) Copyright 2015 by Autodesk, Inc. +// +// The information contained herein is confidential, proprietary +// to Autodesk, Inc., and considered a trade secret as defined +// in section 499C of the penal code of the State of California. +// Use of this information by anyone other than authorized +// employees of Autodesk, Inc. is granted only under a written +// non-disclosure agreement, expressly prescribing the scope +// and manner of such use. + +#include "Base/Security/Mandatory.hh" +#include "StopWatch.hh" + +#ifdef WIN32 + +// Windows implementation +////////////////////////////////////////////////////////////////////////// + +INSECURE_INCLUDE_SECTION_BEGIN +#include <windows.h> +INSECURE_INCLUDE_SECTION_END + +namespace Base { + +class StopWatch::Impl +{ +public: + Impl() + { + QueryPerformanceFrequency(&freq_); + } + + void start() + { + QueryPerformanceCounter(&starttime_); + } + + void stop() + { + QueryPerformanceCounter(&endtime_); + } + + double elapsed() const + { + return (double)(endtime_.QuadPart - starttime_.QuadPart) / + (double)freq_.QuadPart * 1000.0f; + } + +private: + LARGE_INTEGER starttime_, endtime_; + LARGE_INTEGER freq_; +}; + +} //namespace Base + +#else + +// Linux implementation +////////////////////////////////////////////////////////////////////////// + +#include <sys/time.h> + +namespace Base { + + +class StopWatch::Impl +{ +public: + StopWatch() + { + starttime_.tv_sec = starttime_.tv_usec = 0; + endtime_.tv_sec = endtime_.tv_usec = 0; + } + + void start() + { + starttime_ = current_time(); + } + + void stop() + { + endtime_ = current_time(); + } + + /// Get the total time in milli-seconds (watch has to be stopped). + double elapsed() const + { + return ((endtime_.tv_sec - starttime_.tv_sec )*1000.0 + + (endtime_.tv_usec - starttime_.tv_usec)*0.001); + } + +private: + timeval starttime_, endtime_; + +private: + static timeval current_time() + { + struct timeval tv; + gettimeofday(&tv, 0); + return tv; + } +}; + +} //namespace Base + +#endif// + +// StopWatch interface implementation +////////////////////////////////////////////////////////////////////////// + +namespace Base { + +StopWatch::StopWatch() + : impl_(new Impl) +{} + +StopWatch::~StopWatch() +{ delete impl_; } + +void StopWatch::start() +{ + impl_->start(); +} + +double StopWatch::restart() +{ + double t = stop(); + start(); + return t; +} + +double StopWatch::stop() +{ + impl_->stop(); + return elapsed(); +} + +double StopWatch::elapsed() const +{ + return impl_->elapsed(); +} + +} // namespace Base diff --git a/Utils/StopWatch.hh b/Utils/StopWatch.hh new file mode 100644 index 0000000000000000000000000000000000000000..230f57b02031b3069b2d71b0e9d960a839ef29d8 --- /dev/null +++ b/Utils/StopWatch.hh @@ -0,0 +1,100 @@ +//============================================================================= +// +// OpenFlipper +// Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen +// www.openflipper.org +// +//----------------------------------------------------------------------------- +// +// License +// +// OpenFlipper is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// OpenFlipper is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with OpenFlipper. If not, see <http://www.gnu.org/licenses/>. +// +//----------------------------------------------------------------------------- +// +// $Revision: 3468 $ +// $Author: moebius $ +// $Date: 2008-10-17 14:58:52 +0200 (Fri, 17 Oct 2008) $ +// +//============================================================================= + +// (C) Copyright 2015 by Autodesk, Inc. +// +// The information contained herein is confidential, proprietary +// to Autodesk, Inc., and considered a trade secret as defined +// in section 499C of the penal code of the State of California. +// Use of this information by anyone other than authorized +// employees of Autodesk, Inc. is granted only under a written +// non-disclosure agreement, expressly prescribing the scope +// and manner of such use. + +//============================================================================= +// +// CLASS StopWatch +// +//============================================================================= + +#ifndef BASE_STOPWATCH_HH_INCLUDED +#define BASE_STOPWATCH_HH_INCLUDED + +namespace Base { + +//== CLASS DEFINITION ========================================================= + +/** \class StopWatch StopWatch.hh + + This class can be used for measuring time, providing optimal granularity + for the current platform. +**/ + +class StopWatch +{ +public: + + /// Constructor + StopWatch(); + + /// Destructor + ~StopWatch(); + + /// Start time measurement + void start(); + + /// Restart, return time elapsed until now. + double restart(); + + /// Stop time measurement, return time. + double stop(); + + /// Get the total time in milli-seconds (watch has to be stopped). + double elapsed() const; + +private: + class Impl; + Impl* impl_; + +private: + // disable copy and assignment + StopWatch(const StopWatch&); + StopWatch& operator=(const StopWatch&); +}; + + +//============================================================================= +} // namespace Base + +//============================================================================= +#endif//BASE_STOPWATCH_HH_INCLUDED +//============================================================================= +