pm2-perfmonitor 1.2.1 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,28 +5,44 @@ A pm2 module for performance monitor.
5
5
  # Features
6
6
 
7
7
  - Automatically detect **zombie** processes and restart it.
8
- - Monitor the number of zombie process restarts (pm2 monit).
8
+ - Monitor the number of zombie process restarts (`pm2 monit`).
9
+ - [Added in v2] Support **CPU overload** protection (automatic restart + `perf` collection).
10
+ - [Added in v2] Monitor the number of CPU Overload process restarts (`pm2 monit`).
9
11
 
10
12
  # Installation
11
13
 
12
14
  ```bash
15
+ # install or update
13
16
  $ pm2 install pm2-perfmonitor
17
+
18
+ # uninstall
19
+ $ pm2 uninstall pm2-perfmonitor
14
20
  ```
15
21
 
16
22
  > NOTE: the command is `pm2 install` NOT `npm install`
17
23
 
18
24
  # Configure
19
25
 
20
- | Property | Default Value | Description |
21
- | :-----------------------------: | :-----------: | :----------------------------------------------------------------------------------: |
22
- | `enabled` | `true` | Specify whether to enable this module |
23
- | `excludeApps` | - | Specify the application name that needs to be excluded from guardianship |
24
- | `includeApps` | - | Specify the application name that needs to be guarded |
25
- | `workerInterval` | `60000` | Timed task execution interval (ms) |
26
- | `zombieDetection` | `true` | Specify whether to enable zombie process protection |
27
- | `zombieMaxHits` | `10` | Specify the maximum occurrence frequency of zombie status |
28
- | `autoRestartWhenZombieDetected` | `true` | Specify whether to automatically restart zombie processes |
29
- | `zombieMaxRestarts` | `0` | The maximum number of zombie process restarts can be set to `0` to indicate no limit |
26
+ | Property | Defaults | Description | Supported |
27
+ | :-----------------------------: | :-----------------: | :-----------------------------------------------------------------------------------------------------: | :----------: |
28
+ | `enabled` | `true` | Specify whether to enable this module | v1 and above |
29
+ | `excludeApps` | - | Specify the application name that needs to be excluded from guardianship | v1 and above |
30
+ | `includeApps` | - | Specify the application name that needs to be guarded | v1 and above |
31
+ | `workerInterval` | `60000` | Timed task execution interval (ms) | v1 and above |
32
+ | `zombieDetection` | `true` | Specify whether to enable zombie process protection | v1 and above |
33
+ | `zombieMaxHits` | `10` | Specify the maximum occurrence frequency of zombie status | v1 and above |
34
+ | `autoRestartWhenZombieDetected` | `true` | Specify whether to automatically restart zombie processes | v1 and above |
35
+ | `zombieMaxRestarts` | `0` | Specify the maximum number of restarts for zombie processes (set to `0` to indicate no limit) | v1 and above |
36
+ | `cpuOverloadDetection` | `false` | Specify whether to enable CPU overload protection | v2 |
37
+ | `cpuOverloadThreshold` | `90` | Specify the threshold for determining CPU overload | v2 |
38
+ | `cpuOverloadMaxHits` | `5` | Maximum number of consecutive occurrences of CPU overload allowed (automatically restarts when reached) | v2 |
39
+ | `enableNodeInspectorCollection` | `false` | Specify whether to enable `node:inspector` performance collection | v2 |
40
+ | `nodeInspectorSampleDuration` | `10` | Specify the performance collection duration (s) for `node:inspector` | v2 |
41
+ | `enablePerfCollection` | `false` | Specify whether to enable `perf` performance collection | v2 |
42
+ | `perfReportGenerationDir` | `/var/log/pm2/perf` | Specify the directory for generating performance reports for `perf` | v2 |
43
+ | `flamegraphDir` | `/opt/FlameGraph` | Specify the directory for `flamegraph` flame map generation tool | v2 |
44
+ | `perfSampleDuration` | `10` | Specify the sampling duration (s) for `perf` | v2 |
45
+ | `perfSampleFrequency` | `99` | Specify the sampling frequency (Hz) for `perf` | v2 |
30
46
 
31
47
  # How to set these values ?
32
48
 
@@ -36,3 +52,4 @@ After having installed the module you have to type : `pm2 set pm2-perfmonitor:<p
36
52
 
37
53
  - `pm2 set pm2-perfmonitor:includeApps myNuxtApp1, myNextApp2` (Only detect applications named `myNuxtApp1` and `myNextApp2`)
38
54
  - `pm2 set pm2-perfmonitor:workerInterval 120000` (Check every `2` minutes)
55
+ - `pm2 set pm2-perfmonitor:cpuOverloadDetection true`(enable **CPU overload** protection)
package/lib/app.js CHANGED
@@ -1,39 +1,15 @@
1
1
  const pmx = require('pmx')
2
2
  const pm2 = require('pm2')
3
- const { getPm2ListAsync } = require('./pm2-extra')
4
- const { parseParamToArray, parseParamToNumber, parseBool } = require('./utils')
5
-
6
- const defaultOptions = {
7
- enabled: true,
8
- /**
9
- * 排除的 app 名
10
- */
11
- excludeApps: [],
12
- /**
13
- * 包含的 app 名
14
- */
15
- includeApps: [],
16
- /**
17
- * 定时检测间隔(ms)
18
- */
19
- workerInterval: 60000,
20
- /**
21
- * 是否开启僵尸进程守护
22
- */
23
- zombieDetection: true,
24
- /**
25
- * 僵尸状态最大出现次数
26
- */
27
- zombieMaxHits: 10,
28
- /**
29
- * 僵尸状态达到最大容忍度时,是否自动重启僵尸进程
30
- */
31
- autoRestartWhenZombieDetected: true,
32
- /**
33
- * 僵尸进程最大重启次数,设置为0表示不限制
34
- */
35
- zombieMaxRestarts: 0,
36
- }
3
+ const { listAppsAsync, restartAppAsync } = require('./pm2-extra')
4
+ const {
5
+ parseParamToArray,
6
+ parseParamToNumber,
7
+ parseBool,
8
+ sleepAsync,
9
+ } = require('./utils')
10
+ const { defaultOptions } = require('./defaults')
11
+ const { sendMessage } = require('./message')
12
+ const { performPerfSampling } = require('./perf-sampler')
37
13
 
38
14
  const conf = pmx.initModule({}, (err, incomingConf) => {
39
15
  if (err) {
@@ -60,11 +36,29 @@ const AUTO_RESTART_WHEN_ZOMBIE_DETECTED = parseBool(
60
36
  const ZOMBIE_MAX_HITS = parseParamToNumber(conf.zombieMaxHits)
61
37
  const ZOMBIE_MAX_RESTARTS = parseParamToNumber(conf.zombieMaxRestarts)
62
38
 
39
+ const cpuOverloadDetection = parseBool(conf.cpuOverloadDetection)
40
+ const cpuOverloadThreshold = parseParamToNumber(conf.cpuOverloadThreshold)
41
+ const cpuOverloadMaxHits = parseParamToNumber(conf.cpuOverloadMaxHits)
42
+ const enablePerfCollection = parseBool(conf.enablePerfCollection)
43
+ const perfReportGenerationDir = conf.perfReportGenerationDir
44
+ const flamegraphDir = conf.flamegraphDir
45
+ const perfSampleDuration = parseParamToNumber(conf.perfSampleDuration)
46
+ const perfSampleFrequency = parseParamToNumber(conf.perfSampleFrequency)
47
+ const enableNodeInspectorCollection = parseBool(
48
+ conf.enableNodeInspectorCollection,
49
+ )
50
+ const nodeInspectorSampleDuration = parseParamToNumber(
51
+ conf.nodeInspectorSampleDuration,
52
+ )
53
+
63
54
  // 存储每个进程的 CPU 采样历史(pm_id -> [cpu1, cpu2, ...])
64
- const cpuHistory = new Map()
55
+ const zombieCpuHistory = new Map()
65
56
  const zombieRestartHistory = new Map()
66
57
  const restartFailedHistory = new Map()
67
58
 
59
+ const cpuOverloadHistory = new Map()
60
+ const cpuOverloadRestartHistory = new Map()
61
+
68
62
  /**
69
63
  * @param {'log' | 'info' | 'error' | 'warn'} type
70
64
  *
@@ -82,82 +76,129 @@ const isZombie = (history) => {
82
76
  }
83
77
 
84
78
  /**
85
- * check zombie process
79
+ * @param { number[] } history
86
80
  */
87
- const zombieProcessChecker = async () => {
88
- if (!ZOMBIE_DETECTION) return
81
+ const isCpuOverload = (history) => {
82
+ return (
83
+ history.length >= cpuOverloadMaxHits &&
84
+ history.every((v) => v >= cpuOverloadThreshold)
85
+ )
86
+ }
87
+
88
+ /**
89
+ * @param { number } pm_id
90
+ * @param { number } appCpuUsage
91
+ * @returns { number[] } 对应 pm_id 的 CPU 使用率数组
92
+ */
93
+ const setZombieCpuHistory = (pm_id, appCpuUsage) => {
94
+ if (!zombieCpuHistory.has(pm_id)) {
95
+ zombieCpuHistory.set(pm_id, [])
96
+ }
97
+
98
+ const history = zombieCpuHistory.get(pm_id)
99
+
100
+ history.push(appCpuUsage)
101
+
102
+ // 只保留最近 ZOMBIE_MAX_HITS 次记录
103
+ if (history.length > ZOMBIE_MAX_HITS) {
104
+ history.shift()
105
+ }
106
+
107
+ return history
108
+ }
109
+
110
+ /**
111
+ * @param { number } pm_id
112
+ * @param { number } appCpuUsage
113
+ * @returns { number[] } 对应 pm_id 的 CPU 使用率数组
114
+ */
115
+ const setCpuOverloadHistory = (pm_id, appCpuUsage) => {
116
+ if (!cpuOverloadHistory.has(pm_id)) {
117
+ cpuOverloadHistory.set(pm_id, [])
118
+ }
89
119
 
120
+ const history = cpuOverloadHistory.get(pm_id)
121
+
122
+ history.push(appCpuUsage)
123
+
124
+ // 只保留最近 x 次记录
125
+ if (history.length > cpuOverloadMaxHits) {
126
+ history.shift()
127
+ }
128
+
129
+ return history
130
+ }
131
+
132
+ const setRestartFailedHistory = (pm_id) => {
133
+ if (!restartFailedHistory.has(pm_id)) {
134
+ restartFailedHistory.set(pm_id, 1)
135
+ } else {
136
+ restartFailedHistory.set(pm_id, restartFailedHistory.get(pm_id) + 1)
137
+ }
138
+ }
139
+
140
+ /**
141
+ * check process
142
+ */
143
+ const processChecker = async () => {
90
144
  try {
91
- const apps = await getPm2ListAsync()
145
+ const apps = await listAppsAsync()
92
146
 
93
- apps.forEach((app) => {
94
- const { name, pm_id, monit, pm2_env } = app
147
+ for (const app of apps) {
148
+ const { name, pm_id, monit, pm2_env, pid } = app
95
149
 
96
150
  const appStatus = pm2_env?.status
97
151
  const appCpuUsage = monit?.cpu || 0
98
152
 
153
+ // 非目标应用,跳过
99
154
  if (
100
155
  MODULE_NAME === name ||
101
156
  (INCLUDE_APPS.length > 0 && !INCLUDE_APPS.includes(name)) ||
102
157
  (EXCLUDE_APPS.length > 0 && EXCLUDE_APPS.includes(name))
103
158
  ) {
104
- return
159
+ continue
105
160
  }
106
161
 
107
- // 2. 只处理 online 状态的进程
162
+ // 只处理 online 状态的进程
108
163
  if (appStatus !== 'online') {
109
164
  // 进程不在 online 状态时,清空其历史记录,避免干扰
110
- cpuHistory.delete(pm_id)
111
- return
112
- }
165
+ zombieCpuHistory.delete(pm_id)
166
+ cpuOverloadHistory.delete(pm_id)
113
167
 
114
- if (!cpuHistory.has(pm_id)) {
115
- cpuHistory.set(pm_id, [])
168
+ continue
116
169
  }
117
170
 
118
- const history = cpuHistory.get(pm_id)
119
-
120
- history.push(appCpuUsage)
171
+ const history = setZombieCpuHistory(pm_id, appCpuUsage)
172
+ const history2 = setCpuOverloadHistory(pm_id, appCpuUsage)
121
173
 
122
- // 只保留最近 ZOMBIE_MAX_HITS 次记录
123
- if (history.length > ZOMBIE_MAX_HITS) {
124
- history.shift()
174
+ // 发送消息通知对应应用进程,采样 CPU 性能
175
+ if (enableNodeInspectorCollection) {
176
+ if (appCpuUsage >= cpuOverloadThreshold) {
177
+ await sendMessage(pm_id, 'cpu-profile-start')
178
+ await sleepAsync(nodeInspectorSampleDuration * 1000)
179
+ await sendMessage(pm_id, 'cpu-profile-stop')
180
+ }
125
181
  }
126
182
 
127
- // 4. 判断是否为僵尸:最近 ZOMBIE_MAX_HITS 次全是 0%
128
-
129
- if (isZombie(history)) {
130
- logger('info', `Zombie detected: ${name} (pm_id: ${pm_id})`)
183
+ // 判断是否为僵尸:最近 ZOMBIE_MAX_HITS 次全是 0%
184
+ if (ZOMBIE_DETECTION && isZombie(history)) {
185
+ logger(
186
+ 'info',
187
+ `Zombie detected: ${name} (pm_id: ${pm_id}, pid: ${app.pid})`,
188
+ )
131
189
 
132
190
  if (AUTO_RESTART_WHEN_ZOMBIE_DETECTED) {
133
191
  if (
134
192
  ZOMBIE_MAX_RESTARTS > 0 &&
135
193
  zombieRestartHistory.get(pm_id) >= ZOMBIE_MAX_RESTARTS
136
194
  ) {
137
- return
195
+ continue
138
196
  }
139
197
 
140
198
  logger('info', 'restarting...')
141
199
 
142
- pm2.restart(pm_id, (restartErr) => {
143
- if (restartErr) {
144
- logger(
145
- 'error',
146
- `Restart failed for ${name} (pm_id: ${pm_id}):`,
147
- restartErr,
148
- )
149
-
150
- if (!restartFailedHistory.has(pm_id)) {
151
- restartFailedHistory.set(pm_id, 1)
152
- } else {
153
- restartFailedHistory.set(
154
- pm_id,
155
- restartFailedHistory.get(pm_id) + 1,
156
- )
157
- }
158
-
159
- return
160
- }
200
+ try {
201
+ await restartAppAsync(pm_id)
161
202
 
162
203
  if (!zombieRestartHistory.has(pm_id)) {
163
204
  zombieRestartHistory.set(pm_id, 1)
@@ -169,15 +210,71 @@ const zombieProcessChecker = async () => {
169
210
 
170
211
  logger(
171
212
  'info',
172
- `Restarted ${name} (pm_id: ${pm_id}) successfully!!! Restarted ${zombieRestartHistory.get(pm_id)} times`,
213
+ `[ZOMBIE] Restarted ${name} (pm_id: ${pm_id}) successfully!!! Restarted ${zombieRestartHistory.get(pm_id)} times`,
173
214
  )
174
215
 
175
216
  // 重启后清除该进程的历史记录,避免刚重启又被判定为僵尸
176
- cpuHistory.delete(pm_id)
217
+ zombieCpuHistory.delete(pm_id)
218
+ } catch (restartErr) {
219
+ logger(
220
+ 'error',
221
+ `[ZOMBIE] Restart failed for ${name} (pm_id: ${pm_id}):`,
222
+ restartErr,
223
+ )
224
+
225
+ setRestartFailedHistory(pm_id)
226
+ }
227
+ }
228
+ }
229
+ // CPU 是否持续过载
230
+ else if (cpuOverloadDetection && isCpuOverload(history2)) {
231
+ logger(
232
+ 'info',
233
+ `CPU Overload detected: ${name} (pm_id: ${pm_id}, pid: ${app.pid})`,
234
+ )
235
+
236
+ if (enablePerfCollection) {
237
+ await performPerfSampling({
238
+ pid,
239
+ moduleName: MODULE_NAME,
240
+ perfDir: perfReportGenerationDir,
241
+ flamegraphDir,
242
+ sampleDuration: perfSampleDuration,
243
+ sampleFrequency: perfSampleFrequency,
177
244
  })
178
245
  }
246
+
247
+ try {
248
+ logger('info', 'restarting...')
249
+
250
+ await restartAppAsync(pm_id)
251
+
252
+ if (!cpuOverloadRestartHistory.has(pm_id)) {
253
+ cpuOverloadRestartHistory.set(pm_id, 1)
254
+ } else {
255
+ cpuOverloadRestartHistory.set(
256
+ pm_id,
257
+ cpuOverloadRestartHistory.get(pm_id) + 1,
258
+ )
259
+ }
260
+
261
+ logger(
262
+ 'info',
263
+ `[CPU OVERLOAD] Restarted ${name} (pm_id: ${pm_id}) successfully!!! Restarted ${cpuOverloadRestartHistory.get(pm_id)} times`,
264
+ )
265
+
266
+ cpuOverloadHistory.delete(pm_id)
267
+ } catch (restartErr) {
268
+ logger(
269
+ 'error',
270
+ `[CPU OVERLOAD] Restart failed for ${name} (pm_id: ${pm_id}):`,
271
+ restartErr,
272
+ )
273
+
274
+ setRestartFailedHistory(pm_id)
275
+ }
179
276
  }
180
- })
277
+ }
181
278
  } catch (err) {
182
279
  logger('error', err)
183
280
  }
@@ -196,10 +293,10 @@ const runModule = () => {
196
293
 
197
294
  logger('info', 'Connected to PM2, starting monitor...')
198
295
 
199
- zombieProcessChecker()
296
+ processChecker()
200
297
 
201
298
  setInterval(() => {
202
- zombieProcessChecker()
299
+ processChecker()
203
300
  }, WORKER_INTERVAL)
204
301
  })
205
302
 
@@ -243,7 +340,7 @@ const runModule = () => {
243
340
  value: () => {
244
341
  const res = []
245
342
 
246
- for (const [pmId, arr] of cpuHistory) {
343
+ for (const [pmId, arr] of zombieCpuHistory) {
247
344
  if (isZombie(arr)) {
248
345
  res.push(pmId)
249
346
  }
@@ -254,6 +351,40 @@ const runModule = () => {
254
351
  return res.join(',')
255
352
  },
256
353
  })
354
+
355
+ Probe.metric({
356
+ name: 'CPU Overload Restarts',
357
+ value: () => {
358
+ const res = []
359
+
360
+ for (const [k, v] of cpuOverloadRestartHistory) {
361
+ if (v > 0) {
362
+ res.push([k, v])
363
+ }
364
+ }
365
+
366
+ if (!res.length) return 'N/A'
367
+
368
+ return res.map((v) => `[${v[0]}]:${v[1]}`).join(' ; ')
369
+ },
370
+ })
371
+
372
+ Probe.metric({
373
+ name: 'CPU Overload Processes',
374
+ value: () => {
375
+ const res = []
376
+
377
+ for (const [pmId, arr] of cpuOverloadHistory) {
378
+ if (isCpuOverload(arr)) {
379
+ res.push(pmId)
380
+ }
381
+ }
382
+
383
+ if (!res.length) return 'N/A'
384
+
385
+ return res.join(',')
386
+ },
387
+ })
257
388
  }
258
389
 
259
390
  runModule()
@@ -0,0 +1,85 @@
1
+ const defaultOptions = {
2
+ enabled: true,
3
+ /**
4
+ * 排除的 app 名
5
+ */
6
+ excludeApps: [],
7
+ /**
8
+ * 包含的 app 名
9
+ */
10
+ includeApps: [],
11
+ /**
12
+ * 定时检测间隔(ms)
13
+ */
14
+ workerInterval: 60000,
15
+ /**
16
+ * 是否开启僵尸进程守护
17
+ */
18
+ zombieDetection: true,
19
+ /**
20
+ * 僵尸状态最大出现次数
21
+ */
22
+ zombieMaxHits: 10,
23
+ /**
24
+ * 僵尸状态达到最大容忍度时,是否自动重启僵尸进程
25
+ */
26
+ autoRestartWhenZombieDetected: true,
27
+ /**
28
+ * 僵尸进程最大重启次数,设置为0表示不限制
29
+ */
30
+ zombieMaxRestarts: 0,
31
+
32
+ /**
33
+ * 是否开启 CPU 过载守护
34
+ */
35
+ cpuOverloadDetection: false,
36
+
37
+ /**
38
+ * 判定 CPU 过载阈值
39
+ */
40
+ cpuOverloadThreshold: 90,
41
+
42
+ /**
43
+ * 允许 CPU 过载最大连续出现次数,达到时自动重启
44
+ */
45
+ cpuOverloadMaxHits: 5,
46
+
47
+ /**
48
+ * 是否开启 perf 性能采集
49
+ */
50
+ enablePerfCollection: false,
51
+
52
+ /**
53
+ * 性能报告生成目录
54
+ */
55
+ perfReportGenerationDir: '/var/log/pm2/perf',
56
+
57
+ /**
58
+ * flamegraph 火焰图生成工具目录
59
+ */
60
+ flamegraphDir: '/opt/FlameGraph',
61
+
62
+ /**
63
+ * perf 采样持续时间 (s)
64
+ */
65
+ perfSampleDuration: 10,
66
+
67
+ /**
68
+ * perf 采样频率 (Hz)
69
+ */
70
+ perfSampleFrequency: 99,
71
+
72
+ /**
73
+ * 是否开启 node:inspector 性能采集
74
+ */
75
+ enableNodeInspectorCollection: false,
76
+
77
+ /**
78
+ * node:inspector 性能采集持续时间 (s)
79
+ */
80
+ nodeInspectorSampleDuration: 10,
81
+ }
82
+
83
+ module.exports = {
84
+ defaultOptions,
85
+ }
package/lib/message.js ADDED
@@ -0,0 +1,35 @@
1
+ const pm2 = require('pm2')
2
+
3
+ /**
4
+ * @param { number } pm_id - pm2 应用id
5
+ * @param { string } eventName - 事件名
6
+ * @param { object } [data] - 发送的数据
7
+ * @returns { Promise<void> }
8
+ */
9
+ const sendMessage = (pm_id, eventName, data) => {
10
+ return new Promise((resolve, reject) => {
11
+ pm2.sendDataToProcessId(
12
+ pm_id,
13
+ {
14
+ id: pm_id,
15
+ type: 'process:msg',
16
+ topic: true,
17
+ data: {
18
+ event: `pm2-perfmonitor:${eventName}`,
19
+ data,
20
+ },
21
+ },
22
+ (err) => {
23
+ if (err) {
24
+ return reject(err)
25
+ }
26
+
27
+ resolve()
28
+ },
29
+ )
30
+ })
31
+ }
32
+
33
+ module.exports = {
34
+ sendMessage,
35
+ }
@@ -0,0 +1,246 @@
1
+ const fs = require('fs-extra')
2
+ const path = require('path')
3
+
4
+ let execaCommandCache
5
+
6
+ /**
7
+ * 获取 execa 函数(缓存)
8
+ * @returns { import('execa')['execa'] }
9
+ */
10
+ const getExeca = async () => {
11
+ if (!execaCommandCache) {
12
+ const execaModule = await import('execa')
13
+ execaCommandCache = execaModule.execa
14
+ }
15
+ return execaCommandCache
16
+ }
17
+
18
+ /**
19
+ * 执行命令(不通过 shell,直接使用参数数组)
20
+ * @param {string} cmd - 命令名称
21
+ * @param {string[]} args - 参数列表
22
+ * @param {object} options - execa 选项
23
+ * @returns {Promise<boolean>} 是否成功
24
+ */
25
+ const execCommand = async (cmd, args, options = {}) => {
26
+ try {
27
+ const execa = await getExeca()
28
+ await execa(cmd, args, {
29
+ stdio: 'inherit',
30
+ ...options,
31
+ })
32
+ return true
33
+ } catch (err) {
34
+ console.error(`Command failed: ${cmd} ${args.join(' ')}`, err.message)
35
+ return false
36
+ }
37
+ }
38
+
39
+ /**
40
+ * 生成安全的文件时间戳(不依赖区域)
41
+ */
42
+ const getSafeTimestamp = () => {
43
+ const now = new Date()
44
+ const y = now.getFullYear()
45
+ const m = String(now.getMonth() + 1).padStart(2, '0')
46
+ const d = String(now.getDate()).padStart(2, '0')
47
+ const h = String(now.getHours()).padStart(2, '0')
48
+ const min = String(now.getMinutes()).padStart(2, '0')
49
+ const s = String(now.getSeconds()).padStart(2, '0')
50
+ return `${y}${m}${d}_${h}${min}${s}`
51
+ }
52
+
53
+ /**
54
+ * 执行 Perf 采样并生成火焰图
55
+ * @param {Object} options - 配置项
56
+ * @param {number} options.pid - 进程 PID
57
+ * @param {string} options.moduleName - 模块名(用于日志前缀)
58
+ * @param {string} options.perfDir - Perf 文件存储目录(当未提供 perfDataFile 时用于生成默认路径)
59
+ * @param {string} options.flamegraphDir - 火焰图工具目录
60
+ * @param {number} [options.sampleDuration=10] - 采样时长(秒)
61
+ * @param {number} [options.sampleFrequency=99] - 采样频率(Hz)
62
+ * @param {string} [options.perfDataFile] - 自定义 perf 数据文件路径(若未提供则自动生成)
63
+ * @param {boolean} [options.keepPerfData=false] - 是否保留原始 perf 数据文件(默认 false,即采样后删除)
64
+ */
65
+ const performPerfSampling = async ({
66
+ pid,
67
+ moduleName,
68
+ perfDir,
69
+ flamegraphDir,
70
+ sampleDuration = 10,
71
+ sampleFrequency = 99,
72
+ perfDataFile: customPerfDataFile,
73
+ keepPerfData = false,
74
+ }) => {
75
+ const logger = (type, ...args) => {
76
+ console[type](`[${moduleName}]`, ...args)
77
+ }
78
+
79
+ // --- 参数校验 ---
80
+ if (!perfDir) {
81
+ logger('error', 'perfDir cannot be empty')
82
+ return
83
+ }
84
+ if (!flamegraphDir) {
85
+ logger('error', 'flamegraphDir cannot be empty')
86
+ return
87
+ }
88
+
89
+ // PID 必须为数字且为正整数
90
+ const pidNum = Number(pid)
91
+ if (!Number.isInteger(pidNum) || pidNum <= 0) {
92
+ logger('error', `Invalid PID: ${pid} – must be a positive integer`)
93
+ return
94
+ }
95
+
96
+ const finalDuration =
97
+ typeof sampleDuration === 'number' && sampleDuration > 0
98
+ ? sampleDuration
99
+ : 10
100
+ const finalFrequency =
101
+ typeof sampleFrequency === 'number' && sampleFrequency > 0
102
+ ? sampleFrequency
103
+ : 99
104
+
105
+ // 确保 perf 目录存在(用于默认路径,或自定义路径的父目录)
106
+ try {
107
+ await fs.ensureDir(perfDir)
108
+ logger('info', `Perf directory ready: ${perfDir}`)
109
+ } catch (err) {
110
+ logger('error', `Failed to create perf directory: ${err.message}`)
111
+ return
112
+ }
113
+
114
+ // 检查 perf 权限
115
+ try {
116
+ const execa = await getExeca()
117
+
118
+ await execa('perf', ['--version'], { timeout: 5000 })
119
+
120
+ logger('info', 'Perf permission check passed')
121
+ } catch (err) {
122
+ logger('error', `Perf permission check failed: ${err.message}`)
123
+ logger(
124
+ 'error',
125
+ 'Please ensure the perf command is installed and the user has permission to run it.\n' +
126
+ 'You can configure the system to allow non-root perf by setting:\n' +
127
+ ' echo -1 | sudo tee /proc/sys/kernel/perf_event_paranoid\n' +
128
+ ' sudo setcap cap_sys_admin+ep $(which perf)',
129
+ )
130
+ return // 无权限则直接退出
131
+ }
132
+
133
+ // 生成时间戳(仅当需要默认路径时)
134
+ const timestamp = getSafeTimestamp()
135
+
136
+ // 确定 perf 数据文件路径
137
+ let perfDataFile
138
+ if (customPerfDataFile) {
139
+ perfDataFile = customPerfDataFile
140
+ // 确保自定义路径的父目录存在
141
+ const parentDir = path.dirname(perfDataFile)
142
+ try {
143
+ await fs.ensureDir(parentDir)
144
+ } catch (err) {
145
+ logger(
146
+ 'error',
147
+ `Failed to create directory for custom perfDataFile: ${err.message}`,
148
+ )
149
+ return
150
+ }
151
+ } else {
152
+ perfDataFile = path.join(perfDir, `perf.${pidNum}.${timestamp}.data`)
153
+ }
154
+
155
+ // 定义其他文件路径(基于 perfDir 和时间戳,与 perfDataFile 解耦)
156
+ const perfStacksFile = path.join(
157
+ perfDir,
158
+ `perf.${pidNum}.${timestamp}.stacks`,
159
+ )
160
+ const perfFoldedFile = path.join(
161
+ perfDir,
162
+ `perf.${pidNum}.${timestamp}.folded`,
163
+ )
164
+ const perfSvgFile = path.join(perfDir, `perf.${pidNum}.${timestamp}.svg`)
165
+
166
+ try {
167
+ logger(
168
+ 'info',
169
+ `PID:${pidNum} Starting perf sampling (${finalDuration}s, ${finalFrequency}Hz)`,
170
+ )
171
+
172
+ // --- Step 1: perf record ---
173
+ const recordOk = await execCommand('perf', [
174
+ 'record',
175
+ '-o',
176
+ perfDataFile,
177
+ '-F',
178
+ String(finalFrequency),
179
+ '-p',
180
+ String(pidNum),
181
+ '-g',
182
+ '--',
183
+ 'sleep',
184
+ String(finalDuration),
185
+ ])
186
+ if (!recordOk) return
187
+
188
+ // --- Step 2: perf script 导出为文本堆栈 ---
189
+ const scriptOk = await execCommand('perf', ['script', '-i', perfDataFile], {
190
+ stdout: fs.createWriteStream(perfStacksFile),
191
+ })
192
+ if (!scriptOk) return
193
+
194
+ logger('info', `PID:${pidNum} Perf sampling completed: ${perfStacksFile}`)
195
+
196
+ // 根据 keepPerfData 决定是否删除原始数据文件
197
+ if (!keepPerfData) {
198
+ await fs.remove(perfDataFile).catch(() => {})
199
+ }
200
+
201
+ // --- Step 3: 检查火焰图工具 ---
202
+ const stackcollapsePath = path.join(flamegraphDir, 'stackcollapse-perf.pl')
203
+ const flamegraphPath = path.join(flamegraphDir, 'flamegraph.pl')
204
+
205
+ const isStackcollapseValid = await fs
206
+ .access(stackcollapsePath, fs.constants.X_OK)
207
+ .then(() => true)
208
+ .catch(() => false)
209
+ const isFlamegraphValid = await fs
210
+ .access(flamegraphPath, fs.constants.X_OK)
211
+ .then(() => true)
212
+ .catch(() => false)
213
+
214
+ if (isStackcollapseValid && isFlamegraphValid) {
215
+ // --- Step 4: 生成折叠文件 ---
216
+ const collapseOk = await execCommand(
217
+ stackcollapsePath,
218
+ [perfStacksFile],
219
+ {
220
+ stdout: fs.createWriteStream(perfFoldedFile),
221
+ },
222
+ )
223
+ if (!collapseOk) return
224
+
225
+ // --- Step 5: 生成 SVG 火焰图 ---
226
+ const flameOk = await execCommand(flamegraphPath, [perfFoldedFile], {
227
+ stdout: fs.createWriteStream(perfSvgFile),
228
+ })
229
+ if (flameOk) {
230
+ logger('info', `PID:${pidNum} Flame graph generated: ${perfSvgFile}`)
231
+ }
232
+ } else {
233
+ const missing = []
234
+ if (!isStackcollapseValid) missing.push('stackcollapse-perf.pl')
235
+ if (!isFlamegraphValid) missing.push('flamegraph.pl')
236
+ logger(
237
+ 'info',
238
+ `PID:${pidNum} Skip flame graph – missing/not executable: ${missing.join(', ')}`,
239
+ )
240
+ }
241
+ } catch (err) {
242
+ logger('error', `PID:${pidNum} Perf sampling exception: ${err.message}`)
243
+ }
244
+ }
245
+
246
+ module.exports = { performPerfSampling }
package/lib/pm2-extra.js CHANGED
@@ -3,7 +3,7 @@ const pm2 = require('pm2')
3
3
  /**
4
4
  * @returns { Promise<pm2.ProcessDescription[]> }
5
5
  */
6
- const getPm2ListAsync = () => {
6
+ const listAppsAsync = () => {
7
7
  return new Promise((resolve, reject) => {
8
8
  pm2.list((err, apps) => {
9
9
  if (err) {
@@ -19,7 +19,7 @@ const getPm2ListAsync = () => {
19
19
  * @param { string | number} pm_id
20
20
  * @returns { Promise<void> }
21
21
  */
22
- const pm2StopAsync = (pm_id) => {
22
+ const stopAppAsync = (pm_id) => {
23
23
  return new Promise((resolve, reject) => {
24
24
  pm2.stop(pm_id, (err) => {
25
25
  if (err) {
@@ -31,7 +31,24 @@ const pm2StopAsync = (pm_id) => {
31
31
  })
32
32
  }
33
33
 
34
+ /**
35
+ * @param { string | number} pm_id
36
+ * @returns { Promise<void> }
37
+ */
38
+ const restartAppAsync = (pm_id) => {
39
+ return new Promise((resolve, reject) => {
40
+ pm2.restart(pm_id, (err) => {
41
+ if (err) {
42
+ return reject(err)
43
+ }
44
+
45
+ resolve()
46
+ })
47
+ })
48
+ }
49
+
34
50
  module.exports = {
35
- getPm2ListAsync,
36
- pm2StopAsync,
51
+ listAppsAsync,
52
+ stopAppAsync,
53
+ restartAppAsync,
37
54
  }
package/lib/utils.js CHANGED
@@ -28,8 +28,18 @@ const parseBool = (value, defaultVal = false) => {
28
28
  return defaultVal
29
29
  }
30
30
 
31
+ /**
32
+ * @param { number} duration - sleep duration (ms)
33
+ */
34
+ const sleepAsync = (duration = 0) => {
35
+ return new Promise((resolve) => {
36
+ setTimeout(resolve, duration)
37
+ })
38
+ }
39
+
31
40
  module.exports = {
32
41
  parseParamToArray,
33
42
  parseParamToNumber,
34
43
  parseBool,
44
+ sleepAsync,
35
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pm2-perfmonitor",
3
- "version": "1.2.1",
3
+ "version": "2.1.1",
4
4
  "description": "A pm2 module for performance monitoring. Automatically detect zombie processes and restart it",
5
5
  "author": {
6
6
  "name": "elenh",
@@ -20,8 +20,12 @@
20
20
  },
21
21
  "homepage": "https://github.com/yisibell/pm2-perfmonitor",
22
22
  "scripts": {
23
- "start": "pm2 delete app1 || true && pm2 start ecosystem.app.config.cjs",
24
- "dev": "pm2 start ecosystem.dev.config.cjs",
23
+ "start-or-restart:app": "pm2 startOrRestart ecosystem.app.config.cjs --update-env",
24
+ "start:app": "pm2 start ecosystem.app.config.cjs",
25
+ "restart:app": "pm2 restart ecosystem.app.config.cjs",
26
+ "delete-start:app": "pm2 delete app1 || true && pm2 start ecosystem.app.config.cjs",
27
+ "start": "node ./scripts/app.js --env=app",
28
+ "dev": "pm2 restart ecosystem.dev.config.cjs",
25
29
  "release": "changelogen --release && npm publish --access=public && git push --follow-tags"
26
30
  },
27
31
  "keywords": [
@@ -38,11 +42,14 @@
38
42
  ],
39
43
  "config": {},
40
44
  "dependencies": {
45
+ "execa": "^9.6.1",
46
+ "fs-extra": "^11.3.4",
41
47
  "pm2": "latest",
42
48
  "pmx": "latest"
43
49
  },
44
50
  "devDependencies": {
45
51
  "changelogen": "^0.6.2",
46
- "cz-conventional-changelog": "^3.3.0"
52
+ "cz-conventional-changelog": "^3.3.0",
53
+ "minimist": "^1.2.8"
47
54
  }
48
- }
55
+ }