| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "components/drive/job_queue.h" | 5 #include "components/drive/job_queue.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 | 11 |
| 12 namespace drive { | 12 namespace drive { |
| 13 | 13 |
| 14 JobQueue::Item::Item() : batchable(false), size(0) { | 14 JobQueue::Item::Item() : batchable(false), size(0) { |
| 15 } | 15 } |
| 16 | 16 |
| 17 JobQueue::Item::Item(JobID id, bool batchable, uint64 size) | 17 JobQueue::Item::Item(JobID id, bool batchable, uint64_t size) |
| 18 : id(id), batchable(batchable), size(size) { | 18 : id(id), batchable(batchable), size(size) {} |
| 19 } | |
| 20 | 19 |
| 21 JobQueue::Item::~Item() { | 20 JobQueue::Item::~Item() { |
| 22 } | 21 } |
| 23 | 22 |
| 24 JobQueue::JobQueue(size_t num_max_concurrent_jobs, | 23 JobQueue::JobQueue(size_t num_max_concurrent_jobs, |
| 25 size_t num_priority_levels, | 24 size_t num_priority_levels, |
| 26 size_t num_max_batch_jobs, | 25 size_t num_max_batch_jobs, |
| 27 size_t max_batch_size) | 26 size_t max_batch_size) |
| 28 : num_max_concurrent_jobs_(num_max_concurrent_jobs), | 27 : num_max_concurrent_jobs_(num_max_concurrent_jobs), |
| 29 queue_(num_priority_levels), | 28 queue_(num_priority_levels), |
| 30 num_max_batch_jobs_(num_max_batch_jobs), | 29 num_max_batch_jobs_(num_max_batch_jobs), |
| 31 max_batch_size_(max_batch_size) { | 30 max_batch_size_(max_batch_size) { |
| 32 } | 31 } |
| 33 | 32 |
| 34 JobQueue::~JobQueue() { | 33 JobQueue::~JobQueue() { |
| 35 } | 34 } |
| 36 | 35 |
| 37 void JobQueue::PopForRun(int accepted_priority, std::vector<JobID>* jobs) { | 36 void JobQueue::PopForRun(int accepted_priority, std::vector<JobID>* jobs) { |
| 38 DCHECK_LT(accepted_priority, static_cast<int>(queue_.size())); | 37 DCHECK_LT(accepted_priority, static_cast<int>(queue_.size())); |
| 39 jobs->clear(); | 38 jobs->clear(); |
| 40 | 39 |
| 41 // Too many jobs are running already. | 40 // Too many jobs are running already. |
| 42 if (running_.size() >= num_max_concurrent_jobs_) | 41 if (running_.size() >= num_max_concurrent_jobs_) |
| 43 return; | 42 return; |
| 44 | 43 |
| 45 // Looks up the queue in the order of priority upto |accepted_priority|. | 44 // Looks up the queue in the order of priority upto |accepted_priority|. |
| 46 uint64 total_size = 0; | 45 uint64_t total_size = 0; |
| 47 bool batchable = true; | 46 bool batchable = true; |
| 48 for (int priority = 0; priority <= accepted_priority; ++priority) { | 47 for (int priority = 0; priority <= accepted_priority; ++priority) { |
| 49 while (!queue_[priority].empty()) { | 48 while (!queue_[priority].empty()) { |
| 50 const auto& item = queue_[priority].front(); | 49 const auto& item = queue_[priority].front(); |
| 51 total_size += item.size; | 50 total_size += item.size; |
| 52 batchable = batchable && item.batchable && | 51 batchable = batchable && item.batchable && |
| 53 jobs->size() < num_max_batch_jobs_ && | 52 jobs->size() < num_max_batch_jobs_ && |
| 54 total_size <= max_batch_size_; | 53 total_size <= max_batch_size_; |
| 55 if (!(jobs->empty() || batchable)) | 54 if (!(jobs->empty() || batchable)) |
| 56 return; | 55 return; |
| 57 jobs->push_back(item.id); | 56 jobs->push_back(item.id); |
| 58 running_.insert(item.id); | 57 running_.insert(item.id); |
| 59 queue_[priority].pop_front(); | 58 queue_[priority].pop_front(); |
| 60 } | 59 } |
| 61 } | 60 } |
| 62 } | 61 } |
| 63 | 62 |
| 64 void JobQueue::GetQueuedJobs(int priority, std::vector<JobID>* jobs) const { | 63 void JobQueue::GetQueuedJobs(int priority, std::vector<JobID>* jobs) const { |
| 65 DCHECK_LT(priority, static_cast<int>(queue_.size())); | 64 DCHECK_LT(priority, static_cast<int>(queue_.size())); |
| 66 jobs->clear(); | 65 jobs->clear(); |
| 67 for (const Item& item : queue_[priority]) { | 66 for (const Item& item : queue_[priority]) { |
| 68 jobs->push_back(item.id); | 67 jobs->push_back(item.id); |
| 69 } | 68 } |
| 70 } | 69 } |
| 71 | 70 |
| 72 void JobQueue::Push(JobID id, int priority, bool batchable, uint64 size) { | 71 void JobQueue::Push(JobID id, int priority, bool batchable, uint64_t size) { |
| 73 DCHECK_LT(priority, static_cast<int>(queue_.size())); | 72 DCHECK_LT(priority, static_cast<int>(queue_.size())); |
| 74 queue_[priority].push_back(Item(id, batchable, size)); | 73 queue_[priority].push_back(Item(id, batchable, size)); |
| 75 } | 74 } |
| 76 | 75 |
| 77 void JobQueue::MarkFinished(JobID id) { | 76 void JobQueue::MarkFinished(JobID id) { |
| 78 size_t num_erased = running_.erase(id); | 77 size_t num_erased = running_.erase(id); |
| 79 DCHECK_EQ(1U, num_erased); | 78 DCHECK_EQ(1U, num_erased); |
| 80 } | 79 } |
| 81 | 80 |
| 82 std::string JobQueue::ToString() const { | 81 std::string JobQueue::ToString() const { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 100 for (auto it = queue_[i].begin(); it != queue_[i].end(); ++it) { | 99 for (auto it = queue_[i].begin(); it != queue_[i].end(); ++it) { |
| 101 if (it->id == id) { | 100 if (it->id == id) { |
| 102 queue_[i].erase(it); | 101 queue_[i].erase(it); |
| 103 break; | 102 break; |
| 104 } | 103 } |
| 105 } | 104 } |
| 106 } | 105 } |
| 107 } | 106 } |
| 108 | 107 |
| 109 } // namespace drive | 108 } // namespace drive |
| OLD | NEW |