Repository navigation
mips32le (MT7621 router): where the CPU actually goes - measurements, not estimates #7550
Replies: 2 comments
Measured, not estimated: AES-GCM is 45 % of the CPU, not 10 %I built a small benchmark and ran it on the device itself (MT7621, KeeneticOS 5.02, Go 1.27.4, (CPU > wall because GCM runs on more than one thread under GOMAXPROCS.) The corrected breakdownMy first post estimated AES at ~10 % of total CPU from a generic throughput figure. That estimate was wrong.
The per-MB CPU of a bare GCM loop is 1.43 MB/s; full xray achieves 1.0 MB/s. The difference is the TLS framing, socket syscalls, buffer copies and scheduler noise I described in the first post — real, but no longer the dominant term. What that means for the two proposalsProposal B (MIPS AES asm) is worth far more than I said. With OpenSSL's
That is a 22–34 % CPU reduction, which on a 2-core 880 MHz router is the difference between one saturated core and headroom. Proposal A (fewer TLS records) is still real but smaller — it attacks part of the 55 % remainder, not the 45 %. Why this matters for
|
Tried the Go stdlib overlay: the compiler is already optimalFollow-up on proposal B. I did not guess — I built and measured it. What I tested
I wrote a replacement
The two binaries hash differently ( Why — the generated code is already what you'd write
No Where the cycles actually goIPC 0.13 on a 284-instruction block means the pipeline is stalled, not busy. The MIPS 1004Kc is in-order: every I also ruled out the obvious alternatives: warming the tables in L1 changed nothing (2168 vs 2169 cycles), and So the headroom is not in which instructions are emitted, it is in how many dependent loads are in flight — exactly what hand-written MIPS assembly (OpenSSL's Corrected cost model for this core
What this means for proposal BA Revised recommendation: proposal A (fewer, larger TLS records) is the better first step, because it attacks the remaining 55 % without touching crypto at all. If the maintainers want B, it has to be genuine assembly, and I would benchmark it on this device before proposing it upstream. The benchmark sources are ~120 lines total and I can paste them if anyone wants to reproduce. Every number above is from the actual MT7621 on my desk, not an estimate. |
Uh oh!
There was an error while loading. Please reload this page.
Context
Keenetic Ultra II router, MediaTek MT7621 (MIPS32R2 1004Kc, 2 physical cores / 4 VPE, 880 MHz), 128 MB RAM, KeeneticOS 5.02 + Entware mipsel-softfloat. Xray 26.11.9, official
xray_smipsle build (go1.27.3).Topology: LAN → router (Xray client) → XHTTP+REALITY over TLS to an entry node → internet. Everything runs on the router itself, no chain of two proxies.
The measurement
Tunnel throughput caps at ~1.0 MB/s (8.5 Mbit/s) while the WAN link is 75 Mbit/s. Per-process CPU of
xray, measured asutime+stimedelta from/proc/PID/stat:mode: auto, chrome)bufferSize4 KBbufferSize16 KBbufferSize32 KBbufferSize64 KBstream-one+ xmux 1-2 + firefoxConstant ~156 jiffies/MB = 1.56 s CPU per MB = 0.64 MB/s per core.
bufferSizeand Xmux change nothing. Xmux does work (TCP connections to the entry node go 5 → 7–8 and stay), yet throughput does not move.Per-thread breakdown over 8 MB (
/proc/PID/task/*/stat, 9 threads total):All useful work sits in 2 of 9 threads → it is single-connection bound, not scheduler bound.
GOMAXPROCS=3is already set and is not the limit.Where the time is not
I estimated the obvious candidates and they do not add up:
Even a 4× faster AES (OpenSSL
aes-mips32r2asm, or EIP-93 hardware) buys only ~7 % of total CPU. That is not where the 90 % is.Where the time is (hypothesis, not proven)
I could not profile: the 26.11.9 release binary has no symbols and the router has no
perf/pprof/strace. So this is inference from reading the code:common/buf/writer.go:38—BufferToBytesWriter.WriteMultiBufferends withnet.Buffers(bs).WriteTo(w.Writer).net.Buffers.WriteTo(net/net.go:852) takes thebuffersWriterfast path only if the writer implements it.tls.Conndoes not. So it falls back tofor _, b := range v { w.Write(b) }— onetls.Conn.Writeper 8 KB buffer, each producing its own TLS record.utls/conn.go:879—tcpMSSEstimate = 1208, so before 128 KB have been sent each application-data record carries only1208 - 5 (header) - 8 (nonce) - 16 (tag) = 1179bytes. With 8 KB buffers that is 7 TLS records per buffer, 889 per MB.Each record goes through
halfConn.encrypt→sliceForAppendon a pooledoutBuf→ AES-GCM →c.write(outBuf)→ onewrite()to the socket.So per 8 KB chunk: 1 readv → 7 × (record framing + AES-GCM on 1179 B +
write()). The per-record setup (key lookup, GHASH init, nonce assembly) is paid 7× more often than necessary, and at 1179 B granularity instead of 16 KB.Question
Would a change in this area be welcome, and in which form?
A. Fewer, larger records on the TLS write path. Instead of relying on
net.Buffers.WriteTofalling into per-bufferWrite(), coalesce the MultiBuffer into one contiguous buffer before handing it to the TLS writer (aBufferToBytesWritervariant with a copy), and/or let utls usemaxPlaintextinstead oftcpMSSEstimatefor throughput-oriented transports. Cost: one memcpy of the payload; benefit: 889 → 64 records per MB.B. AES asm for
linux && mipslebehind a build tag.common/crypto/aes.gohas exactly three entry points (NewCipher,NewGCM,NewGCMTLS). A//go:build linux && mipsle && cgofile using OpenSSL'saes-mips.pl(EVP_aes_128_gcm) would be ~30 lines and leave every other platform byte-identical. Expected 3–6 % total CPU — helpful, but not the 90 %.My measurements say A is worth more than B, but A touches the shared write path while B is a self-contained build-tag file.
Is either of interest? If yes I will open a PR with benchmarks from this device before/after. I can also provide full configs,
/proc/crypto, per-thread stats and exact reproduction steps on request.All reactions