Commit abd81a52 authored by thestig@chromium.org's avatar thestig@chromium.org

Cleanup: Typedef std::pairs in TaskManager code. Also making goat teleporting thread safe!

BUG=none
TEST=none

Review URL: http://codereview.chromium.org/9565041

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124662 0039d316-1c4b-4281-b951-d872f2087c98
parent 41247fdc
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/i18n/number_formatting.h" #include "base/i18n/number_formatting.h"
#include "base/i18n/rtl.h" #include "base/i18n/rtl.h"
#include "base/process_util.h" #include "base/process_util.h"
#include "base/rand_util.h"
#include "base/string_number_conversions.h" #include "base/string_number_conversions.h"
#include "base/stringprintf.h" #include "base/stringprintf.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
...@@ -83,7 +84,7 @@ string16 FormatStatsSize(const WebKit::WebCache::ResourceTypeStat& stat) { ...@@ -83,7 +84,7 @@ string16 FormatStatsSize(const WebKit::WebCache::ResourceTypeStat& stat) {
TaskManagerModel::TaskManagerModel(TaskManager* task_manager) TaskManagerModel::TaskManagerModel(TaskManager* task_manager)
: update_requests_(0), : update_requests_(0),
update_state_(IDLE), update_state_(IDLE),
goat_salt_(rand()), goat_salt_(base::RandUint64()),
last_unique_id_(0) { last_unique_id_(0) {
AddResourceProvider( AddResourceProvider(
new TaskManagerBrowserProcessResourceProvider(task_manager)); new TaskManagerBrowserProcessResourceProvider(task_manager));
...@@ -309,8 +310,8 @@ SkBitmap TaskManagerModel::GetResourceIcon(int index) const { ...@@ -309,8 +310,8 @@ SkBitmap TaskManagerModel::GetResourceIcon(int index) const {
return *default_icon; return *default_icon;
} }
std::pair<int, int> TaskManagerModel::GetGroupRangeForResource(int index) TaskManagerModel::GroupRange
const { TaskManagerModel::GetGroupRangeForResource(int index) const {
CHECK_LT(index, ResourceCount()); CHECK_LT(index, ResourceCount());
TaskManager::Resource* resource = resources_[index]; TaskManager::Resource* resource = resources_[index];
GroupMap::const_iterator group_iter = GroupMap::const_iterator group_iter =
...@@ -337,7 +338,7 @@ int TaskManagerModel::GetGroupIndexForResource(int index) const { ...@@ -337,7 +338,7 @@ int TaskManagerModel::GetGroupIndexForResource(int index) const {
group_index++; group_index++;
} }
DCHECK(group_index != -1); DCHECK_NE(group_index, -1);
return group_index; return group_index;
} }
...@@ -498,7 +499,7 @@ bool TaskManagerModel::GetPrivateMemory(int index, size_t* result) const { ...@@ -498,7 +499,7 @@ bool TaskManagerModel::GetPrivateMemory(int index, size_t* result) const {
base::ProcessHandle handle = resources_[index]->GetProcess(); base::ProcessHandle handle = resources_[index]->GetProcess();
MemoryUsageMap::const_iterator iter = memory_usage_map_.find(handle); MemoryUsageMap::const_iterator iter = memory_usage_map_.find(handle);
if (iter == memory_usage_map_.end()) { if (iter == memory_usage_map_.end()) {
std::pair<size_t, size_t> usage; MemoryUsageEntry usage;
if (!GetAndCacheMemoryMetrics(handle, &usage)) if (!GetAndCacheMemoryMetrics(handle, &usage))
return false; return false;
...@@ -514,7 +515,7 @@ bool TaskManagerModel::GetSharedMemory(int index, size_t* result) const { ...@@ -514,7 +515,7 @@ bool TaskManagerModel::GetSharedMemory(int index, size_t* result) const {
base::ProcessHandle handle = resources_[index]->GetProcess(); base::ProcessHandle handle = resources_[index]->GetProcess();
MemoryUsageMap::const_iterator iter = memory_usage_map_.find(handle); MemoryUsageMap::const_iterator iter = memory_usage_map_.find(handle);
if (iter == memory_usage_map_.end()) { if (iter == memory_usage_map_.end()) {
std::pair<size_t, size_t> usage; MemoryUsageEntry usage;
if (!GetAndCacheMemoryMetrics(handle, &usage)) if (!GetAndCacheMemoryMetrics(handle, &usage))
return false; return false;
...@@ -850,7 +851,7 @@ void TaskManagerModel::Refresh() { ...@@ -850,7 +851,7 @@ void TaskManagerModel::Refresh() {
return; return;
} }
goat_salt_ = rand(); goat_salt_ = base::RandUint64();
// Compute the CPU usage values. // Compute the CPU usage values.
// Note that we compute the CPU usage for all resources (instead of doing it // Note that we compute the CPU usage for all resources (instead of doing it
...@@ -1104,9 +1105,8 @@ void TaskManager::OpenAboutMemory() { ...@@ -1104,9 +1105,8 @@ void TaskManager::OpenAboutMemory() {
browser->window()->Show(); browser->window()->Show();
} }
bool TaskManagerModel::GetAndCacheMemoryMetrics( bool TaskManagerModel::GetAndCacheMemoryMetrics(base::ProcessHandle handle,
base::ProcessHandle handle, MemoryUsageEntry* usage) const {
std::pair<size_t, size_t>* usage) const {
MetricsMap::const_iterator iter = metrics_map_.find(handle); MetricsMap::const_iterator iter = metrics_map_.find(handle);
if (iter == metrics_map_.end()) if (iter == metrics_map_.end())
return false; return false;
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#pragma once #pragma once
#include <map> #include <map>
#include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
...@@ -136,6 +135,7 @@ class TaskManager { ...@@ -136,6 +135,7 @@ class TaskManager {
// Returns resource identifier that is unique within single task manager // Returns resource identifier that is unique within single task manager
// session (between StartUpdating and StopUpdating). // session (between StartUpdating and StopUpdating).
int get_unique_id() { return unique_id_; } int get_unique_id() { return unique_id_; }
protected: protected:
Resource() : unique_id_(0) {} Resource() : unique_id_(0) {}
...@@ -263,6 +263,9 @@ class TaskManagerModelObserver { ...@@ -263,6 +263,9 @@ class TaskManagerModelObserver {
// The model that the TaskManager is using. // The model that the TaskManager is using.
class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> { class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> {
public: public:
// (start, length)
typedef std::pair<int, int> GroupRange;
explicit TaskManagerModel(TaskManager* task_manager); explicit TaskManagerModel(TaskManager* task_manager);
void AddObserver(TaskManagerModelObserver* observer); void AddObserver(TaskManagerModelObserver* observer);
...@@ -355,8 +358,8 @@ class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> { ...@@ -355,8 +358,8 @@ class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> {
// Returns icon to be used for resource (for example a favicon). // Returns icon to be used for resource (for example a favicon).
SkBitmap GetResourceIcon(int index) const; SkBitmap GetResourceIcon(int index) const;
// Returns a pair (start, length) of the group range of resource. // Returns the group range of resource.
std::pair<int, int> GetGroupRangeForResource(int index) const; GroupRange GetGroupRangeForResource(int index) const;
// Returns an index of groups to which the resource belongs. // Returns an index of groups to which the resource belongs.
int GetGroupIndexForResource(int index) const; int GetGroupIndexForResource(int index) const;
...@@ -450,8 +453,9 @@ class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> { ...@@ -450,8 +453,9 @@ class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> {
typedef std::map<base::ProcessHandle, base::ProcessMetrics*> MetricsMap; typedef std::map<base::ProcessHandle, base::ProcessMetrics*> MetricsMap;
typedef std::map<base::ProcessHandle, double> CPUUsageMap; typedef std::map<base::ProcessHandle, double> CPUUsageMap;
typedef std::map<TaskManager::Resource*, int64> ResourceValueMap; typedef std::map<TaskManager::Resource*, int64> ResourceValueMap;
typedef std::map<base::ProcessHandle, // Private memory in bytes, shared memory in bytes.
std::pair<size_t, size_t> > MemoryUsageMap; typedef std::pair<size_t, size_t> MemoryUsageEntry;
typedef std::map<base::ProcessHandle, MemoryUsageEntry> MemoryUsageMap;
// Updates the values for all rows. // Updates the values for all rows.
void Refresh(); void Refresh();
...@@ -487,7 +491,7 @@ class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> { ...@@ -487,7 +491,7 @@ class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> {
// Looks up the data for |handle| and puts it in the mutable cache // Looks up the data for |handle| and puts it in the mutable cache
// |memory_usage_map_|. // |memory_usage_map_|.
bool GetAndCacheMemoryMetrics(base::ProcessHandle handle, bool GetAndCacheMemoryMetrics(base::ProcessHandle handle,
std::pair<size_t, size_t>* usage) const; MemoryUsageEntry* usage) const;
// Adds a resource provider to be managed. // Adds a resource provider to be managed.
void AddResourceProvider(TaskManager::ResourceProvider* provider); void AddResourceProvider(TaskManager::ResourceProvider* provider);
...@@ -536,7 +540,7 @@ class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> { ...@@ -536,7 +540,7 @@ class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> {
UpdateState update_state_; UpdateState update_state_;
// A salt lick for the goats. // A salt lick for the goats.
int goat_salt_; uint64 goat_salt_;
// Resource identifier that is unique within single session. // Resource identifier that is unique within single session.
int last_unique_id_; int last_unique_id_;
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -69,8 +69,10 @@ class SortHelper { ...@@ -69,8 +69,10 @@ class SortHelper {
model_(model) {} model_(model) {}
bool operator()(int a, int b) { bool operator()(int a, int b) {
std::pair<int, int> group_range1 = model_->GetGroupRangeForResource(a); TaskManagerModel::GroupRange group_range1 =
std::pair<int, int> group_range2 = model_->GetGroupRangeForResource(b); model_->GetGroupRangeForResource(a);
TaskManagerModel::GroupRange group_range2 =
model_->GetGroupRangeForResource(b);
if (group_range1 == group_range2) { if (group_range1 == group_range2) {
// The two rows are in the same group, sort so that items in the same // The two rows are in the same group, sort so that items in the same
// group always appear in the same order. |ascending_| is intentionally // group always appear in the same order. |ascending_| is intentionally
...@@ -359,7 +361,7 @@ class SortHelper { ...@@ -359,7 +361,7 @@ class SortHelper {
if (taskManager_->IsBrowserProcess(modelIndex)) if (taskManager_->IsBrowserProcess(modelIndex))
selectionContainsBrowserProcess = true; selectionContainsBrowserProcess = true;
std::pair<int, int> rangePair = TaskManagerModel::GroupRange rangePair =
model_->GetGroupRangeForResource(modelIndex); model_->GetGroupRangeForResource(modelIndex);
NSMutableIndexSet* indexSet = [NSMutableIndexSet indexSet]; NSMutableIndexSet* indexSet = [NSMutableIndexSet indexSet];
for (int j = 0; j < rangePair.second; ++j) for (int j = 0; j < rangePair.second; ++j)
......
...@@ -866,8 +866,10 @@ gint TaskManagerGtk::CompareImpl(GtkTreeModel* model, GtkTreeIter* a, ...@@ -866,8 +866,10 @@ gint TaskManagerGtk::CompareImpl(GtkTreeModel* model, GtkTreeIter* a,
return model_->CompareValues(row1, row2, id); return model_->CompareValues(row1, row2, id);
// Otherwise, make sure grouped resources are shown together. // Otherwise, make sure grouped resources are shown together.
std::pair<int, int> group_range1 = model_->GetGroupRangeForResource(row1); TaskManagerModel::GroupRange group_range1 =
std::pair<int, int> group_range2 = model_->GetGroupRangeForResource(row2); model_->GetGroupRangeForResource(row1);
TaskManagerModel::GroupRange group_range2 =
model_->GetGroupRangeForResource(row2);
if (group_range1 == group_range2) { if (group_range1 == group_range2) {
// Sort within groups. // Sort within groups.
...@@ -929,7 +931,7 @@ void TaskManagerGtk::OnSelectionChanged(GtkTreeSelection* selection) { ...@@ -929,7 +931,7 @@ void TaskManagerGtk::OnSelectionChanged(GtkTreeSelection* selection) {
AutoReset<bool> autoreset(&ignore_selection_changed_, true); AutoReset<bool> autoreset(&ignore_selection_changed_, true);
// The set of groups that should be selected. // The set of groups that should be selected.
std::set<std::pair<int, int> > ranges; std::set<TaskManagerModel::GroupRange> ranges;
bool selection_contains_browser_process = false; bool selection_contains_browser_process = false;
GtkTreeModel* model; GtkTreeModel* model;
...@@ -947,7 +949,7 @@ void TaskManagerGtk::OnSelectionChanged(GtkTreeSelection* selection) { ...@@ -947,7 +949,7 @@ void TaskManagerGtk::OnSelectionChanged(GtkTreeSelection* selection) {
g_list_foreach(paths, reinterpret_cast<GFunc>(gtk_tree_path_free), NULL); g_list_foreach(paths, reinterpret_cast<GFunc>(gtk_tree_path_free), NULL);
g_list_free(paths); g_list_free(paths);
for (std::set<std::pair<int, int> >::iterator iter = ranges.begin(); for (std::set<TaskManagerModel::GroupRange>::iterator iter = ranges.begin();
iter != ranges.end(); ++iter) { iter != ranges.end(); ++iter) {
for (int i = 0; i < iter->second; ++i) { for (int i = 0; i < iter->second; ++i) {
GtkTreePath* child_path = gtk_tree_path_new_from_indices(iter->first + i, GtkTreePath* child_path = gtk_tree_path_new_from_indices(iter->first + i,
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -170,7 +170,8 @@ SkBitmap TaskManagerTableModel::GetIcon(int row) { ...@@ -170,7 +170,8 @@ SkBitmap TaskManagerTableModel::GetIcon(int row) {
void TaskManagerTableModel::GetGroupRangeForItem(int item, void TaskManagerTableModel::GetGroupRangeForItem(int item,
views::GroupRange* range) { views::GroupRange* range) {
std::pair<int, int> range_pair = model_->GetGroupRangeForResource(item); TaskManagerModel::GroupRange range_pair =
model_->GetGroupRangeForResource(item);
range->start = range_pair.first; range->start = range_pair.first;
range->length = range_pair.second; range->length = range_pair.second;
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/string_number_conversions.h" #include "base/string_number_conversions.h"
...@@ -26,7 +27,7 @@ ...@@ -26,7 +27,7 @@
namespace { namespace {
Value* CreateColumnValue(const TaskManagerModel* tm, Value* CreateColumnValue(const TaskManagerModel* tm,
const std::string column_name, const std::string& column_name,
const int i) { const int i) {
if (column_name == "uniqueId") if (column_name == "uniqueId")
return Value::CreateIntegerValue(tm->GetResourceUniqueId(i)); return Value::CreateIntegerValue(tm->GetResourceUniqueId(i));
...@@ -134,7 +135,7 @@ void CreateGroupColumnList(const TaskManagerModel* tm, ...@@ -134,7 +135,7 @@ void CreateGroupColumnList(const TaskManagerModel* tm,
const int index, const int index,
const int length, const int length,
DictionaryValue* val) { DictionaryValue* val) {
ListValue *list = new ListValue(); ListValue* list = new ListValue();
for (int i = index; i < (index + length); ++i) { for (int i = index; i < (index + length); ++i) {
list->Append(CreateColumnValue(tm, column_name, i)); list->Append(CreateColumnValue(tm, column_name, i));
} }
...@@ -184,9 +185,7 @@ DictionaryValue* CreateTaskGroupValue( ...@@ -184,9 +185,7 @@ DictionaryValue* CreateTaskGroupValue(
return val; return val;
int index = tm->GetResourceIndexForGroup(group_index, 0); int index = tm->GetResourceIndexForGroup(group_index, 0);
std::pair<int, int> group_range; int length = tm->GetGroupRangeForResource(index).second;
group_range = tm->GetGroupRangeForResource(index);
int length = group_range.second;
// Forces to set following 3 columns regardless of |enable_columns|. // Forces to set following 3 columns regardless of |enable_columns|.
val->SetInteger("index", index); val->SetInteger("index", index);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment