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

improve timing in scrot --delay #261

Merged

Conversation

guijan
Copy link
Contributor

@guijan guijan commented Apr 5, 2023

This will conflict with #257, has only been minimally tested, and there are other places in the code where it's useful but hasn't been put to use yet, so it remains a draft.

Old nanosleep() code (master) on OpenBSD:
openbsd-scrot-nanosleep
New clock_nanosleep() code (this PR), falling back to a local clock_nanosleep() implementation on systems lacking this POSIX function like OpenBSD and OS X, running on OpenBSD:
openbsd-scrot-clock_nanosleep
The timing error is consistently smaller across several runs. If you subtract the user and system runtime and the 60 second sleep from the real runtime, you get the timing error. It has been reduced by an order of magnitude here.

@guijan guijan force-pushed the use-clock_nanosleep branch 2 times, most recently from 1647a6d to 6310502 Compare April 11, 2023 03:39
@guijan
Copy link
Contributor Author

guijan commented Apr 11, 2023

Turns out the other nanosleep() calls shouldn't even be there (#274), so this PR is ready for review now that it's rebased and tested on OpenBSD.

@guijan guijan marked this pull request as ready for review April 11, 2023 14:11
@guijan guijan changed the title fix drift in scrot --delay $DELAY --count improve timing in scrot --delay Apr 12, 2023
src/util.c Outdated Show resolved Hide resolved
@guijan guijan force-pushed the use-clock_nanosleep branch from 6310502 to 7c43218 Compare May 15, 2023 19:12
@guijan
Copy link
Contributor Author

guijan commented May 15, 2023

Now the PR is significantly different, I've done very basic testing on OpenBSD, but it still needs more testing.

@guijan guijan force-pushed the use-clock_nanosleep branch from 7c43218 to 83fc97d Compare May 15, 2023 19:33
Copy link
Collaborator

@N-R-K N-R-K left a comment

Choose a reason for hiding this comment

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

Haven't tested yet, but a couple comments from quickly glancing over.

src/options.c Outdated Show resolved Hide resolved
src/options.c Outdated Show resolved Hide resolved
src/options.h Show resolved Hide resolved
src/scrot.c Outdated Show resolved Hide resolved
src/scrot.c Outdated Show resolved Hide resolved
src/scrot.c Outdated Show resolved Hide resolved
@guijan guijan force-pushed the use-clock_nanosleep branch from 83fc97d to e5f4e08 Compare May 17, 2023 15:13
src/scrot.c Show resolved Hide resolved
src/scrot.c Outdated Show resolved Hide resolved
@guijan guijan force-pushed the use-clock_nanosleep branch from e5f4e08 to 202c347 Compare May 17, 2023 15:27
@N-R-K N-R-K linked an issue May 17, 2023 that may be closed by this pull request
src/scrot.c Outdated Show resolved Hide resolved
@guijan guijan force-pushed the use-clock_nanosleep branch from 202c347 to 83b7d8c Compare May 18, 2023 14:22
Copy link
Collaborator

@N-R-K N-R-K left a comment

Choose a reason for hiding this comment

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

Looks mostly OK, I'll test it out sometime today.

src/options.c Outdated Show resolved Hide resolved
src/util.h Show resolved Hide resolved
@guijan guijan force-pushed the use-clock_nanosleep branch from 83b7d8c to 2ba3d62 Compare May 19, 2023 22:40
Copy link
Collaborator

@N-R-K N-R-K left a comment

Choose a reason for hiding this comment

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

Tested it out and it seems to work OK.

However, one weird thing I noticed while stepping through the new code under gdb was that it was taking the nanosleep path instead of the clock_nanosleep one. Which was weird since linux should support clock_nanosleep. Turned out, the reason was because I didn't re-run the configure scripts.

But this lead me to question how much value clock_nanosleep is providing anyways, is it worth the check and the ifdef ?

image

On my system at least, the answer seems to be no.

I'm fine merging this PR as-is, since not re-running configure was a user-error. But if you don't have any objection to it, I'd like to just use the nanosleep path unconditionally and remove the clock_nanosleep check.

Upgrade scrotSleep() to scrotWaitUntil() which sleeps until a given time
on the clock rather than sleeping for a given period of time. In all
cases, this improves the timing by compensating for the time passed
between entering main() and taking the screenshot. When used with
--count, it fixes the drift caused by calling nanosleep() in a
loop.

Also remove an incorrect comment and rename the delay_selection variable
to delaySelection to match our style.
@guijan guijan force-pushed the use-clock_nanosleep branch from 2ba3d62 to a2ffc11 Compare May 20, 2023 12:36
@guijan
Copy link
Contributor Author

guijan commented May 20, 2023

Yeah, in an algorithmic sense, both paths are the same: they both make the error relative to the sleep constant. It just annoys me when we don't use the good features of each platform. #ifdef removed.

@N-R-K N-R-K merged commit c4f0dd0 into resurrecting-open-source-projects:master May 20, 2023
tmp.tv_sec--;
tmp.tv_nsec += 1000000000L;
}
} while (nanosleep(&tmp, &tmp) == -1 && errno == EINTR);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just noticed, I don't think the 2nd tmp argument is doing anything. It'll just overwritten by clock_gettime at the top of the loop body.

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.

scrot --delay drift
2 participants