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 variable) or the Java Virtual Machine (JVM) property -Dftemul.virtualThreads=true

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. A response_delay holds back only the one exchange it applies to: it is scheduled after CoAP deduplication and applied once per exchange, retransmissions that arrive during the hold are ignored, and nothing else on the device waits for it. A notify_delay (lwm2m.notification.delay) holds back only the one Notify it applies to: it is scheduled above the CoAP reliability layer, so it is applied once per Notify and never to its retransmissions, the Notifies leave in the order they were sent, and nothing else on the device waits for it. Order wins over each Notify’s own delay: if the delay is lowered while a Notify is held, the held Notify can leave before its full delay. A value that is not an integer is ignored with a WARN and counts as no delay.

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

  • LwM2M mode ON also refuses to start on JDK 25 before 25.0.3. On those versions a virtual thread can wake from a timed wait seconds late (JDK bug JDK-8370887), so under CPU contention a device misses requests and its own timers for tens of seconds. Use JDK 25.0.3 or later, or the image’s JDK 27.

  • 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, except a leaf minted with lwm2m.x509.leaf.validity_window=expired. 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.validity_window

(unset)

Optional, LwM2M only, for tests. The only allowed value is expired (exact, lower case, not trimmed). The device then mints a leaf that is already expired: notBefore is the first mint time minus 25 hours and notAfter is the first mint time minus 24 hours. Issuer, chain, CN, key usage and extended key usage are the same as for a default leaf. lwm2m.x509.leaf.validity_days and FTEMUL_X509_LEAF_MAX_VALIDITY_HOURS do not change this window. An absent or blank key gives the default window. This differs from lwm2m.x509.leaf.cn, where a blank value fails device creation. A blank key on a device that does not mint a leaf is accepted. The key is a template option. POST /api/protocolOptions changes it for devices that start after the call, and each start reads it again. The key needs a device that mints a leaf: X.509 security mode on a secure connection with lwm2m.x509.ca.keystore, and no client keystore and no EST enrollment. A malformed value fails device creation with lwm2m.x509.leaf.validity_window must be expired, or unset or blank for the current window. The value is not part of the message. A valid value on a device that mints no leaf fails device creation with lwm2m.x509.leaf.validity_window is set but this device mints no leaf: it needs the X.509 security mode on a secure connection with lwm2m.x509.ca.keystore. The format check runs before the second check.

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 Message Queuing Telemetry Transport (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.

Socket read timeout

Eclipse Paho is the MQTT client library of the emulator. Paho reads each socket with a 1-second timeout. The receive loop of an idle device therefore wakes once per second. With 50,000 devices, the receive loops wake about 50,000 times per second and load the processor.

In mode ON, the emulator raises the read timeout of each MQTT connection to 60 seconds after every connect and reconnect.

  • Keep-alive: no change. The ping timer still detects a broker without a ping response.

  • Stop: the connection still closes at once. The stop interrupts the receive loop and closes the socket.

  • Log: the first raised timeout logs one INFO message, MQTT socket read timeout raised.

  • Paho upgrade without socket access: one WARN message with a stack trace. The 1-second timeout stays.

  • Switch off: no change. The 1-second timeout stays.

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

  • On JDK 25 before 25.0.3 with the mode on, startup logs one WARN line: event=usp-scale-mode-jdk-defect virtualThreads=true runtime=<JDK version> defect=JDK-8370887 fixedIn=25.0.3 effect="virtual-thread timed waits can wake seconds late" action="run JDK 25.0.3 or later, or set FTEMUL_VIRTUAL_THREADS=false". The emulator still starts. The line is absent when the mode is off or the JDK is not affected. See the known issue under Sizing and limits.

  • 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 Java Flight Recorder (JFR) event jdk.VirtualThreadPinned.

  • The option -Djdk.tracePinnedThreads does not exist on Java Development Kit (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.

The USP data model stays in memory and writes no files. Devices with the same data model share one template.

The heap per device depends on the param update setting. The setting is on by default.

Setting Heap per device What each device keeps

param update on (default)

about 350 KB

Its own copy of every parameter outside Device.ManagementServer.

param update off

about 40 KB

Only its own changes to the shared template.

Both figures come from a data model of 2,000 parameters, with connected devices and no Auto Configuration Server (ACS) requests. With param update off, a device after a 20-second wait needs about the same heap as an active device. The two measured runs used connected devices and no ACS requests. The figures do not cover devices under ACS load.

Use the figures as a sizing guide, not as a promise. Multiply the heap per device by the device count. Then add the base heap of the application and extra headroom for garbage collection. For example, 1,000 devices with param update on need about 350 MB for devices alone. The container needs more memory than -Xmx, because of socket buffers, class metadata and thread stacks. The ephemeral ports limit the device count too.

A broker closes a connection when the MQTT CONNECT packet arrives late. HiveMQ Community Edition closes the connection after 10 seconds by default. For this reason the JDK version matters for burst starts.

Known issue: slow burst start on JDK 25 before 25.0.3

On JDK 25 before 25.0.3, mode ON can start USP devices slowly. These JDK versions carry JDK-8370887: a virtual thread can wake from a timed wait seconds late, mostly under CPU contention.

Measured on Linux with 2 CPUs: a burst start of 1,000 devices with the per-device fallback executors (no shared executor), JDK 25.0.1 and JDK 25.0.4.1 running at the same time on the same 2 CPUs, 4 rounds:

  • JDK 25.0.1: in every round, the slowest device start took 30.0 to 30.7 seconds. Between 4 and 214 starts per round took longer than 10 seconds.

  • JDK 25.0.4.1: the slowest device start took 2.8 to 3.6 seconds. No start took longer than 10 seconds.

  • An earlier run on macOS with JDK 25 (25+36) showed the same start time of up to about 31 seconds. In 1 of 6 runs there, one device stopped answering requests after it subscribed. JDK 27 started every device within about 1 second.

A 10-second broker CONNECT timeout (see above) can therefore close such a device’s connection. The fallback executors are embedded or test use; the GUI and the device manager do not use them. The defect is in the JDK, so it also affects their shared executor, but that path has not been measured on JDK 25.0.1.

USP keeps mode ON on these JDK versions, because ON is its default. Instead, startup logs one WARN line (see Check the mode).

The Docker image runs JDK 27 and is not affected. The desktop GUI and the web application use the local JDK when started without Docker: use JDK 25.0.3 or later, or turn the mode off with FTEMUL_VIRTUAL_THREADS=false.

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.

For developers

The heap figures come from the test class UspMqttMemoryReport, with mutual TLS, scale mode on and 1,000 devices started in batches of 50. The heap per device is read at 100 devices and at 1,000 devices. The test is opt-in. Its Javadoc lists the properties. Add -Dusp.memory.updateParameter=true to measure with param update on. The idle figure adds -Dusp.memory.idleWaitMs=20000.

The property usp.resource.shared_template turns the shared data-model template on or off. Set it as a device property or as a JVM option -Dusp.resource.shared_template. The default is true. The value false gives each device its own tree in memory, with no files. Use false for a comparison or as a fallback.

To measure the heap per device with the 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>.

  3. Add -Dusp.scale.devices=<N> to set the device count.

  4. Read the printed heap per device, the thread counts and the resident memory.

  5. Read the jdk.VirtualThreadPinned events, grouped by stack.

    The report prints them for start-up and for the 60-second idle window after it. The keep-alive timer fires in that window.

A default test run never selects the class, because its name does not end in Test. Name it with -Dtest.

Without -Dusp.scale.report=true, the test does not run.