MCPcopy
hub / github.com/dqzboy/Docker-Proxy / getCpuInfo

Function getCpuInfo

hubcmdui/server-utils.js:109–204  ·  view source on GitHub ↗

* 根据平台获取CPU信息 * @param {string} platform - 操作系统平台 * @returns {Promise } CPU信息

(platform)

Source from the content-addressed store, hash-verified

107 * @returns {Promise<object>} CPU信息
108 */
109async function getCpuInfo(platform) {
110 if (platform === 'linux') {
111 try {
112 // Linux平台使用/proc/stat和/proc/cpuinfo
113 const [loadData, cpuData] = await Promise.all([
114 execCommand("cat /proc/loadavg"),
115 execCommand("cat /proc/cpuinfo | grep 'model name' | head -1")
116 ]);
117
118 const cpuLoad = loadData.split(' ').slice(0, 3).map(parseFloat);
119 const cpuCores = os.cpus().length;
120 const modelMatch = cpuData.match(/model name\s*:\s*(.*)/);
121 const model = modelMatch ? modelMatch[1].trim() : os.cpus()[0].model;
122 const percent = Math.round((cpuLoad[0] / cpuCores) * 100);
123
124 return {
125 model,
126 cores: cpuCores,
127 load1: cpuLoad[0].toFixed(2),
128 load5: cpuLoad[1].toFixed(2),
129 load15: cpuLoad[2].toFixed(2),
130 percent: percent > 100 ? 100 : percent
131 };
132 } catch (error) {
133 throw error;
134 }
135 } else if (platform === 'darwin') {
136 // macOS平台
137 try {
138 const cpuLoad = os.loadavg();
139 const cpuCores = os.cpus().length;
140 const model = os.cpus()[0].model;
141 const systemProfilerData = await execCommand("system_profiler SPHardwareDataType | grep 'Processor Name'");
142 const cpuMatch = systemProfilerData.match(/Processor Name:\s*(.*)/);
143 const cpuModel = cpuMatch ? cpuMatch[1].trim() : model;
144
145 return {
146 model: cpuModel,
147 cores: cpuCores,
148 load1: cpuLoad[0].toFixed(2),
149 load5: cpuLoad[1].toFixed(2),
150 load15: cpuLoad[2].toFixed(2),
151 percent: Math.round((cpuLoad[0] / cpuCores) * 100)
152 };
153 } catch (error) {
154 throw error;
155 }
156 } else if (platform === 'win32') {
157 // Windows平台
158 try {
159 // 使用wmic获取CPU信息
160 const cpuData = await execCommand('wmic cpu get Name,NumberOfCores /value');
161 const cpuLines = cpuData.split('\r\n');
162
163 let model = os.cpus()[0].model;
164 let cores = os.cpus().length;
165
166 cpuLines.forEach(line => {

Callers 1

getSystemInfoFunction · 0.85

Calls 2

execCommandFunction · 0.85
allMethod · 0.80

Tested by

no test coverage detected