summaryrefslogtreecommitdiffstats
path: root/chromium/ash/system/monitor/tray_monitor.cc
blob: bc3d532ea55761462618d800f411176fc6bff7a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/monitor/tray_monitor.h"

#include "ash/system/tray/tray_item_view.h"
#include "base/process/memory.h"
#include "base/process/process_metrics.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/gpu_data_manager.h"
#include "ui/base/text/bytes_formatting.h"
#include "ui/views/border.h"
#include "ui/views/controls/label.h"

namespace {
const int kRefreshTimeoutMs = 1000;
}

namespace ash {
namespace internal {

TrayMonitor::TrayMonitor(SystemTray* system_tray)
    : SystemTrayItem(system_tray),
      label_(NULL) {
  refresh_timer_.Start(FROM_HERE,
      base::TimeDelta::FromMilliseconds(kRefreshTimeoutMs),
      this, &TrayMonitor::OnTimer);
}

TrayMonitor::~TrayMonitor() {
  label_ = NULL;
}

views::View* TrayMonitor::CreateTrayView(user::LoginStatus status) {
  TrayItemView* view = new TrayItemView(this);
  view->CreateLabel();
  label_ = view->label();
  label_->SetAutoColorReadabilityEnabled(false);
  label_->SetEnabledColor(SK_ColorWHITE);
  label_->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255));
  label_->SetShadowColors(SkColorSetARGB(64, 0, 0, 0),
                          SkColorSetARGB(64, 0, 0, 0));
  label_->SetShadowOffset(0, 1);
  label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label_->SetFont(label_->font().DeriveFont(-2));
  return view;
}

void TrayMonitor::DestroyTrayView() {
  label_ = NULL;
}

void TrayMonitor::OnTimer() {
  content::GpuDataManager::GetGpuProcessHandlesCallback callback =
      base::Bind(&TrayMonitor::OnGotHandles, base::Unretained(this));
  refresh_timer_.Stop();
  content::GpuDataManager::GetInstance()->GetGpuProcessHandles(callback);
}

void TrayMonitor::OnGotHandles(const std::list<base::ProcessHandle>& handles) {
  base::SystemMemoryInfoKB mem_info;
  base::GetSystemMemoryInfo(&mem_info);
  std::string output;
  base::string16 free_bytes =
      ui::FormatBytes(static_cast<int64>(mem_info.free) * 1024);
  output = base::StringPrintf("free: %s", UTF16ToUTF8(free_bytes).c_str());
#if defined(OS_CHROMEOS)
  if (mem_info.gem_size != -1) {
    base::string16 gem_size = ui::FormatBytes(mem_info.gem_size);
    output += base::StringPrintf("  gmem: %s", UTF16ToUTF8(gem_size).c_str());
    if (mem_info.gem_objects != -1)
      output += base::StringPrintf("  gobjects: %d", mem_info.gem_objects);
  }
#endif
  size_t total_private_bytes = 0, total_shared_bytes = 0;
  for (std::list<base::ProcessHandle>::const_iterator i = handles.begin();
       i != handles.end(); ++i) {
    base::ProcessMetrics* pm = base::ProcessMetrics::CreateProcessMetrics(*i);
    size_t private_bytes, shared_bytes;
    pm->GetMemoryBytes(&private_bytes, &shared_bytes);
    total_private_bytes += private_bytes;
    total_shared_bytes += shared_bytes;
    delete pm;
  }
  base::string16 private_size = ui::FormatBytes(total_private_bytes);
  base::string16 shared_size = ui::FormatBytes(total_shared_bytes);

  output += base::StringPrintf("\nGPU private: %s  shared: %s",
                               UTF16ToUTF8(private_size).c_str(),
                               UTF16ToUTF8(shared_size).c_str());
  label_->SetText(UTF8ToUTF16(output));
  refresh_timer_.Start(FROM_HERE,
      base::TimeDelta::FromMilliseconds(kRefreshTimeoutMs),
      this, &TrayMonitor::OnTimer);
}

}  // namespace internal
}  // namespace ash