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

Increase the time resolution beyond whole seconds for rate mode (argument -r) #106

Merged
merged 1 commit into from
Nov 25, 2023

Conversation

edgar-bonet
Copy link
Collaborator

The program torture.c outputs a waveform of amplitude 5 and period 0.72 s. Its peak derivative is therefore expected to be

5 × (2π ÷ 0.72) ≈ 43.6

However,

./torture | ./ttyplot -r

shows a waveform that peaks at about 0.5, which is off by two orders of magnitude. The reason is that data points arrive every 10 ms, and ttyplot “rounds” this value to a full second, effectively underestimating the rate by a factor 100.

This pull request fixes this behavior by handling time with microsecond resolution. It also moves the computation of rates to its own function for better code clarity.

Copy link
Collaborator

@hartwork hartwork left a comment

Choose a reason for hiding this comment

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

Hi @edgar-bonet I think there is a chance that there is a misunderstanding of what -r was meant to do. Maybe I'm wrong, that's okay, let's find out!

The way I understand the current -r is that it is meant to be used with accumulators, values that only ever go up. For example, let's take a data source like this:

# while grep numa_hit /proc/vmstat | awk '{print $2}' ; do sleep 0.25 ; done
113308336
113308915
113309576
113310145
113310700
113311263
113311825
113312415
113312971
113313544
^C

Now if we diff these values, we can see they are roughly 700 apart:

# a=0; while sleep 0.25; do b="$(grep numa_hit /proc/vmstat | awk '{print $2}')"; echo "$((b - a))"; a="${b}"; done
113417603
678
685
688
686
692
782
706
693
695
698
^C

When I pipe that as input to ./ttyplot -r now as…

# while grep numa_hit /proc/vmstat | awk '{print $2}' ; do sleep 0.25 ; done | ./ttyplot -r

…on master, ttyplot display values and average match that "roughly 700 apart" reality, seems like a match.

When I do the same now on branch rate(commit 0863f67) the values and average are way higher, more like 2400.

Therefore my impression is that we have two different scenarios here and that maybe we need an additional flag to support your scenario in addition under a new command line argument rather than replacing the current -r.

What do you think?

@edgar-bonet
Copy link
Collaborator Author

@hartwork: Replace sleep 0.25 with sleep 2. You will see that both master an this PR's branch compute the same thing: a rate of roughly 2400 hits/s.

@hartwork
Copy link
Collaborator

@hartwork: Replace sleep 0.25 with sleep 2.

@edgar-bonet … because the old code uses a timer with seconds resolution and therefore 0.25 is too fast for it then I suppose, interesting!

I guess then maybe this pull request is just doing the right thing? 😃

The variable naming x and y seem a bit unfortunate given that (1) both seem to be y coordinates and (2) the application uses x and y already for 2-dimensional coordinates in ncurses space. If you could pick a different pair of names, I'd likely be all for the new ones. What do you think?

ttyplot.c Outdated Show resolved Hide resolved
Times are measured using time(), which has a one second resolution.
Because of this, ttyplot can give vastly underestimated rates if data
points arrive faster than once a second.

Increase the time resolution by using gettimeofday() instead of time().
Also, move the computation of the rates out of the main loop, into their
own function, in order to improve readability.
@edgar-bonet
Copy link
Collaborator Author

@hartwork wrote:

The variable naming x and y seem a bit unfortunate

Indeed it is. I replaced them with v1 and v2, which are more consistent with the current naming. I also added const qualifiers as you suggested. Squashed and force-pushed.

Copy link
Collaborator

@hartwork hartwork left a comment

Choose a reason for hiding this comment

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

@edgar-bonet thanks for the adjustments 👍 Let's see how @tenox7 likes it.

@tenox7
Copy link
Owner

tenox7 commented Nov 23, 2023

interesting, so basically if samples for rate calculator are sub second interval the data will be lost? I would love to fix this, but I will need to do a lot more testing, typically with network equipment/snmp where I use it the most

@tenox7
Copy link
Owner

tenox7 commented Nov 23, 2023

also note that torture.c was never designed for rate testing, it would be cool if you could add it as well, maybe make the sleep/interval an arg/flag and option for rate (-r) ?

ideally torture -r should be able to generate a similar wave plot like without -r by manipulating rate (higher/lower)

@edgar-bonet
Copy link
Collaborator Author

@tenox7 wrote:

if samples for rate calculator are sub second interval the data will be lost?

Not exactly. Each number received on the input adds one bar to the plot, so no data is lost. The issue is that, in master, the values are incorrect. If the interval is large, the rates displayed are real rates (i.e. time derivatives), in counts per second. If the interval becomes smaller than one second, then ttyplot “rounds” it up to one full second, which results in overestimated rates.

The issue also impacts intervals that are longer (but not much longer) than one second. For example, the command

for n in $(seq 1 10 1000); do echo $n; sleep 1.5; done

creates a counter that increments by ten units every 1.5 seconds. This is a rate of 6.7 counts per second. Piping this to ttyplot in master shows a rate that alternates between 5 and 10. With this pull request, ttyplot shows a very steady rate of 6.7.

torture.c was never designed for rate testing, it would be cool if you could add it as well [...]

I would gladly do so. Within this PR or in a follow-up one?

@hartwork
Copy link
Collaborator

I would gladly do so. Within this PR or in a follow-up one?

My vote for merging to master what we have here and then doing a follow-up. Fewer bits in flight, baby steps, shorter-lived branches. Clear vote for a merge from my side.

edgar-bonet added a commit to edgar-bonet/ttyplot that referenced this pull request Nov 23, 2023
* Increase the time resolution in rate mode
@edgar-bonet edgar-bonet changed the base branch from master to development November 25, 2023 21:12
@edgar-bonet edgar-bonet merged commit 95467bf into tenox7:development Nov 25, 2023
3 checks passed
@edgar-bonet edgar-bonet deleted the rate branch November 25, 2023 22:38
@hartwork hartwork added this to the 1.6.0 milestone Nov 28, 2023
hartwork added a commit that referenced this pull request Nov 28, 2023
Fix clock display (regression from conflict resolution for #106)
@hartwork hartwork changed the title Increase the time resolution in rate mode Increase the time resolution beyond whole seconds for rate mode (-r) Nov 30, 2023
@hartwork hartwork changed the title Increase the time resolution beyond whole seconds for rate mode (-r) Increase the time resolution beyond whole seconds for rate mode (argument -r) Nov 30, 2023
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.

3 participants