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.
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
pybind/mgr: fix divided by zero error #26270
pybind/mgr: fix divided by zero error #26270
Changes from all commits
fe4efd0
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Just to understand, when is this case happening? A possible alternative could be to return
float('nan')
(Not a Number) as assuming this is0.0
can be misleading.nan
is a perfectly valid Python float object, so you can still perform arithmetic operations with it: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.
JOSN left out NaN, but
json.dumps
generates them:No idea what clients will do with NaN values then. I'd like to keep it simple and not run into this complexity.
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.
IMHO
0.0
is not good because it gives a wrong output.It seems that, while a lot of JSON parsers are capable of dealing with
NaN
(it's a valid JSnumber
value), it's not strict JSON:(That also implies that we should use
json.dumps(..., allow_nan=False)
always if we want to comply with the ECMA JSON standard).In that case you can leverage on the
null
value, as it's perfectly strict JSON. I would do the mapping 'NaN' tonull
in the JSON dumping, while keepingnan
for other output formats.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.
When returning something like
[0.4, 0.23, 123.0, 123.5, null, 42, 42,42, 0.5, 0.0]
, I wonder how many null related exceptions we're going to generate in client code. I don't think lots of users will expectnull
values here.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.
I share that concern too, but the issue here is whether it's preferable something that might not work:
Than something that works but contains false data:
If someone draws a chart from the latter data, they will see the graph suddenly dropping to zero. Most visualization engines (e.g.: Grafana) properly treat
null
values, but 0.0 is a perfectly valid value.If you use Python to parse that, you'll get a
None
(Java, PHP, etc., do the same), which will quickly trigger an exception if you try to do math on it: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.
I think so, too. I plan to amend this PR this week.
In case the dashboard becomes the official API, we should make sure, we're not returning nasty surprises, like stray
null
values. But yep, let's huddle if it makes sense.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.
Sorry for coming late to this discussion.
I'm going to step back a bit on the discussion of whether we should return
0.0
orNone
when we get aZeroDivisionError
exception, and analyze why are we getting aZeroDivisionError
when calculating the derivative of a sequence of data points.If we are getting a
ZeroDivisionError
is because we have two consecutive data points that have two (possibly different) values for the same timestamp. In mathematical terms, this is the same as saying thatf(t) = a and f(t) = b
at the same time, which is impossible. When calculating a derivative of a sequence of data points we are assuming that the data points were generated by a continuous function, and thus the derivative function expects thatdata2[0] - data1[0]
will be always different from zero.Since this is not the case because of timestamp resolution, we might get two perf counter values for the same timestamp. So what we should do is to filter those consecutive data points that have the same timestamp and keep just one of them.
Now we need to decide on the filtering algorithm for these special datapoints. Given a sequence of data points with the same timestamp, we can:
a) keep just the first data point of the sequence
b) keep just the last data point of the sequence
c) calculate the average of the data points
By doing this filtering we will never have a
ZeroDivisionError
again while calculating the derivative function, and solves this problem.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.
IMO we should just calculate the difference (option c), but using the absolute like I did in my draft PR #28603 . Just calculate the time difference first and use
1
if it's0
.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.
I agree here. Why do we get those broken perf values from C++ in the first place?
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.
It's not broken. The problem is that two perf values could have been separated by only a few nanoseconds but the clock only had milisecond resolution.