Debian Codegraph @

core/libs/dngwriter/extra/xmp_sdk/source/PerfUtils.cpp

raw
1 // =================================================================================================
2 // Copyright 2006 Adobe
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
6 // of the Adobe license agreement accompanying it. If you have received this file from a source other
7 // than Adobe, then your use, modification, or distribution of it requires the prior written permission
8 // of Adobe.
9 // =================================================================================================
10
11 #if AdobePrivate
12 // =================================================================================================
13 // Change history
14 // ==============
15 //
16 // Writers:
17 // AWL Alan Lillich
18 // ADC Amandeep Chawla
19 //
20 // mm-dd-yy who Description of changes, most recent on top.
21 //
22 // 03-21-13 ADC 5.6-f047 [3526905] In MAC and Linux, progress notifications are not returned to the registered callbacks as per the set time-interval.
23 // 03-13-13 ADC 5.6-f046 Remove deprecated OSX APIs from XMP toolkit code.
24 //
25 // 06-22-12 AWL 5.5-f019 Add file update progress tracking to the MPEG-4 handler.
26 //
27 // 09-21-06 AWL Initial creation.
28 //
29 // =================================================================================================
30 #endif
31
32 #include "public/include/XMP_Environment.h"
33 #include "public/include/XMP_Const.h"
34
35 #include "source/PerfUtils.hpp"
36
37 #include <stdio.h>
38
39 #if XMP_WinBuild
40 #pragma warning ( disable : 4996 ) // '...' was declared deprecated
41 #endif
42
43 // =================================================================================================
44 // =================================================================================================
45
46 #if XMP_WinBuild
47
48 #pragma warning ( disable : 4800 ) // forcing bool to 0/1
49
50 const char * PerfUtils::GetTimerInfo()
51 {
52 LARGE_INTEGER freq;
53 static char msgBuffer[1000];
54
55 bool ok = (bool) QueryPerformanceFrequency ( &freq );
56 if ( ! ok ) throw XMP_Error ( kXMPErr_ExternalFailure, "Failure from QueryPerformanceFrequency" );
57
58 if ( freq.HighPart != 0 ) {
59 return "Windows PerfUtils measures finer than nanoseconds, using the QueryPerformanceCounter() timer";
60 }
61
62 double rate = 1.0 / (double)freq.LowPart;
63 _snprintf ( msgBuffer, sizeof(msgBuffer),
64 "Windows PerfUtils measures %e second, using the QueryPerformanceCounter() timer", rate );
65 return msgBuffer;
66
67 } // PerfUtils::GetTimerInfo
68
69 // ------------------------------------------------------------------------------------------------
70
71 PerfUtils::MomentValue PerfUtils::NoteThisMoment()
72 {
73 LARGE_INTEGER now;
74
75 bool ok = (bool) QueryPerformanceCounter ( &now );
76 if ( ! ok ) throw XMP_Error ( kXMPErr_ExternalFailure, "Failure from QueryPerformanceCounter" );
77 return now.QuadPart;
78
79 } // PerfUtils::NoteThisMoment
80
81 // ------------------------------------------------------------------------------------------------
82
83 double PerfUtils::GetElapsedSeconds ( PerfUtils::MomentValue start, PerfUtils::MomentValue finish )
84 {
85 LARGE_INTEGER freq;
86
87 bool ok = (bool) QueryPerformanceFrequency ( &freq );
88 if ( ! ok ) throw XMP_Error ( kXMPErr_ExternalFailure, "Failure from QueryPerformanceFrequency" );
89
90 const double scale = (double)freq.QuadPart;
91
92 const double elapsed = (double)(finish - start) / scale;
93 return elapsed;
94
95 } // PerfUtils::GetElapsedSeconds
96
97 #endif
98
99 // =================================================================================================
100
101 #if XMP_UNIXBuild | XMP_AndroidBuild
102
103 const char * PerfUtils::GetTimerInfo()
104 {
105 return "UNIX PerfUtils measures nano seconds, using the POSIX clock_gettime() timer";
106 } // PerfUtils::GetTimerInfo
107
108 // ------------------------------------------------------------------------------------------------
109
110 PerfUtils::MomentValue PerfUtils::NoteThisMoment()
111 {
112 MomentValue moment = kZeroMoment;
113 if ( clock_gettime( CLOCK_MONOTONIC, &moment ) != 0 )
114 throw XMP_Error( kXMPErr_ExternalFailure, "Failure from clock_gettime" );
115 return moment;
116 } // PerfUtils::NoteThisMoment
117
118 // ------------------------------------------------------------------------------------------------
119
120 double PerfUtils::GetElapsedSeconds ( PerfUtils::MomentValue start, PerfUtils::MomentValue finish )
121 {
122 double startInSeconds = start.tv_sec + start.tv_nsec / 1000000000.0;
123 double finishInSeconds = finish.tv_sec + finish.tv_nsec / 1000000000.0;
124 return finishInSeconds - startInSeconds;
125 } // PerfUtils::GetElapsedSeconds
126
127 #endif
128
129 // =================================================================================================
130
131 #if XMP_MacBuild | XMP_iOSBuild
132
133 #include <mach/mach.h>
134 #include <mach/mach_time.h>
135
136 const char * PerfUtils::GetTimerInfo()
137 {
138 return "Mac PerfUtils measures nano seconds, using the mach_absolute_time() timer";
139 } // PerfUtils::GetTimerInfo
140
141 // ------------------------------------------------------------------------------------------------
142
143 PerfUtils::MomentValue PerfUtils::NoteThisMoment()
144 {
145 return mach_absolute_time();
146 } // PerfUtils::NoteThisMoment
147
148 // ------------------------------------------------------------------------------------------------
149
150 double PerfUtils::GetElapsedSeconds ( PerfUtils::MomentValue start, PerfUtils::MomentValue finish )
151 {
152 static mach_timebase_info_data_t sTimebaseInfo;
153 static double sConversionFactor = 0.0;
154 // If this is the first time we've run, get the timebase.
155 // We can use denom == 0 to indicate that sTimebaseInfo is
156 // uninitialized because it makes no sense to have a zero
157 // denominator is a fraction.
158
159 if ( sTimebaseInfo.denom == 0 ) {
160 mach_timebase_info(&sTimebaseInfo);
161 sConversionFactor = ((double)sTimebaseInfo.denom / (double)sTimebaseInfo.numer);
162 sConversionFactor /= 1000000000.0;
163 }
164
165 return ( finish - start ) * sConversionFactor;
166 } // PerfUtils::GetElapsedSeconds
167
168 #endif
169
170