Skip to content

Set the FreeBSD clock types #879

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

Merged

Conversation

etcwilde
Copy link
Member

@etcwilde etcwilde commented Jun 6, 2025

Dispatch passes everything through in nanoseconds, but the default time on FreeBSD is in milliseconds, resulting in delays taking a million times longer than expected. This stands out most with Task.sleep, where a 2 second sleep would take hours.

By setting the EVFILT_TIMER filter flag to use nanoseconds, we get the expected delay.

Also sets up the absolute time flag. The spelling on FreeBSD is NOTE_ABSTIME instead of NOTE_ABSOLUTE.

Dispatch passes everything through in nanoseconds, but the default time
on FreeBSD is in milliseconds, resulting in delays taking a million
times longer than expected. This stands out most with `Task.sleep`,
where a 2 second sleep would take hours.

By setting the EVFILT_TIMER filter flag to use nanoseconds, we get the
expected delay.

Also sets up the absolute time flag. The spelling on FreeBSD is
`NOTE_ABSTIME` instead of `NOTE_ABSOLUTE`.
@etcwilde
Copy link
Member Author

etcwilde commented Jun 6, 2025

@swift-ci please test

@akmorrison
Copy link

Could you check that this runs in approximately the right amount of time under all three clocks? I'm not 100% sure how monotonic time and wall time are computed on freebsd, but there might be other bugs lurking there.

Something like:

dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), queue, ^{
    printf("Ran after 2s of uptime clock\n");
});
dispatch_after(dispatch_time(DISPATCH_WALLTIME_NOW, 3 * NSEC_PER_SEC), queue, ^{
    printf("Ran after 3s of walltime clock\n");
});
dispatch_after(dispatch_time(DISPATCH_MONOTONICTIME_NOW, 4 * NSEC_PER_SEC), queue, ^{
    printf("Ran after 4s of monotonictime clock\n");
});

@etcwilde
Copy link
Member Author

etcwilde commented Jun 6, 2025

#include "dispatch/dispatch.h"
#include "dispatch/private.h"

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), queue, ^{
                        printf("Ran after 2s of uptime clock\n");
        });
        dispatch_after(dispatch_time(DISPATCH_WALLTIME_NOW, 3 * NSEC_PER_SEC), queue, ^{
                        printf("Ran after 3s of walltime clock\n");
        });
        dispatch_after(dispatch_time(DISPATCH_MONOTONICTIME_NOW, 4 * NSEC_PER_SEC), queue, ^{
                        printf("Ran after 4s of monotonictime clock\n");
        });
        dispatch_after(dispatch_time(DISPATCH_MONOTONICTIME_NOW, 5 * NSEC_PER_SEC), queue, ^{
                printf("Exiting after 5s of monotonic clock\n");
                exit(0);
        });
        dispatch_main();
}
ewilde@Taquito /tmp %  time ./a.out 
Ran after 2s of uptime clock
Ran after 3s of walltime clock
Ran after 4s of monotonictime clock
Exiting after 5s of monotonic clock
./a.out  0.75s user 4.15s system 98% cpu 5.003 total

Seems to be working as I would expect.

@michael-yuji
Copy link
Member

cc @3405691582 as OpenBSD might need this too

@etcwilde
Copy link
Member Author

etcwilde commented Jun 7, 2025

swiftlang/swift#82074
@swift-ci please test Windows platform

@3405691582
Copy link
Member

Note I fudged around this here (l2406), not knowing about these defines. OpenBSD probably needs this too, but would have to undo this fudge factor. I can't test the change for another week or two though. c4814ec

@etcwilde
Copy link
Member Author

etcwilde commented Jun 7, 2025

Ah yeah, I saw that cutout while tracing through the code and wondered what that was about. I'll merge this once it passes and when you have a chance can put up the PR to change the clocks once you have a chance to try it.

@etcwilde etcwilde merged commit b8acb7b into swiftlang:main Jun 7, 2025
2 checks passed
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.

5 participants