在 Node.js 中,负载我们关注的起聊比较的是 CPU 负载,但是聊何在有 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 负载。
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.0042353782487158530.0041004836708654120.00178085581923311870.0023717725598384650.0024768595957239477
这样就可以得到了应用的 GC 负载。
完整代码参考 https://github.com/theanarkh/nodejs-native-gc-load。
责任编辑:姜华 来源: 编程杂技 Node.jsCPU 负载(责任编辑:探索)
东方空间完成4亿元A轮融资 老股东鼎和高达、天府三江资本等机构持续加持
电科院(300215.SZ)公布消息:完成了2021年度第一期债权融资计划挂牌
九强生物(300406.SZ):收到1项专利证书 提升公司核心竞争力
金富科技(003018.SZ)2020年度净利润降14.99% 基本每股收益0.44元
持续做好“六稳”、“六保”工作 11月安徽省经济运行总体平稳