File tree Expand file tree Collapse file tree
profiling/energy-profiler/daemon Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // @HEADER
2+ // ************************************************************************
3+ //
4+ // Kokkos v. 4.0
5+ // Copyright (2022) National Technology & Engineering
6+ // Solutions of Sandia, LLC (NTESS).
7+ //
8+ // Under the terms of Contract DE-NA0003525 with NTESS,
9+ // the U.S. Government retains certain rights in this software.
10+ //
11+ // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12+ // See https://kokkos.org/LICENSE for license information.
13+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14+ //
15+ // @HEADER
16+
17+ #include " daemon.hpp"
18+ #include < stdexcept>
19+ #include < thread>
20+
21+ void Daemon::start () {
22+ if (!running_) {
23+ running_ = true ;
24+ thread_ = std::thread (&Daemon::tick, this );
25+ } else {
26+ throw std::runtime_error (" Daemon already started" );
27+ }
28+ }
29+
30+ void Daemon::tick () {
31+ while (running_) {
32+ std::chrono::high_resolution_clock::time_point start_time =
33+ std::chrono::high_resolution_clock::now ();
34+
35+ // Execute the function
36+ func_ ();
37+
38+ std::chrono::high_resolution_clock::time_point end_time =
39+ std::chrono::high_resolution_clock::now ();
40+ std::chrono::milliseconds execution_duration =
41+ std::chrono::duration_cast<std::chrono::milliseconds>(end_time -
42+ start_time);
43+
44+ // Calculate how long to sleep to maintain the interval
45+ if (execution_duration < interval_) {
46+ std::chrono::milliseconds sleep_duration = interval_ - execution_duration;
47+ std::this_thread::sleep_for (sleep_duration);
48+ }
49+ }
50+ }
51+
52+ void Daemon::stop () {
53+ if (running_) {
54+ running_ = false ;
55+ thread_.join ();
56+ } else {
57+ throw std::runtime_error (" Daemon not started" );
58+ }
59+ }
Original file line number Diff line number Diff line change 1+ // @HEADER
2+ // ************************************************************************
3+ //
4+ // Kokkos v. 4.0
5+ // Copyright (2022) National Technology & Engineering
6+ // Solutions of Sandia, LLC (NTESS).
7+ //
8+ // Under the terms of Contract DE-NA0003525 with NTESS,
9+ // the U.S. Government retains certain rights in this software.
10+ //
11+ // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12+ // See https://kokkos.org/LICENSE for license information.
13+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14+ //
15+ // @HEADER
16+
17+ #pragma once
18+
19+ #include < functional>
20+ #include < thread>
21+ #include < chrono>
22+
23+ class Daemon {
24+ public:
25+ Daemon (std::function<void ()> func, int interval_ms)
26+ : interval_(interval_ms), func_(func){};
27+
28+ void start ();
29+ void tick ();
30+ void stop ();
31+ bool is_running () const { return running_; }
32+ std::thread& get_thread () { return thread_; }
33+
34+ private:
35+ std::chrono::milliseconds interval_;
36+ bool running_{false };
37+ std::function<void ()> func_;
38+ std::thread thread_;
39+ };
You can’t perform that action at this time.
0 commit comments