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.
definition.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 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#pragma once
26
32
33namespace tim
34{
35//
36//--------------------------------------------------------------------------------------//
37//
38// plotting
39//
40//--------------------------------------------------------------------------------------//
41//
42namespace plotting
43{
44//
45//--------------------------------------------------------------------------------------//
46//
47#if defined(TIMEMORY_PLOTTING_SOURCE) || !defined(TIMEMORY_USE_PLOTTING_EXTERN)
48//
49//--------------------------------------------------------------------------------------//
50//
52plot(const string_t& _label, const string_t& _prefix, const string_t& _dir,
54{
55 auto_lock_t lk(type_mutex<std::ostream>());
56
58 PRINT_HERE("%s", "");
59
61 {
62 fprintf(stderr, "[%s]> Empty '%s' (env: '%s'). Plot generation is disabled...\n",
63 _label.c_str(), "tim::settings::python_exe()", "TIMEMORY_PYTHON_EXE");
64 return;
65 }
66
68 PRINT_HERE("%s", "");
69
71
72 const auto& _file = _json_file;
73 {
74 std::ifstream ifs(_file.c_str());
75 bool exists = ifs.good();
76 ifs.close();
77 if(!exists)
78 {
79 fprintf(stderr,
80 "[%s]> file '%s' does not exist. Plot generation is disabled...\n",
81 _label.c_str(), _file.c_str());
82 return;
83 }
84 }
85
86 auto _ctor = get_env<std::string>("TIMEMORY_LIBRARY_CTOR", "");
87 auto _bann = get_env<std::string>("TIMEMORY_BANNER", "");
88 auto _plot = get_env<std::string>("TIMEMORY_CXX_PLOT_MODE", "");
89 // if currently in plotting mode, we dont want to plot again
90 if(_plot.length() > 0)
91 return;
92
93 // set-up environment such that forking is safe
94 set_env<std::string>("TIMEMORY_LIBRARY_CTOR", "OFF", 1);
95 set_env<std::string>("TIMEMORY_BANNER", "OFF", 1);
96 set_env<std::string>("TIMEMORY_CXX_PLOT_MODE", "1", 1);
97 auto cmd =
98 operation::join(" ", settings::python_exe(), "-m", "timemory.plotting", "-f",
99 _file, "-t", TIMEMORY_JOIN("\"", "", _prefix, ""), "-o", _dir);
100
102 cmd += " -e";
103
105 PRINT_HERE("PLOT COMMAND: '%s'", cmd.c_str());
106
107 std::stringstream _log{};
108 auto _success = launch_process(cmd.c_str(), _info + " plot generation failed", &_log);
109 if(_success)
110 {
111 std::cout << _log.str() << '\n';
112 }
113 else
114 {
115 std::cerr << _log.str() << '\n';
116 }
117
118 // revert the environment
119 set_env<std::string>("TIMEMORY_CXX_PLOT_MODE", _plot, 1);
120 set_env<std::string>("TIMEMORY_BANNER", _bann, 1);
121 set_env<std::string>("TIMEMORY_LIBRARY_CTOR", _ctor, 1);
122}
123//
124//--------------------------------------------------------------------------------------//
125//
126#endif // !defined(TIMEMORY_USE_EXTERN) || defined(TIMEMORY_PLOTTING_SOURCE)
127//
128//--------------------------------------------------------------------------------------//
129//
130} // namespace plotting
131} // namespace tim
132//
133//--------------------------------------------------------------------------------------//
134//
auto join(const char *sep, Arg &&arg, Args &&... args)
Definition: declaration.hpp:74
const string_t const string_t bool const string_t & _json_file
Definition: definition.hpp:54
TIMEMORY_PLOTTING_LINKAGE(void) plot(const string_t &_label
const string_t const string_t & _dir
Definition: definition.hpp:52
std::string string_t
Definition: types.hpp:46
const string_t const string_t bool _echo_dart
Definition: definition.hpp:53
void plot(const std::string &_label, const std::string &_prefix, const std::string &_dir, bool _echo_dart, const std::string &_json_file)
const auto & _file
Definition: definition.hpp:72
const string_t & _prefix
Definition: definition.hpp:52
Definition: kokkosp.cpp:39
std::unique_lock< mutex_t > auto_lock_t
Unique lock type around mutex_t.
Definition: locking.hpp:42
python_exe
Definition: settings.cpp:1783
bool launch_process(const char *cmd, const std::string &extra="", std::ostream *os=nullptr)
#define PRINT_HERE(...)
Definition: macros.hpp:152
#define TIMEMORY_JOIN(delim,...)
Definition: macros.hpp:90
#define TIMEMORY_LABEL(...)
Definition: macros.hpp:106