24

Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

Embed Size (px)

Citation preview

Page 1: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)
Page 2: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

Are Your V8 Garbage Collection Logs Speaking to You? Joyee Cheung, Alibaba Cloud

Page 3: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

What Prompted us to Look into themNode.js at Alibaba

Page 4: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

What Prompted Us to Look Into ThemWhy GC Logs?

v Java is the mainstream at Alibabav Chose Node.js for productivity, but it’s meaningless without availabilityv Usually not the bottleneck, but with a huge traffic, every optimization counts

v Garbage collection is important in long-running, data-intensive serversv High CPU usage: hurts responsivenessv High memory usage: affects other processesv Memory leaks: misled by developers, crashes the process(OOM)v Java developers know their VM and its GCv …

v More about Node.js at Alibabav https://www.linux.com/blog/can-nodejs-scale-ask-team-alibaba

Page 5: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

What Prompted Us to Look Into ThemWhy GC Logs?

v Tools from V8/Chromium are more about optimizing frontend applications

CPU profiles aremore about function calls,less about objects

v Heap snapshots and timelines are more about allocations, less about collections

v Lack of high-level overview of the heap, too much details

Hard to use in staging/production environment

Page 6: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

What Prompted Us to Look Into ThemWhy GC Logs?

v V8 provides garbage collection logs v Not documented, not available in the devtools, need to use command line argsv Higher-level than heap profiles, lower-level than the number of memory usuagev Help verify the fixes by displaying patterns of GCv Not silver bullet, but nice to have

v This talk is based on the versions of V8 used by Node v4 & v6 LTSv LTS plan stablizes the V8 versions for each LTS and its GC logs

Page 7: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

An Introduction to V8 GCWhat is Garbage Collection

Page 8: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

An Introduction to V8 GCHow Most GCs are triggered

Page 9: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

An Introduction to V8 GCStop-The-World, Parallel, Concurrent GC

Page 10: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

An Introduction to V8 GCGenerational GC

Page 11: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

An Introduction to V8 GCHeap Organization

Page 12: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

An Introduction to V8 GCHeap Limits(Default)

v deps/v8/src/heap/heap.h: Heap::Heap()

16MB1MB1.4GB0.7GB

512MB

v Can be configured via command line argumentsv --max_old_space_size, --max_semi_space_size, --max_executable_space_size

Page 13: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

An Introduction to V8 GCNew Generation – Scavenge GC

Page 14: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

An Introduction to V8 GCOld Generation – Mark-Sweep/Compact GC

Page 15: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

An Introduction to V8 GCGC Pauses in V8

Page 16: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

How to Get The GC Logs

v Plenty of options (node --v8-options| grep gc)v We’ll be focusing on

v --trace_gcv Print a line briefly describing when, how, what and why for each GC

v --trace_gc_verbosev Combined with --trace_gc, summarizes about each space and the

heap after each GCv --trace_gc_nvp

v Print name-value pairs describing how each step of GC goesv In case you do want to read the code

v deps/v8/src/heap, start with gc-tracer.cc

Page 17: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

How to Read The GC Logs

v --trace_gcWHERE: [PID:Isolate]

WHEN: Time since the process has started

WHAT: Type of GC

HOW MUCH:Size of all objects(Size of memory allocated from OS)

HOW LONG:GC pause/Time spent on external memory

WHY

Page 18: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

How to Read The GC Logs

v --trace_gc_verbose && --trace_gc

Memory V8 allocated from the OS

All memory reserved for this space (insidededicated pages)

Maintained by external C++ code

Adjust heap growing factor and the allocation limit that triggers the next incremental marking

Page 19: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

How to Read The GC Logs

v --trace_gc_nvp ms=Mark-Sweep, s=Scavenge

Page 20: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

What We’ve Found

v Tons of Scavenge GC in New Space eating up the CPUv Cache misses caused by inappropriate caching strategiesv Excessive deep clones/object merging

v Large Object Space keeps growingv Serialization/deserialization of huge RPC payload

v Code Space keeps growingv Bugs in templating engines

v Old Space keeps growingv Closures in long connections

Page 21: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

Tools

v Our parserv Up on NPMv For people who already have

applications running in production

https://github.com/aliyun-node/v8-gc-log-parser

http://alinode.aliyun.com/blog(translation coming soon)

Visualization coming soon!

Page 22: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

Tools

v --trace_gc_object_statsv Feed the output to https://mlippautz.github.io/v8-heap-stats/

v Requires Node >= v7v More about the memory usuage, less about GC

v --track_gc_object_statsv Combined with --log_timer_events, writes a log file to diskv Can be viewed with chrome://tracing (Incomplete???)v Pending PR: https://github.com/nodejs/node/pull/9304

Page 23: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

THANKSTwitter/Github: @joyeecheung

[email protected]://alinode.aliyun.com

Page 24: Are your v8 garbage collection logs speaking to you?Joyee Cheung -Alibaba Cloud(Alibaba Group)

An Introduction to V8 GCAccurate GC