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/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/time/time.h"
11 #include "base/tracked_objects.h"
12 #include "content/public/common/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::ProcessDataSnapshot
;
28 // Re-serializes the |location| into |dictionary|.
29 void LocationSnapshotToValue(const LocationSnapshot
& location
,
30 base::DictionaryValue
* dictionary
) {
31 dictionary
->Set("file_name",
32 base::Value::CreateStringValue(location
.file_name
));
33 // Note: This function name is not escaped, and templates have less-than
34 // characters, which means this is not suitable for display as HTML unless
36 dictionary
->Set("function_name",
37 base::Value::CreateStringValue(location
.function_name
));
38 dictionary
->Set("line_number",
39 base::Value::CreateIntegerValue(location
.line_number
));
42 // Re-serializes the |birth| into |dictionary|. Prepends the |prefix| to the
43 // "thread" and "location" key names in the dictionary.
44 void BirthOnThreadSnapshotToValue(const BirthOnThreadSnapshot
& birth
,
45 const std::string
& prefix
,
46 base::DictionaryValue
* dictionary
) {
47 DCHECK(!prefix
.empty());
49 scoped_ptr
<base::DictionaryValue
> location_value(new base::DictionaryValue
);
50 LocationSnapshotToValue(birth
.location
, location_value
.get());
51 dictionary
->Set(prefix
+ "_location", location_value
.release());
53 dictionary
->Set(prefix
+ "_thread",
54 base::Value::CreateStringValue(birth
.thread_name
));
57 // Re-serializes the |death_data| into |dictionary|.
58 void DeathDataSnapshotToValue(const DeathDataSnapshot
& death_data
,
59 base::DictionaryValue
* dictionary
) {
60 dictionary
->Set("count",
61 base::Value::CreateIntegerValue(death_data
.count
));
62 dictionary
->Set("run_ms",
63 base::Value::CreateIntegerValue(death_data
.run_duration_sum
));
64 dictionary
->Set("run_ms_max",
65 base::Value::CreateIntegerValue(death_data
.run_duration_max
));
66 dictionary
->Set("run_ms_sample",
67 base::Value::CreateIntegerValue(
68 death_data
.run_duration_sample
));
69 dictionary
->Set("queue_ms",
70 base::Value::CreateIntegerValue(
71 death_data
.queue_duration_sum
));
72 dictionary
->Set("queue_ms_max",
73 base::Value::CreateIntegerValue(
74 death_data
.queue_duration_max
));
75 dictionary
->Set("queue_ms_sample",
76 base::Value::CreateIntegerValue(
77 death_data
.queue_duration_sample
));
81 // Re-serializes the |snapshot| into |dictionary|.
82 void TaskSnapshotToValue(const TaskSnapshot
& snapshot
,
83 base::DictionaryValue
* dictionary
) {
84 BirthOnThreadSnapshotToValue(snapshot
.birth
, "birth", dictionary
);
86 scoped_ptr
<base::DictionaryValue
> death_data(new base::DictionaryValue
);
87 DeathDataSnapshotToValue(snapshot
.death_data
, death_data
.get());
88 dictionary
->Set("death_data", death_data
.release());
90 dictionary
->Set("death_thread",
91 base::Value::CreateStringValue(snapshot
.death_thread_name
));
95 } // anonymous namespace
97 namespace task_profiler
{
100 void TaskProfilerDataSerializer::ToValue(
101 const ProcessDataSnapshot
& process_data
,
103 base::DictionaryValue
* dictionary
) {
104 scoped_ptr
<base::ListValue
> tasks_list(new base::ListValue
);
105 for (std::vector
<TaskSnapshot
>::const_iterator it
=
106 process_data
.tasks
.begin();
107 it
!= process_data
.tasks
.end(); ++it
) {
108 scoped_ptr
<base::DictionaryValue
> snapshot(new base::DictionaryValue
);
109 TaskSnapshotToValue(*it
, snapshot
.get());
110 tasks_list
->Append(snapshot
.release());
112 dictionary
->Set("list", tasks_list
.release());
114 dictionary
->SetInteger("process_id", process_data
.process_id
);
115 dictionary
->SetString("process_type",
116 content::GetProcessTypeNameInEnglish(process_type
));
118 scoped_ptr
<base::ListValue
> descendants_list(new base::ListValue
);
119 for (std::vector
<ParentChildPairSnapshot
>::const_iterator it
=
120 process_data
.descendants
.begin();
121 it
!= process_data
.descendants
.end(); ++it
) {
122 scoped_ptr
<base::DictionaryValue
> parent_child(new base::DictionaryValue
);
123 BirthOnThreadSnapshotToValue(it
->parent
, "parent", parent_child
.get());
124 BirthOnThreadSnapshotToValue(it
->child
, "child", parent_child
.get());
125 descendants_list
->Append(parent_child
.release());
127 dictionary
->Set("descendants", descendants_list
.release());
131 bool TaskProfilerDataSerializer::WriteToFile(const base::FilePath
& path
) {
133 JSONStringValueSerializer
serializer(&output
);
134 serializer
.set_pretty_print(true);
136 scoped_ptr
<base::DictionaryValue
> root(new base::DictionaryValue());
138 base::ListValue
* snapshot_list
= new base::ListValue();
139 base::DictionaryValue
* shutdown_snapshot
= new base::DictionaryValue();
140 base::ListValue
* per_process_data
= new base::ListValue();
142 root
->SetInteger("version", 1);
143 root
->SetString("userAgent", content::GetUserAgent(GURL()));
145 // TODO(ramant): Collect data from other processes, then add that data to the
146 // 'per_process_data' array here. Should leverage the TrackingSynchronizer
147 // class to implement this.
148 ProcessDataSnapshot this_process_data
;
149 tracked_objects::ThreadData::Snapshot(false, &this_process_data
);
150 scoped_ptr
<base::DictionaryValue
> this_process_data_json(
151 new base::DictionaryValue
);
152 TaskProfilerDataSerializer::ToValue(this_process_data
,
153 content::PROCESS_TYPE_BROWSER
,
154 this_process_data_json
.get());
155 per_process_data
->Append(this_process_data_json
.release());
157 shutdown_snapshot
->SetInteger(
159 (base::Time::Now() - base::Time::UnixEpoch()).InSeconds());
160 shutdown_snapshot
->Set("data", per_process_data
);
161 snapshot_list
->Append(shutdown_snapshot
);
162 root
->Set("snapshots", snapshot_list
);
164 serializer
.Serialize(*root
);
165 int data_size
= static_cast<int>(output
.size());
167 return data_size
== file_util::WriteFile(path
, output
.data(), data_size
);
170 } // namespace task_profiler