OpenCV 4.5.3(日本語機械翻訳)
allocator_stats.impl.hpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4
5 #ifndef OPENCV_CORE_ALLOCATOR_STATS_IMPL_HPP
6 #define OPENCV_CORE_ALLOCATOR_STATS_IMPL_HPP
7
8 #include "./allocator_stats.hpp"
9
10 //#define OPENCV_DISABLE_ALLOCATOR_STATS
11
12 #ifdef CV_CXX11
13
14 #include <atomic>
15
16 #ifndef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE
17 #if defined(__GNUC__) && (\
18 (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 4) || \
19 (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && !defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)) \
20 )
21 #define OPENCV_ALLOCATOR_STATS_COUNTER_TYPE int
22 #endif
23 #endif
24
25 #ifndef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE
26 #define OPENCV_ALLOCATOR_STATS_COUNTER_TYPE long long
27 #endif
28
29 #else // CV_CXX11
30
31 #ifndef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE
32 #define OPENCV_ALLOCATOR_STATS_COUNTER_TYPE int // CV_XADD supports int only
33 #endif
34
35 #endif // CV_CXX11
36
37 namespace cv { namespace utils {
38
39 #ifdef CV__ALLOCATOR_STATS_LOG
40 namespace {
41 #endif
42
44{
45 #ifdef OPENCV_DISABLE_ALLOCATOR_STATS
46
47 public:
49 ~AllocatorStatistics() CV_OVERRIDE {}
50
51 uint64_t getCurrentUsage() const CV_OVERRIDE { return 0; }
52 uint64_t getTotalUsage() const CV_OVERRIDE { return 0; }
53 uint64_t getNumberOfAllocations() const CV_OVERRIDE { return 0; }
54 uint64_t getPeakUsage() const CV_OVERRIDE { return 0; }
55
57 void resetPeakUsage() CV_OVERRIDE {};
58
59 void onAllocate(size_t /*sz*/) {}
60 void onFree(size_t /*sz*/) {}
61
62 #elif defined(CV_CXX11)
63
64 protected:
65 typedef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE counter_t;
66 std::atomic<counter_t> curr, total, total_allocs, peak;
67 public:
69 ~AllocatorStatistics() CV_OVERRIDE {}
70
71 uint64_t getCurrentUsage() const CV_OVERRIDE { return (uint64_t)curr.load(); }
72 uint64_t getTotalUsage() const CV_OVERRIDE { return (uint64_t)total.load(); }
73 uint64_t getNumberOfAllocations() const CV_OVERRIDE { return (uint64_t)total_allocs.load(); }
74 uint64_t getPeakUsage() const CV_OVERRIDE { return (uint64_t)peak.load(); }
75
77 void resetPeakUsage() CV_OVERRIDE { peak.store(curr.load()); }
78
79 // Controller interface
80 void onAllocate(size_t sz)
81 {
82 #ifdef CV__ALLOCATOR_STATS_LOG
83 CV__ALLOCATOR_STATS_LOG(cv::format("allocate: %lld (curr=%lld)", (long long int)sz, (long long int)curr.load()));
84 #endif
85
86 counter_t new_curr = curr.fetch_add((counter_t)sz) + (counter_t)sz;
87
88 // peak = std::max((uint64_t)peak, new_curr);
89 auto prev_peak = peak.load();
90 while (prev_peak < new_curr)
91 {
92 if (peak.compare_exchange_weak(prev_peak, new_curr))
93 break;
94 }
95 // end of peak = max(...)
96
97 total += (counter_t)sz;
98 total_allocs++;
99 }
100 void onFree(size_t sz)
101 {
102 #ifdef CV__ALLOCATOR_STATS_LOG
103 CV__ALLOCATOR_STATS_LOG(cv::format("free: %lld (curr=%lld)", (long long int)sz, (long long int)curr.load()));
104 #endif
105 curr -= (counter_t)sz;
106 }
107
108 #else // non C++11
109
110 protected:
111 typedef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE counter_t;
112 volatile counter_t curr, total, total_allocs, peak; // overflow is possible, CV_XADD operates with 'int' only
113 public:
115 : curr(0), total(0), total_allocs(0), peak(0)
116 {}
117 ~AllocatorStatistics() CV_OVERRIDE {}
118
119 uint64_t getCurrentUsage() const CV_OVERRIDE { return (uint64_t)curr; }
120 uint64_t getTotalUsage() const CV_OVERRIDE { return (uint64_t)total; }
121 uint64_t getNumberOfAllocations() const CV_OVERRIDE { return (uint64_t)total_allocs; }
122 uint64_t getPeakUsage() const CV_OVERRIDE { return (uint64_t)peak; }
123
124 void resetPeakUsage() CV_OVERRIDE { peak = curr; }
125
126 // Controller interface
127 void onAllocate(size_t sz)
128 {
129 #ifdef CV__ALLOCATOR_STATS_LOG
130 CV__ALLOCATOR_STATS_LOG(cv::format("allocate: %lld (curr=%lld)", (long long int)sz, (long long int)curr));
131 #endif
132
133 counter_t new_curr = (counter_t)CV_XADD(&curr, (counter_t)sz) + (counter_t)sz;
134
135 peak = std::max((counter_t)peak, new_curr); // non-thread safe
136
137 //CV_XADD(&total, (uint64_t)sz); // overflow with int, non-reliable...
138 total += sz;
139
140 CV_XADD(&total_allocs, (counter_t)1);
141 }
142 void onFree(size_t sz)
143 {
144 #ifdef CV__ALLOCATOR_STATS_LOG
145 CV__ALLOCATOR_STATS_LOG(cv::format("free: %lld (curr=%lld)", (long long int)sz, (long long int)curr));
146 #endif
147 CV_XADD(&curr, (counter_t)-sz);
148 }
149 #endif
150};
151
152 #ifdef CV__ALLOCATOR_STATS_LOG
153} // namespace
154 #endif
155
156}} // namespace
157
158 #endif // OPENCV_CORE_ALLOCATOR_STATS_IMPL_HPP
Definition: allocator_stats.impl.hpp:44
void resetPeakUsage() CV_OVERRIDE
Definition: allocator_stats.impl.hpp:124
Definition: allocator_stats.hpp:13
CV_EXPORTS_W void max(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element maximum of two arrays or an array and a scalar.
cv
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75