CpuProfile ->.cpuprofile.

148 views
Skip to first unread message

Jane Chen

unread,
Dec 8, 2015, 8:32:35 PM12/8/15
to v8-users
I'm trying to serialize the CpuProfile object to a JSON serialization compatible to Chrome's .cpuprofile file.  The part I'm not clear about is the samples array in the .cpuprofile file:

"head":
{}
,
"startTime": 1611259.054962,
"endTime": 1611262.312964,
"samples": [
2,
3,
3,
3,

So it looks like an integer array, but what are they?  Line numbers?  How do I get it from CpuProfileNode returned from CpuProfile::GetSample()?

Thanks a lot!

Ben Noordhuis

unread,
Dec 9, 2015, 7:08:09 AM12/9/15
to v8-u...@googlegroups.com
"samples" corresponds to the record_samples argument to
v8::StartProfiling::CpuProfiler(). When false, you get the aggregated
hit count. When true, you get data that can be plotted on a timeline.
Put another way, record_samples=false only tells you "how much",
record_samples=true also tells you "when".

The array itself is basically a mapping of CpuProfileNodes from
CpuProfile::GetSample() to integers and sorted by timestamp in
ascending order.

Jane Chen

unread,
Dec 9, 2015, 2:55:55 PM12/9/15
to v8-users
So you mean the integers in the samples array are indexes of CpuProfileNodes?  But does CpuProfileNode have an index?  Still struggling to see how to get the integers in the samples array.

Ben Noordhuis

unread,
Dec 9, 2015, 5:09:49 PM12/9/15
to v8-u...@googlegroups.com
On Wed, Dec 9, 2015 at 8:55 PM, Jane Chen <jxch...@gmail.com> wrote:
> So you mean the integers in the samples array are indexes of
> CpuProfileNodes? But does CpuProfileNode have an index? Still struggling
> to see how to get the integers in the samples array.

You can map them to numeric indexes like this:

int monotonic_counter = 0;
std::map<const v8::CpuProfileNode*, int> nodes;
std::vector<int> samples;
for (int i = 0; i < profile->GetSamplesCount(); i += 1) {
int& sample = nodes[profile->GetSample(i)];
if (sample == 0) sample = ++monotonic_counter;
samples.push_back(sample);
}
// now write out |samples|

If you use an appropriately sized hash table instead of a map you can
bring algorithm complexity down from O(n*lg n) to amortized O(n).
Hope that helps.

Jane Chen

unread,
Dec 9, 2015, 7:08:53 PM12/9/15
to v8-users
Thanks a lot!  It sure helps.

I found CpuProfileNode.GetNodeId().  Maybe that's the pre-computed id which is the same as what you show here?

Jim Acquavella

unread,
Dec 10, 2015, 12:30:51 AM12/10/15
to v8-users
I wish V8 would do this for us.  I'm about to implement this as well, though v8 is crashing soon after I start profiling (addressed in another thread).  

I would recommend you download the Chromium source and look at how it converts from V8's cpu profile data into the file.  See chromium\src\third_party\WebKit\Source\core\inspector\v8\V8ProfilerAgentImpl.cpp for inspiration.

-Jim

Ben Noordhuis

unread,
Dec 10, 2015, 6:53:30 AM12/10/15
to v8-u...@googlegroups.com
On Thu, Dec 10, 2015 at 1:08 AM, Jane Chen <jxch...@gmail.com> wrote:
> Thanks a lot! It sure helps.
>
> I found CpuProfileNode.GetNodeId(). Maybe that's the pre-computed id which
> is the same as what you show here?

Yes, you're right. Now that I look at our code base, we actually use
GetNodeId(). The std::map approach we used in node.js v0.10 / V8 3.14
compatibility code, where that method does not exist.

Jane Chen

unread,
Dec 10, 2015, 12:31:45 PM12/10/15
to v8-users
Great.  Thanks again.

I guess I really should just download the Chromium source code.  But deoptReason is a string, while GetDeoptInfos() returns a vector.  Maybe I'm looking at mismatching v8 versions here?

Jim Acquavella

unread,
Dec 10, 2015, 1:12:44 PM12/10/15
to v8-users
I believe your goal is to write a version of the cpuprofile file that is compatible with the latest Chrome.  In that case, you should just match the current Chromium implementation:

https://chromium.googlesource.com/chromium/src.git/+/master/third_party/WebKit/Source/core/inspector/v8/V8ProfilerAgentImpl.cpp


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/baUOMwd1fT4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jim Acquavella

unread,
Mar 28, 2016, 1:49:20 PM3/28/16
to v8-users
Reply all
Reply to author
Forward
0 new messages