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.cpp
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 all
15// 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#ifndef TIMEMORY_UTILITY_UTILITY_CPP_
26#define TIMEMORY_UTILITY_UTILITY_CPP_ 1
27
31
32#if !defined(TIMEMORY_UTILITY_HEADER_MODE)
34#endif
35
36namespace tim
37{
38//
41{
42#if defined(TIMEMORY_UNIX)
43 char* _cfname = realpath(_fname.c_str(), nullptr);
44 _fname = std::string(_cfname);
45 free(_cfname);
46
47 while(_fname.find("\\\\") != std::string::npos)
48 _fname.replace(_fname.find("\\\\"), 2, "/");
49 while(_fname.find('\\') != std::string::npos)
50 _fname.replace(_fname.find('\\'), 1, "/");
51
52 return _fname.substr(0, _fname.find_last_of('/'));
53#elif defined(TIMEMORY_WINDOWS)
54 while(_fname.find('/') != std::string::npos)
55 _fname.replace(_fname.find('/'), 1, "\\");
56
57 _fname = _fname.substr(0, _fname.find_last_of('\\'));
58 return (_fname.at(_fname.length() - 1) == '\\')
59 ? _fname.substr(0, _fname.length() - 1)
60 : _fname;
61#endif
62}
63
64//--------------------------------------------------------------------------------------//
65
66int
68{
69 return filepath::makedir(std::move(_dir), umask);
70}
71
72//--------------------------------------------------------------------------------------//
73
74bool
75get_bool(const std::string& strbool, bool _default) noexcept
76{
77 // empty string returns default
78 if(strbool.empty())
79 return _default;
80
81 // check if numeric
82 if(strbool.find_first_not_of("0123456789") == std::string::npos)
83 {
84 if(strbool.length() > 1 || strbool[0] != '0')
85 return true;
86 return false;
87 }
88
89 // convert to lowercase
90 auto _val = std::string{ strbool };
91 for(auto& itr : _val)
92 itr = tolower(itr);
93
94 // check for matches to acceptable forms of false
95 for(const auto& itr : { "off", "false", "no", "n", "f" })
96 {
97 if(_val == itr)
98 return false;
99 }
100
101 // check for matches to acceptable forms of true
102 for(const auto& itr : { "on", "true", "yes", "y", "t" })
103 {
104 if(_val == itr)
105 return true;
106 }
107
108 return _default;
109}
110
111//--------------------------------------------------------------------------------------//
112
113std::vector<std::string>
115{
116 std::vector<std::string> _cmdline;
117#if defined(TIMEMORY_LINUX)
118 std::stringstream fcmdline;
119 fcmdline << "/proc/" << _pid << "/cmdline";
120 std::ifstream ifs(fcmdline.str().c_str());
121 if(ifs)
122 {
123 char cstr;
124 std::string sarg;
125 while(!ifs.eof())
126 {
127 ifs >> cstr;
128 if(!ifs.eof())
129 {
130 if(cstr != '\0')
131 {
132 sarg += cstr;
133 }
134 else
135 {
136 _cmdline.push_back(sarg);
137 sarg = "";
138 }
139 }
140 }
141 ifs.close();
142 }
143
144#else
145 consume_parameters(_pid);
146#endif
147 return _cmdline;
148}
149//
150namespace utility
151{
152//
154: std::string(osrepr(_path))
155{}
156
157path::path(char* _path)
158: std::string(osrepr(std::string(_path)))
159{}
160
161path::path(const path& rhs)
162: std::string(osrepr(rhs))
163{}
164
165path::path(const char* _path)
166: std::string(osrepr(std::string(const_cast<char*>(_path))))
167{}
168
169path&
171{
172 std::string::operator=(osrepr(rhs));
173 return *this;
174}
175
176path&
178{
179 if(this != &rhs)
180 std::string::operator=(osrepr(rhs));
181 return *this;
182}
183
184path&
186{
187 std::string::operator=(osrepr(std::string::insert(__pos, __s)));
188 return *this;
189}
190
191path&
192path::insert(size_type __pos, const path& __s)
193{
194 std::string::operator=(osrepr(std::string::insert(__pos, __s)));
195 return *this;
196}
197
200{
201#if defined(TIMEMORY_WINDOWS)
202 return "\\";
203#elif defined(TIMEMORY_UNIX)
204 return "/";
205#endif
206}
207
210{
211#if defined(TIMEMORY_WINDOWS)
212 return "/";
213#elif defined(TIMEMORY_UNIX)
214 return "\\";
215#endif
216}
217
218//
219// OS-dependent representation
222{
223#if defined(TIMEMORY_WINDOWS)
224 filepath::replace(_path, '/', "\\");
225 filepath::replace(_path, "\\\\", "\\");
226#elif defined(TIMEMORY_UNIX)
227 filepath::replace(_path, '\\', "/");
228 filepath::replace(_path, "//", "/");
229#endif
230 return _path;
231}
232
233// common representation
236{
237 return filepath::canonical(std::move(_path));
238}
239//
240} // namespace utility
241} // namespace tim
242//
243
244#endif // !defined(TIMEMORY_UTILITY_UTILITY_CPP_)
TIMEMORY_UTILITY_INLINE path & insert(size_type __pos, const std::string &__s)
Definition: utility.cpp:185
static TIMEMORY_UTILITY_INLINE std::string inverse()
Definition: utility.cpp:209
static TIMEMORY_UTILITY_INLINE std::string osrepr(std::string _path)
Definition: utility.cpp:221
static TIMEMORY_UTILITY_INLINE std::string canonical(std::string _path)
Definition: utility.cpp:235
static TIMEMORY_UTILITY_INLINE std::string os()
Definition: utility.cpp:199
TIMEMORY_UTILITY_INLINE path & operator=(const std::string &rhs)
Definition: utility.cpp:170
TIMEMORY_UTILITY_INLINE path(const std::string &_path)
Definition: utility.cpp:153
std::string::size_type size_type
Definition: utility.hpp:179
STL namespace.
int makedir(std::string _dir, int umask=0777)
Definition: filepath.hpp:142
std::string & replace(std::string &_path, char _c, const char *_v)
Definition: filepath.hpp:66
std::string canonical(std::string _path)
Definition: filepath.hpp:134
_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
std::string dirname(std::string _fname)
Definition: utility.cpp:40
std::vector< std::string > read_command_line(pid_t _pid)
Definition: utility.cpp:114
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
int makedir(std::string _dir, int umask)
Definition: utility.cpp:67
void consume_parameters(ArgsT &&...)
Definition: types.hpp:285