timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
units.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to
9// deal in the Software without restriction, including without limitation the
10// rights to use, copy, modify, merge, publish, distribute, sublicense, and
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23// IN THE SOFTWARE.
24
25/** \file timemory/units.hpp
26 * \headerfile timemory/units.hpp "timemory/units.hpp"
27 * Timing and memory units
28 *
29 */
30
31#pragma once
32
35
36#include <cctype>
37#include <cstdint>
38#include <cstdlib>
39#include <iostream>
40#include <ratio>
41#include <string>
42#include <tuple>
43#include <vector>
44
45#if defined(TIMEMORY_UNIX)
46# include <unistd.h>
47#endif
48
49#if defined(TIMEMORY_WINDOWS)
50// without this, windows will define macros for min and max
51# if !defined(NOMINMAX)
52# define NOMINMAX
53# endif
54# if !defined(WIN32_LEAN_AND_MEAN)
55# define WIN32_LEAN_AND_MEAN
56# endif
57// clang-format off
58# include <windows.h>
59# include <sysinfoapi.h>
60# include <time.h>
61// clang-format on
62#endif
63
64namespace tim
65{
66//--------------------------------------------------------------------------------------//
67
68namespace units
69{
70static constexpr int64_t nsec = 1;
71static constexpr int64_t usec = 1000 * nsec;
72static constexpr int64_t msec = 1000 * usec;
73static constexpr int64_t csec = 10 * msec;
74static constexpr int64_t dsec = 10 * csec;
75static constexpr int64_t sec = 10 * dsec;
76static constexpr int64_t minute = 60 * sec;
77static constexpr int64_t hour = 60 * minute;
78
79static constexpr int64_t byte = 1;
80static constexpr int64_t kilobyte = 1000 * byte;
81static constexpr int64_t megabyte = 1000 * kilobyte;
82static constexpr int64_t gigabyte = 1000 * megabyte;
83static constexpr int64_t terabyte = 1000 * gigabyte;
84static constexpr int64_t petabyte = 1000 * terabyte;
85
86static constexpr int64_t kibibyte = 1024 * byte;
87static constexpr int64_t mebibyte = 1024 * kibibyte;
88static constexpr int64_t gibibyte = 1024 * mebibyte;
89static constexpr int64_t tebibyte = 1024 * gibibyte;
90static constexpr int64_t pebibyte = 1024 * tebibyte;
91
92static constexpr int64_t B = 1;
93static constexpr int64_t KB = 1000 * B;
94static constexpr int64_t MB = 1000 * KB;
95static constexpr int64_t GB = 1000 * MB;
96static constexpr int64_t TB = 1000 * GB;
97static constexpr int64_t PB = 1000 * TB;
98
99static constexpr int64_t Bi = 1;
100static constexpr int64_t KiB = 1024 * Bi;
101static constexpr int64_t MiB = 1024 * KiB;
102static constexpr int64_t GiB = 1024 * MiB;
103static constexpr int64_t TiB = 1024 * GiB;
104static constexpr int64_t PiB = 1024 * TiB;
105
106#if defined(TIMEMORY_LINUX)
107
108inline int64_t
109get_page_size()
110{
111 static auto _pagesz = sysconf(_SC_PAGESIZE);
112 return _pagesz;
113}
114const int64_t clocks_per_sec = sysconf(_SC_CLK_TCK);
115
116#elif defined(TIMEMORY_MACOS)
117
118inline int64_t
119get_page_size()
120{
121 static auto _pagesz = getpagesize();
122 return _pagesz;
123}
124const int64_t clocks_per_sec = sysconf(_SC_CLK_TCK);
125
126#elif defined(TIMEMORY_WINDOWS)
127
128inline int64_t
129get_page_size()
130{
131 static auto _pagesz = []() {
132 SYSTEM_INFO sysInfo;
133 GetSystemInfo(&sysInfo);
134 return sysInfo.dwPageSize;
135 }();
136 return _pagesz;
137}
138const int64_t clocks_per_sec = CLOCKS_PER_SEC;
139
140#endif
141
142//--------------------------------------------------------------------------------------//
143
144inline std::string
145time_repr(int64_t _unit)
146{
147 std::string _sunit;
148 switch(_unit)
149 {
150 case nsec: _sunit = "nsec"; break;
151 case usec: _sunit = "usec"; break;
152 case msec: _sunit = "msec"; break;
153 case csec: _sunit = "csec"; break;
154 case dsec: _sunit = "dsec"; break;
155 case sec: _sunit = "sec"; break;
156 default: _sunit = "UNK"; break;
157 }
158 return _sunit;
159}
160
161//--------------------------------------------------------------------------------------//
162
163inline std::string
164mem_repr(int64_t _unit)
165{
166 std::string _sunit;
167 switch(_unit)
168 {
169 case byte: _sunit = "B"; break;
170 case kilobyte: _sunit = "KB"; break;
171 case megabyte: _sunit = "MB"; break;
172 case gigabyte: _sunit = "GB"; break;
173 case terabyte: _sunit = "TB"; break;
174 case petabyte: _sunit = "PB"; break;
175 case kibibyte: _sunit = "KiB"; break;
176 case mebibyte: _sunit = "MiB"; break;
177 case gibibyte: _sunit = "GiB"; break;
178 case tebibyte: _sunit = "TiB"; break;
179 case pebibyte: _sunit = "PiB"; break;
180 default: _sunit = "UNK"; break;
181 }
182 return _sunit;
183}
184
185//--------------------------------------------------------------------------------------//
186
187inline std::tuple<std::string, int64_t>
189{
190 using string_t = std::string;
191 using return_type = std::tuple<string_t, int64_t>;
192 using inner_t = std::tuple<string_t, string_t, int64_t>;
193
194 if(_unit.length() == 0)
195 return return_type{ "MB", tim::units::megabyte };
196
197 for(auto& itr : _unit)
198 itr = tolower(itr);
199
200 for(const auto& itr : { inner_t{ "byte", "b", tim::units::byte },
201 inner_t{ "kilobyte", "kb", tim::units::kilobyte },
202 inner_t{ "megabyte", "mb", tim::units::megabyte },
203 inner_t{ "gigabyte", "gb", tim::units::gigabyte },
204 inner_t{ "terabyte", "tb", tim::units::terabyte },
205 inner_t{ "petabyte", "pb", tim::units::petabyte },
206 inner_t{ "kibibyte", "kib", tim::units::KiB },
207 inner_t{ "mebibyte", "mib", tim::units::MiB },
208 inner_t{ "gibibyte", "gib", tim::units::GiB },
209 inner_t{ "tebibyte", "tib", tim::units::TiB },
210 inner_t{ "pebibyte", "pib", tim::units::PiB } })
211 {
212 if(_unit == std::get<0>(itr) || _unit == std::get<1>(itr))
213 {
214 if(std::get<0>(itr) == "byte")
215 return return_type(std::get<0>(itr), std::get<2>(itr));
216 return return_type(std::get<1>(itr), std::get<2>(itr));
217 }
218 }
219
220 std::cerr << "Warning!! No memory unit matching \"" << _unit << "\". Using default..."
221 << std::endl;
222
223 return return_type{ "MB", tim::units::megabyte };
224}
225
226//--------------------------------------------------------------------------------------//
227
228inline std::tuple<std::string, int64_t>
230{
231 using string_t = std::string;
232 using strset_t = std::unordered_set<string_t>;
233 using return_type = std::tuple<string_t, int64_t>;
234 using inner_t = std::tuple<string_t, strset_t, int64_t>;
235
236 if(_unit.length() == 0)
237 return return_type{ "sec", tim::units::sec };
238
239 for(auto& itr : _unit)
240 itr = tolower(itr);
241
242 for(const auto& itr :
243 { inner_t{ "nsec", strset_t{ "ns", "nanosecond", "nanoseconds" },
244 tim::units::nsec },
245 inner_t{ "usec", strset_t{ "us", "microsecond", "microseconds" },
246 tim::units::usec },
247 inner_t{ "msec", strset_t{ "ms", "millisecond", "milliseconds" },
248 tim::units::msec },
249 inner_t{ "csec", strset_t{ "cs", "centisecond", "centiseconds" },
250 tim::units::csec },
251 inner_t{ "dsec", strset_t{ "ds", "decisecond", "deciseconds" },
252 tim::units::dsec },
253 inner_t{ "sec", strset_t{ "s", "second", "seconds" }, tim::units::sec },
254 inner_t{ "min", strset_t{ "minute", "minutes" }, tim::units::minute },
255 inner_t{ "hr", strset_t{ "hr", "hour", "hours" }, tim::units::hour } })
256 {
257 if(_unit == std::get<0>(itr) ||
258 std::get<1>(itr).find(_unit) != std::get<1>(itr).end())
259 {
260 return return_type{ std::get<0>(itr), std::get<2>(itr) };
261 }
262 }
263
264 std::cerr << "Warning!! No timing unit matching \"" << _unit << "\". Using default..."
265 << std::endl;
266
267 return return_type{ "sec", tim::units::sec };
268}
269
270//--------------------------------------------------------------------------------------//
271
272} // namespace units
273
274//======================================================================================//
275
276} // namespace tim
return _hash_map end()
std::tuple< std::string, int64_t > get_timing_unit(std::string _unit)
Definition: units.hpp:229
std::string mem_repr(int64_t _unit)
Definition: units.hpp:164
std::string time_repr(int64_t _unit)
Definition: units.hpp:145
std::tuple< std::string, int64_t > get_memory_unit(std::string _unit)
Definition: units.hpp:188
Definition: kokkosp.cpp:39
std::string string_t
Definition: utility.hpp:98
tim::mpl::apply< std::string > string
Definition: macros.hpp:53