-
Notifications
You must be signed in to change notification settings - Fork 0
RAPL collector implementation #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5ac7cd4
chore: setup helix editor configs && update .gitignore
szlachta99 2123226
feat(agent): add minimal intel RAPL collector
szlachta99 9dae314
feat: implement core detection and improve error handling
szlachta99 0094bce
fix: MSR read exception condition
szlachta99 f2e7235
Merge branch 'devel' into agent/feat/rapl-collector
szlachta99 6c6bd22
fix: formatting
szlachta99 5f7dd08
fix: formating 2
szlachta99 cc1514d
fix: formatting pls work
szlachta99 9c51bc0
Apply suggestions from code review
szlachta99 9670153
fix: core ordering
szlachta99 7e67d75
Merge branch 'devel' into agent/feat/rapl-collector
szlachta99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,18 @@ | ||
| [[language]] | ||
| name = "c" | ||
| scope = "source.c" | ||
| file-types = [] | ||
|
|
||
| [[language]] | ||
| name = "cpp" | ||
| scope = "source.cpp" | ||
| file-types = ["cc", "hh", "c++", "cpp", "hpp", "h", "ipp", "tpp", "cxx", "hxx", "ixx", "txx", "ino", "C", "H", "cu", "cuh"] | ||
| language-servers = ["clangd"] | ||
|
|
||
| [language-server.clangd] | ||
| command = "clangd" | ||
| args = [ | ||
| "--header-insertion=never", | ||
| "--query-driver=/usr/bin/g++,/usr/bin/c++", | ||
| "--compile-commands-dir=build" | ||
| ] |
This file contains hidden or 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,94 @@ | ||
| #include "rapl_collector.h" | ||
|
|
||
| #include <fcntl.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <chrono> | ||
| #include <cmath> | ||
| #include <filesystem> | ||
|
|
||
| namespace volta { | ||
| namespace agent { | ||
| namespace collectors { | ||
|
|
||
| RaplCollector::RaplCollector() { | ||
| OpenMSR(); | ||
| uint64_t readout = ReadMSR(0, MSR_RAPL::POWER_UNIT); | ||
| power_units_ = pow(0.5, (double)(readout & 0xf)); | ||
| energy_units_ = pow(0.5, (double)((readout >> 8) & 0x1f)); | ||
| time_units_ = pow(0.5, (double)((readout >> 16) & 0xf)); | ||
| readout = ReadMSR(0, MSR_RAPL::PKG::ENERGY_STATUS); | ||
| last_value = energy_units_ * readout; | ||
| } | ||
|
|
||
| std::vector<Metric> RaplCollector::Collect() { | ||
| uint64_t readout; | ||
|
|
||
| try { | ||
| readout = ReadMSR(0, MSR_RAPL::PKG::ENERGY_STATUS); | ||
| } catch (const MSR_Read_Exception &e) { | ||
| return {}; | ||
| } | ||
|
|
||
| double value = energy_units_ * readout; | ||
|
|
||
| Metric m; | ||
| m.name = "cpu_energy_usage_total"; | ||
| m.value = value - last_value; | ||
| m.timestamp = std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| std::chrono::system_clock::now().time_since_epoch()) | ||
| .count(); | ||
| last_value = value; | ||
| return {m}; | ||
| } | ||
|
|
||
| uint64_t RaplCollector::ReadMSR(uint8_t core, uint32_t offset) { | ||
| uint64_t data; | ||
| if (core + 1 > MSR_files_.size()) { | ||
| throw MSR_Read_Exception(); | ||
| } | ||
| // c-like read for thread safety | ||
| if (pread(MSR_files_[core], &data, sizeof data, offset) != sizeof data) { | ||
| return {}; | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| void RaplCollector::OpenMSR() { | ||
| const std::filesystem::path cpu_base = "/dev/cpu"; | ||
| MSR_files_ = std::vector<int>(); | ||
| std::error_code ec; | ||
|
|
||
| if (!std::filesystem::exists(cpu_base, ec)) { | ||
| throw MSR_Open_Exception(); | ||
| } | ||
| std::vector<std::pair<int, std::filesystem::path>> cpu_entries; | ||
| for (const auto &entry : std::filesystem::directory_iterator(cpu_base)) { | ||
| if (!entry.is_directory()) continue; | ||
| const auto &dirname = entry.path().filename().string(); | ||
| if (!std::ranges::all_of(dirname, ::isdigit)) continue; | ||
| cpu_entries.emplace_back(std::stoi(dirname), entry.path()); | ||
| } | ||
|
|
||
| std::ranges::sort(cpu_entries); | ||
|
|
||
| for (const auto &[id, path] : cpu_entries) { | ||
| int fd = open((path / "msr").c_str(), O_RDONLY); | ||
| if (fd >= 0) { | ||
| MSR_files_.push_back(fd); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void RaplCollector::CloseMSR(int fd) { close(fd); } | ||
|
|
||
| RaplCollector::~RaplCollector() { | ||
| for (auto file : MSR_files_) { | ||
| CloseMSR(file); | ||
| } | ||
| }; | ||
| } // namespace collectors | ||
| } // namespace agent | ||
| } // namespace volta | ||
This file contains hidden or 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,74 @@ | ||
| #ifndef VOLTA_AGENT_SRC_COLLECTORS_RAPL_COLLECTOR_H_ | ||
| #define VOLTA_AGENT_SRC_COLLECTORS_RAPL_COLLECTOR_H_ | ||
|
|
||
| #include "collectors/collector.h" | ||
|
|
||
| namespace volta { | ||
| namespace agent { | ||
| namespace collectors { | ||
|
|
||
| class RaplCollector : public Collector { | ||
| public: | ||
| RaplCollector(); | ||
| // ~RaplCollector() override; | ||
| RaplCollector(const RaplCollector&) = delete; | ||
| RaplCollector& operator=(const RaplCollector&) = delete; | ||
| std::vector<Metric> Collect() override; | ||
| ~RaplCollector(); | ||
|
|
||
| private: | ||
| uint64_t ReadMSR(uint8_t core, uint32_t offset); | ||
| void OpenMSR(); | ||
| void CloseMSR(int fd); | ||
| bool initialized_ = false; | ||
| double power_units_, energy_units_, time_units_; | ||
| std::vector<int> MSR_files_; | ||
| double last_value; | ||
|
|
||
| class MSR_Read_Exception : std::exception {}; | ||
| class MSR_Open_Exception : std::exception {}; | ||
|
|
||
| struct MSR_RAPL { | ||
| static constexpr uint32_t POWER_UNIT = 0x606; | ||
| struct Units { | ||
| static constexpr uint32_t POWER_UNIT_OFFSET = 0; | ||
| static constexpr uint32_t POWER_UNIT_MASK = 0x0F; | ||
| static constexpr uint32_t ENERGY_UNIT_OFFSET = 0x08; | ||
| static constexpr uint32_t ENERGY_UNIT_MASK = 0x1F00; | ||
| static constexpr uint32_t TIME_UNIT_OFFSET = 0x10; | ||
| static constexpr uint32_t TIME_UNIT_MASK = 0xF000; | ||
| }; | ||
|
|
||
| struct PKG { | ||
| static constexpr uint32_t POWER_LIMIT = 0x610; | ||
| static constexpr uint32_t ENERGY_STATUS = 0x611; | ||
| static constexpr uint32_t PERF_STATUS = 0x613; | ||
| static constexpr uint32_t POWER_INFO = 0x614; | ||
| }; | ||
|
|
||
| struct PP0 { | ||
| static constexpr uint32_t POWER_LIMIT = 0x638; | ||
| static constexpr uint32_t ENERGY_STATUS = 0x639; | ||
| static constexpr uint32_t POLICY = 0x63A; | ||
| static constexpr uint32_t PERF_STATUS = 0x63B; | ||
| }; | ||
|
|
||
| struct PP1 { | ||
| static constexpr uint32_t POWER_LIMIT = 0x640; | ||
| static constexpr uint32_t ENERGY_STATUS = 0x641; | ||
| static constexpr uint32_t POLICY = 0x642; | ||
| }; | ||
|
|
||
| struct DRAM { | ||
| static constexpr uint32_t POWER_LIMIT = 0x618; | ||
| static constexpr uint32_t ENERGY_STATUS = 0x619; | ||
| static constexpr uint32_t PERF_STATUS = 0x61B; | ||
| static constexpr uint32_t POWER_INFO = 0x61C; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| } // namespace collectors | ||
| } // namespace agent | ||
| } // namespace volta | ||
| #endif |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.