Skip to content

Modern Docker Management, Designed for Everyone

fix: container metrics not respecting the cpu limit / count

Kyle Mendellf9840de4f06c
diff --git a/frontend/messages/en.json b/frontend/messages/en.json
index 53b7027fe..b62662fd0 100644
--- a/frontend/messages/en.json
+++ b/frontend/messages/en.json
@@ -547,6 +547,9 @@
"containers_stats_memory_page_faults": "Page Faults",
"containers_stats_memory_major_faults": "Major Faults",
"containers_stats_cpu_details": "CPU Details",
+ "containers_stats_cpu_limit": "CPU Limit",
+ "containers_stats_cpu_unit_singular": "CPU",
+ "containers_stats_cpu_unit_plural": "CPUs",
"containers_stats_cpu_usage": "Usage",
"containers_stats_cpu_user_mode": "User Mode",
"containers_stats_cpu_kernel_mode": "Kernel Mode",
diff --git a/frontend/src/lib/types/container.type.ts b/frontend/src/lib/types/container.type.ts
index fdd66cdf0..16306e041 100644
--- a/frontend/src/lib/types/container.type.ts
+++ b/frontend/src/lib/types/container.type.ts
@@ -29,6 +29,10 @@ export interface ContainerPorts {
export interface ContainerHostConfig {
networkMode: string;
restartPolicy?: string;
+ privileged?: boolean;
+ autoRemove?: boolean;
+ nanoCpus?: number;
+ memory?: number;
}
export interface ContainerNetworkSettings {
diff --git a/frontend/src/routes/containers/[containerId]/+page.svelte b/frontend/src/routes/containers/[containerId]/+page.svelte
index 6dba1d0bd..f38af26ed 100644
--- a/frontend/src/routes/containers/[containerId]/+page.svelte
+++ b/frontend/src/routes/containers/[containerId]/+page.svelte
@@ -143,16 +143,22 @@
const cpuDelta = statsData.cpu_stats.cpu_usage.total_usage - (statsData.precpu_stats.cpu_usage?.total_usage || 0);
const systemDelta = statsData.cpu_stats.system_cpu_usage - (statsData.precpu_stats.system_cpu_usage || 0);
- const numberCPUs = statsData.cpu_stats.online_cpus || statsData.cpu_stats.cpu_usage?.percpu_usage?.length || 1;
if (systemDelta > 0 && cpuDelta > 0) {
- const cpuPercent = (cpuDelta / systemDelta) * numberCPUs * 100.0;
- return Math.min(Math.max(cpuPercent, 0), 100 * numberCPUs);
+ const cpuPercent = (cpuDelta / systemDelta) * 100.0;
+ return Math.min(Math.max(cpuPercent, 0), 100);
}
return 0;
};
const cpuUsagePercent = $derived(calculateCPUPercent(stats));
+
+ const cpuLimit = $derived.by(() => {
+ if (container?.hostConfig?.nanoCpus) {
+ return container.hostConfig.nanoCpus / 1e9;
+ }
+ return stats?.cpu_stats?.online_cpus || 0;
+ });
const memoryUsageBytes = $derived(stats?.memory_stats?.usage || 0);
const memoryLimitBytes = $derived(stats?.memory_stats?.limit || 0);
const memoryUsageFormatted = $derived(bytes.format(memoryUsageBytes || 0) || '0 B');
@@ -311,6 +317,7 @@
{container}
{stats}
{cpuUsagePercent}
+ {cpuLimit}
{memoryUsageFormatted}
{memoryLimitFormatted}
{memoryUsagePercent}
diff --git a/frontend/src/routes/containers/components/ContainerStats.svelte b/frontend/src/routes/containers/components/ContainerStats.svelte
index 3b842b4e1..f01b2bc59 100644
--- a/frontend/src/routes/containers/components/ContainerStats.svelte
+++ b/frontend/src/routes/containers/components/ContainerStats.svelte
@@ -11,6 +11,7 @@
container: ContainerDetailsDto;
stats: ContainerStatsType | null;
cpuUsagePercent: number;
+ cpuLimit: number;
memoryUsageFormatted: string;
memoryLimitFormatted: string;
memoryUsagePercent: number;
@@ -21,6 +22,7 @@
container,
stats,
cpuUsagePercent,
+ cpuLimit,
memoryUsageFormatted,
memoryLimitFormatted,
memoryUsagePercent,
@@ -214,7 +216,14 @@
/>
{#if stats.cpu_stats}
<div class="text-muted-foreground mt-2 flex items-center justify-between text-xs">
- <span>{m.containers_stats_online_cpus()}: {stats.cpu_stats.online_cpus}</span>
+ {#if cpuLimit > 0}
+ <span
+ >{m.containers_stats_cpu_limit()}: {cpuLimit}
+ {cpuLimit === 1 ? m.containers_stats_cpu_unit_singular() : m.containers_stats_cpu_unit_plural()}</span
+ >
+ {:else}
+ <span>{m.containers_stats_online_cpus()}: {stats.cpu_stats.online_cpus}</span>
+ {/if}
<span>
{m.containers_stats_system_cpu()}: {((stats.cpu_stats.system_cpu_usage || 0) / 1e9).toFixed(2)}s
</span>
@@ -269,7 +278,7 @@
{#if stats.pids_stats && stats.pids_stats.current !== undefined}
<Card.Root variant="subtle" class="flex flex-col justify-center">
<Card.Content class="flex flex-col justify-center p-4">
- <div class="text-muted-foreground mb-2 text-xs font-semibold tracking-wide uppercase">
+ <div class="text-muted-foreground mb-2 text-xs font-semibold uppercase tracking-wide">
{m.containers_process_count()}
</div>
<div class="text-foreground text-2xl font-bold">{stats.pids_stats.current}</div>
@@ -285,7 +294,7 @@
<!-- Network I/O Summary -->
<Card.Root variant="subtle" class="flex flex-col">
<Card.Content class="flex flex-col p-4">
- <div class="text-muted-foreground mb-3 text-xs font-semibold tracking-wide uppercase">
+ <div class="text-muted-foreground mb-3 text-xs font-semibold uppercase tracking-wide">
{m.containers_network_io()}
</div>
<div class="grid flex-1 grid-cols-2 gap-3">
@@ -321,7 +330,7 @@
{#if stats.blkio_stats && stats.blkio_stats.io_service_bytes_recursive && stats.blkio_stats.io_service_bytes_recursive.length > 0}
<Card.Root variant="subtle" class="flex flex-col">
<Card.Content class="flex flex-col p-4">
- <div class="text-muted-foreground mb-3 text-xs font-semibold tracking-wide uppercase">
+ <div class="text-muted-foreground mb-3 text-xs font-semibold uppercase tracking-wide">
{m.containers_block_io()}
</div>
<div class="grid flex-1 grid-cols-2 gap-3">
@@ -352,7 +361,7 @@
<div class="mt-4">
<Card.Root variant="subtle">
<Card.Content class="p-4">
- <div class="text-muted-foreground mb-3 text-xs font-semibold tracking-wide uppercase">
+ <div class="text-muted-foreground mb-3 text-xs font-semibold uppercase tracking-wide">
{m.containers_stats_network_interfaces()}
</div>
<div class="grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
@@ -396,7 +405,7 @@
{#if stats.memory_stats?.stats}
<Card.Root variant="subtle" class="flex flex-col">
<Card.Content class="flex flex-col p-4">
- <div class="text-muted-foreground mb-3 text-xs font-semibold tracking-wide uppercase">
+ <div class="text-muted-foreground mb-3 text-xs font-semibold uppercase tracking-wide">
{m.containers_stats_memory_details()}
</div>
<div class="grid flex-1 grid-cols-2 gap-x-4 gap-y-2 text-xs">
@@ -445,7 +454,7 @@
{#if stats.cpu_stats}
<Card.Root variant="subtle" class="flex flex-col">
<Card.Content class="flex flex-col p-4">
- <div class="text-muted-foreground mb-3 text-xs font-semibold tracking-wide uppercase">
+ <div class="text-muted-foreground mb-3 text-xs font-semibold uppercase tracking-wide">
{m.containers_stats_cpu_details()}
</div>
<div class="grid flex-1 grid-cols-2 gap-x-4 gap-y-2 text-xs">