Mantid
Loading...
Searching...
No Matches
AlgoTimeRegister.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2020 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
11#include <fstream>
12#include <time.h>
13
14namespace Mantid {
15namespace Instrumentation {
16
17namespace {
18Kernel::Logger &LOGGER() {
19 static Kernel::Logger logger("AlgoTimeRegister");
20 return logger;
21}
22} // namespace
23
26
28 : m_regStart_chrono(std::chrono::high_resolution_clock::now()), m_name(nm) {}
29
31 const time_point_ns regFinish = std::chrono::high_resolution_clock::now();
32 {
33 AlgoTimeRegister::Instance().addTime(m_name, std::this_thread::get_id(), m_regStart_chrono, regFinish);
34 }
35}
36
37void AlgoTimeRegisterImpl::addTime(const std::string &name, const Kernel::time_point_ns &begin,
38 const Kernel::time_point_ns &end) {
39 AlgoTimeRegister::Instance().addTime(name, std::this_thread::get_id(), begin, end);
40}
41
43 auto writeEnable = Kernel::ConfigService::Instance().getValue<bool>("performancelog.write").value();
44 if (!writeEnable) {
45 LOGGER().debug() << "performancelog.write is disabled (off/0/false)\n";
46 return false;
47 }
48 auto filename = Kernel::ConfigService::Instance().getString("performancelog.filename");
49 if (filename.empty()) {
50 LOGGER().debug() << "performancelog.filename is empty, please provide valid filename\n";
51 return false;
52 }
53 if (m_filename == filename && m_hasWrittenToFile) {
54 return true;
55 }
56
57 m_filename = filename;
58
59 LOGGER().debug() << "Performance log file: " << m_filename << '\n';
60
61 std::fstream fs;
62
63 fs.open(m_filename, std::ios::out);
64 if (fs.is_open()) {
65 fs << "START_POINT: " << std::chrono::duration_cast<std::chrono::nanoseconds>(m_start.time_since_epoch()).count()
66 << " MAX_THREAD: " << PARALLEL_GET_MAX_THREADS << "\n";
67 fs.close();
68 m_hasWrittenToFile = true;
69 } else {
70 LOGGER().notice() << "Failed to open the file, timing will not write to file.\n";
71 return false;
72 }
73 return true;
74}
75
76void AlgoTimeRegisterImpl::addTime(const std::string &name, const std::thread::id thread_id,
77 const Kernel::time_point_ns &begin, const Kernel::time_point_ns &end) {
78 std::lock_guard<std::mutex> lock(AlgoTimeRegister::Instance().m_mutex);
79 if (writeToFile()) {
80 std::fstream fs;
81 fs.open(m_filename, std::ios::out | std::ios::app);
82 if (fs.is_open()) {
83 const std::chrono::nanoseconds st = begin - m_start;
84 const std::chrono::nanoseconds fi = end - m_start;
85 fs << "ThreadID=" << thread_id << ", AlgorithmName=" << name << ", StartTime=" << st.count()
86 << ", EndTime=" << fi.count() << "\n";
87
88 fs.close();
89 }
90 }
91}
92
94 : m_start(std::chrono::high_resolution_clock::now()), m_hasWrittenToFile(false) {}
95
97
98} // namespace Instrumentation
99} // namespace Mantid
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
#define PARALLEL_GET_MAX_THREADS
void addTime(const std::string &name, const std::thread::id thread_id, const Kernel::time_point_ns &begin, const Kernel::time_point_ns &end)
Mantid::Kernel::SingletonHolder< ConfigServiceImpl > ConfigService
std::chrono::time_point< std::chrono::high_resolution_clock > time_point_ns
Definition Algorithm.h:39
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.