
Show HN: detecting cache latency inside a Web browser - Marat_Dukhan
https://maratyszcza.github.io/laff-demos/pointer-chasing.html
======
Marat_Dukhan
Author here. I made this demo and a related matrix-matrix multiplication demo
[1] back in 2015 for Robert van de Geijn's Linear Algebra: Foundations to
Frontiers MOOC class [2]. In the light of Spectre attack and recent browsers'
changes to reduce precision of timers, I remembered of this project, and
decided to check if it still works now, 3 years later. Surprisingly, it still
works well!

The source code is available on GitHub [3].

[1] [https://maratyszcza.github.io/laff-
demos/dgemm.html](https://maratyszcza.github.io/laff-demos/dgemm.html)

[2] [https://www.edx.org/course/linear-algebra-foundations-
fronti...](https://www.edx.org/course/linear-algebra-foundations-frontiers-
utaustinx-ut-5-02x)

[3] [https://github.com/Maratyszcza/laff-
demos](https://github.com/Maratyszcza/laff-demos)

~~~
madez
Could you make the website viewable without JavaScript?

Edit: I think the downvotes are unjustified. For clarification, if that wasn't
clear by context, I don't expect to get the JavaScript test results from my
computer while viewing the website without JavaScript. Demanding that would
obviously be nonsense. Rather than that I assume there is information on that
website that is interesting to read even without using JavaScript personally.
Or is using JavaScript now a requirement to learn about JavaScript?

~~~
detaro
There's only a graph of the results, so you are not missing any other content.

~~~
madez
Thanks for the info.

------
kaoD
On my pretty outdated Core 2 Duo Q9300:
[https://i.imgur.com/HQXm5FU.png](https://i.imgur.com/HQXm5FU.png)

The CPU is advertised as 6 MB L2 cache[0] but it has "L1 = 4 x 32 KB 8-way set
associative data caches and L2 = 2 x 3 MB 12-way set associative caches (each
L2 cache is shared between 2 cores)"[1].

You can clearly see the constant time access to L1 cache up to 32KB. Then
grows to (linear?) access time up to 2MB (where it still fits in one of the L2
caches) and then again a different (log-like?) function from there on.

This is really neat.

[0] [https://ark.intel.com/products/33922/Intel-Core2-Quad-
Proces...](https://ark.intel.com/products/33922/Intel-Core2-Quad-
Processor-Q9300-6M-Cache-2_50-GHz-1333-MHz-FSB)

[1] [http://www.cpu-world.com/CPUs/Core_2/Intel-
Core%202%20Quad%2...](http://www.cpu-world.com/CPUs/Core_2/Intel-
Core%202%20Quad%20Q9300%20EU80580PJ0606M%20\(BX80580Q9300\).html)

------
esnard
Somewhat related: using the cache latency to estimate the CPU cache size.
[https://fromwhenceitca.me/cache_size/cache_size.html](https://fromwhenceitca.me/cache_size/cache_size.html)

------
jacquesm
So, who thought it was a great idea again to use a mechanism intended for
document transfer to run unsigned, unaudited code on just about every client
in the world?

~~~
nicolaslem
To be fair the problem is not related to web browsers. It's more about running
code in general.

I start to realize that running someone else's code means trusting the
developer. No matter the amount of sandboxing and the layers of abstraction,
eventually his code will be able to run as root using exploits that are yet to
be discovered.

~~~
jacquesm
Sure, but until the advent of JS you at least knew whose code you were going
to run. Now you are more or less required to run untrusted code just to get
through daily life.

~~~
kevingadd
And web platform people remain convinced that HTTPS transport encryption is
sufficient to protect everyone, even though desktop app, OS, and bootloader
people have been doing code-signing for something like two decades.

------
Sephr
This is similar to how I detected total CPU cores in 2013 to implement
navigator.hardwareConcurrency: [https://eligrey.com/blog/cpu-core-estimation-
with-javascript...](https://eligrey.com/blog/cpu-core-estimation-with-
javascript/)

------
laCour
i5-4690K (L1 4x32KB; L2 4x256KB; L3 6MB):
[https://i.imgur.com/FUs1isW.png](https://i.imgur.com/FUs1isW.png)

LG v30 (L2 2MB):
[https://i.imgur.com/q5R3bLY.png](https://i.imgur.com/q5R3bLY.png)

------
polyfemos
And with the latest iOS 11.2.2, this (admittedly cool) piece of code no longer
works.

Still works on the latest MacOS (10.13.2) though.

~~~
Marat_Dukhan
Hmm...I just tried on iPhone 7/iOS 11.2.2, and it still works, albeit takes
very long to start.

------
mherrmann
On my OnePlus X Android phone:
[https://imgur.com/a/C1hb7](https://imgur.com/a/C1hb7)

~~~
chrisper
On my i7 8700k [https://imgur.com/a/D71v1](https://imgur.com/a/D71v1)

------
Yetanfou
Thinkpad T42p with Pentium M 1.8GHz, 2MB L2 cache

[https://imgur.com/agxy8e7](https://imgur.com/agxy8e7)

------
Mister_Snuggles
I'd like to see some text explaining what I'm looking at.

Based on the title, it's something to do with cache latency, but that doesn't
really help me. What exactly is it measuring? How does it perform this
measurement? What are the limitations of this technique? What are the wider
implications of this? What uses does it have, both potentially nefarious ones
and potentially beneficial ones?

------
yeukhon
Is there plan to collect this data voluntarily? Would be interesting to see
the different percentiles

~~~
Marat_Dukhan
No, it is a static web page, and all code runs only locally in your browser.

------
chrisper
Is that Ram only or also L1 etc. caches?

~~~
frik
Okay, so how can we deactivate ASM.js and WebAssembly? (in the light of
Meltdown and Spectre)

The config in Chrome is broken, WebAssembly cannot be deactivated anymore with
chrome://flags/#enable-webassembly , setting it to "deactivated" and it's
still active.

~~~
Marat_Dukhan
Asm.js is not necessary, simple JavaScript interpreter is enough. This demo
used to work before most browsers implemented optimizers for Asm.js

