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 +
10#include <fstream>
11#include <time.h>
12
13namespace Mantid {
14namespace Instrumentation {
15
16namespace {
17Kernel::Logger &LOGGER() {
18 static Kernel::Logger logger("AlgoTimeRegister");
19 return logger;
20}
21} // namespace
22
25
27 : m_regStart_chrono(std::chrono::high_resolution_clock::now()), m_name(nm) {}
28
30 const time_point_ns regFinish = std::chrono::high_resolution_clock::now();
31 {
32 AlgoTimeRegister::Instance().addTime(m_name, std::this_thread::get_id(), m_regStart_chrono, regFinish);
33 }
34}
35
36void AlgoTimeRegisterImpl::addTime(const std::string &name, const Kernel::time_point_ns &begin,
37 const Kernel::time_point_ns &end) {
38 AlgoTimeRegister::Instance().addTime(name, std::this_thread::get_id(), begin, end);
39}
40
42 auto writeEnable = Kernel::ConfigService::Instance().getValue<bool>("performancelog.write").value();
43 if (!writeEnable) {
44 LOGGER().debug() << "performancelog.write is disabled (off/0/false)\n";
45 return false;
46 }
47 auto filename = Kernel::ConfigService::Instance().getString("performancelog.filename");
48 if (filename.empty()) {
49 LOGGER().debug() << "performancelog.filename is empty, please provide valid filename\n";
50 return false;
51 }
52 if (m_filename == filename && m_hasWrittenToFile) {
53 return true;
54 }
55
56 m_filename = filename;
57
58 LOGGER().debug() << "Performance log file: " << m_filename << '\n';
59
60 std::fstream fs;
61
62 fs.open(m_filename, std::ios::out);
63 if (fs.is_open()) {
64 fs << "START_POINT: " << std::chrono::duration_cast<std::chrono::nanoseconds>(m_start.time_since_epoch()).count()
65 << " MAX_THREAD: " << PARALLEL_GET_MAX_THREADS << "\n";
66 fs.close();
67 m_hasWrittenToFile = true;
68 } else {
69 LOGGER().notice() << "Failed to open the file, timing will not write to file.\n";
70 return false;
71 }
72 return true;
73}
74
75void AlgoTimeRegisterImpl::addTime(const std::string &name, const std::thread::id thread_id,
76 const Kernel::time_point_ns &begin, const Kernel::time_point_ns &end) {
77 std::lock_guard<std::mutex> lock(AlgoTimeRegister::Instance().m_mutex);
78 if (writeToFile()) {
79 std::fstream fs;
80 fs.open(m_filename, std::ios::out | std::ios::app);
81 if (fs.is_open()) {
82 const std::chrono::nanoseconds st = begin - m_start;
83 const std::chrono::nanoseconds fi = end - m_start;
84 fs << "ThreadID=" << thread_id << ", AlgorithmName=" << name << ", StartTime=" << st.count()
85 << ", EndTime=" << fi.count() << "\n";
86
87 fs.close();
88 }
89 }
90}
91
93 : m_start(std::chrono::high_resolution_clock::now()), m_hasWrittenToFile(false) {}
94
96
97} // namespace Instrumentation
98} // 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.