MCPcopy Create free account
hub / github.com/Alexays/Waybar / parseCpuFrequencies

Method parseCpuFrequencies

src/modules/cpu_frequency/linux.cpp:5–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3#include "modules/cpu_frequency.hpp"
4
5std::vector<float> waybar::modules::CpuFrequency::parseCpuFrequencies() {
6 const std::string file_path_ = "/proc/cpuinfo";
7 std::ifstream info(file_path_);
8 if (!info.is_open()) {
9 throw std::runtime_error("Can't open " + file_path_);
10 }
11 std::vector<float> frequencies;
12 std::string line;
13 while (getline(info, line)) {
14 if (line.substr(0, 7).compare("cpu MHz") != 0) {
15 continue;
16 }
17
18 std::string frequency_str = line.substr(line.find(":") + 2);
19 float frequency = std::strtol(frequency_str.c_str(), nullptr, 10);
20 frequencies.push_back(frequency);
21 }
22 info.close();
23
24 if (frequencies.size() <= 0) {
25 std::string cpufreq_dir = "/sys/devices/system/cpu/cpufreq";
26 if (std::filesystem::exists(cpufreq_dir)) {
27 std::vector<std::string> frequency_files = {"/cpuinfo_min_freq", "/cpuinfo_max_freq"};
28 for (auto& p : std::filesystem::directory_iterator(cpufreq_dir)) {
29 for (auto freq_file : frequency_files) {
30 std::string freq_file_path = p.path().string() + freq_file;
31 if (std::filesystem::exists(freq_file_path)) {
32 std::string freq_value;
33 std::ifstream freq(freq_file_path);
34 if (freq.is_open()) {
35 getline(freq, freq_value);
36 float frequency = std::strtol(freq_value.c_str(), nullptr, 10);
37 frequencies.push_back(frequency / 1000);
38 freq.close();
39 }
40 }
41 }
42 }
43 }
44 }
45
46 return frequencies;
47}

Callers

nothing calls this directly

Calls 1

closeMethod · 0.80

Tested by

no test coverage detected