Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / drive / job_queue.cc
blobd890abf846428539c3de5a12d78adea7c3e1a1fd
1 // Copyright 2013 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.
5 #include "components/drive/job_queue.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
12 namespace drive {
14 JobQueue::Item::Item() : batchable(false), size(0) {
17 JobQueue::Item::Item(JobID id, bool batchable, uint64 size)
18 : id(id), batchable(batchable), size(size) {
21 JobQueue::Item::~Item() {
24 JobQueue::JobQueue(size_t num_max_concurrent_jobs,
25 size_t num_priority_levels,
26 size_t num_max_batch_jobs,
27 size_t max_batch_size)
28 : num_max_concurrent_jobs_(num_max_concurrent_jobs),
29 queue_(num_priority_levels),
30 num_max_batch_jobs_(num_max_batch_jobs),
31 max_batch_size_(max_batch_size) {
34 JobQueue::~JobQueue() {
37 void JobQueue::PopForRun(int accepted_priority, std::vector<JobID>* jobs) {
38 DCHECK_LT(accepted_priority, static_cast<int>(queue_.size()));
39 jobs->clear();
41 // Too many jobs are running already.
42 if (running_.size() >= num_max_concurrent_jobs_)
43 return;
45 // Looks up the queue in the order of priority upto |accepted_priority|.
46 uint64 total_size = 0;
47 bool batchable = true;
48 for (int priority = 0; priority <= accepted_priority; ++priority) {
49 while (!queue_[priority].empty()) {
50 const auto& item = queue_[priority].front();
51 total_size += item.size;
52 batchable = batchable && item.batchable &&
53 jobs->size() < num_max_batch_jobs_ &&
54 total_size <= max_batch_size_;
55 if (!(jobs->empty() || batchable))
56 return;
57 jobs->push_back(item.id);
58 running_.insert(item.id);
59 queue_[priority].pop_front();
64 void JobQueue::GetQueuedJobs(int priority, std::vector<JobID>* jobs) const {
65 DCHECK_LT(priority, static_cast<int>(queue_.size()));
66 jobs->clear();
67 for (const Item& item : queue_[priority]) {
68 jobs->push_back(item.id);
72 void JobQueue::Push(JobID id, int priority, bool batchable, uint64 size) {
73 DCHECK_LT(priority, static_cast<int>(queue_.size()));
74 queue_[priority].push_back(Item(id, batchable, size));
77 void JobQueue::MarkFinished(JobID id) {
78 size_t num_erased = running_.erase(id);
79 DCHECK_EQ(1U, num_erased);
82 std::string JobQueue::ToString() const {
83 size_t pending = 0;
84 for (size_t i = 0; i < queue_.size(); ++i)
85 pending += queue_[i].size();
86 return base::StringPrintf("pending: %d, running: %d",
87 static_cast<int>(pending),
88 static_cast<int>(running_.size()));
91 size_t JobQueue::GetNumberOfJobs() const {
92 size_t count = running_.size();
93 for (size_t i = 0; i < queue_.size(); ++i)
94 count += queue_[i].size();
95 return count;
98 void JobQueue::Remove(JobID id) {
99 for (size_t i = 0; i < queue_.size(); ++i) {
100 for (auto it = queue_[i].begin(); it != queue_[i].end(); ++it) {
101 if (it->id == id) {
102 queue_[i].erase(it);
103 break;
109 } // namespace drive