Live data from Hacker News

Myths Programmers Believe about CPU Caches (2018)

software.rajivprab.com

11–20 of 143 posts

Re: Myths Programmers Believe about CPU Caches (2018)

#11
post #6

Earlier quoted context omitted.

Modern CPUs are pushing 1MB L1 and 8MB L2 or more. You can fit a dozen FreeRTOS instances in that space. It would be pretty cool to see someone build a system that used a high end CPU but didn't have any installed ram, though I'm not sure if the CPU microcode would be ok with that.

Those numbers are for all cores. No modern CPU has anywhere near that of L1 and L2 per core.

For context, the latest 7950x has 64KB of L1 per core, and 1MB L2

Re: Myths Programmers Believe about CPU Caches (2018)

#12
post #5

My favourite myth is that I still believe it's possible to write code which operates totally out of L1 and L2 cache. Not just tight ASM on bare metal: C code or similar, compiled down, to run under a modern UNIX/POSIX os on a multi-core host. I have never explored HOW this would work, or WHAT I would do to achieve it, but I believe it, implicitly. For it to be true I would have to understand the implications of every…

If you only boot one EFI process, or a small linux kernel + single binary small enough, you can get there.

so I'm imagining speedyOS which assumes one core for the OS, and runs its userspace jobs on the other cores, and if you wire a job to a core and its been coded to fit in L1/L2 then.. it just "does" -as long as the other jobs are somehow co-erced to run in the other cores, "swapping" in and out as need be. If you can prevent any clicktick disrupting your run state on this one "golden" user core, it just runs as fast as it can, subject to the OS timeslice effects on it, and any unavoidable synchronisation into other bits of the combined CPU/ALU/Memory/Bus system as a whole.

Re: Myths Programmers Believe about CPU Caches (2018)

#13
We worked with the Intel guys, in my last gig. They were incredibly helpful.

They were an impressive lot, and they helped us out, quite a bit. They often sent engineers over, for weeks at a time, to help us optimize.

The cache thing was a 100X improvement thing, and it came from the oddest places. There's a lot of "that doesn't make sense!" stuff, with preserving caches.

I don't remember all the tricks, but we were constantly surprised.

One thing that saved us, was instrumentation. Intel had a bunch of utilities that they wrote, and that kept showing us that the clever thing we did, was not so clever.

Re: Myths Programmers Believe about CPU Caches (2018)

#14
post #11

Earlier quoted context omitted.

Those numbers are for all cores. No modern CPU has anywhere near that of L1 and L2 per core.

For context, the latest 7950x has 64KB of L1 per core, and 1MB L2

And I know we were talking about L1/2, but Epycs can have over 1GB of total L3.

Re: Myths Programmers Believe about CPU Caches (2018)

#15

We worked with the Intel guys, in my last gig. They were incredibly helpful. They were an impressive lot, and they helped us out, quite a bit. They often sent engineers over, for weeks at a time, to help us optimize. The cache thing was a 100X improvement thing, and it came from the oddest places. There's a lot of "that doesn't make sense!" stuff, with preserving caches. I don't remember all the tricks, but we were c…

What kind of work were you doing that's required that? Like domain of programing? Sounds interesting

>that kept showing us that the clever thing we did, was not so clever.

I had the same experience talking with some people network people from Intel. Was pretty funny. To any Intel lurkers here, nice job! Lol

Re: Myths Programmers Believe about CPU Caches (2018)

#16
post #15

We worked with the Intel guys, in my last gig. They were incredibly helpful. They were an impressive lot, and they helped us out, quite a bit. They often sent engineers over, for weeks at a time, to help us optimize. The cache thing was a 100X improvement thing, and it came from the oddest places. There's a lot of "that doesn't make sense!" stuff, with preserving caches. I don't remember all the tricks, but we were c…

What kind of work were you doing that's required that? Like domain of programing? Sounds interesting >that kept showing us that the clever thing we did, was not so clever. I had the same experience talking with some people network people from Intel. Was pretty funny. To any Intel lurkers here, nice job! Lol

We wrote image processing pipeline code.

Very complex algorithms, on lots of data, that needed to be done quickly.

Re: Myths Programmers Believe about CPU Caches (2018)

#17

Hmm, I think the discussion is missing a few things needed for a complete picture of the situation. First, ARM and x86 coherency models differ, so a big disclaimer is needed regarding the protocol. Most ARM processors use the MOESI protocol instead of the MESI protocol. Second, synchronization isn't just because of register volatility and such. Synchronization is needed in general because without the appropriate lock…

Any recommendations for something more thorough to read or watch?

Re: Myths Programmers Believe about CPU Caches (2018)

#18
Dealing with caches, memory ordering, and memory barriers can be truly mind-warping stuff, even for those who have spent years dealing with basic cache coherency before. If you want a challenge, try to absorb all this in one sitting.

https://www.kernel.org/doc/Documentation/memory-barriers.txt

I kept an earlier version of this close to hand at all times a couple of jobs ago where we were using our own chips with a very weak memory ordering. The implementation team had mostly come from Alpha, which had the weakest memory ordering ever, and in the intervening years a lot of bugs related to missing memory barriers had crept into the kernel because nobody was using anything nearly as weak. I specifically remember at least one in NBD, at least one in NFS, and many in Lustre. Pain in the ass to debug, because by the time you can look at anything the values have "settled" and seem correct.

For extra fun, as weak as the memory ordering was, the first run of chips didn't even get that right. LL/SC wouldn't work reliably unless the LL was issued twice, so we actually modified compilers to do that. Ew.

Re: Myths Programmers Believe about CPU Caches (2018)

#19

Hmm, I think the discussion is missing a few things needed for a complete picture of the situation. First, ARM and x86 coherency models differ, so a big disclaimer is needed regarding the protocol. Most ARM processors use the MOESI protocol instead of the MESI protocol. Second, synchronization isn't just because of register volatility and such. Synchronization is needed in general because without the appropriate lock…

> First, ARM and x86 coherency models differ, so a big disclaimer is needed regarding the protocol. Most ARM processors use the MOESI protocol instead of the MESI protocol.

MOESI is called out.

> The above are just some of the possible scenarios that can occur. In reality, there are numerous variations of the above design, and no 2 implementations are the same. For example, some designs have an O/F state.

However that's not an ARM v x86 thing; AMD has at least previously used MOESI.

> Second, synchronization isn't just because of register volatility and such. Synchronization is needed in general because without the appropriate lock/barrier instructions, compilers make assumptions about how loads and stores may be reordered with respect to one another.

Compiler barriers are different than hardware memory barriers. There are reasons to have one, but not the other.

Re: Myths Programmers Believe about CPU Caches (2018)

#20

This article gives the impression that everything is the compiler's fault when you end up with conflicting reads in different cores, but that's not right. From the point of view of someone outside the CPU, yes you can say that simultaneous reads might never give different answers. But everything happening at that level barely resembles the original software. Dozens of instructions are happening at any moment, overlap…

The author has a good grasp of coherence but either ignores or fails to understand memory ordering.
Post reply on HN