Page 1 of 1

why is my rep movsb not faster

Posted: Tue Mar 26, 2024 11:49 pm
by songziming
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?

Re: why is my rep movsb not faster

Posted: Wed Mar 27, 2024 5:12 pm
by BigBuda
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.

Re: why is my rep movsb not faster

Posted: Wed Mar 27, 2024 7:40 pm
by Octocontrabass
songziming wrote:framebuffer
Intel wrote:Fast-string operation is used only if the source and destination addresses both use either the WB or WC memory types.
What memory type is your framebuffer?

Re: why is my rep movsb not faster

Posted: Wed Mar 27, 2024 9:42 pm
by songziming
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.

Re: why is my rep movsb not faster

Posted: Wed Mar 27, 2024 10:53 pm
by Octocontrabass
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.

Re: why is my rep movsb not faster

Posted: Thu Mar 28, 2024 9:20 pm
by songziming
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.

Re: why is my rep movsb not faster

Posted: Thu Mar 28, 2024 10:57 pm
by Octocontrabass
songziming wrote:What's the correct way to get TSC frequency?
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:The buffer is allocated on the first run, and I ran the test several times.
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).

Re: why is my rep movsb not faster

Posted: Sat Mar 30, 2024 5:39 am
by songziming
Octocontrabass wrote:The multiple test runs need to be completely automatic with nothing else between each run
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.

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.

Re: why is my rep movsb not faster

Posted: Sat Mar 30, 2024 11:12 am
by Octocontrabass
songziming wrote:I also tested copying data into framebuffer, their result is different.
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.

Re: why is my rep movsb not faster

Posted: Sat Mar 30, 2024 7:21 pm
by songziming
Octocontrabass wrote:What memory type is your framebuffer?
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.

Re: why is my rep movsb not faster

Posted: Sat Mar 30, 2024 10:00 pm
by Octocontrabass
That's why it's slower. Try setting up your framebuffer for WC instead of UC.