RavEngine
Loading...
Searching...
No Matches
NvFlowTimer.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2014-2022 NVIDIA Corporation. All rights reserved.
26
27#pragma once
28
29#include "NvFlowTypes.h"
30
31#if defined(_WIN32)
32#include <Windows.h>
33#else
34#include <time.h>
35#endif
36
37#include "NvFlowArray.h"
38
39NV_FLOW_INLINE void NvFlowTimeStamp_capture(NvFlowUint64* ptr)
40{
41#if defined(_WIN32)
42 LARGE_INTEGER tmpCpuTime = {};
43 QueryPerformanceCounter(&tmpCpuTime);
44 (*ptr) = tmpCpuTime.QuadPart;
45#else
46 timespec timeValue = {};
47 clock_gettime(CLOCK_MONOTONIC, &timeValue);
48 (*ptr) = 1E9 * NvFlowUint64(timeValue.tv_sec) + NvFlowUint64(timeValue.tv_nsec);
49#endif
50}
51
52NV_FLOW_INLINE NvFlowUint64 NvFlowTimeStamp_frequency()
53{
54#if defined(_WIN32)
55 LARGE_INTEGER tmpCpuFreq = {};
56 QueryPerformanceFrequency(&tmpCpuFreq);
57 return tmpCpuFreq.QuadPart;
58#else
59 return 1E9;
60#endif
61}
62
63NV_FLOW_INLINE float NvFlowTimeStamp_diff(NvFlowUint64 begin, NvFlowUint64 end, NvFlowUint64 freq)
64{
65 return (float)(((double)(end - begin) / (double)(freq)));
66}
67
68#if 1
69#define NV_FLOW_PROFILE_BEGIN(profileInterval, profileOffset)
70#define NV_FLOW_PROFILE_TIMESTAMP(name)
71#define NV_FLOW_PROFILE_FLUSH(name, logPrint)
72#else
73#define NV_FLOW_PROFILE_BEGIN(profileInterval, profileOffset) \
74 static int profileCount = profileOffset; \
75 profileCount++; \
76 if (profileCount >= profileInterval) \
77 { \
78 profileCount = 0; \
79 } \
80 NvFlowArray<NvFlowUint64, 32u> profileTimes; \
81 NvFlowArray<const char*, 32u> profileNames; \
82 const NvFlowBool32 profileEnabled = (profileCount == 0);
83
84#define NV_FLOW_PROFILE_TIMESTAMP(name) \
85 if (profileEnabled) \
86 { \
87 NvFlowTimeStamp_capture(&profileTimes[profileTimes.allocateBack()]); \
88 profileNames.pushBack(#name); \
89 }
90
91#define NV_FLOW_PROFILE_FLUSH(name, logPrint) \
92 if (profileEnabled && logPrint && profileTimes.size >= 2u) \
93 { \
94 NvFlowUint64 freq = NvFlowTimeStamp_frequency(); \
95 float totalTime = NvFlowTimeStamp_diff(profileTimes[0u], profileTimes[profileTimes.size - 1u], freq); \
96 for (NvFlowUint64 idx = 1u; idx < profileTimes.size; idx++) \
97 { \
98 float time = NvFlowTimeStamp_diff(profileTimes[idx - 1u], profileTimes[idx], freq); \
99 if (time >= 0.001f * totalTime) \
100 { \
101 logPrint(eNvFlowLogLevel_warning, "[%s] %f ms", profileNames[idx], 1000.f * time); \
102 } \
103 } \
104 logPrint(eNvFlowLogLevel_warning, "Total [%s] %f ms", #name, 1000.f * totalTime); \
105 }
106#endif