如何计算 Node.js GC 负载

在Node.js中,由于V8引擎的存在,GC负载是一个重要的性能指标。文章介绍了如何利用V8提供的GCPrologueCallback和GCEpilogueCallback钩子函数来计算GC耗时,进而分析GC负载。通过在GC开始和结束时记录时间,可以计算出每次GC的耗时并累计得到总耗时,进一步根据过去一段时间内的GC耗用来计算负载。文章还提供了一个简单的计算GC负载的示例代码。
摘要由CSDN通过智能技术生成

在 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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值