why is my rep movsb not faster
-
- Member
- Posts: 70
- Joined: Fri Jun 28, 2013 1:48 am
- Contact:
why is my rep movsb not faster
Tested on real machine, Intel core i3-5005U, 64-bit mode. CPUID shows enhanced rep movsb is supported.
Use rep movsb and rep movsq to copy from memory to framebuffer, and rep movsq about 8x faster than rep movsb. (EDIT: about 3~4 times, see the reply)
It seems rep movsb has no speed up at all. Source and target pointer are both 16-byte aligned. Is there any flag I have to enable?
Use rep movsb and rep movsq to copy from memory to framebuffer, and rep movsq about 8x faster than rep movsb. (EDIT: about 3~4 times, see the reply)
It seems rep movsb has no speed up at all. Source and target pointer are both 16-byte aligned. Is there any flag I have to enable?
Last edited by songziming on Thu Mar 28, 2024 5:55 am, edited 1 time in total.
Reinventing the Wheel, code: https://github.com/songziming/wheel
Re: why is my rep movsb not faster
I may be shooting in the dark here, but I recall reading something about all movs instructions being optimized the same way as movsb. If this is true (and hopefully someone with more knowledge on this will pitch in) then your results would make sense - movsq moves eight bytes at a time instead of one for movsb, i.e., it would not mean that you're not using the enhanced movsb, it would only mean that the relative difference in performance would be more or less the same, as both are (supposedly!!) optimized in equivalent ways. Please be aware that I'm not confident on the reliability of the original source.
Writing a bootloader in under 15 minutes: https://www.youtube.com/watch?v=0E0FKjvTA0M
-
- Member
- Posts: 5512
- Joined: Mon Mar 25, 2013 7:01 pm
Re: why is my rep movsb not faster
songziming wrote:framebuffer
What memory type is your framebuffer?Intel wrote:Fast-string operation is used only if the source and destination addresses both use either the WB or WC memory types.
-
- Member
- Posts: 70
- Joined: Fri Jun 28, 2013 1:48 am
- Contact:
Re: why is my rep movsb not faster
I also tested copy data from physical address zero to a dynamically allocated temp buffer. Copy size is 8 pages (32K). And time measure is using timestamp counter.
In this test no framebuffer is involved. There's no other running task, so no preemption.
"mem copy" means copy using rep movsb, "fast copy" means rep movsq. source and destination is always page aligned.
In this test no framebuffer is involved. There's no other running task, so no preemption.
"mem copy" means copy using rep movsb, "fast copy" means rep movsq. source and destination is always page aligned.
Reinventing the Wheel, code: https://github.com/songziming/wheel
-
- Member
- Posts: 5512
- Joined: Mon Mar 25, 2013 7:01 pm
Re: why is my rep movsb not faster
Your results are very inconsistent. Does your test program repeat the tests several times, or does it only run them once?
What is your CPU's TSC frequency?
Bit 0 of IA32_MISC_ENABLE (MSR 0x1A0) enables fast string instructions. It should already be set by firmware, but you might as well check.
What is your CPU's TSC frequency?
Bit 0 of IA32_MISC_ENABLE (MSR 0x1A0) enables fast string instructions. It should already be set by firmware, but you might as well check.
-
- Member
- Posts: 70
- Joined: Fri Jun 28, 2013 1:48 am
- Contact:
Re: why is my rep movsb not faster
Bit 0 of IA32_MISC_ENABLE is enabled, I've checked.
What's the correct way to get TSC frequency? What I got from cpuid.15h and cpuid.16h is zero.
The buffer is allocated on the first run, and I ran the test several times. Interrupt is also disabled before the test.
What's the correct way to get TSC frequency? What I got from cpuid.15h and cpuid.16h is zero.
The buffer is allocated on the first run, and I ran the test several times. Interrupt is also disabled before the test.
Reinventing the Wheel, code: https://github.com/songziming/wheel
-
- Member
- Posts: 5512
- Joined: Mon Mar 25, 2013 7:01 pm
Re: why is my rep movsb not faster
You're using a Broadwell CPU, so the TSC frequency is calculated by taking the value in bits 8-15 of MSR_PLATFORM_INFO (0xCE) and multiplying by 100MHz.songziming wrote:What's the correct way to get TSC frequency?
It doesn't count if you have to manually start each test run! The multiple test runs need to be completely automatic with nothing else between each run (so don't display results until all runs are complete).songziming wrote:The buffer is allocated on the first run, and I ran the test several times.
-
- Member
- Posts: 70
- Joined: Fri Jun 28, 2013 1:48 am
- Contact:
Re: why is my rep movsb not faster
I modified the test code, and the result seems valid. Time for movsb and movsq is close. I also tested copying data into framebuffer, their result is different.Octocontrabass wrote:The multiple test runs need to be completely automatic with nothing else between each run
So I think it's ok to use movsb for normal memcpy, and use special aligned copy in framebuffer driver.
btw, tsc frequency is 2GHz.
Reinventing the Wheel, code: https://github.com/songziming/wheel
-
- Member
- Posts: 5512
- Joined: Mon Mar 25, 2013 7:01 pm
Re: why is my rep movsb not faster
What memory type is your framebuffer? Fast-string instructions only work with WB and WC, but the firmware will usually set the framebuffer to UC.songziming wrote:I also tested copying data into framebuffer, their result is different.
-
- Member
- Posts: 70
- Joined: Fri Jun 28, 2013 1:48 am
- Contact:
Re: why is my rep movsb not faster
MTRR shows they're uncacheable, and I didn't set PAT/PCD/PWT bit in the page entry, IA32_PAT[7:0] is 6 (writeback). So the memory type for framebuffer is UC.Octocontrabass wrote:What memory type is your framebuffer?
Reinventing the Wheel, code: https://github.com/songziming/wheel
-
- Member
- Posts: 5512
- Joined: Mon Mar 25, 2013 7:01 pm
Re: why is my rep movsb not faster
That's why it's slower. Try setting up your framebuffer for WC instead of UC.