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.
utility.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 deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
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 FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24//
25
26/** \file utility/utility.hpp
27 * \headerfile utility/utility.hpp "timemory/utility/utility.hpp"
28 * General utility functions
29 *
30 */
31
32#pragma once
33
34#include "timemory/api.hpp"
45
46#if defined(TIMEMORY_USE_LIBUNWIND)
47# include <libunwind.h>
48#endif
49
50// C library
51#include <cctype>
52#include <cerrno>
53#include <cmath>
54#include <cstdint>
55#include <cstdio>
56#include <cstdlib>
57#include <cstring>
58// I/O
59#include <fstream>
60#include <iomanip>
61#include <iosfwd>
62#include <iostream>
63#include <sstream>
64#include <string>
65// general
66#include <functional>
67#include <limits>
68#include <regex>
69#include <typeindex>
70#include <utility>
71// container
72#include <vector>
73// threading
74#include <atomic>
75#include <mutex>
76#include <thread>
77
78#if defined(TIMEMORY_UNIX)
79# include <execinfo.h>
80# include <sys/stat.h>
81# include <sys/types.h>
82#elif defined(TIMEMORY_WINDOWS)
83# include <direct.h>
84using pid_t = int;
85#endif
86
87#if !defined(TIMEMORY_DEFAULT_UMASK)
88# define TIMEMORY_DEFAULT_UMASK 0777
89#endif
90
91//--------------------------------------------------------------------------------------//
92
93namespace tim
94{
95// alias here for common string type
96// there is also a string_view_t alias in macros/language.hpp which is std::string in
97// c++14 and std::string_view in c++17 and newer
99
100//--------------------------------------------------------------------------------------//
101
102template <typename Tp>
103inline bool
104isfinite(const Tp& arg)
105{
106#if defined(TIMEMORY_WINDOWS)
107 // Windows seems to be missing std::isfinite
108 return (arg == arg && arg != std::numeric_limits<Tp>::infinity() &&
109 arg != -std::numeric_limits<Tp>::infinity())
110 ? true
111 : false;
112#else
113 return std::isfinite(arg);
114#endif
115}
116
117//======================================================================================//
118//
119// General functions
120//
121//======================================================================================//
122
123namespace internal
124{
125template <typename Tp, typename Up = Tp>
126inline auto
127typeid_hash(int) -> decltype(demangle<Tp>(), size_t{})
128{
129 return std::type_index(typeid(Tp)).hash_code();
130}
131//
132template <typename Tp, typename Up = Tp>
133inline auto
134typeid_hash(long)
135{
136 return 0;
137}
138} // namespace internal
139
140template <typename Tp>
141inline auto
143{
144 return internal::typeid_hash<Tp>(0);
145}
146
147//--------------------------------------------------------------------------------------//
148
150 dirname(std::string _fname);
151
152//--------------------------------------------------------------------------------------//
153
156
157//--------------------------------------------------------------------------------------//
158
160get_bool(const std::string& strbool, bool _default = false) noexcept;
161
162//
163//--------------------------------------------------------------------------------------//
164//
165TIMEMORY_UTILITY_INLINE std::vector<std::string>
166 read_command_line(pid_t _pid);
167
168//======================================================================================//
169//
170// path
171//
172//======================================================================================//
173
174namespace utility
175{
176class path : public std::string
177{
178public:
179 using size_type = std::string::size_type;
180
181public:
183 TIMEMORY_UTILITY_INLINE path(char* _path);
185 TIMEMORY_UTILITY_INLINE path(const char* _path);
186
187 TIMEMORY_UTILITY_INLINE path& operator=(const std::string& rhs);
188 TIMEMORY_UTILITY_INLINE path& operator=(const path& rhs);
191
192 // OS-dependent representation
195 static TIMEMORY_UTILITY_INLINE std::string inverse();
197};
198} // namespace utility
199
200//--------------------------------------------------------------------------------------//
201
202inline namespace hash
203{
204template <typename T>
205TIMEMORY_INLINE size_t
206get_hash(T&& obj)
207{
208 return std::hash<decay_t<T>>()(std::forward<T>(obj));
209}
210
211TIMEMORY_INLINE size_t
213{
214 return std::hash<string_view_t>{}(str);
215}
216
217TIMEMORY_INLINE size_t
218get_hash(const char* cstr)
219{
220 return std::hash<string_view_t>{}(cstr);
221}
222
223template <typename T>
224struct hasher
225{
226 inline size_t operator()(T&& val) const { return get_hash(std::forward<T>(val)); }
227 inline size_t operator()(const T& val) const { return get_hash(val); }
228};
229} // namespace hash
230//--------------------------------------------------------------------------------------//
231
232} // namespace tim
233
234#if defined(TIMEMORY_UTILITY_HEADER_MODE)
236#endif
std::string::size_type size_type
Definition: utility.hpp:179
STL namespace.
std::string canonical(std::string _path)
Definition: filepath.hpp:134
size_t get_hash(T &&obj)
Definition: utility.hpp:206
_reported insert(_hash_id)
const string_t const string_t & _dir
Definition: definition.hpp:52
Definition: kokkosp.cpp:39
bool get_bool(const std::string &strbool, bool _default) noexcept
Definition: utility.cpp:75
auto typeid_hash()
Definition: utility.hpp:142
std::string dirname(std::string _fname)
Definition: utility.cpp:40
std::string string_t
Definition: utility.hpp:98
const std::string & string_view_cref_t
Definition: language.hpp:103
bool isfinite(const Tp &arg)
Definition: utility.hpp:104
std::vector< std::string > read_command_line(pid_t _pid)
Definition: utility.cpp:114
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
const std::string std::ostream * os
int makedir(std::string _dir, int umask)
Definition: utility.cpp:67
size_t operator()(T &&val) const
Definition: utility.hpp:226
size_t operator()(const T &val) const
Definition: utility.hpp:227
#define TIMEMORY_UTILITY_INLINE
Definition: macros.hpp:308
#define TIMEMORY_DEFAULT_UMASK
Definition: utility.hpp:88