Home | History | Annotate | Download | only in monitor
      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.
      4 
      5 #include "ash/system/monitor/tray_monitor.h"
      6 
      7 #include "ash/system/tray/tray_item_view.h"
      8 #include "base/process/memory.h"
      9 #include "base/process/process_metrics.h"
     10 #include "base/strings/stringprintf.h"
     11 #include "base/strings/utf_string_conversions.h"
     12 #include "content/public/browser/gpu_data_manager.h"
     13 #include "ui/base/text/bytes_formatting.h"
     14 #include "ui/views/border.h"
     15 #include "ui/views/controls/label.h"
     16 
     17 namespace {
     18 const int kRefreshTimeoutMs = 1000;
     19 }
     20 
     21 namespace ash {
     22 namespace internal {
     23 
     24 TrayMonitor::TrayMonitor(SystemTray* system_tray)
     25     : SystemTrayItem(system_tray),
     26       label_(NULL) {
     27   refresh_timer_.Start(FROM_HERE,
     28       base::TimeDelta::FromMilliseconds(kRefreshTimeoutMs),
     29       this, &TrayMonitor::OnTimer);
     30 }
     31 
     32 TrayMonitor::~TrayMonitor() {
     33   label_ = NULL;
     34 }
     35 
     36 views::View* TrayMonitor::CreateTrayView(user::LoginStatus status) {
     37   TrayItemView* view = new TrayItemView(this);
     38   view->CreateLabel();
     39   label_ = view->label();
     40   label_->SetAutoColorReadabilityEnabled(false);
     41   label_->SetEnabledColor(SK_ColorWHITE);
     42   label_->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255));
     43   label_->SetShadowColors(SkColorSetARGB(64, 0, 0, 0),
     44                           SkColorSetARGB(64, 0, 0, 0));
     45   label_->SetShadowOffset(0, 1);
     46   label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
     47   label_->SetFont(label_->font().DeriveFont(-2));
     48   return view;
     49 }
     50 
     51 void TrayMonitor::DestroyTrayView() {
     52   label_ = NULL;
     53 }
     54 
     55 void TrayMonitor::OnTimer() {
     56   content::GpuDataManager::GetGpuProcessHandlesCallback callback =
     57       base::Bind(&TrayMonitor::OnGotHandles, base::Unretained(this));
     58   refresh_timer_.Stop();
     59   content::GpuDataManager::GetInstance()->GetGpuProcessHandles(callback);
     60 }
     61 
     62 void TrayMonitor::OnGotHandles(const std::list<base::ProcessHandle>& handles) {
     63   base::SystemMemoryInfoKB mem_info;
     64   base::GetSystemMemoryInfo(&mem_info);
     65   std::string output;
     66   base::string16 free_bytes =
     67       ui::FormatBytes(static_cast<int64>(mem_info.free) * 1024);
     68   output = base::StringPrintf("free: %s", UTF16ToUTF8(free_bytes).c_str());
     69 #if defined(OS_CHROMEOS)
     70   if (mem_info.gem_size != -1) {
     71     base::string16 gem_size = ui::FormatBytes(mem_info.gem_size);
     72     output += base::StringPrintf("  gmem: %s", UTF16ToUTF8(gem_size).c_str());
     73     if (mem_info.gem_objects != -1)
     74       output += base::StringPrintf("  gobjects: %d", mem_info.gem_objects);
     75   }
     76 #endif
     77   size_t total_private_bytes = 0, total_shared_bytes = 0;
     78   for (std::list<base::ProcessHandle>::const_iterator i = handles.begin();
     79        i != handles.end(); ++i) {
     80     base::ProcessMetrics* pm = base::ProcessMetrics::CreateProcessMetrics(*i);
     81     size_t private_bytes, shared_bytes;
     82     pm->GetMemoryBytes(&private_bytes, &shared_bytes);
     83     total_private_bytes += private_bytes;
     84     total_shared_bytes += shared_bytes;
     85     delete pm;
     86   }
     87   base::string16 private_size = ui::FormatBytes(total_private_bytes);
     88   base::string16 shared_size = ui::FormatBytes(total_shared_bytes);
     89 
     90   output += base::StringPrintf("\nGPU private: %s  shared: %s",
     91                                UTF16ToUTF8(private_size).c_str(),
     92                                UTF16ToUTF8(shared_size).c_str());
     93   label_->SetText(UTF8ToUTF16(output));
     94   refresh_timer_.Start(FROM_HERE,
     95       base::TimeDelta::FromMilliseconds(kRefreshTimeoutMs),
     96       this, &TrayMonitor::OnTimer);
     97 }
     98 
     99 }  // namespace internal
    100 }  // namespace ash
    101