-
-
Notifications
You must be signed in to change notification settings - Fork 732
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
Split cpu module #2114
Merged
Merged
Split cpu module #2114
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
729564c
Introduced separate load module
mmhat c36fe3a
Introduce cpu_frequency module
mmhat 888adb5
Introduce cpu_usage module
mmhat 982ffde
Use labels instead of buttons
mmhat dce6a98
Added changes made to the cpu module
mmhat c45f668
cpu module: Reuse getCpuFrequency of cpu_frequency module
mmhat d1602e3
cpu module: Reuse getCpuUsage of cpu_usage module
mmhat 8d7341d
cpu module: Reuse getLoad of load module
mmhat 93d66a9
Moved cpu/common.cpp to cpu.cpp
mmhat 91b6629
Fixed format errors
mmhat d5203e5
Fixed cpu module: Provide stub implementation for parseCpuFrequencies
mmhat 80a34ee
Fixed formatting again
mmhat 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 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,32 @@ | ||
#pragma once | ||
|
||
#include <fmt/format.h> | ||
|
||
#include <cstdint> | ||
#include <fstream> | ||
#include <numeric> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "ALabel.hpp" | ||
#include "util/sleeper_thread.hpp" | ||
|
||
namespace waybar::modules { | ||
|
||
class CpuFrequency : public ALabel { | ||
public: | ||
CpuFrequency(const std::string&, const Json::Value&); | ||
virtual ~CpuFrequency() = default; | ||
auto update() -> void override; | ||
|
||
// This is a static member because it is also used by the cpu module. | ||
static std::tuple<float, float, float> getCpuFrequency(); | ||
|
||
private: | ||
static std::vector<float> parseCpuFrequencies(); | ||
|
||
util::SleeperThread thread_; | ||
}; | ||
|
||
} // namespace waybar::modules |
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,34 @@ | ||
#pragma once | ||
|
||
#include <fmt/format.h> | ||
|
||
#include <cstdint> | ||
#include <fstream> | ||
#include <numeric> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "ALabel.hpp" | ||
#include "util/sleeper_thread.hpp" | ||
|
||
namespace waybar::modules { | ||
|
||
class CpuUsage : public ALabel { | ||
public: | ||
CpuUsage(const std::string&, const Json::Value&); | ||
virtual ~CpuUsage() = default; | ||
auto update() -> void override; | ||
|
||
// This is a static member because it is also used by the cpu module. | ||
static std::tuple<std::vector<uint16_t>, std::string> getCpuUsage(std::vector<std::tuple<size_t, size_t>>&); | ||
|
||
private: | ||
static std::vector<std::tuple<size_t, size_t>> parseCpuinfo(); | ||
|
||
std::vector<std::tuple<size_t, size_t>> prev_times_; | ||
|
||
util::SleeperThread thread_; | ||
}; | ||
|
||
} // namespace waybar::modules |
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,31 @@ | ||
#pragma once | ||
|
||
#include <fmt/format.h> | ||
|
||
#include <cstdint> | ||
#include <fstream> | ||
#include <numeric> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "ALabel.hpp" | ||
#include "util/sleeper_thread.hpp" | ||
|
||
namespace waybar::modules { | ||
|
||
class Load : public ALabel { | ||
public: | ||
Load(const std::string&, const Json::Value&); | ||
virtual ~Load() = default; | ||
auto update() -> void override; | ||
|
||
// This is a static member because it is also used by the cpu module. | ||
static std::tuple<double, double, double> getLoad(); | ||
|
||
private: | ||
|
||
util::SleeperThread thread_; | ||
}; | ||
|
||
} // namespace waybar::modules |
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,63 @@ | ||
#include "modules/cpu.hpp" | ||
|
||
#include "modules/cpu_frequency.hpp" | ||
#include "modules/cpu_usage.hpp" | ||
#include "modules/load.hpp" | ||
|
||
// In the 80000 version of fmt library authors decided to optimize imports | ||
// and moved declarations required for fmt::dynamic_format_arg_store in new | ||
// header fmt/args.h | ||
#if (FMT_VERSION >= 80000) | ||
#include <fmt/args.h> | ||
#else | ||
#include <fmt/core.h> | ||
#endif | ||
|
||
waybar::modules::Cpu::Cpu(const std::string& id, const Json::Value& config) | ||
: ALabel(config, "cpu", id, "{usage}%", 10) { | ||
thread_ = [this] { | ||
dp.emit(); | ||
thread_.sleep_for(interval_); | ||
}; | ||
} | ||
|
||
auto waybar::modules::Cpu::update() -> void { | ||
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both | ||
auto [load1, load5, load15] = Load::getLoad(); | ||
auto [cpu_usage, tooltip] = CpuUsage::getCpuUsage(prev_times_); | ||
auto [max_frequency, min_frequency, avg_frequency] = CpuFrequency::getCpuFrequency(); | ||
if (tooltipEnabled()) { | ||
label_.set_tooltip_text(tooltip); | ||
} | ||
auto format = format_; | ||
auto total_usage = cpu_usage.empty() ? 0 : cpu_usage[0]; | ||
auto state = getState(total_usage); | ||
if (!state.empty() && config_["format-" + state].isString()) { | ||
format = config_["format-" + state].asString(); | ||
} | ||
|
||
if (format.empty()) { | ||
event_box_.hide(); | ||
} else { | ||
event_box_.show(); | ||
auto icons = std::vector<std::string>{state}; | ||
fmt::dynamic_format_arg_store<fmt::format_context> store; | ||
store.push_back(fmt::arg("load", load1)); | ||
store.push_back(fmt::arg("usage", total_usage)); | ||
store.push_back(fmt::arg("icon", getIcon(total_usage, icons))); | ||
store.push_back(fmt::arg("max_frequency", max_frequency)); | ||
store.push_back(fmt::arg("min_frequency", min_frequency)); | ||
store.push_back(fmt::arg("avg_frequency", avg_frequency)); | ||
for (size_t i = 1; i < cpu_usage.size(); ++i) { | ||
auto core_i = i - 1; | ||
auto core_format = fmt::format("usage{}", core_i); | ||
store.push_back(fmt::arg(core_format.c_str(), cpu_usage[i])); | ||
auto icon_format = fmt::format("icon{}", core_i); | ||
store.push_back(fmt::arg(icon_format.c_str(), getIcon(cpu_usage[i], icons))); | ||
} | ||
label_.set_markup(fmt::vformat(format, store)); | ||
} | ||
|
||
// Call parent update | ||
ALabel::update(); | ||
} |
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,15 @@ | ||||||
#include <spdlog/spdlog.h> | ||||||
|
||||||
#include <cmath> // NAN | ||||||
|
||||||
#include "modules/cpu_frequency.hpp" | ||||||
|
||||||
std::vector<float> waybar::modules::CpuFrequency::parseCpuFrequencies() { | ||||||
static std::vector<float> frequencies; | ||||||
if (frequencies.empty()) { | ||||||
spdlog::warn( | ||||||
"cpu/bsd: parseCpuFrequencies is not implemented, expect garbage in {*_frequency}"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chase module rename and maybe slightly deduplicate wording
Suggested change
|
||||||
frequencies.push_back(NAN); | ||||||
} | ||||||
return frequencies; | ||||||
} |
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,67 @@ | ||
#include "modules/cpu_frequency.hpp" | ||
|
||
// In the 80000 version of fmt library authors decided to optimize imports | ||
// and moved declarations required for fmt::dynamic_format_arg_store in new | ||
// header fmt/args.h | ||
#if (FMT_VERSION >= 80000) | ||
#include <fmt/args.h> | ||
#else | ||
#include <fmt/core.h> | ||
#endif | ||
|
||
waybar::modules::CpuFrequency::CpuFrequency(const std::string& id, const Json::Value& config) | ||
: ALabel(config, "cpu_frequency", id, "{avg_frequency}", 10) { | ||
thread_ = [this] { | ||
dp.emit(); | ||
thread_.sleep_for(interval_); | ||
}; | ||
} | ||
|
||
auto waybar::modules::CpuFrequency::update() -> void { | ||
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both | ||
auto [max_frequency, min_frequency, avg_frequency] = CpuFrequency::getCpuFrequency(); | ||
if (tooltipEnabled()) { | ||
auto tooltip = | ||
fmt::format("Minimum frequency: {}\nAverage frequency: {}\nMaximum frequency: {}\n", | ||
min_frequency, avg_frequency, max_frequency); | ||
label_.set_tooltip_text(tooltip); | ||
} | ||
auto format = format_; | ||
auto state = getState(avg_frequency); | ||
if (!state.empty() && config_["format-" + state].isString()) { | ||
format = config_["format-" + state].asString(); | ||
} | ||
|
||
if (format.empty()) { | ||
event_box_.hide(); | ||
} else { | ||
event_box_.show(); | ||
auto icons = std::vector<std::string>{state}; | ||
fmt::dynamic_format_arg_store<fmt::format_context> store; | ||
store.push_back(fmt::arg("icon", getIcon(avg_frequency, icons))); | ||
store.push_back(fmt::arg("max_frequency", max_frequency)); | ||
store.push_back(fmt::arg("min_frequency", min_frequency)); | ||
store.push_back(fmt::arg("avg_frequency", avg_frequency)); | ||
label_.set_markup(fmt::vformat(format, store)); | ||
} | ||
|
||
// Call parent update | ||
ALabel::update(); | ||
} | ||
|
||
std::tuple<float, float, float> waybar::modules::CpuFrequency::getCpuFrequency() { | ||
std::vector<float> frequencies = CpuFrequency::parseCpuFrequencies(); | ||
if (frequencies.empty()) { | ||
return {0.f, 0.f, 0.f}; | ||
} | ||
auto [min, max] = std::minmax_element(std::begin(frequencies), std::end(frequencies)); | ||
float avg_frequency = | ||
std::accumulate(std::begin(frequencies), std::end(frequencies), 0.0) / frequencies.size(); | ||
|
||
// Round frequencies with double decimal precision to get GHz | ||
float max_frequency = std::ceil(*max / 10.0) / 100.0; | ||
float min_frequency = std::ceil(*min / 10.0) / 100.0; | ||
avg_frequency = std::ceil(avg_frequency / 10.0) / 100.0; | ||
|
||
return {max_frequency, min_frequency, avg_frequency}; | ||
} |
32 changes: 2 additions & 30 deletions
32
src/modules/cpu/linux.cpp → src/modules/cpu_frequency/linux.cpp
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe remove the conditional as the module code is still compiled in.
cpu
is like a superset, socpu_frequency
printing different warning is confusing: