From b7e23e1376e10c0e0a3c3c4a7afaed45e4d5fb1f Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 15 Apr 2021 08:59:36 +0800 Subject: [PATCH] fix: process cpu usage PR-URL: https://github.com/X-Profiler/xprofiler/pull/95 Reviewed-BY: hyj1991 --- .autod.conf.js | 2 +- package.json | 8 ++++---- src/logbypass/cpu.cc | 30 ++++++++++++++---------------- test/fixtures/command.test.js | 2 +- xprofiler.js | 2 +- 5 files changed, 21 insertions(+), 23 deletions(-) diff --git a/.autod.conf.js b/.autod.conf.js index 60c7f8a..9c8ef3f 100644 --- a/.autod.conf.js +++ b/.autod.conf.js @@ -9,7 +9,7 @@ module.exports = { dep: [ 'moment', 'nan', - 'node-pre-gyp', + '@mapbox/node-pre-gyp', 'uuid', 'yargs', ], diff --git a/package.json b/package.json index 6dc26b1..320a69d 100644 --- a/package.json +++ b/package.json @@ -54,14 +54,14 @@ }, "homepage": "https://github.com/X-Profiler/xprofiler#readme", "dependencies": { + "@mapbox/node-pre-gyp": "^1.0.3", "moment": "^2.29.1", "nan": "^2.14.2", - "node-pre-gyp": "^0.16.0", - "uuid": "^8.3.1", + "uuid": "^8.3.2", "yargs": "^15.4.1" }, "devDependencies": { - "@istanbuljs/schema": "^0.1.2", + "@istanbuljs/schema": "^0.1.3", "autod": "^3.1.1", "clang-format": "^1.4.0", "codecov": "^3.8.1", @@ -72,7 +72,7 @@ "mocha": "^7.2.0", "nyc": "^15.1.0", "tunnel-agent": "^0.6.0", - "urllib": "^2.36.1" + "urllib": "^2.37.0" }, "xctlIpcPath": { "unix": "xprofiler-ctl-uds-path.sock", diff --git a/src/logbypass/cpu.cc b/src/logbypass/cpu.cc index 33a5297..6f98591 100644 --- a/src/logbypass/cpu.cc +++ b/src/logbypass/cpu.cc @@ -5,16 +5,16 @@ namespace xprofiler { #define INIT_CPU_PERIOD(period) \ static double *cpu_##period = new double[period]; \ static int cpu_##period##_array_index = 0; \ - static int cpu_##period##_array_length = period; + static int cpu_##period##_array_length = period; \ + static int cpu_##period##_array_not_full = true; -#define SET_CPU_USAGE(period) \ - bool cpu_##period##_array_not_full = \ - cpu_##period##_array_index < cpu_##period##_array_length; \ - if (cpu_##period##_array_not_full) { \ - cpu_##period[cpu_##period##_array_index++] = cpu_now; \ - } else { \ - cpu_##period##_array_index = 0; \ - cpu_##period[cpu_##period##_array_index++] = cpu_now; \ +#define SET_CPU_USAGE(period) \ + if (cpu_##period##_array_index < cpu_##period##_array_length) { \ + cpu_##period[cpu_##period##_array_index++] = cpu_now; \ + } else { \ + cpu_##period##_array_index = 0; \ + cpu_##period##_array_not_full = false; \ + cpu_##period[cpu_##period##_array_index++] = cpu_now; \ } #define CALAULATE_AVERAGE(total_length, period) \ @@ -24,13 +24,11 @@ namespace xprofiler { if (total_length != 0) \ cpu_##period##_average = cpu_##period##_average / total_length; -#define CALAULATE_CPU_USAGE_IN_PERIOD(period) \ - bool cpu_##period##_array_not_full = \ - cpu_##period##_array_index < cpu_##period##_array_length; \ - if (cpu_##period##_array_not_full) { \ - CALAULATE_AVERAGE(cpu_##period##_array_index, period) \ - } else { \ - CALAULATE_AVERAGE(cpu_##period##_array_length, period) \ +#define CALAULATE_CPU_USAGE_IN_PERIOD(period) \ + if (cpu_##period##_array_not_full) { \ + CALAULATE_AVERAGE(cpu_##period##_array_index, period) \ + } else { \ + CALAULATE_AVERAGE(cpu_##period##_array_length, period) \ } // init cpu now diff --git a/test/fixtures/command.test.js b/test/fixtures/command.test.js index 64aec86..d81ef12 100644 --- a/test/fixtures/command.test.js +++ b/test/fixtures/command.test.js @@ -119,7 +119,7 @@ const diag = { osVersion: /^([\w\s()-_/.:~#]+|)$/, dumpTime: /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, loadTime: /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, - vmState: /^[A-Z]+$/, + vmState: /^[A-Za-z]+$/, jsStacks: isArray, nativeStacks: isArray, heapStatistics: { diff --git a/xprofiler.js b/xprofiler.js index 81006f3..1c7a3b8 100644 --- a/xprofiler.js +++ b/xprofiler.js @@ -10,7 +10,7 @@ const moment = require('moment'); const pkg = require('./package.json'); // xprofiler.node -const binary = require('node-pre-gyp'); +const binary = require('@mapbox/node-pre-gyp'); const bindingPath = binary.find(path.resolve(path.join(__dirname, './package.json'))); const xprofiler = require(bindingPath);