|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import fs from 'fs'; |
| 21 | +import { join as joinPath } from 'path'; |
| 22 | +import { MetricsCollector, OpsOsMetrics } from './types'; |
| 23 | + |
| 24 | +type OsCgroupMetrics = Pick<OpsOsMetrics, 'cpu' | 'cpuacct'>; |
| 25 | + |
| 26 | +interface OsCgroupMetricsCollectorOptions { |
| 27 | + cpuPath?: string; |
| 28 | + cpuAcctPath?: string; |
| 29 | +} |
| 30 | + |
| 31 | +export class OsCgroupMetricsCollector implements MetricsCollector<OsCgroupMetrics> { |
| 32 | + // Used to prevent unnecessary file reads on systems not using cgroups |
| 33 | + private noCgroupPresent = false; |
| 34 | + |
| 35 | + constructor(private readonly options: OsCgroupMetricsCollectorOptions) {} |
| 36 | + |
| 37 | + public async collect(): Promise<OsCgroupMetrics> { |
| 38 | + if (this.noCgroupPresent) { |
| 39 | + return {}; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + const cgroups = await readControlGroups(); |
| 44 | + const cpuPath = this.options.cpuPath || cgroups[GROUP_CPU]; |
| 45 | + const cpuAcctPath = this.options.cpuAcctPath || cgroups[GROUP_CPUACCT]; |
| 46 | + |
| 47 | + // prevents undefined cgroup paths |
| 48 | + if (!cpuPath || !cpuAcctPath) { |
| 49 | + this.noCgroupPresent = true; |
| 50 | + return {}; |
| 51 | + } |
| 52 | + |
| 53 | + const [cpuAcctUsage, cpuFsPeriod, cpuFsQuota, cpuStat] = await Promise.all([ |
| 54 | + readCPUAcctUsage(cpuAcctPath), |
| 55 | + readCPUFsPeriod(cpuPath), |
| 56 | + readCPUFsQuota(cpuPath), |
| 57 | + readCPUStat(cpuPath), |
| 58 | + ]); |
| 59 | + |
| 60 | + return { |
| 61 | + cpuacct: { |
| 62 | + control_group: cpuAcctPath, |
| 63 | + usage_nanos: cpuAcctUsage, |
| 64 | + }, |
| 65 | + |
| 66 | + cpu: { |
| 67 | + control_group: cpuPath, |
| 68 | + cfs_period_micros: cpuFsPeriod, |
| 69 | + cfs_quota_micros: cpuFsQuota, |
| 70 | + stat: cpuStat, |
| 71 | + }, |
| 72 | + }; |
| 73 | + } catch (err) { |
| 74 | + if (err.code === 'ENOENT') { |
| 75 | + return {}; |
| 76 | + } else { |
| 77 | + throw err; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public reset() {} |
| 83 | +} |
| 84 | + |
| 85 | +const CONTROL_GROUP_RE = new RegExp('\\d+:([^:]+):(/.*)'); |
| 86 | +const CONTROLLER_SEPARATOR_RE = ','; |
| 87 | + |
| 88 | +const PROC_SELF_CGROUP_FILE = '/proc/self/cgroup'; |
| 89 | +const PROC_CGROUP_CPU_DIR = '/sys/fs/cgroup/cpu'; |
| 90 | +const PROC_CGROUP_CPUACCT_DIR = '/sys/fs/cgroup/cpuacct'; |
| 91 | + |
| 92 | +const GROUP_CPUACCT = 'cpuacct'; |
| 93 | +const CPUACCT_USAGE_FILE = 'cpuacct.usage'; |
| 94 | + |
| 95 | +const GROUP_CPU = 'cpu'; |
| 96 | +const CPU_FS_PERIOD_US_FILE = 'cpu.cfs_period_us'; |
| 97 | +const CPU_FS_QUOTA_US_FILE = 'cpu.cfs_quota_us'; |
| 98 | +const CPU_STATS_FILE = 'cpu.stat'; |
| 99 | + |
| 100 | +async function readControlGroups() { |
| 101 | + const data = await fs.promises.readFile(PROC_SELF_CGROUP_FILE); |
| 102 | + |
| 103 | + return data |
| 104 | + .toString() |
| 105 | + .split(/\n/) |
| 106 | + .reduce((acc, line) => { |
| 107 | + const matches = line.match(CONTROL_GROUP_RE); |
| 108 | + |
| 109 | + if (matches !== null) { |
| 110 | + const controllers = matches[1].split(CONTROLLER_SEPARATOR_RE); |
| 111 | + controllers.forEach((controller) => { |
| 112 | + acc[controller] = matches[2]; |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + return acc; |
| 117 | + }, {} as Record<string, string>); |
| 118 | +} |
| 119 | + |
| 120 | +async function fileContentsToInteger(path: string) { |
| 121 | + const data = await fs.promises.readFile(path); |
| 122 | + return parseInt(data.toString(), 10); |
| 123 | +} |
| 124 | + |
| 125 | +function readCPUAcctUsage(controlGroup: string) { |
| 126 | + return fileContentsToInteger(joinPath(PROC_CGROUP_CPUACCT_DIR, controlGroup, CPUACCT_USAGE_FILE)); |
| 127 | +} |
| 128 | + |
| 129 | +function readCPUFsPeriod(controlGroup: string) { |
| 130 | + return fileContentsToInteger(joinPath(PROC_CGROUP_CPU_DIR, controlGroup, CPU_FS_PERIOD_US_FILE)); |
| 131 | +} |
| 132 | + |
| 133 | +function readCPUFsQuota(controlGroup: string) { |
| 134 | + return fileContentsToInteger(joinPath(PROC_CGROUP_CPU_DIR, controlGroup, CPU_FS_QUOTA_US_FILE)); |
| 135 | +} |
| 136 | + |
| 137 | +async function readCPUStat(controlGroup: string) { |
| 138 | + const stat = { |
| 139 | + number_of_elapsed_periods: -1, |
| 140 | + number_of_times_throttled: -1, |
| 141 | + time_throttled_nanos: -1, |
| 142 | + }; |
| 143 | + |
| 144 | + try { |
| 145 | + const data = await fs.promises.readFile( |
| 146 | + joinPath(PROC_CGROUP_CPU_DIR, controlGroup, CPU_STATS_FILE) |
| 147 | + ); |
| 148 | + return data |
| 149 | + .toString() |
| 150 | + .split(/\n/) |
| 151 | + .reduce((acc, line) => { |
| 152 | + const fields = line.split(/\s+/); |
| 153 | + |
| 154 | + switch (fields[0]) { |
| 155 | + case 'nr_periods': |
| 156 | + acc.number_of_elapsed_periods = parseInt(fields[1], 10); |
| 157 | + break; |
| 158 | + |
| 159 | + case 'nr_throttled': |
| 160 | + acc.number_of_times_throttled = parseInt(fields[1], 10); |
| 161 | + break; |
| 162 | + |
| 163 | + case 'throttled_time': |
| 164 | + acc.time_throttled_nanos = parseInt(fields[1], 10); |
| 165 | + break; |
| 166 | + } |
| 167 | + |
| 168 | + return acc; |
| 169 | + }, stat); |
| 170 | + } catch (err) { |
| 171 | + if (err.code === 'ENOENT') { |
| 172 | + return stat; |
| 173 | + } |
| 174 | + |
| 175 | + throw err; |
| 176 | + } |
| 177 | +} |
0 commit comments