OLD | NEW |
| (Empty) |
1 // Copyright 2015 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. | |
4 | |
5 #include "chrome/browser/caps/generate_state_json.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <string> | |
12 #include <utility> | |
13 | |
14 #include "base/bind.h" | |
15 #include "base/cpu.h" | |
16 #include "base/files/file.h" | |
17 #include "base/json/json_writer.h" | |
18 #include "base/location.h" | |
19 #include "base/memory/ref_counted.h" | |
20 #include "base/single_thread_task_runner.h" | |
21 #include "base/strings/stringprintf.h" | |
22 #include "base/sys_info.h" | |
23 #include "base/threading/thread_restrictions.h" | |
24 #include "base/threading/thread_task_runner_handle.h" | |
25 #include "base/time/time.h" | |
26 #include "base/values.h" | |
27 #include "build/build_config.h" | |
28 #include "chrome/browser/task_manager/task_manager.h" | |
29 | |
30 namespace { | |
31 | |
32 std::string Key(base::ProcessId pid, const char* category) { | |
33 return category ? | |
34 base::StringPrintf("process.%d.%s", pid, category) : | |
35 base::StringPrintf("process.%d", pid); | |
36 } | |
37 | |
38 using MemoryFn1 = bool (TaskManagerModel::*)( | |
39 int index, size_t* result1) const; | |
40 using MemoryFn2 = bool (TaskManagerModel::*)( | |
41 int index, size_t* result1, bool*) const; | |
42 | |
43 int InMBFromB(size_t result_in_bytes) { | |
44 return static_cast<int>(result_in_bytes / (1024 * 1024)); | |
45 } | |
46 | |
47 int InMBFromB(const TaskManagerModel* model, MemoryFn1 mfn, int index) { | |
48 size_t result_in_bytes = 0; | |
49 bool res = (model->*mfn)(index, &result_in_bytes); | |
50 return res ? InMBFromB(result_in_bytes) : 0; | |
51 } | |
52 | |
53 int InMBFromB(const TaskManagerModel* model, MemoryFn2 mfn, int index) { | |
54 size_t result_in_bytes = 0; | |
55 bool ignored; | |
56 bool res = (model->*mfn)(index, &result_in_bytes, &ignored); | |
57 return res ? InMBFromB(result_in_bytes) : 0; | |
58 } | |
59 | |
60 class TaskManagerDataDumper : | |
61 public base::RefCountedThreadSafe<TaskManagerDataDumper> { | |
62 public: | |
63 TaskManagerDataDumper(scoped_refptr<TaskManagerModel> model, base::File file) | |
64 : model_(model), file_(std::move(file)) { | |
65 model_->RegisterOnDataReadyCallback( | |
66 base::Bind(&TaskManagerDataDumper::OnDataReady, this)); | |
67 model->StartListening(); | |
68 // Note that GenerateStateJSON 'new's this object which is reference | |
69 // counted. | |
70 AddRef(); | |
71 } | |
72 | |
73 private: | |
74 friend class base::RefCountedThreadSafe<TaskManagerDataDumper>; | |
75 ~TaskManagerDataDumper() { | |
76 } | |
77 | |
78 void OnDataReady() { | |
79 // Some data (for example V8 memory) has not yet arrived, so we wait. | |
80 // TODO(cpu): Figure out how to make this reliable. | |
81 static base::TimeDelta delay = base::TimeDelta::FromMilliseconds(250); | |
82 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
83 FROM_HERE, base::Bind(&TaskManagerDataDumper::OnDataReadyDelayed, this), | |
84 delay); | |
85 } | |
86 | |
87 void OnDataReadyDelayed() { | |
88 model_->StopListening(); | |
89 // Task manager finally computed (most) values. Lets generate the JSON | |
90 // data and send it over the pipe. | |
91 base::DictionaryValue dict; | |
92 GatherComputerValues(&dict); | |
93 GatherChromeValues(&dict); | |
94 | |
95 std::string json; | |
96 auto options = base::JSONWriter::OPTIONS_PRETTY_PRINT; | |
97 if (!base::JSONWriter::WriteWithOptions(dict, options, &json)) | |
98 return; | |
99 | |
100 file_.WriteAtCurrentPos(json.c_str(), json.size()); | |
101 file_.Close(); | |
102 // this Release() causes our destruction. | |
103 Release(); | |
104 } | |
105 | |
106 private: | |
107 // TODO(cpu): split the key names below into a separate header that both | |
108 // caps and chrome can use. | |
109 | |
110 void GatherComputerValues(base::DictionaryValue* dict) { | |
111 base::CPU cpu; | |
112 dict->SetInteger("system.cpu.type", cpu.type()); | |
113 dict->SetInteger("system.cpu.family", cpu.family()); | |
114 dict->SetInteger("system.cpu.model", cpu.model()); | |
115 dict->SetInteger("system.cpu.stepping", cpu.stepping()); | |
116 dict->SetString("system.cpu.brand", cpu.cpu_brand()); | |
117 dict->SetInteger("system.cpu.logicalprocessors", | |
118 base::SysInfo::NumberOfProcessors()); | |
119 dict->SetInteger("system.cpu.logicalprocessors", | |
120 base::SysInfo::NumberOfProcessors()); | |
121 int64_t memory = base::SysInfo::AmountOfPhysicalMemory(); | |
122 dict->SetInteger("system.memory.physical", InMBFromB(memory)); | |
123 memory = base::SysInfo::AmountOfAvailablePhysicalMemory(); | |
124 dict->SetInteger("system.memory.available", InMBFromB(memory)); | |
125 dict->SetInteger("system.uptime", base::SysInfo::Uptime().InSeconds()); | |
126 dict->SetString("os.name", base::SysInfo::OperatingSystemName()); | |
127 #if !defined(OS_LINUX) | |
128 int32_t major, minor, bugfix; | |
129 base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix); | |
130 dict->SetInteger("os.version.major", major); | |
131 dict->SetInteger("os.version.minor", minor); | |
132 dict->SetInteger("os.version.bugfix", bugfix); | |
133 dict->SetString("os.arch", base::SysInfo::OperatingSystemArchitecture()); | |
134 #endif | |
135 } | |
136 | |
137 void GatherChromeValues(base::DictionaryValue* dict) { | |
138 for (int index = 0; index != model_->ResourceCount(); ++index) { | |
139 auto pid = model_->GetProcessId(index); | |
140 auto tabs_key = Key(pid, "tabs"); | |
141 base::ListValue* tabs; | |
142 if (!dict->GetList(tabs_key, &tabs)) { | |
143 tabs = new base::ListValue; | |
144 dict->Set(tabs_key, tabs); | |
145 tabs->AppendString(model_->GetResourceTitle(index)); | |
146 | |
147 dict->SetInteger(Key(pid, "memory.physical"), | |
148 InMBFromB(model_.get(), | |
149 &TaskManagerModel::GetPhysicalMemory, index)); | |
150 dict->SetInteger(Key(pid, "memory.private"), | |
151 InMBFromB(model_.get(), | |
152 &TaskManagerModel::GetPrivateMemory, index)); | |
153 dict->SetInteger(Key(pid, "memory.shared"), | |
154 InMBFromB(model_.get(), | |
155 &TaskManagerModel::GetSharedMemory, index)); | |
156 dict->SetInteger(Key(pid, "memory.video"), | |
157 InMBFromB(model_.get(), | |
158 &TaskManagerModel::GetVideoMemory, index)); | |
159 dict->SetInteger(Key(pid, "memory.V8.total"), | |
160 InMBFromB(model_.get(), | |
161 &TaskManagerModel::GetV8Memory, index)); | |
162 dict->SetInteger(Key(pid, "memory.V8.used"), | |
163 InMBFromB(model_.get(), | |
164 &TaskManagerModel::GetV8MemoryUsed, index)); | |
165 dict->SetInteger(Key(pid, "memory.sqlite"), | |
166 InMBFromB(model_.get(), | |
167 &TaskManagerModel::GetSqliteMemoryUsedBytes, index)); | |
168 dict->SetString(Key(pid, "uptime"), | |
169 ProcessUptime(model_->GetProcess(index))); | |
170 } else { | |
171 // TODO(cpu): Probably best to write the MD5 hash of the title. | |
172 tabs->AppendString(model_->GetResourceTitle(index)); | |
173 } | |
174 } | |
175 } | |
176 | |
177 std::string ProcessUptime(base::ProcessHandle process) { | |
178 #if defined(OS_WIN) | |
179 FILETIME creation_time; | |
180 FILETIME exit_time; | |
181 FILETIME kernel_time; | |
182 FILETIME user_time; | |
183 | |
184 if (!GetProcessTimes(process, &creation_time, &exit_time, | |
185 &kernel_time, &user_time)) | |
186 return std::string("~"); | |
187 | |
188 auto ct_delta = base::Time::Now() - base::Time::FromFileTime(creation_time); | |
189 return base::StringPrintf("%lld", ct_delta.InSeconds()); | |
190 #else | |
191 return std::string(); | |
192 #endif | |
193 } | |
194 | |
195 scoped_refptr<TaskManagerModel> model_; | |
196 base::File file_; | |
197 }; | |
198 | |
199 } // namespace | |
200 | |
201 namespace caps { | |
202 | |
203 void GenerateStateJSON( | |
204 scoped_refptr<TaskManagerModel> model, base::File file) { | |
205 new TaskManagerDataDumper(model, std::move(file)); | |
206 } | |
207 | |
208 } | |
OLD | NEW |