Skip to content

Commit

Permalink
fix: process cpu usage
Browse files Browse the repository at this point in the history
PR-URL: #95
Reviewed-BY: hyj1991 <[email protected]>
  • Loading branch information
hyj1991 authored Apr 15, 2021
1 parent c446ebb commit b7e23e1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
dep: [
'moment',
'nan',
'node-pre-gyp',
'@mapbox/node-pre-gyp',
'uuid',
'yargs',
],
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
30 changes: 14 additions & 16 deletions src/logbypass/cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion xprofiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b7e23e1

Please sign in to comment.