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.
timestamp.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_COMPONENT_TIMESTAMP_TIMESTAMP_CPP_
26#define TIMEMORY_COMPONENT_TIMESTAMP_TIMESTAMP_CPP_ 1
27
29
30#if !defined(TIMEMORY_COMPONENT_TIMESTAMP_HEADER_ONLY_MODE)
32# define TIMEMORY_COMPONENT_TIMESTAMP_INLINE
33#else
34# define TIMEMORY_COMPONENT_TIMESTAMP_INLINE inline
35#endif
36
37namespace tim
38{
39namespace component
40{
44{
45 return "timestamp";
46}
47
51{
52 return "Provides a timestamp for every sample and/or phase";
53}
54
58{
59 return std::chrono::system_clock::now();
60}
61
65{
66 char _repr[64];
67 std::memset(_repr, '\0', sizeof(_repr));
68 std::time_t _value = std::chrono::system_clock::to_time_t(_tp);
69 // alternative: "%c %Z"
70 if(std::strftime(_repr, sizeof(_repr), "%a %b %d %T %Y %Z", std::localtime(&_value)))
71 return std::string{ _repr };
72 return std::string{};
73}
74
76int64_t
78{
79 return std::chrono::duration_cast<duration_type>(record().time_since_epoch()).count();
80}
81
83void
85{
87 base_type::set_value(record());
88}
89
91void
93{
95 base_type::set_value(record());
96}
97
101{
102 return base_type::get_value();
103}
104
108{
109 return as_string(base_type::get_value());
110}
111
115{
116 return (*this = _rhs);
117}
118
122{
123 return *this;
124}
125
127timestamp& timestamp::operator/=(int64_t) { return *this; }
128
132{
133 static auto _v = record();
134 return _v;
135}
136
137} // namespace component
138
139namespace data
140{
141namespace base
142{
143template <>
145stream_entry::construct<timestamp_value_t>(const timestamp_value_t& val)
146{
148 ss.setf(m_format);
149 ss << std::setprecision(m_precision) << component::timestamp::as_string(val) << " / "
150 << val.time_since_epoch().count();
151 m_value = ss.str();
152 if(settings::max_width() > 0 && m_value.length() > (size_t) settings::max_width())
153 {
154 //
155 // don't truncate and add ellipsis if max width is really small
156 //
157 if(settings::max_width() > 20)
158 {
159 m_value = m_value.substr(0, settings::max_width() - 3);
160 m_value += "...";
161 }
162 else
163 {
164 m_value = m_value.substr(0, settings::max_width());
165 }
166 }
167}
168} // namespace base
169} // namespace data
170} // namespace tim
171
172#endif // TIMEMORY_COMPONENT_TIMESTAMP_TIMESTAMP_CPP_
typename component::timestamp::time_point_type timestamp_value_t
Definition: timestamp.hpp:121
Definition: kokkosp.cpp:39
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
max_width
Definition: settings.cpp:1645
this component stores the timestamp of when a bundle was started and is specialized such that the "ti...
Definition: timestamp.hpp:50
static value_type record()
Definition: timestamp.cpp:57
static std::string label()
Definition: timestamp.cpp:43
static std::string as_string(const time_point_type &_tp)
Definition: timestamp.cpp:64
typename clock_type::time_point time_point_type
Definition: timestamp.hpp:54
value_type get() const
Definition: timestamp.cpp:100
static int64_t count()
Definition: timestamp.cpp:77
static value_type get_reference_ts()
Definition: timestamp.cpp:131
timestamp & operator/=(const timestamp &)
Definition: timestamp.cpp:121
timestamp & operator+=(const timestamp &)
Definition: timestamp.cpp:114
std::string get_display() const
Definition: timestamp.cpp:107
static std::string description()
Definition: timestamp.cpp:50
timestamp_entry_t value_type
Definition: timestamp.hpp:51
format_flags m_format
Definition: stream.hpp:134
std::stringstream stringstream_t
Definition: stream.hpp:58
int m_precision
Definition: stream.hpp:133
string_t m_value
Definition: stream.hpp:135
#define TIMEMORY_COMPONENT_TIMESTAMP_INLINE
Definition: timestamp.cpp:32