LwM2M Scale Mode and Load Testing
Scale mode lets one emulator container register and hold thousands of LwM2M devices (multi-device runs with clientsCount x concurrentClients). It is off by default; with it off the emulator behaves exactly as before.
Enabling scale mode
| Switch | Default | Description |
|---|---|---|
|
off |
Turns scale mode on. Read once at JVM start; changing it needs a restart. Either source set to |
|
mode ON: max(4096, planned devices); mode OFF: CPUs x 4 |
Size of the shared pool that runs every device’s Observe, firmware/software update and reboot work. Overrides both defaults. |
When the mode is ON:
-
The emulator’s internal thread pools run on virtual threads. Per-device work runs on per-device executor views over one shared timer, so an idle device holds about 0 platform threads (measured locally: 0 platform threads per device against 6 to 7 with the mode off).
-
Each device still has its own UDP socket and its own serialization, so a
response_delayornotify_delaysleep blocks only that device. -
The startup log carries
event=emulator-scale-mode virtualThreads=true shadow=… carriers=<n>. If the emulator cannot switch every pool, it refuses to start (event=emulator-scale-mode-refused) rather than run half-virtual. -
Use
-Djdk.virtualThreadScheduler.parallelism=<n>to change the number of carrier threads (default: the container’s CPUs).
When the mode is OFF (default), and for a single device in either mode, behaviour is unchanged.
|
A load run also needs a high file-descriptor limit (one per device, for example |
Per-device credentials
Both options apply to the device’s security mode. They are read when a run is initialised (/api/init) and when a device starts; a change pushed to a running fleet does not alter the identity of devices that already started.
PSK per device
Set lwm2m.psk.per_device=true:
-
The PSK identity of each device is its endpoint name.
-
The key is the first 16 bytes of
HMAC-SHA256(key = template PSK key, data = UTF-8 endpoint name). The template key is the hex-encoded PSK key configured on the connection, and must be at least 16 bytes. -
A server can therefore provision every device from the formula alone.
-
Absent or
false: every device uses the template identity and key, as before.
X.509 from a CA
With the X.509 security mode and lwm2m.x509.ca.keystore set, every device gets its own certificate minted at start (no EST involved).
| Key | Default | Description |
|---|---|---|
|
(unset) |
Path, inside the container, of the CA PKCS#12 file (mount it read-only). Unset: the device uses the keystore-based credential as before. |
|
the store’s only key entry |
Alias of the CA key entry. |
|
|
Validity of each minted leaf, in days. The emulator host caps this value with |
|
(unset) |
Optional, per device. The subject |
|
|
What the device presents: |
|
(unset) |
Path of a file holding the CA keystore password. This is the only password source: |
|
|
Upper limit, in hours, for the validity of every minted leaf, with or without |
The minted leaf has CN equal to the device’s endpoint name (or to lwm2m.x509.leaf.cn, if set), an EC P-256 key, a random serial, clientAuth extended key usage and is signed by the CA key. A self-signed CA is not presented as part of the chain, so leaf_and_ca then presents the leaf only. The certificate is kept per endpoint and CN for the run: a reboot or wake keeps it, a new /api/init mints anew. Minting and EST enrollment on the same device are mutually exclusive and fail device creation with a clear message.
Mount the same CA into every container of a fleet: the server then needs one trust entry for all of them.
Transport knobs
| Key | Default | Description |
|---|---|---|
|
|
CoAP acknowledgment timeout in ms. Must be an integer of at least 1; |
|
|
Protocol-stage threads per device. Same validation (at least 1). It keeps its effect in scale mode: it sets how many tasks the device runs in parallel. |
|
|
Receiver threads per device. Same validation (at least 1). |
|
|
Sender threads per device. Same validation (at least 1). |
|
|
Scale mode only: the largest inbound DTLS datagram in bytes. Range 1500 to 16490; any other value fails device creation. Read once per run at |
A DTLS datagram larger than the limit is counted and logged as event=dtls-datagram-over-limit deviceId=… peer=<address:port> sizeAtLeast=<limit+1> limit=<limit> count=<n>: the first one per endpoint start, then every 1,000th. Raise the limit if a handshake flight (for example an X.509 chain) does not fit.
Devices with identical ack_timeout and thread-count values share one transport configuration (at most 16 distinct sets per JVM; further sets are private to the device, logged once as event=network-config-cap-reached).
Behaviour changes
-
WRITE-ATTRIBUTES sent to an instance that the server created (a server-Created instance) is now applied. It was dropped before.
-
Invalid transport knobs (see above) are refused when the device is created instead of being accepted.
USP MQTT MTP
The same switch also moves the USP MQTT message transfer protocol (MTP) onto virtual threads. For USP the default is on, unlike LwM2M, where it stays off. With the switch off, the USP emulator behaves as before.
-
With the switch unset, USP runs in mode ON and LwM2M in mode OFF.
-
FTEMUL_VIRTUAL_THREADS=falseor-Dftemul.virtualThreads=falseturns USP back to mode OFF.trueturns the mode on for USP and for LwM2M together. There is no separate USP switch. -
The system property wins over the environment variable.
-
The switch is read once at JVM start. Changing it needs a restart.
-
The load scripts
loadtest/emulator-capacity.shandloadtest/emucap-native-measure.shset the same variable. A run of these scripts with the mode on therefore also changes USP devices in that JVM.
Threads per device
| Item | Mode OFF | Mode ON (default) |
|---|---|---|
MQTT client loops (receive, send, callback) and ping |
About 4 to 5 platform threads per device. |
Virtual threads. The only platform thread is one shared timer, |
Reconnect, request and periodic-notification work, and the bulk-push fallback pool |
Platform threads per device. |
Per-device virtual threads. An idle worker retires after 30 seconds. |
Connect and disconnect work |
A short-lived platform thread per call. |
A virtual thread per call. |
The goal in mode ON is zero platform threads per MQTT device, so the platform thread count does not grow with the number of devices.
The shared timer only starts each keep-alive ping. The ping itself runs on a virtual thread, so a slow ping never blocks the timer. The timer handles one schedule per device in each keep-alive cycle.
Not covered
These parts keep their platform threads in both modes:
-
The STOMP MTP.
-
The WebSocket MTP.
-
The BulkData MQTT v3 push client.
Check the mode
-
The first start of the MQTT client logs one INFO line:
event=usp-mqtt-virtual-threads USP MQTT MTP virtual-thread mode ON (switch=ftemul.virtualThreads/FTEMUL_VIRTUAL_THREADS) timer=usp-mqtt-timer. The line is absent when the mode is off. -
To find pinned virtual threads, record the JFR event
jdk.VirtualThreadPinned. The option-Djdk.tracePinnedThreadsdoes not exist on JDK 24 and later.
Sizing and limits
Virtual-thread stacks live on the heap. At the limit, the failure changes with the mode:
-
Mode OFF: one device fails to get a native thread.
-
Mode ON: the whole JVM runs out of heap memory (
OutOfMemoryError). The web UI and the other protocols stop too.
After threads, the next limits are:
-
Ephemeral ports. All devices connect from one source IP to one broker endpoint, so the TCP source ports bound the count. A Linux default allows about 28,000 ports, and the maximum is about 64,000.
-
File descriptors (
nofile). Use at least65536, as in the note above. -
The container heap limit,
-Xmx. The Docker image default is-Xmx1g. Raise it for large fleets.
To measure the heap per device, run the test class UspMqttScaleReport:
-
Start an external MQTT broker.
-
From the repository root, run
mvn -pl usp-emulator -am test -Dtest=UspMqttScaleReport -Dsurefire.failIfNoSpecifiedTests=false -Dusp.scale.report=true -Dusp.scale.brokerUrl=<broker URL>. Add-Dusp.scale.devices=<N>to set the device count. The class name does not end inTest, so a default test run never selects it: name it with-Dtest. -
Read the printed heap per device, the thread counts and the resident memory. The report also prints the
jdk.VirtualThreadPinnedevents grouped by stack, for start-up and for a 60-second idle window after it, and the keep-alive timer fires in that window.
Without -Dusp.scale.report=true, the test is skipped. The printed heap per device is an upper bound, because the report runs with data-model serialization off. Use it for sizing, not as a promise.
A broker closes a connection when the MQTT CONNECT packet arrives late (HiveMQ CE: 10 seconds by default). For this reason the JDK version matters for burst starts.
|
Known issue: slow burst start on JDK 25
On JDK 25 (observed on macOS), mode ON can start devices slowly in one case. The case is the per-device fallback executors, with no shared executor. This case is embedded or test use. The GUI and the device manager do not use it.
The Docker image runs JDK 27, and mode ON is recommended there. The desktop GUI and the web application run on JDK 25 when started without Docker. Mode ON on JDK 25 is not measured on that production path (the GUI and device-manager executor). The slowest start of about 5 seconds came from a test stand-in for it, a shared pool of 64 virtual worker threads, not from the production executor. Run mode ON on JDK 27. |
Load testing
The repository ships a capacity harness under loadtest/. Its README.md is the reference for the knob table, verdicts and the host launch; it is not repeated here.
-
emulator-capacity.sh: capacity tool. Steps a fleet up (registered devices, threads, memory per device, the point where it breaks) for five arms: scale mode OFF and ON, each with no security, PSK and (ON only) X.509. -
emucap-host.sh: host launcher. Runs all arms under one operations lock on the demo host, pinned off the QA CPUs, and tears down by label. Its window (WINDOW=any|night), give-up and hard-stop times are documented inloadtest/README.md, section "Demo host launch". -
emucap-native-measure.sh: native measure tool. Runs the emulator jar directly (no container, loopback only) in scale mode and measures the live heap per device, with a PASS/FAIL verdict against the slice’s target.
Run the authoritative numbers on the demo host only; local runs are indicative.