libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
latency_timestamps.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2019 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
20 
21 #include <cassert>
22 #include <fstream>
23 #include <iostream>
24 
25 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE {
26 
27 void
28 latency_timestamps_itf::timestamp::to_csv(std::ostream &os) const noexcept(false) {
29  os<<std::setprecision(std::numeric_limits<double>::digits10 + 1)<<cpu_timer::TSC_to_microsec(start)
30  <<','<<std::setprecision(std::numeric_limits<double>::digits10 + 1)<<cpu_timer::TSC_to_microsec(end);
31 }
32 
33 latency_timestamps::latency_timestamps(size_type num_tses) noexcept(false)
36  num_timestamps(num_tses) {
37  assert(current_timestamp!=nullptr);
38 }
39 
40 void
41 latency_timestamps::to_csv(std::ostream &os) const noexcept(false) {
42  os
43  <<ppd::api_threading_traits<ppd::platform_api, ppd::sequential_mode>::demangle_name(typeid(timer_t))
44  <<",Number of entries,"<<size()
45  <<",microsec\n";
46  for (element_type const * stamp=timestamps.get(); stamp!=current_timestamp; ++stamp) {
47  assert(std::less<element_type const *>()(stamp, current_timestamp));
48  assert(stamp);
49  assert(stamp->is_valid());
50  stamp->to_csv(os);
51  os<<'\n';
52  }
53 }
54 
55 void
56 latency_timestamps::write_to_csv_file(std::ostream &os, char const * const root) const noexcept(false) {
57  if (num_timestamps>0) {
58  const std::string timings_filename(make_filename(root)+".csv");
59  os<<"Writing "<<size()<<" latencies to '"<<timings_filename<<"', (this might take some time)..."<<std::endl;
60  std::ofstream csv_file(timings_filename.c_str());
61  to_csv(csv_file);
62  os<<"... Written."<<std::endl;
63  }
64 }
65 
66 void
67 latency_timestamps::write_to_named_csv_file(std::ostream &os, std::string const &base_filename) const noexcept(false) {
68  if (num_timestamps>0) {
69  const std::string timings_filename(base_filename+".csv");
70  os<<"Writing "<<size()<<" latencies to '"<<timings_filename<<"', (this might take some time)..."<<std::endl;
71  std::ofstream csv_file(timings_filename.c_str());
72  to_csv(csv_file);
73  os<<"... Written."<<std::endl;
74  }
75 }
76 
77 } }