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