-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add mfence
before issuing rdtsc
#30
Conversation
This should make all memory load/ store operations happening before RDTSC (in program order) globally visible. Read https://stackoverflow.com/a/12634857. Signed-off-by: Anjan Roy <[email protected]>
…d on different optimization settings Signed-off-by: Anjan Roy <[email protected]>
Thanks for this! I'm positive about the change. Have you witness any difference in behavior? From a quick search I see some slight difference between |
Comparison of unsigned expression in `>= 0` is always **true**. Signed-off-by: Anjan Roy <[email protected]>
Thanks :)
One of the motivation behind submitting this patch is, when using
I don't think using |
We're getting some CI failures @ https://github.com/oreparaz/dudect/actions/runs/7466176204/job/20402300482, but they are failing due to different reasons. |
Nice to see that by introducing
Let's keep the [1] https://www.felixcloutier.com/x86/mfence : "MFENCE does not serialize the instruction stream".
Fixing in #31 . I'll rebase this, add a bit of documentation and merge. Thanks! |
See #32 for more comments on this. |
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.
Thanks! Merging...
Makes sense, just another resource link from GNU/Linux kernel https://github.com/torvalds/linux/blob/9f8413c4a66f2fb776d3dc3c9ed20bf435eb305e/arch/x86/include/asm/msr.h#L201-L220. |
Why it is important ? Read oreparaz/dudect#30 Signed-off-by: Anjan Roy <[email protected]>
Given the fact that a x86 CPU doesn't guarantee when it'll actually issue
RDTSC
instruction, if used in isolation, we must use it with a memory fence instruction such asMFENCE
so that all memory load/ store operations, which appear beforeRDTSC
instruction, in the program order (i.e. source code or compiler generated assembly), are globally visible.It's necessary to use a memory fence either
lfence
ormfence
to enforce ordering constraints on CPU issuingrdtsc
instruction.See
Note, I use
mfence
(serializes both load and stores) instead oflfence
(serializes only loads), just to be safer. It shouldn't hurt.More about memory fencing @ https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.htm#text=fence&ig_expand=3977,4395,4395.