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::TaskSnapshot
;
23 using tracked_objects::ProcessDataPhaseSnapshot
;
27 // Re-serializes the |location| into |dictionary|.
28 void LocationSnapshotToValue(const LocationSnapshot
& location
,
29 base::DictionaryValue
* dictionary
) {
30 dictionary
->SetString("file_name", location
.file_name
);
31 // Note: This function name is not escaped, and templates have less-than
32 // characters, which means this is not suitable for display as HTML unless
34 dictionary
->SetString("function_name", location
.function_name
);
35 dictionary
->SetInteger("line_number", location
.line_number
);
38 // Re-serializes the |birth| into |dictionary|. Prepends the |prefix| to the
39 // "thread" and "location" key names in the dictionary.
40 void BirthOnThreadSnapshotToValue(const BirthOnThreadSnapshot
& birth
,
41 const std::string
& prefix
,
42 base::DictionaryValue
* dictionary
) {
43 DCHECK(!prefix
.empty());
45 scoped_ptr
<base::DictionaryValue
> location_value(new base::DictionaryValue
);
46 LocationSnapshotToValue(birth
.location
, location_value
.get());
47 dictionary
->Set(prefix
+ "_location", location_value
.release());
49 dictionary
->Set(prefix
+ "_thread", new base::StringValue(birth
.thread_name
));
52 // Re-serializes the |death_data| into |dictionary|.
53 void DeathDataSnapshotToValue(const DeathDataSnapshot
& death_data
,
54 base::DictionaryValue
* dictionary
) {
55 dictionary
->SetInteger("count", death_data
.count
);
56 dictionary
->SetInteger("run_ms", death_data
.run_duration_sum
);
57 dictionary
->SetInteger("run_ms_max", death_data
.run_duration_max
);
58 dictionary
->SetInteger("run_ms_sample", death_data
.run_duration_sample
);
59 dictionary
->SetInteger("queue_ms", death_data
.queue_duration_sum
);
60 dictionary
->SetInteger("queue_ms_max", death_data
.queue_duration_max
);
61 dictionary
->SetInteger("queue_ms_sample", death_data
.queue_duration_sample
);
64 // Re-serializes the |snapshot| into |dictionary|.
65 void TaskSnapshotToValue(const TaskSnapshot
& snapshot
,
66 base::DictionaryValue
* dictionary
) {
67 BirthOnThreadSnapshotToValue(snapshot
.birth
, "birth", dictionary
);
69 scoped_ptr
<base::DictionaryValue
> death_data(new base::DictionaryValue
);
70 DeathDataSnapshotToValue(snapshot
.death_data
, death_data
.get());
71 dictionary
->Set("death_data", death_data
.release());
73 dictionary
->SetString("death_thread", snapshot
.death_thread_name
);
76 } // anonymous namespace
78 namespace task_profiler
{
81 void TaskProfilerDataSerializer::ToValue(
82 const ProcessDataPhaseSnapshot
& process_data_phase
,
83 base::ProcessId process_id
,
85 base::DictionaryValue
* dictionary
) {
86 scoped_ptr
<base::ListValue
> tasks_list(new base::ListValue
);
87 for (const auto& task
: process_data_phase
.tasks
) {
88 scoped_ptr
<base::DictionaryValue
> snapshot(new base::DictionaryValue
);
89 TaskSnapshotToValue(task
, snapshot
.get());
90 tasks_list
->Append(snapshot
.release());
92 dictionary
->Set("list", tasks_list
.release());
94 dictionary
->SetInteger("process_id", process_id
);
95 dictionary
->SetString("process_type",
96 content::GetProcessTypeNameInEnglish(process_type
));
99 } // namespace task_profiler