Skip to content

Conversation

@kripken
Copy link
Member

@kripken kripken commented Nov 7, 2019

Pthreads get the current time from their parent thread during
startup, so they can return results that make sense relative to it.
Without that, the time on a pthread can be "earlier" than
the time on the main thread, even if the pthread checks the
time later.

This broke in #9569 when we removed the magic globals
from worker.js, as now such values must be passed
explicitly. So that global just had the initial 0 value. The fix
is to pass the value on Module and use it from there, safely.

This adds a test. Timing tests are always risky, but this
just checks monotonicity of the time, and looks 100%
consistent when I run it locally, so hopefully it's ok. If we
see flakes on CI we may need to remove it though.

Fixes #9783

@niklasf
Copy link
Contributor

niklasf commented Nov 7, 2019

Awesome! On this branch, I am no longer observing any negative time differences. Neither in the minimal example, nor in stockfish.wasm.

@kripken
Copy link
Member Author

kripken commented Nov 7, 2019

Great! Thanks for confirming @niklasf

@kripken kripken requested a review from sbc100 November 13, 2019 16:59
@kripken
Copy link
Member Author

kripken commented Dec 3, 2019

friendly review ping @dschuff @sbc100

// performance.now() is specced to return a wallclock time in msecs since that Web Worker/main thread launched. However for pthreads this can cause
// subtle problems in emscripten_get_now() as this essentially would measure time from pthread_create(), meaning that the clocks between each threads
// would be wildly out of sync. Therefore sync all pthreads to the clock on the main browser thread, so that different threads see a somewhat
// coherent clock across each of them (+/- 0.1msecs in testing)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you wrap these comments at 80?

pthread_attr_init(&attr);

pthread_t thread;
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you need to set PTHREAD_CREATE_DETACHED here? Maybe we can just remove these attrs completely and pass NULL to keep the test cleaner.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since we cannot join the thread in this case, it seems best to explicitly detach it. Can try to remove, if you insist.


cond_var.notify_one();

return NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove all these extra newlines?

# clock.
@requires_threads
def test_pthread_reltime(self):
self.btest(path_from_root('tests', 'pthread', 'test_pthread_reltime.cpp'), expected='3', args=['-s', 'USE_PTHREADS=1', '-s', 'PTHREAD_POOL_SIZE=1'])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this in node now too? We should probably have a plan for centralizing the thread tests that run in both browser and node. Perhaps test_threads.py?

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense to me, but since it seems like a larger undertaking I hope this does not block landing this fix.

TimePoint now() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think i'd find this test more readable and use fewer lines of code is it was just C and used clock_gettime.. but thats probably personal preference so I won't ask you to change that.

@niklasf
Copy link
Contributor

niklasf commented Jan 1, 2020

Hi everyone! I tried to address the review in #10137 (with two exceptions commented here).

@kripken
Copy link
Member Author

kripken commented Jan 6, 2020

Landed in #10137 with @niklasf's help.

@kripken kripken closed this Jan 6, 2020
@kripken kripken deleted the ptime branch January 6, 2020 17:53
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.

Regression in steady_clock skew between pthreads

4 participants