Skip to content
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

feat(temperature): Add zone-type setting #2752

Merged

Conversation

xphoniex
Copy link
Contributor

@xphoniex xphoniex commented Jun 22, 2022

What type of PR is this? (check all applicable)

  • Feature
  • Bug Fix

Description

CPU's thermal-zone changes on every boot on my machine, sometimes it's 1 and other times it's 2. Others also have had this issue and resorted to scripts updating the thermal-zone before starting polybar.

This can simply be fixed using a zone-type config option:

If you look at the content in /sys/class/thermal/thermal_zone%zone%/type, you'll get the name of the sensor and you can easily deduct zone from that. For example for Intel CPUs, you should set zone-type = x86_pkg_temp and you'll be set.

Related Issues & Documents

Ref #2572

Documentation (check all applicable)

  • This PR requires changes to the Wiki documentation (describe the changes)

@xphoniex
Copy link
Contributor Author

polite reminder @patrick96 , can we merge this?

@patrick96
Copy link
Member

I'm not around right now, so can't really review this. Will review once I'm back and have time. But general idea looks good. Except that you just try the first 10 zones instead of listing files in the folder and iterating through those.

@xphoniex xphoniex force-pushed the xphoniex/add-zone-type-to-temperature-mod branch from eb369e4 to 5b96d0f Compare July 2, 2022 04:37
@xphoniex
Copy link
Contributor Author

xphoniex commented Jul 2, 2022

I'm not around right now, so can't really review this. Will review once I'm back and have time. But general idea looks good. Except that you just try the first 10 zones instead of listing files in the folder and iterating through those.

Okay, cool.

Re iterations, rewrote it to use INT_MAX (here), this is simpler than listing the files (no allocation needed, and no need to then extract zone # from the file string).

I have a few useful contributions which I can open PR for once this is merged.

@patrick96
Copy link
Member

I'm not the biggest fan of iterating until no file is found. Is it guaranteed that all thermal_zoneX appear in order without gaps?

Also, the error message for when no zone is found is very confusing:

The file '/sys/class/thermal/thermal_zone1/type' does not exist

The error message should tell the user which zone type it failed to find (and maybe even list all available zone types).


I have a few useful contributions which I can open PR for once this is merged.

Please open a discussion topic for those. That way we can see how those will fit into polybar.

@xphoniex xphoniex force-pushed the xphoniex/add-zone-type-to-temperature-mod branch from 5b96d0f to 6ced7ce Compare July 9, 2022 16:40
@xphoniex
Copy link
Contributor Author

xphoniex commented Jul 9, 2022

I'm not the biggest fan of iterating until no file is found. Is it guaranteed that all thermal_zoneX appear in order without gaps?

Also, the error message for when no zone is found is very confusing:

The file '/sys/class/thermal/thermal_zone1/type' does not exist

The error message should tell the user which zone type it failed to find (and maybe even list all available zone types).

There's this in kernel, but still not sure about gaps as there are ways to unregister sensors.

I suppose you've seen this issue before so I implemented what you suggested, check the diff please.

@xphoniex xphoniex force-pushed the xphoniex/add-zone-type-to-temperature-mod branch from 6ced7ce to 379499a Compare July 9, 2022 16:48
@xphoniex
Copy link
Contributor Author

reminder

@codecov
Copy link

codecov bot commented Jul 25, 2022

Codecov Report

Merging #2752 (379499a) into master (b5764c8) will decrease coverage by 0.02%.
The diff coverage is 0.00%.

❗ Current head 379499a differs from pull request most recent head 0ba3557. Consider uploading reports for the commit 0ba3557 to get more accurate results

@@            Coverage Diff             @@
##           master    #2752      +/-   ##
==========================================
- Coverage   13.52%   13.49%   -0.03%     
==========================================
  Files         152      152              
  Lines       11396    11417      +21     
==========================================
  Hits         1541     1541              
- Misses       9855     9876      +21     
Flag Coverage Δ
unittests 13.49% <0.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/modules/temperature.cpp 0.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us.

Copy link
Member

@patrick96 patrick96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also mention this feature in the ¢hangelog.

}

if (!zone_found) {
throw module_error("zone-type '" + m_zone_type + "' was not found, available zone types:" + available_zones);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw module_error("zone-type '" + m_zone_type + "' was not found, available zone types:" + available_zones);
throw module_error("zone-type '" + m_zone_type + "' was not found, available zone types: " + available_zones);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (auto &z: zone_list) {
string zone_file = string_util::replace(PATH_THERMAL_ZONE_TYPE, "%zone%", to_string(z));
string z_zone_type = string_util::strip_trailing_newline(file_util::contents(zone_file));
available_zones += " " + z_zone_type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Instead of manually constructing the string, we could store the types in a vector and finally construct the string using. string_util::join(available_zones, ", ")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 29 to 35
vector<string> thermal_list = file_util::list_files("/sys/class/thermal/");
for (auto &thermal: thermal_list) {
string zone_str = regex_replace(thermal, std::regex("thermal_zone([0-9]+)"), string("$1"), std::regex_constants::format_no_copy);
if (!zone_str.empty()) {
zone_list.push_back(stoi(zone_str));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, but could make this code cleaner: Instead of extracting the zone number, we could file_util::glob for /sys/class/thermal/thermal_zone* and just add /type for getting the type and /temp to get m_path, the zone number isn't really needed for the module itself.

Pick whatever solution you like better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the zone number as an effort to keep behavior consistent for when there are multiple sensors with the same name. We know we'll always pick the one with lowest number, which depending on hardware and kernel may or may not be random.

Do you still want me to change this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's a good point. I think sorting the paths should be sufficient to keep consistency (even if it doesn't guarantee that the lowest number is picked).

It's up to you, whichever solution you like better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
@xphoniex xphoniex force-pushed the xphoniex/add-zone-type-to-temperature-mod branch from 379499a to 0ba3557 Compare July 30, 2022 12:09
@xphoniex
Copy link
Contributor Author

@patrick96
Copy link
Member

Looks very good now. I'll not be at my computer for a few weeks, so I'll have to do the final review + testing once I get back.

@xphoniex
Copy link
Contributor Author

bump to keep the PR in my Recent activity

@patrick96 patrick96 added this to the 3.7.0 milestone Aug 21, 2022
@patrick96 patrick96 merged commit 6ccecbf into polybar:master Aug 21, 2022
@patrick96
Copy link
Member

Thanks!

@xphoniex xphoniex deleted the xphoniex/add-zone-type-to-temperature-mod branch August 21, 2022 17:29
@patrick96 patrick96 mentioned this pull request Nov 5, 2023
24 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants