| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/system/syslogs_provider.h" | 5 #include "chrome/browser/chromeos/system/syslogs_provider.h" |
| 6 | 6 |
| 7 #include <functional> | |
| 8 #include <set> | |
| 9 | 7 |
| 10 #include "base/bind.h" | 8 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 12 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 13 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 14 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 15 #include "base/logging.h" | 13 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
| 18 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 19 #include "base/stringprintf.h" | |
| 20 #include "base/utf_string_conversions.h" | |
| 21 #include "chrome/browser/memory_details.h" | 17 #include "chrome/browser/memory_details.h" |
| 22 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 23 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 24 | 20 |
| 25 using content::BrowserThread; | 21 using content::BrowserThread; |
| 26 | 22 |
| 27 namespace chromeos { | 23 namespace chromeos { |
| 28 namespace system { | 24 namespace system { |
| 29 namespace { | 25 namespace { |
| 30 | 26 |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 SyslogsMemoryHandler( | 241 SyslogsMemoryHandler( |
| 246 scoped_refptr<CancelableRequest<ReadCompleteCallback> > request, | 242 scoped_refptr<CancelableRequest<ReadCompleteCallback> > request, |
| 247 LogDictionaryType* logs, | 243 LogDictionaryType* logs, |
| 248 std::string* zip_content) | 244 std::string* zip_content) |
| 249 : request_(request), | 245 : request_(request), |
| 250 logs_(logs), | 246 logs_(logs), |
| 251 zip_content_(zip_content) { | 247 zip_content_(zip_content) { |
| 252 } | 248 } |
| 253 | 249 |
| 254 virtual void OnDetailsAvailable() OVERRIDE { | 250 virtual void OnDetailsAvailable() OVERRIDE { |
| 255 const ProcessData& chrome = processes()[0]; // Chrome is the first entry. | 251 (*logs_)["mem_usage"] = ToLogString(); |
| 256 // Process info, sorted by memory used (highest to lowest). | |
| 257 typedef std::pair<int, std::string> ProcInfo; | |
| 258 typedef std::set<ProcInfo, std::greater<ProcInfo> > ProcInfoSet; | |
| 259 ProcInfoSet process_info; | |
| 260 for (ProcessMemoryInformationList::const_iterator iter1 = | |
| 261 chrome.processes.begin(); | |
| 262 iter1 != chrome.processes.end(); ++iter1) { | |
| 263 std::string process_string( | |
| 264 ProcessMemoryInformation::GetFullTypeNameInEnglish( | |
| 265 iter1->type, iter1->renderer_type)); | |
| 266 if (!iter1->titles.empty()) { | |
| 267 std::string titles(" ["); | |
| 268 for (std::vector<string16>::const_iterator iter2 = | |
| 269 iter1->titles.begin(); | |
| 270 iter2 != iter1->titles.end(); ++iter2) { | |
| 271 if (iter2 != iter1->titles.begin()) | |
| 272 titles += "|"; | |
| 273 titles += UTF16ToUTF8(*iter2); | |
| 274 } | |
| 275 titles += "]"; | |
| 276 process_string += titles; | |
| 277 } | |
| 278 // Use private working set for memory used calculation. | |
| 279 int ws_mbytes = static_cast<int>(iter1->working_set.priv) / 1024; | |
| 280 process_info.insert(std::make_pair(ws_mbytes, process_string)); | |
| 281 } | |
| 282 // Add one line for each reverse-sorted entry. | |
| 283 std::string mem_string; | |
| 284 for (ProcInfoSet::iterator iter = process_info.begin(); | |
| 285 iter != process_info.end(); ++iter) { | |
| 286 mem_string += | |
| 287 iter->second + base::StringPrintf(": %d MB", iter->first) + "\n"; | |
| 288 } | |
| 289 (*logs_)["mem_usage"] = mem_string; | |
| 290 // This will call the callback on the calling thread. | 252 // This will call the callback on the calling thread. |
| 291 request_->ForwardResult(logs_, zip_content_); | 253 request_->ForwardResult(logs_, zip_content_); |
| 292 } | 254 } |
| 293 | 255 |
| 294 private: | 256 private: |
| 295 virtual ~SyslogsMemoryHandler() {} | 257 virtual ~SyslogsMemoryHandler() {} |
| 296 | 258 |
| 297 scoped_refptr<CancelableRequest<ReadCompleteCallback> > request_; | 259 scoped_refptr<CancelableRequest<ReadCompleteCallback> > request_; |
| 298 LogDictionaryType* logs_; | 260 LogDictionaryType* logs_; |
| 299 std::string* zip_content_; | 261 std::string* zip_content_; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 zip_content = new std::string(); | 294 zip_content = new std::string(); |
| 333 LoadCompressedLogs(zip_file, zip_content); | 295 LoadCompressedLogs(zip_file, zip_content); |
| 334 file_util::Delete(zip_file, false); | 296 file_util::Delete(zip_file, false); |
| 335 } | 297 } |
| 336 | 298 |
| 337 // SyslogsMemoryHandler will clean itself up. | 299 // SyslogsMemoryHandler will clean itself up. |
| 338 // SyslogsMemoryHandler::OnDetailsAvailable() will modify |logs| and call | 300 // SyslogsMemoryHandler::OnDetailsAvailable() will modify |logs| and call |
| 339 // request->ForwardResult(logs, zip_content). | 301 // request->ForwardResult(logs, zip_content). |
| 340 scoped_refptr<SyslogsMemoryHandler> | 302 scoped_refptr<SyslogsMemoryHandler> |
| 341 handler(new SyslogsMemoryHandler(request, logs, zip_content)); | 303 handler(new SyslogsMemoryHandler(request, logs, zip_content)); |
| 342 handler->StartFetch(); | 304 // TODO(jamescook): Maybe we don't need to update histograms here? |
| 305 handler->StartFetch(MemoryDetails::UPDATE_USER_METRICS); |
| 343 } | 306 } |
| 344 | 307 |
| 345 void SyslogsProviderImpl::LoadCompressedLogs(const FilePath& zip_file, | 308 void SyslogsProviderImpl::LoadCompressedLogs(const FilePath& zip_file, |
| 346 std::string* zip_content) { | 309 std::string* zip_content) { |
| 347 DCHECK(zip_content); | 310 DCHECK(zip_content); |
| 348 if (!file_util::ReadFileToString(zip_file, zip_content)) { | 311 if (!file_util::ReadFileToString(zip_file, zip_content)) { |
| 349 LOG(ERROR) << "Cannot read compressed logs file from " << | 312 LOG(ERROR) << "Cannot read compressed logs file from " << |
| 350 zip_file.value().c_str(); | 313 zip_file.value().c_str(); |
| 351 } | 314 } |
| 352 } | 315 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 372 return Singleton<SyslogsProviderImpl, | 335 return Singleton<SyslogsProviderImpl, |
| 373 DefaultSingletonTraits<SyslogsProviderImpl> >::get(); | 336 DefaultSingletonTraits<SyslogsProviderImpl> >::get(); |
| 374 } | 337 } |
| 375 | 338 |
| 376 SyslogsProvider* SyslogsProvider::GetInstance() { | 339 SyslogsProvider* SyslogsProvider::GetInstance() { |
| 377 return SyslogsProviderImpl::GetInstance(); | 340 return SyslogsProviderImpl::GetInstance(); |
| 378 } | 341 } |
| 379 | 342 |
| 380 } // namespace system | 343 } // namespace system |
| 381 } // namespace chromeos | 344 } // namespace chromeos |
| OLD | NEW |