Hacker News new | ask | show | jobs
by jaen 11 days ago
The slowest part is always emulating the MMU, as that adds a 2-3× overhead to every memory access. In that sense ARM Sv39 is not a great choice.

Usually emulators use a direct-mapped cache (basically a software TLB).

In this case though, since you control the kernel, it could use something like the MIPS software-managed TLB of the olden days, with bespoke paging data structures and fast-path page fault handlers, perhaps also using PowerPC Inverted Page Tables as inspiration. (and eg. changing page allocation strategies to constrain pages in the lower address space region to simplify cache look up, and something to prevent cache set conflicts).

Given that the working set is small tho in this use case, eg. just a kernel-managed direct-mapped `page_number[MAX_PAGES] → base_address` table is sufficient and fast. (with some trick for page faults; also who needs granular RWX access permissions...)

3 comments

I tested this, doesn't seem to offer a speedup in real workloads, there is a speedup only in a simulated tlb thrashing microbenchmark

  wasm32-jit on a ryzen 9 3900, lower ms = faster

  workload                  baseline  flat-mmu  winner
  ------------------------  --------  --------  ------
  conflict (tlb thrashing)    491 ms     39 ms  flat-mmu 12.7×
  chase (pointer chasing)     448 ms    116 ms  flat-mmu 3.9×
  stream (sequential rmw)      78 ms     80 ms  baseline 1.03×
  sieve (primes)               81 ms     84 ms  baseline 1.04×
  high-conflict (>64 mib)     484 ms    506 ms  baseline 1.05×
  fib (cpu)                    35 ms     37 ms  baseline 1.05×
  mem (memory ops)             69 ms     75 ms  baseline 1.08×
  sha (hashing)                97 ms    106 ms  baseline 1.09×
  boundary (64 mib edge)       73 ms     82 ms  baseline 1.13×
  high (hot pages >64 mib)     27 ms     32 ms  baseline 1.21×
  control (hot pages)          27 ms     32 ms  baseline 1.21×
If what's supposed to be just removing code and instructions from the hot path is benchmarking as slower, I think there's a problem with the implementation :)

Did you check the final compiled assembly for the memory access path?

Also measuring only the whole system in a comparison benchmark is not statistically appropriate/legible, what's desired is something like causal profiling (to isolate the slowness of the rest of the system vs. the part that's being optimized):

https://github.com/plasma-umass/coz https://rocm.docs.amd.com/projects/rocprofiler-systems/en/la...

You are right, after more cleaning up the micro-benchmarks recovered. I still could not get it to translate to improvements in real-world tests (nix eval, sqlite, etc). The original patch put a 64 MiB range check and a second lookup in front and kept the old path as the fallback

  microbenchmark  flat map   8x TLB
  conflict        13.7x      14.5x
  chase           3.6x       3.5x
  high-conflict   0.81x      16.2x
  high            0.76x      1.00x
  mem/sha/sieve   0.90-1.03  ~1.00

  real world: 
  cold boot       278 -> 276 ms
  nix eval        670 -> 726 ms 
  sqlite 10k      676 -> 724 ms
I learned this from inspecting the code that TCG on QEMU generates... was very surprised when a load ballooned into tens of instructions!
Isn't Sv39 RISC-V thing?
Yeah, the project mixes the ARM ISA and the RISC-V page table format...