Skip to content

Switch Mem::Meter to use std::chrono API#2392

Open
yadij wants to merge 1 commit intosquid-cache:masterfrom
yadij:rm-dtime-3
Open

Switch Mem::Meter to use std::chrono API#2392
yadij wants to merge 1 commit intosquid-cache:masterfrom
yadij:rm-dtime-3

Conversation

@yadij
Copy link
Contributor

@yadij yadij commented Mar 19, 2026

.. instead of our old time API.

.. instead of our old time API.
@yadij yadij added the S-could-use-an-approval An approval may speed this PR merger (but is not required) label Mar 19, 2026
Copy link
Contributor

@rousskov rousskov left a comment

Choose a reason for hiding this comment

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

Please stop posting new pull requests until the backlog is dealt with. I plan to come back to this PR after that happens.

ssize_t currentLevel() const {return level;}
ssize_t peak() const {return hwater_level;}
time_t peakTime() const {return hwater_stamp;}
const Timestamp &peakTime() const {return hwater_stamp;}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please do not introduce unnecessary references. The size of this data member is usually 8 bytes, same as a reference size, but without the reference complications.

Suggested change
const Timestamp &peakTime() const {return hwater_stamp;}
auto peakTime() const { return hwater_stamp; }

if (hwater_level < level) {
hwater_level = level;
hwater_stamp = squid_curtime ? squid_curtime : time(nullptr);
hwater_stamp = std::chrono::system_clock::now();
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC, we should continue using Squid current time "cache" in code like this. getCurrentTime() is already using std::chrono::system_clock::now() internally, so we need to remember/expose that value via a function. I did not check whether there are open PRs doing that already.


auto AsHours = [](const auto &base) -> double {
const auto span = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - base);
return span.count() / 3600.0;
Copy link
Contributor

Choose a reason for hiding this comment

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

This kind of manual math is essentially a reinterpret_cast<> equivalent. It is error-prone. I hope it is possible to avoid this unchecked (by compiler) time "casting" when using std::chrono in new code.

stream << delim;
}

auto AsHours = [](const auto &base) -> double {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use lowercase for local variables, avoid unwanted references (see another change request), and use const/AAA as much as possible.

And since this is not an I/O manipulator but a casting function, let's call it "[convert] toX()" rather than "[print] asX()".

Here is a sketch:

Suggested change
auto AsHours = [](const auto &base) -> double {
const auto toHours = [](const auto base) {

}

auto AsHours = [](const auto &base) -> double {
const auto span = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - base);
Copy link
Contributor

Choose a reason for hiding this comment

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

I hope it is possible to avoid duration_cast in this math. If it is possible, we should avoid it. I do not have a ready-to-use recipe at this time.

@rousskov rousskov removed the S-could-use-an-approval An approval may speed this PR merger (but is not required) label Mar 19, 2026
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