Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: chrome/browser/task_management/sampling/task_manager_impl.cc

Issue 2028753002: Make Task Manager sort more meaningful (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unittest Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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/task_management/sampling/task_manager_impl.h" 5 #include "chrome/browser/task_management/sampling/task_manager_impl.h"
6 6
7 #include <algorithm>
7 #include <string> 8 #include <string>
9 #include <unordered_map>
10 #include <unordered_set>
8 #include <vector> 11 #include <vector>
9 12
10 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/containers/adapters.h"
11 #include "base/stl_util.h" 15 #include "base/stl_util.h"
12 #include "build/build_config.h" 16 #include "build/build_config.h"
13 #include "chrome/browser/task_management/providers/browser_process_task_provider .h" 17 #include "chrome/browser/task_management/providers/browser_process_task_provider .h"
14 #include "chrome/browser/task_management/providers/child_process_task_provider.h " 18 #include "chrome/browser/task_management/providers/child_process_task_provider.h "
15 #include "chrome/browser/task_management/providers/web_contents/web_contents_tas k_provider.h" 19 #include "chrome/browser/task_management/providers/web_contents/web_contents_tas k_provider.h"
16 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/gpu_data_manager.h" 21 #include "content/public/browser/gpu_data_manager.h"
18 22
19 #if defined(OS_CHROMEOS) 23 #if defined(OS_CHROMEOS)
20 #include "chrome/browser/task_management/providers/arc/arc_process_task_provider .h" 24 #include "chrome/browser/task_management/providers/arc/arc_process_task_provider .h"
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 *stats = task->GetWebCacheStats(); 247 *stats = task->GetWebCacheStats();
244 248
245 return true; 249 return true;
246 } 250 }
247 251
248 const TaskIdList& TaskManagerImpl::GetTaskIdsList() const { 252 const TaskIdList& TaskManagerImpl::GetTaskIdsList() const {
249 DCHECK(is_running_) << "Task manager is not running. You must observe the " 253 DCHECK(is_running_) << "Task manager is not running. You must observe the "
250 "task manager for it to start running"; 254 "task manager for it to start running";
251 255
252 if (sorted_task_ids_.empty()) { 256 if (sorted_task_ids_.empty()) {
253 sorted_task_ids_.reserve(task_groups_by_task_id_.size()); 257 // |comparator| groups and sorts by subtask-ness (to push all subtasks to be
258 // last), then by process type (e.g. the browser process should be first;
259 // renderer processes should be together), then tab id (processes used by
260 // the same tab should be kept together, and a tab should have a stable
261 // position in the list as it cycles through processes, and tab creation
262 // order is meaningful), and finally by task id (when all else is equal, put
263 // the oldest tasks first).
264 auto comparator = [](const Task* a, const Task* b) -> bool {
265 return std::make_tuple(a->HasParentTask(), a->GetType(), a->GetTabId(),
266 a->task_id()) <
267 std::make_tuple(b->HasParentTask(), b->GetType(), b->GetTabId(),
268 b->task_id());
269 };
254 270
255 // Ensure browser process group of task IDs are at the beginning of the 271 const size_t num_groups = task_groups_by_proc_id_.size();
256 // list. 272 const size_t num_tasks = task_groups_by_task_id_.size();
257 auto it = task_groups_by_proc_id_.find(base::GetCurrentProcId());
258 DCHECK(it != task_groups_by_proc_id_.end());
259 const TaskGroup* browser_group = it->second;
260 browser_group->AppendSortedTaskIds(&sorted_task_ids_);
261 273
274 // Populate |tasks_to_visit| with one task from each group.
275 std::vector<const Task*> tasks_to_visit;
276 tasks_to_visit.reserve(num_groups);
277 std::unordered_map<const Task*, std::vector<const Task*>> children;
262 for (const auto& groups_pair : task_groups_by_proc_id_) { 278 for (const auto& groups_pair : task_groups_by_proc_id_) {
263 if (groups_pair.second == browser_group) 279 // The first task in the group (per |comparator|) is the one used for
280 // sorting the group relative to other groups.
281 const std::vector<Task*>& tasks = groups_pair.second->tasks();
282 Task* group_task =
283 *std::min_element(tasks.begin(), tasks.end(), comparator);
284 tasks_to_visit.push_back(group_task);
285
286 // Build the parent-to-child map, for use later.
287 for (const Task* task : tasks) {
288 if (task->HasParentTask())
289 children[task->GetParentTask()].push_back(task);
290 else
291 DCHECK(!group_task->HasParentTask());
292 }
293 }
294
295 // Now sort |tasks_to_visit| in reverse order (putting the browser process
296 // at back()). We will treat it as a stack from now on.
297 std::sort(tasks_to_visit.rbegin(), tasks_to_visit.rend(), comparator);
298 DCHECK_EQ(Task::BROWSER, tasks_to_visit.back()->GetType());
299
300 // Using |tasks_to_visit| as a stack, and |visited_groups| to track which
301 // groups we've already added, add groups to |sorted_task_ids_| until all
302 // groups have been added.
303 sorted_task_ids_.reserve(num_tasks);
304 std::unordered_set<TaskGroup*> visited_groups;
305 visited_groups.reserve(num_groups);
306 std::vector<Task*> current_group_tasks; // Outside loop for fewer mallocs.
307 while (visited_groups.size() < num_groups) {
308 DCHECK(!tasks_to_visit.empty());
309 TaskGroup* current_group =
310 GetTaskGroupByTaskId(tasks_to_visit.back()->task_id());
311 tasks_to_visit.pop_back();
312
313 // Mark |current_group| as visited. If this fails, we've already added
314 // the group, and should skip over it.
315 if (!visited_groups.insert(current_group).second)
264 continue; 316 continue;
265 317
266 groups_pair.second->AppendSortedTaskIds(&sorted_task_ids_); 318 // Make a copy of |current_group->tasks()|, sort it, and append the ids.
319 current_group_tasks = current_group->tasks();
320 std::sort(current_group_tasks.begin(), current_group_tasks.end(),
321 comparator);
322 for (Task* task : current_group_tasks)
323 sorted_task_ids_.push_back(task->task_id());
324
325 // Find the children of the tasks we just added, and push them into
326 // |tasks_to_visit|, so that we visit them soon. Work in reverse order,
327 // so that we visit them in forward order.
328 for (Task* parent : base::Reversed(current_group_tasks)) {
329 auto children_of_parent = children.find(parent);
330 if (children_of_parent != children.end()) {
331 // Sort children[parent], and then append in reversed order.
332 std::sort(children_of_parent->second.begin(),
333 children_of_parent->second.end(), comparator);
334 tasks_to_visit.insert(tasks_to_visit.end(),
335 children_of_parent->second.rbegin(),
336 children_of_parent->second.rend());
337 }
338 }
267 } 339 }
340 DCHECK_EQ(num_tasks, sorted_task_ids_.size());
268 } 341 }
269 342
270 return sorted_task_ids_; 343 return sorted_task_ids_;
271 } 344 }
272 345
273 TaskIdList TaskManagerImpl::GetIdsOfTasksSharingSameProcess( 346 TaskIdList TaskManagerImpl::GetIdsOfTasksSharingSameProcess(
274 TaskId task_id) const { 347 TaskId task_id) const {
275 DCHECK(is_running_) << "Task manager is not running. You must observe the " 348 DCHECK(is_running_) << "Task manager is not running. You must observe the "
276 "task manager for it to start running"; 349 "task manager for it to start running";
277 350
278 TaskIdList result; 351 TaskIdList result;
279 GetTaskGroupByTaskId(task_id)->AppendSortedTaskIds(&result); 352 TaskGroup* group = GetTaskGroupByTaskId(task_id);
353 if (group) {
354 result.reserve(group->tasks().size());
355 for (Task* task : group->tasks())
356 result.push_back(task->task_id());
357 }
280 return result; 358 return result;
281 } 359 }
282 360
283 size_t TaskManagerImpl::GetNumberOfTasksOnSameProcess(TaskId task_id) const { 361 size_t TaskManagerImpl::GetNumberOfTasksOnSameProcess(TaskId task_id) const {
284 return GetTaskGroupByTaskId(task_id)->num_tasks(); 362 return GetTaskGroupByTaskId(task_id)->num_tasks();
285 } 363 }
286 364
287 void TaskManagerImpl::TaskAdded(Task* task) { 365 void TaskManagerImpl::TaskAdded(Task* task) {
288 DCHECK(task); 366 DCHECK(task);
289 367
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 groups_itr.second->AreBackgroundCalculationsDone(); 527 groups_itr.second->AreBackgroundCalculationsDone();
450 } 528 }
451 if (are_all_processes_data_ready) { 529 if (are_all_processes_data_ready) {
452 NotifyObserversOnRefreshWithBackgroundCalculations(GetTaskIdsList()); 530 NotifyObserversOnRefreshWithBackgroundCalculations(GetTaskIdsList());
453 for (const auto& groups_itr : task_groups_by_proc_id_) 531 for (const auto& groups_itr : task_groups_by_proc_id_)
454 groups_itr.second->ClearCurrentBackgroundCalculationsFlags(); 532 groups_itr.second->ClearCurrentBackgroundCalculationsFlags();
455 } 533 }
456 } 534 }
457 535
458 } // namespace task_management 536 } // namespace task_management
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698