CGroupMaxCPU and CGroupMemoryTotal metrics in system.asynchronous_metrics. The CPU that a replica uses is recorded in system.metric_log, and the memory it uses in system.asynchronous_metric_log. These tables are specific to each replica, so the queries below read them from all replicas with clusterAllReplicas.
Allocated CPU and memory now
One row per replica:CPU used compared with CPU allocated
Each row ofsystem.metric_log contains, in the ProfileEvent_OSCPUVirtualTimeMicroseconds column, the CPU time in microseconds that the server recorded since the previous row. The sum over one minute for all replicas, divided by 60 seconds, is the average number of CPU cores that the service used in that minute. The second subquery returns the CGroupMaxCPU value of every replica in the same minute. A replica that started or stopped during the minute counts only for the seconds in which it wrote rows, so the CPU used is compared with the CPU that was available in that minute.
cores_usedis the average number of CPU cores used by all replicas together.cores_allocatedis the average number of CPU cores allocated to all replicas together. A replica with 4 cores that ran for 30 seconds of the minute adds 2.utilizationis the ratio of the two. A value of 1 means that all the allocated CPU was in use.
seconds_with_data is the number of seconds of the minute in which the replica wrote a row to system.metric_log. The query assumes one row per second, which is the default collection interval of metric_log.
cores_used is based on the same column as the CPU Usage (cores) chart of the Cloud overview dashboard in system.dashboards. The CPU usage graph of the Cloud console is built from a different metric, so the two do not have to match exactly.
CPU peaks per replica
An average over all replicas and over a long time hides peaks and replicas that are busier than the others. This query returns, for every replica and every 30 minutes, the average number of CPU cores in use and the number of cores in use during the busiest minute:cpu_cores of the same replica from the first query.
The period starts and ends at a whole minute, so no minute is counted in part. The first and the last row of a replica can cover fewer than 30 minutes. A minute in which the replica started or stopped has CPU time for only a part of it, so its value is lower than the usage while the replica ran.
Do not take the maximum of single rows of system.metric_log as the peak. A thread adds its CPU time to the counter from time to time, for example after it has processed a block of data, not continuously. For example, a query that processes one large block in one thread for several seconds reports almost nothing while it runs, and then all of its CPU time in a single row. Such a row shows more cores than were in use during that second, and the value can exceed the number of cores of the replica. A sum over a minute evens this out.
Memory used per replica
MemoryResident is the amount of physical memory used by the server process. It is also the metric behind the Memory usage graph of the Cloud console. The query compares its maximum in every 30 minutes with the memory allocated to the replica:
CurrentMetric_MemoryTracking column of system.metric_log. The advanced dashboard shows it as Memory (tracked, bytes).
What to keep in mind
- The
hostnamecolumn of the log tables holds the fully qualified domain name of the server that wrote the row. The queries keep the part before the first dot to get a short replica name. merge('system', '^metric_log')reads all versions of a log table. After an upgrade that changes the structure of a system log table, ClickHouse renames the old table and creates a new one, see querying multiple versions.skip_unavailable_shards = 1skips replicas that are being added or removed at the moment of the query, see querying across nodes.- When a service is scaled, new replicas are added before the old ones are removed, see make before break. While both exist,
cores_allocatedincludes both, each for the seconds in which it wrote rows. - The same page says that 30 days of system table data are kept when replicas are replaced, so do not expect older history.
- The log tables have rows only for the time when a replica was running. An idle or stopped service leaves a gap.
- The periods of the two CPU queries end one minute before the start of the current minute. The server writes rows to the log tables in batches, every 7.5 seconds by default (
flush_interval_milliseconds, see system tables), so the newest minute can lack its last rows. - Queries on system tables add load to the service and prevent it from idling. For continuous monitoring, use the Cloud console or the Prometheus endpoint.
- ClickHouse Cloud takes its scaling decisions from its own measurements, as described in scaling recommendations. Use these queries to understand your workload, not to reproduce those decisions.