OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // See header file for description of class | 5 // See header file for description of class |
6 | 6 |
7 #include "chrome/browser/net/dns_master.h" | 7 #include "chrome/browser/net/dns_master.h" |
8 | 8 |
9 #include <sstream> | 9 #include <sstream> |
10 | 10 |
11 #include "base/histogram.h" | 11 #include "base/histogram.h" |
12 #include "base/stats_counters.h" | 12 #include "base/stats_counters.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/thread.h" | 14 #include "base/thread.h" |
15 #include "base/win_util.h" | 15 #include "base/win_util.h" |
16 #include "chrome/browser/net/dns_slave.h" | 16 #include "chrome/browser/net/dns_slave.h" |
17 | 17 |
18 using base::TimeDelta; | 18 using base::TimeDelta; |
19 | 19 |
20 namespace chrome_browser_net { | 20 namespace chrome_browser_net { |
21 | 21 |
22 DnsMaster::DnsMaster(TimeDelta shutdown_wait_time) | 22 DnsMaster::DnsMaster(TimeDelta shutdown_wait_time) |
23 : slave_count_(0), | 23 : slaves_have_work_(&lock_), |
| 24 slave_count_(0), |
| 25 running_slave_count_(0), |
24 shutdown_(false), | 26 shutdown_(false), |
25 running_slave_count_(0), | 27 kShutdownWaitTime_(shutdown_wait_time) { |
26 kShutdownWaitTime_(shutdown_wait_time), | 28 for (size_t i = 0; i < kSlaveCountMax; i++) { |
27 slaves_have_work_(&lock_) { | |
28 for ( int i = 0; i < kSlaveCountMax; i++ ) { | |
29 thread_ids_[i] = 0; | 29 thread_ids_[i] = 0; |
30 thread_handles_[i] = 0; | 30 thread_handles_[i] = 0; |
31 slaves_[i] = NULL; | 31 slaves_[i] = NULL; |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 // Overloaded Resolve() to take a vector of names. | 35 // Overloaded Resolve() to take a vector of names. |
36 void DnsMaster::ResolveList(const NameList& hostnames, | 36 void DnsMaster::ResolveList(const NameList& hostnames, |
37 DnsHostInfo::ResolutionMotivation motivation) { | 37 DnsHostInfo::ResolutionMotivation motivation) { |
38 bool need_to_signal = false; | 38 bool need_to_signal = false; |
39 { | 39 { |
40 AutoLock auto_lock(lock_); | 40 AutoLock auto_lock(lock_); |
41 if (shutdown_) return; | 41 if (shutdown_) return; |
42 if (slave_count_ < kSlaveCountMin) { | 42 if (slave_count_ < kSlaveCountMin) { |
43 for (int target_count = std::min(static_cast<int>(hostnames.size()), | 43 for (int target_count = std::min(hostnames.size(), kSlaveCountMin); |
44 kSlaveCountMin); | |
45 target_count > 0; | 44 target_count > 0; |
46 target_count--) | 45 target_count--) |
47 PreLockedCreateNewSlaveIfNeeded(); | 46 PreLockedCreateNewSlaveIfNeeded(); |
48 } else { | 47 } else { |
49 PreLockedCreateNewSlaveIfNeeded(); // Allocate one per list call. | 48 PreLockedCreateNewSlaveIfNeeded(); // Allocate one per list call. |
50 } | 49 } |
51 | 50 |
52 for (NameList::const_iterator it = hostnames.begin(); | 51 for (NameList::const_iterator it = hostnames.begin(); |
53 it < hostnames.end(); | 52 it < hostnames.end(); |
54 it++) { | 53 it++) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 } | 106 } |
108 | 107 |
109 DnsBenefit benefit = prefetched_host_info.AccruePrefetchBenefits( | 108 DnsBenefit benefit = prefetched_host_info.AccruePrefetchBenefits( |
110 navigation_info); | 109 navigation_info); |
111 switch (benefit) { | 110 switch (benefit) { |
112 case PREFETCH_NAME_FOUND: | 111 case PREFETCH_NAME_FOUND: |
113 case PREFETCH_NAME_NONEXISTANT: | 112 case PREFETCH_NAME_NONEXISTANT: |
114 // Remain under lock to push data. | 113 // Remain under lock to push data. |
115 cache_hits_.push_back(*navigation_info); | 114 cache_hits_.push_back(*navigation_info); |
116 if (referrer_based_prefetch) { | 115 if (referrer_based_prefetch) { |
117 std::string& motivating_referrer( | 116 std::string motivating_referrer( |
118 prefetched_host_info.referring_hostname()); | 117 prefetched_host_info.referring_hostname()); |
119 if (!motivating_referrer.empty()) { | 118 if (!motivating_referrer.empty()) { |
120 referrers_[motivating_referrer].AccrueValue( | 119 referrers_[motivating_referrer].AccrueValue( |
121 navigation_info->benefits_remaining(), hostname); | 120 navigation_info->benefits_remaining(), hostname); |
122 } | 121 } |
123 } | 122 } |
124 return true; | 123 return true; |
125 | 124 |
126 case PREFETCH_CACHE_EVICTION: | 125 case PREFETCH_CACHE_EVICTION: |
127 // Remain under lock to push data. | 126 // Remain under lock to push data. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 if (queued_info) { | 162 if (queued_info) { |
164 need_to_signal = true; | 163 need_to_signal = true; |
165 queued_info->SetReferringHostname(host_name); | 164 queued_info->SetReferringHostname(host_name); |
166 } | 165 } |
167 } | 166 } |
168 } | 167 } |
169 if (need_to_signal) | 168 if (need_to_signal) |
170 slaves_have_work_.Signal(); | 169 slaves_have_work_.Signal(); |
171 } | 170 } |
172 | 171 |
173 static char* PluralOptionalHostname(size_t count) { | |
174 if (count == 1) | |
175 return "hostname"; | |
176 return "hostnames"; | |
177 } | |
178 | |
179 // Provide sort order so all .com's are together, etc. | 172 // Provide sort order so all .com's are together, etc. |
180 struct RightToLeftStringSorter { | 173 struct RightToLeftStringSorter { |
181 bool operator()(const std::string& left, const std::string& right) const { | 174 bool operator()(const std::string& left, const std::string& right) const { |
182 if (left == right) return true; | 175 if (left == right) return true; |
183 size_t left_already_matched = left.size(); | 176 size_t left_already_matched = left.size(); |
184 size_t right_already_matched = right.size(); | 177 size_t right_already_matched = right.size(); |
185 | 178 |
186 // Ensure both strings have characters. | 179 // Ensure both strings have characters. |
187 if (!left_already_matched) return true; | 180 if (!left_already_matched) return true; |
188 if (!right_already_matched) return false; | 181 if (!right_already_matched) return false; |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 AutoLock auto_lock(lock_); // For map access (changing info values). | 396 AutoLock auto_lock(lock_); // For map access (changing info values). |
404 DnsHostInfo* info = &results_[hostname]; | 397 DnsHostInfo* info = &results_[hostname]; |
405 DCHECK(info->HasHostname(hostname)); | 398 DCHECK(info->HasHostname(hostname)); |
406 if (info->is_marked_to_delete()) | 399 if (info->is_marked_to_delete()) |
407 results_.erase(hostname); | 400 results_.erase(hostname); |
408 else | 401 else |
409 info->SetNoSuchNameState(); | 402 info->SetNoSuchNameState(); |
410 } | 403 } |
411 | 404 |
412 bool DnsMaster::PreLockedCreateNewSlaveIfNeeded() { | 405 bool DnsMaster::PreLockedCreateNewSlaveIfNeeded() { |
413 // Don't create more then max. | 406 // Don't create more than max. |
414 if (kSlaveCountMax <= slave_count_ || shutdown_) | 407 if (kSlaveCountMax <= slave_count_ || shutdown_) |
415 return false; | 408 return false; |
416 | 409 |
417 DnsSlave* slave_instance = new DnsSlave(this, slave_count_); | 410 DnsSlave* slave_instance = new DnsSlave(this, slave_count_); |
418 DWORD thread_id; | 411 DWORD thread_id; |
419 size_t stack_size = 0; | 412 size_t stack_size = 0; |
420 unsigned int flags = CREATE_SUSPENDED; | 413 unsigned int flags = CREATE_SUSPENDED; |
421 if (win_util::GetWinVersion() >= win_util::WINVERSION_XP) { | 414 if (win_util::GetWinVersion() >= win_util::WINVERSION_XP) { |
422 // 128kb stack size. | 415 // 128kb stack size. |
423 stack_size = 128*1024; | 416 stack_size = 128*1024; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 results_.clear(); | 532 results_.clear(); |
540 // Put back in the names being worked on by slaves. | 533 // Put back in the names being worked on by slaves. |
541 for (Results::iterator it = assignees.begin(); assignees.end() != it; ++it) { | 534 for (Results::iterator it = assignees.begin(); assignees.end() != it; ++it) { |
542 DCHECK(it->second.is_marked_to_delete()); | 535 DCHECK(it->second.is_marked_to_delete()); |
543 results_[it->first] = it->second; | 536 results_[it->first] = it->second; |
544 } | 537 } |
545 } | 538 } |
546 | 539 |
547 } // namespace chrome_browser_net | 540 } // namespace chrome_browser_net |
548 | 541 |
OLD | NEW |