| 1 | /* |
| 2 | * Copyright (c) 2017 - 2021, Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included |
| 12 | * in all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | //! |
| 23 | //! \file cm_perf.cpp |
| 24 | //! \brief Contains functions which recode time and calculate performance |
| 25 | //! |
| 26 | |
| 27 | #include <iostream> |
| 28 | #include <cm_debug.h> |
| 29 | #include "cm_log.h" |
| 30 | |
| 31 | #if CM_LOG_ON |
| 32 | |
| 33 | CmTimer::CmTimer(const std::string FunctionName) |
| 34 | { |
| 35 | uint32_t success = MosUtilities::MosQueryPerformanceFrequency((uint64_t *)&m_freq.QuadPart); |
| 36 | CM_ASSERT(success); |
| 37 | |
| 38 | m_funcName = FunctionName; |
| 39 | m_cycles = 0; |
| 40 | m_start.QuadPart = 0; |
| 41 | m_end.QuadPart = 0; |
| 42 | m_bstopped = false; |
| 43 | |
| 44 | Start(); |
| 45 | } |
| 46 | |
| 47 | CmTimer::~CmTimer() |
| 48 | { |
| 49 | if(!m_bstopped) |
| 50 | { |
| 51 | Stop(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void CmTimer::Start() |
| 56 | { |
| 57 | uint32_t success = MosUtilities::MosQueryPerformanceCounter((uint64_t *)&m_start.QuadPart); |
| 58 | CM_ASSERT(success); |
| 59 | |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | void CmTimer::Stop() |
| 64 | { |
| 65 | uint32_t success = MosUtilities::MosQueryPerformanceCounter((uint64_t *)&m_end.QuadPart); |
| 66 | CM_ASSERT(success); |
| 67 | |
| 68 | m_bstopped = true; |
| 69 | |
| 70 | m_cycles = m_end.QuadPart - m_start.QuadPart; |
| 71 | |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | float CmTimer::TotalMilliSecond() |
| 76 | { |
| 77 | return (float)m_cycles*(float)1000.0 / (float)m_freq.QuadPart; |
| 78 | } |
| 79 | |
| 80 | std::string CmTimer::ToString() |
| 81 | { |
| 82 | std::ostringstream oss; |
| 83 | oss << m_funcName << " Duration " << TotalMilliSecond() << std::endl; |
| 84 | return oss.str(); |
| 85 | } |
| 86 | |
| 87 | #endif |