Skip to content

Commit 4c33e51

Browse files
committed
Bugfix CPU utilization
1 parent 43d635c commit 4c33e51

File tree

6 files changed

+30
-25
lines changed

6 files changed

+30
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import osUtils from 'lup-system';
3030

3131
Output:
3232
```
33-
CPU Utilization: 45.3
33+
CPU Utilization: 0.2313135420902806
3434
Drives: [
3535
{
3636
filesystem: 'C:',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lup-system",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "NodeJS library to retrieve system information and utilization.",
55
"files": [
66
"lib/**/*"

src/__tests__/CPU.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ test('getCpuTemperature', async () => {
66
});
77

88
test('getCpuUtilization', async () => {
9-
const utilization = await getCpuUtilization();
10-
console.log('CPU utilization:', utilization); // TODO REMOVE
9+
const utilization = await getCpuUtilization();
10+
console.log('CPU utilization:', utilization); // TODO REMOVE
1111
});
1212

1313
afterAll(() => {

src/cpu.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { execCommand, sleep } from './utils';
55
/** Intervall in milliseconds at which CPU utilization is computed. */
66
export let CPU_COMPUTE_UTILIZATION_INTERVAL = 1000;
77

8-
const PREV_CPU_CORES: os.CpuInfo[] = [];
8+
const CPU_COMPUTE_UTILIZATION_INITIAL_DELAY = 50;
9+
10+
let PREV_CPU_CORES: os.CpuInfo[] = [];
911
let CPU_CORE_UTILIZATIONS: number[] = [];
1012
let CPU_UTILIZATION: number = 0;
1113
let CPU_COMPUTE_RUNNING = false;
@@ -24,7 +26,7 @@ async function computeCpuUtilization() {
2426
const prev = PREV_CPU_CORES[i] as os.CpuInfo;
2527
const next = cpuCores[i] as os.CpuInfo;
2628

27-
const nextUsage = next.times.user + next.times.nice + next.times.sys + next.times.irq;
29+
const nextUsage = (next.times.user + next.times.nice + next.times.sys + next.times.irq) * 1.0;
2830
const prevUsage = prev.times.user + prev.times.nice + prev.times.sys + prev.times.irq;
2931
const usage = nextUsage - prevUsage;
3032

@@ -37,12 +39,14 @@ async function computeCpuUtilization() {
3739
totalTotal += total;
3840
}
3941
CPU_UTILIZATION = totalTotal !== 0 ? totalUsage / totalTotal : 0;
42+
PREV_CPU_CORES = cpuCores;
4043
}
4144

4245
async function runCpuComputeInterval() {
4346
CPU_COMPUTE_RUNNING = true;
4447
await computeCpuUtilization();
45-
CPU_COMPUTE_TIMEOUT = setTimeout(runCpuComputeInterval, Math.max(CPU_COMPUTE_UTILIZATION_INTERVAL, 1));
48+
if (CPU_COMPUTE_RUNNING)
49+
CPU_COMPUTE_TIMEOUT = setTimeout(runCpuComputeInterval, Math.max(CPU_COMPUTE_UTILIZATION_INTERVAL, 1));
4650
}
4751

4852
/**
@@ -72,7 +76,7 @@ export function getCpuCoreCount(): number {
7276
export async function getCpuCoreUtilization(): Promise<number[]> {
7377
if (!CPU_COMPUTE_RUNNING) {
7478
await runCpuComputeInterval(); // runs the first computation immediately
75-
await sleep(50); // wait a bit to get initial values
79+
await sleep(CPU_COMPUTE_UTILIZATION_INITIAL_DELAY); // wait a bit to get initial values
7680
await computeCpuUtilization(); // run second computation immediately to get initial values
7781
}
7882
return CPU_CORE_UTILIZATIONS;
@@ -86,7 +90,7 @@ export async function getCpuCoreUtilization(): Promise<number[]> {
8690
export async function getCpuUtilization(): Promise<number> {
8791
if (!CPU_COMPUTE_RUNNING) {
8892
await runCpuComputeInterval(); // runs the first computation immediately
89-
await sleep(50); // wait a bit to get initial values
93+
await sleep(CPU_COMPUTE_UTILIZATION_INITIAL_DELAY); // wait a bit to get initial values
9094
await computeCpuUtilization(); // run second computation immediately to get initial values
9195
}
9296
return CPU_UTILIZATION;

src/drive.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type DriveInfo = {
2525
/** The total size of the drive in bytes. */
2626
total: number;
2727

28-
/** The current drive utilization as a percentage (0-1). */
28+
/** The current drive utilization as a percentage (0-1). */
2929
utilization: number;
3030
};
3131

@@ -53,13 +53,13 @@ export async function getDrives(includeVirtual: boolean = false): Promise<DriveI
5353
const used = parseInt(parts[3], 10) || 0;
5454
const total = parseInt(parts[2], 10) || 0;
5555
drives.push({
56-
filesystem: parts[0] as string,
57-
type,
58-
total,
59-
used,
60-
free: parseInt(parts[4], 10) || 0,
61-
utilization: total !== 0 ? used / total : 0,
62-
mount: (parts[6] || parts[0]) as string,
56+
filesystem: parts[0] as string,
57+
type,
58+
total,
59+
used,
60+
free: parseInt(parts[4], 10) || 0,
61+
utilization: total !== 0 ? used / total : 0,
62+
mount: (parts[6] || parts[0]) as string,
6363
});
6464
}
6565
break;
@@ -74,13 +74,13 @@ export async function getDrives(includeVirtual: boolean = false): Promise<DriveI
7474
const total = parseInt(drive.Size, 10) || 0;
7575
const free = parseInt(drive.FreeSpace, 10) || 0;
7676
drives.push({
77-
filesystem: drive.Caption,
78-
mount: drive.VolumeName || drive.Caption,
79-
type: (drive.FileSystem || 'unknown').toLowerCase(),
80-
total,
81-
free,
82-
used: total - free,
83-
utilization: total !== 0 ? (total - free) / total : 0,
77+
filesystem: drive.Caption,
78+
mount: drive.VolumeName || drive.Caption,
79+
type: (drive.FileSystem || 'unknown').toLowerCase(),
80+
total,
81+
free,
82+
used: total - free,
83+
utilization: total !== 0 ? (total - free) / total : 0,
8484
});
8585
}
8686
break;

src/net.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ async function computeNetworkUtilization() {
151151
async function runNetComputeInterval() {
152152
NET_COMPUTE_RUNNING = true;
153153
await computeNetworkUtilization();
154-
NET_COMPUTE_TIMEOUT = setTimeout(runNetComputeInterval, Math.max(NET_COMPUTE_UTILIZATION_INTERVAL, 1));
154+
if (NET_COMPUTE_RUNNING)
155+
NET_COMPUTE_TIMEOUT = setTimeout(runNetComputeInterval, Math.max(NET_COMPUTE_UTILIZATION_INTERVAL, 1));
155156
}
156157

157158
/**

0 commit comments

Comments
 (0)