Skip to content

Commit 0426cbc

Browse files
authored
chore: default machine CPU to Cores/2 (podman-desktop#6755)
1 parent 882d9dc commit 0426cbc

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

extensions/podman/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"type": "number",
5656
"format": "cpu",
5757
"minimum": 1,
58-
"default": 2,
58+
"default": "HOST_HALF_CPU_CORES",
5959
"maximum": "HOST_TOTAL_CPU",
6060
"scope": "ContainerConnection",
6161
"description": "CPU(s)"
@@ -109,7 +109,7 @@
109109
"podman.factory.machine.cpus": {
110110
"type": "number",
111111
"format": "cpu",
112-
"default": 2,
112+
"default": "HOST_HALF_CPU_CORES",
113113
"minimum": 1,
114114
"maximum": "HOST_TOTAL_CPU",
115115
"scope": "ContainerProviderConnectionFactory",

packages/renderer/src/lib/preferences/PreferencesConnectionCreationOrEditRendering.svelte

+12-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
startTask,
3838
} from './preferences-connection-rendering-task';
3939
import PreferencesRenderingItemFormat from './PreferencesRenderingItemFormat.svelte';
40-
import { getInitialValue, isPropertyValidInContext, writeToTerminal } from './Util';
40+
import { calcHalfCpuCores, getInitialValue, isPropertyValidInContext, writeToTerminal } from './Util';
4141
4242
export let properties: IConfigurationPropertyRecordedSchema[] = [];
4343
export let providerInfo: ProviderInfo;
@@ -134,6 +134,17 @@ onMount(async () => {
134134
.filter(property => property.id?.startsWith(providerInfo.id))
135135
.filter(property => isPropertyValidInContext(property.when, globalContext))
136136
.map(property => {
137+
switch (property.default) {
138+
case 'HOST_HALF_CPU_CORES': {
139+
if (osCpu) {
140+
property.default = calcHalfCpuCores(osCpu);
141+
}
142+
break;
143+
}
144+
default: {
145+
break;
146+
}
147+
}
137148
switch (property.maximum) {
138149
case 'HOST_TOTAL_DISKSIZE': {
139150
if (osFreeDisk) {

packages/renderer/src/lib/preferences/Util.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { afterEach, describe, expect, test, vi } from 'vitest';
2121
import type { IConfigurationPropertyRecordedSchema } from '../../../../main/src/plugin/configuration-registry';
2222
import { ContextUI } from '../context/context';
2323
import {
24+
calcHalfCpuCores,
2425
getNormalizedDefaultNumberValue,
2526
isPropertyValidInContext,
2627
isTargetScope,
@@ -231,3 +232,26 @@ describe.each([
231232
expect(validateProxyAddress(address)).toBeDefined();
232233
});
233234
});
235+
236+
describe('calcHalfCpuCores', () => {
237+
test('should return half of the provided CPU cores as a number', () => {
238+
expect(calcHalfCpuCores('4')).toBe(2);
239+
expect(calcHalfCpuCores('10')).toBe(5);
240+
});
241+
242+
test('should return 1 if provided CPU cores are 0', () => {
243+
expect(calcHalfCpuCores('0')).toBe(1);
244+
});
245+
246+
test('should handle same integer value if CPU has one core', () => {
247+
expect(calcHalfCpuCores('1')).toBe(1);
248+
});
249+
250+
test('should return 1 for non-numeric strings', () => {
251+
expect(calcHalfCpuCores('not-a-number')).toBe(1);
252+
});
253+
254+
test('should return 1 for negative numbers', () => {
255+
expect(calcHalfCpuCores('-4')).toBe(1);
256+
});
257+
});

packages/renderer/src/lib/preferences/Util.ts

+10
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,13 @@ export function isContainerConnection(
168168
): connection is ProviderContainerConnectionInfo {
169169
return (connection as ProviderContainerConnectionInfo).endpoint.socketPath !== undefined;
170170
}
171+
172+
export function calcHalfCpuCores(osCpu: string): number {
173+
const cores = parseInt(osCpu, 10);
174+
if (isNaN(cores) || cores <= 0) {
175+
return 1;
176+
}
177+
178+
const hCores = Math.floor(cores / 2);
179+
return hCores === 0 ? 1 : hCores;
180+
}

0 commit comments

Comments
 (0)