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

FTEMUL_VIRTUAL_THREADS=true (environment) or -Dftemul.virtualThreads=true (JVM property)

off

Turns scale mode on. Read once at JVM start; changing it needs a restart. Either source set to true enables it.

-Dftemul.sharedPoolSize=<n>

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_delay or notify_delay sleep 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 --ulimit nofile=65536:65536) and a fromPort below the container’s ephemeral range (for example 20000). The shipped default 56830 caps a container at about 8,700 devices.

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

lwm2m.x509.ca.keystore

(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.

lwm2m.x509.ca.alias

the store’s only key entry

Alias of the CA key entry.

lwm2m.x509.leaf.validity_days

365

Validity of each minted leaf, in days. The emulator host caps this value with FTEMUL_X509_LEAF_MAX_VALIDITY_HOURS: the leaf is valid for the smaller of the two periods. A non-positive number, or one that ends beyond the supported date range, fails device creation.

lwm2m.x509.leaf.cn

(unset)

Optional, per device. The subject CN of the minted leaf, instead of the endpoint name. Allowed characters: letters, digits, ., _, : and -, 1 to 64 characters long. Device creation fails if the value is blank or malformed, or if the device does not mint a leaf. The message names the key and not the value.

lwm2m.x509.leaf.chain

leaf_and_ca

What the device presents: leaf_and_ca (leaf plus issuing CA) or leaf (leaf only). Anything else fails device creation.

FTEMUL_X509_CA_PASSWORD_FILE (environment)

(unset)

Path of a file holding the CA keystore password. This is the only password source: FTEMUL_X509_CA_PASSWORD is never read (it is reported as ignored with a warning), and the password is never a REST field and is never logged.

FTEMUL_X509_LEAF_MAX_VALIDITY_HOURS (environment)

24

Upper limit, in hours, for the validity of every minted leaf, with or without lwm2m.x509.leaf.cn. Valid values are whole numbers from 1 to 999999. An unset or blank value means 24. Any other value (for example 0, a negative number, text, a decimal, or a number with spaces or a leading zero) fails device creation for every minting device, and the message names the variable. A device kept in one run for longer than this limit presents an expired leaf, and its next handshake fails. For long runs, raise the value on the emulator host. A QA X.509 run needs the cap at or below 24, because the default 24 applies host-wide to every device that mints a leaf; a soak run longer than 24 hours must raise it on the emulator host.

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

ack_timeout

10000

CoAP acknowledgment timeout in ms. Must be an integer of at least 1; 0, negative and non-numeric values are now refused at device creation (they were accepted before).

lwm2m.transport.protocol_stage_thread_cnt

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.

lwm2m.transport.receiver_thread_cnt

1

Receiver threads per device. Same validation (at least 1).

lwm2m.transport.sender_thread_cnt

1

Sender threads per device. Same validation (at least 1).

lwm2m.scale.dtls.receive_buffer

2048

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 /api/init, never from a *_gui.properties file. Ignored when the mode is OFF.

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=false or -Dftemul.virtualThreads=false turns USP back to mode OFF. true turns 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.sh and loadtest/emucap-native-measure.sh set 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, usp-mqtt-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.tracePinnedThreads does 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 least 65536, 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:

  1. Start an external MQTT broker.

  2. 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 in Test, so a default test run never selects it: name it with -Dtest.

  3. Read the printed heap per device, the thread counts and the resident memory. The report also prints the jdk.VirtualThreadPinned events 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.

  • A burst start of 1,000 devices took up to about 31 seconds for one device start.

  • In 1 of 6 runs, one device stopped answering requests after it subscribed.

  • The same test on JDK 27, the JDK of the runtime image, started every device within about 1 second.

  • On Linux with JDK 25 and 2 CPUs, the same test started every device within about 2.3 seconds.

  • The root cause is not proven.

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 in loadtest/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.