forked from y-scope/clp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for bounding CPU usage during CLP compression.
- Loading branch information
Jack Luo
committed
Dec 14, 2024
1 parent
ec0821d
commit 063bffe
Showing
7 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "CpuUsageController.hpp" | ||
|
||
#include <csignal> | ||
#include <iostream> | ||
#include <thread> | ||
|
||
namespace clp { | ||
CpuUsageController::CpuUsageController(pid_t pid, double ratio) : m_pid{pid} { | ||
if (0 >= ratio || 1 <= ratio) { | ||
std::cerr << "Invalid ratio: " << ratio << "\n"; | ||
exit(-1); | ||
} | ||
|
||
constexpr std::chrono::duration<double, std::chrono::milliseconds::period> cTimeWindow{50}; | ||
auto const execution_time{cTimeWindow * ratio}; | ||
m_execution_time = std::chrono::milliseconds{ | ||
std::chrono::duration_cast<std::chrono::milliseconds>(execution_time) | ||
}; | ||
m_sleep_time = std::chrono::milliseconds{ | ||
std::chrono::duration_cast<std::chrono::milliseconds>(cTimeWindow - execution_time) | ||
}; | ||
} | ||
|
||
auto CpuUsageController::start() -> void { | ||
while (true) { | ||
std::this_thread::sleep_for(m_execution_time); | ||
if (auto const err{kill(m_pid, SIGSTOP)}; 0 != err) { | ||
std::cerr << "Failed to send `SIGSTOP`. Error code: " << err << "\n"; | ||
exit(-1); | ||
} | ||
std::this_thread::sleep_for(m_sleep_time); | ||
if (auto const err{kill(m_pid, SIGCONT)}; 0 != err) { | ||
std::cerr << "Failed to send `SIGCONT`. Error code: " << err << "\n"; | ||
exit(-1); | ||
} | ||
} | ||
} | ||
} // namespace clp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef CLP_CPUUSAGECONTROLLER_HPP | ||
#define CLP_CPUUSAGECONTROLLER_HPP | ||
|
||
#include <sys/types.h> | ||
|
||
#include <chrono> | ||
|
||
namespace clp { | ||
/** | ||
* Class for controlling the CPU usage of a process. It should be running in a | ||
* separate process. | ||
* The current implementation has the following limitations: | ||
* 1. The controller can only define a upper bound for the target process. It | ||
* does not monitor the CPU usage of the target process. | ||
* 2. The controller exits if it fails to signal the target process. However, | ||
* the signal is sent using `pid`, which means if the target process terminates | ||
* but the original target pid has been reused by other process, the new process | ||
* with the same pid will be controlled unexpected. | ||
* 3. The controller cannot control the thread-level behaviour. It is assumed | ||
* the target process is single-threaded. Otherwise, all threads will be | ||
* controlled with the upper bound. | ||
* 4. There is no guarantee that whether the controller process will run before | ||
* the target monitored process. There is no CPU usage bound guaranteed before | ||
* controller process gets scheduled to run. | ||
*/ | ||
class CpuUsageController { | ||
public: | ||
CpuUsageController(pid_t pid, double ratio); | ||
|
||
[[noreturn]] auto start() -> void; | ||
|
||
private: | ||
pid_t m_pid; | ||
std::chrono::milliseconds m_execution_time{0}; | ||
std::chrono::microseconds m_sleep_time{0}; | ||
}; | ||
} // namespace clp | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters