1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/task_profiler/task_profiler_data_serializer.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/time/time.h"
11 #include "base/tracked_objects.h"
12 #include "chrome/common/chrome_content_client.h"
13 #include "content/public/common/process_type.h"
16 using base::DictionaryValue
;
17 using base::ListValue
;
19 using tracked_objects::BirthOnThreadSnapshot
;
20 using tracked_objects::DeathDataSnapshot
;
21 using tracked_objects::LocationSnapshot
;
22 using tracked_objects::ParentChildPairSnapshot
;
23 using tracked_objects::TaskSnapshot
;
24 using tracked_objects::ProcessDataPhaseSnapshot
;
28 // Re-serializes the |location| into |dictionary|.
29 void LocationSnapshotToValue(const LocationSnapshot
& location
,
30 base::DictionaryValue
* dictionary
) {
31 dictionary
->SetString("file_name", location
.file_name
);
32 // Note: This function name is not escaped, and templates have less-than
33 // characters, which means this is not suitable for display as HTML unless
35 dictionary
->SetString("function_name", location
.function_name
);
36 dictionary
->SetInteger("line_number", location
.line_number
);
39 // Re-serializes the |birth| into |dictionary|. Prepends the |prefix| to the
40 // "thread" and "location" key names in the dictionary.
41 void BirthOnThreadSnapshotToValue(const BirthOnThreadSnapshot
& birth
,
42 const std::string
& prefix
,
43 base::DictionaryValue
* dictionary
) {
44 DCHECK(!prefix
.empty());
46 scoped_ptr
<base::DictionaryValue
> location_value(new base::DictionaryValue
);
47 LocationSnapshotToValue(birth
.location
, location_value
.get());
48 dictionary
->Set(prefix
+ "_location", location_value
.release());
50 dictionary
->Set(prefix
+ "_thread", new base::StringValue(birth
.thread_name
));
53 // Re-serializes the |death_data| into |dictionary|.
54 void DeathDataSnapshotToValue(const DeathDataSnapshot
& death_data
,
55 base::DictionaryValue
* dictionary
) {
56 dictionary
->SetInteger("count", death_data
.count
);
57 dictionary
->SetInteger("run_ms", death_data
.run_duration_sum
);
58 dictionary
->SetInteger("run_ms_max", death_data
.run_duration_max
);
59 dictionary
->SetInteger("run_ms_sample", death_data
.run_duration_sample
);
60 dictionary
->SetInteger("queue_ms", death_data
.queue_duration_sum
);
61 dictionary
->SetInteger("queue_ms_max", death_data
.queue_duration_max
);
62 dictionary
->SetInteger("queue_ms_sample", death_data
.queue_duration_sample
);
65 // Re-serializes the |snapshot| into |dictionary|.
66 void TaskSnapshotToValue(const TaskSnapshot
& snapshot
,
67 base::DictionaryValue
* dictionary
) {
68 BirthOnThreadSnapshotToValue(snapshot
.birth
, "birth", dictionary
);
70 scoped_ptr
<base::DictionaryValue
> death_data(new base::DictionaryValue
);
71 DeathDataSnapshotToValue(snapshot
.death_data
, death_data
.get());
72 dictionary
->Set("death_data", death_data
.release());
74 dictionary
->SetString("death_thread", snapshot
.death_thread_name
);
77 } // anonymous namespace
79 namespace task_profiler
{
82 void TaskProfilerDataSerializer::ToValue(
83 const ProcessDataPhaseSnapshot
& process_data_phase
,
84 base::ProcessId process_id
,
86 base::DictionaryValue
* dictionary
) {
87 scoped_ptr
<base::ListValue
> tasks_list(new base::ListValue
);
88 for (const auto& task
: process_data_phase
.tasks
) {
89 scoped_ptr
<base::DictionaryValue
> snapshot(new base::DictionaryValue
);
90 TaskSnapshotToValue(task
, snapshot
.get());
91 tasks_list
->Append(snapshot
.release());
93 dictionary
->Set("list", tasks_list
.release());
95 dictionary
->SetInteger("process_id", process_id
);
96 dictionary
->SetString("process_type",
97 content::GetProcessTypeNameInEnglish(process_type
));
99 scoped_ptr
<base::ListValue
> descendants_list(new base::ListValue
);
100 for (const auto& entry
: process_data_phase
.descendants
) {
101 scoped_ptr
<base::DictionaryValue
> parent_child(new base::DictionaryValue
);
102 BirthOnThreadSnapshotToValue(entry
.parent
, "parent", parent_child
.get());
103 BirthOnThreadSnapshotToValue(entry
.child
, "child", parent_child
.get());
104 descendants_list
->Append(parent_child
.release());
106 dictionary
->Set("descendants", descendants_list
.release());
109 } // namespace task_profiler