Skip to content

Estimate the number of usable cores to perform data processing in the browser.

License

Notifications You must be signed in to change notification settings

darionco/WebCPU

Repository files navigation

WebCPU

Utility to estimate the number of usable cores to perform data processing in node.js and the browser.

In node.js, it uses the code from this gist to query the number of CPUs on the system. It can be configured to run the same estimation as in the browser.

In the browser, takes ~2 seconds to estimate the number of CPUs, uses WASM (when available) to perform the estimation.

Returns a Promise that resolves to a WebCPUResult.

Installation

yarn add webcpu

or

npm install webcpu

Usage

In Web:

import {WebCPU} from 'webcpu';

WebCPU.detectCPU().then(result => {
    console.log(`Reported Cores: ${result.reportedCores}`);
    console.log(`Estimated Idle Cores: ${result.estimatedIdleCores}`);
    console.log(`Estimated Physical Cores: ${result.estimatedPhysicalCores}`);
});

In Node:

const WebCPU = require('webcpu/dist/umd/webcpu').WebCPU;

WebCPU.detectCPU().then(result => {
    console.log(`Reported Cores: ${result.reportedCores}`);
    console.log(`Estimated Idle Cores: ${result.estimatedIdleCores}`);
    console.log(`Estimated Physical Cores: ${result.estimatedPhysicalCores}`);
});

Description

The core estimation is affected by other tasks in the system, usually the OS scheduler is efficient enough that light tasks (playing music, idle programs, background updates, etc) are evenly distributed across cores and so they will not affect the result of this estimation. Heavy tasks do have an effect in the results of the estimation, it is recommended that you avoid performing heavy tasks while the estimation is running, it is considered good practice to run the estimation periodically to compensate for user's CPU workloads and always keep an optimal number of operational cores.

The estimation is performed by running a mathematical operation in a loop for a predefined amount of time. Modern CPUs run this task simultaneously across physical cores and usually each core completes a very similar number of operations, once hyper threading (or task scheduling) kicks in, a few cores must share their cycles among threads running. By detecting the changes in operations completed by each thread, it is possible to estimate the number of cores in the system.

The current algorithm returns bad estimations for CPUs with asymmetric cores (usually mobile ARM chips) because, as explained above, it detects the changes in number of operations between threads. Asymmetric cores will complete a different number of operations depending on the power of the core the task is scheduled on. Although the returned estimations will be incorrect, they are safe to use to spawn threads.

This utility DOES NOT estimate logical cores, instead it uses navigator.hardwareConcurrency (if available) or simply returns the same number as the estimated physical cores.

Methods

detectCPU

Estimates the number of CPUs in this machine.

Parameters

  • hardcore boolean? Engages hardcore mode, which kills all the workers after every test. (optional, default false)
  • estimateInNode boolean? If true, forces core estimation in Node.js rather than querying the system. (optional, default false)

Returns Promise<WebCPUResult> Result of the estimation.

WebCPUResult

Type: Object

Properties

  • reportedCores (number | null) The result of navigator.hardwareConcurrency or null if not supported. navigator.hardwareConcurrency returns the total number of cores in the system, physical and logical. This is not particularly useful for data computations because logical cores do no improve and, in some cases, even hinder performance in repetitive tasks.
  • estimatedIdleCores number This number represents the estimated number of cores that can be used to compute a repetitive task, like data computations, in parallel. The result of the estimation is affected by system workload at the time of the detection, if this number is used to spawn threads, it is recommended to re-run the detection algorithm periodically to always use an optimal number of cores when computing data.
  • estimatedPhysicalCores number Given the reported number of cores and the result of estimated idle cores, this number represents the "best guess" for the total number of physical cores in the system. This number of threads is safe to use on all platforms.