一起聊一聊如何计算 Node.js GC 负载

开发 前端
操作系统本身会计算每隔线程的 CPU 耗时,所以我们可以通过系统获取这个数据,然后计算出线程的 CPU 负载。

在 Node.js 中,我们关注的比较的是 CPU 负载,但是在有 GC 的语言中,GC 负载也是需要关注的一个指标,因为 GC 过高会影响我们应用的性能。本文介绍关于 GC 负载的一些内容。

如何获取 GC 耗时

操作系统本身会计算每隔线程的 CPU 耗时,所以我们可以通过系统获取这个数据,然后计算出线程的 CPU 负载。但是 GC 不一样,因为 GC 是应用层的一个概念,操作系统是不会感知的,在 Node.js 里,具体来说,是在 V8 里,也没有 API 可以直接获取 GC 的耗时,但是 V8 提供了一些 GC 的钩子函数,我们可以借助这些钩子函数来计算出 GC 的负载。其原理和 CPU 负载类似。V8 提供了以下两个钩子函数,分别在 GC 开始和结束时会执行。

Isolate::GetCurrent()->AddGCPrologueCallback();
Isolate::GetCurrent()->AddGCEpilogueCallback();

通过这两个函数,我们就可以得到每一次 GC 的耗时,再不断累积就可以计算出 GC 的总耗时,从而计算出 GC 负载。下面看一下核心实现。

static void BeforeGCCallback(Isolate* isolate,
                             v8::GCType gc_type,
                             v8::GCCallbackFlags flags,
                             void* data) {
    GCLoad* gc_load = static_cast<GCLoad*>(data);
    if (gc_load->current_gc_type != 0) {
        return;
    }
    gc_load->current_gc_type = gc_type;
    gc_load->start_time = uv_hrtime();
}

static void AfterGCCallback(Isolate* isolate,
                            v8::GCType gc_type,
                            v8::GCCallbackFlags flags,
                            void* data) {
    GCLoad* gc_load = static_cast<GCLoad*>(data);
    if (gc_load->current_gc_type != gc_type) {
        return;
    }
    gc_load->current_gc_type = 0;
    gc_load->total_time += uv_hrtime() - gc_load->start_time;
    gc_load->start_time = 0;
}

void GCLoad::Start(const FunctionCallbackInfo<Value>& args) {
    GCLoad* obj = ObjectWrap::Unwrap<GCLoad>(args.Holder());
    Isolate::GetCurrent()->AddGCPrologueCallback(BeforeGCCallback, static_cast<void*>(obj));
    Isolate::GetCurrent()->AddGCEpilogueCallback(AfterGCCallback, static_cast<void*>(obj));
}

可以看到思路很简单,就是注册两个 GC 钩子函数,然后在 GC 开始钩子中记录开始时间,然后在 GC 结束钩子中记录结束时间,并算出一次 GC 的耗时,再累加起来,这样就可以得到任意时刻 GC 的总耗时,但是拿到总耗时如何计算出 GC 负载呢?

如何计算 GC 负载

负载 = 过去一段时间内的消耗 / 过去的一段时间值,看看如何计算 GC 负载。

class GCLoad {
    lastTime;
    lastTotal;
    binding = null;
    start() {
        if (!this.binding) {
            this.binding = new binding.GCLoad();
            this.binding.start();
        }
    }
    stop() {
        if (this.binding) {
            this.binding.stop();
            this.binding = null;
        }
    }
    load() {
        if (this.binding) {
            const { lastTime, lastTotal } = this;
            const now = process.hrtime();
            const total = this.binding.total();
            this.lastTime = now;
            this.lastTotal = total;
            if (lastTime && lastTotal) {
                const cost = total - lastTotal;
                const interval = (now[0] - lastTime[0]) * 1e6 + (now[1] - lastTime[1]) / 1e3;
                return cost / interval;
            }
        }
    }
    total() {
        if (this.binding) {
            return this.binding.total();
        }
    }
}

计算算法也很简单,就是记录上次的时间和 GC 耗时,然后下次需要记录某个时刻的 GC 负载时,就拿当前的耗时减去上次的耗时,并拿当前的时间减去上次的时间,然后得到过去一段时间内的耗时和过去的时间大小,一处就得到 GC 负载了。

使用

下面看看如何使用。

const { GCLoad } = require('..');
const gcLoad = new GCLoad();
gcLoad.start();
setInterval(() => {
    for (let i = 0; i < 1000; i++) {
        new Array(100);
    }
    gc();
    console.log(gcLoad.load());
}, 3000);

执行上面代码会(node --expose-gc demo.js) 在我电脑上输出如下。

0.004235378248715853
0.004100483670865412
0.0017808558192331187
0.002371772559838465
0.0024768595957239477

这样就可以得到了应用的 GC 负载。

完整代码参考 https://github.com/theanarkh/nodejs-native-gc-load。

责任编辑:姜华 来源: 编程杂技
相关推荐

2023-08-14 08:38:26

反射reflect结构体

2023-09-29 08:58:38

2021-08-07 07:56:59

Node逻辑对象

2022-08-26 00:35:31

Java工作流系统

2024-02-06 08:58:23

开源项目my-tv

2022-08-22 09:20:05

Kubernetes工作负载管理

2022-10-08 11:33:56

边缘计算云计算

2021-10-12 23:45:43

NodeJs事件

2020-07-16 14:40:23

大数据计算框架

2020-11-05 09:27:48

JavaScript开发技术

2023-02-07 06:42:24

Pulsar负载均衡

2020-12-29 05:33:40

TomcatSpringBoot代码

2022-02-16 10:25:36

边缘计算数据中心网络

2018-05-16 08:58:04

用户画像存储

2019-01-31 07:16:06

2014-04-01 11:02:00

Node.jsWeb Socket聊天程序

2018-11-30 12:48:36

SDS故障硬件

2023-03-05 18:40:39

iptables防火墙软件

2022-03-06 20:35:41

并发串行CAP

2023-09-22 17:36:37

点赞
收藏

51CTO技术栈公众号