OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "net/proxy/proxy_resolver_v8_tracing.h" | 5 #include "net/proxy/proxy_resolver_v8_tracing.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
| 12 #include "base/auto_reset.h" |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/metrics/histogram_macros.h" |
14 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
15 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
16 #include "base/synchronization/cancellation_flag.h" | 18 #include "base/synchronization/cancellation_flag.h" |
17 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
18 #include "base/thread_task_runner_handle.h" | 20 #include "base/thread_task_runner_handle.h" |
19 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
20 #include "base/threading/thread_restrictions.h" | 22 #include "base/threading/thread_restrictions.h" |
21 #include "net/base/address_list.h" | 23 #include "net/base/address_list.h" |
22 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
23 #include "net/base/network_interfaces.h" | 25 #include "net/base/network_interfaces.h" |
24 #include "net/dns/host_resolver.h" | 26 #include "net/dns/host_resolver.h" |
25 #include "net/proxy/proxy_info.h" | 27 #include "net/proxy/proxy_info.h" |
26 #include "net/proxy/proxy_resolver_error_observer.h" | 28 #include "net/proxy/proxy_resolver_error_observer.h" |
27 #include "net/proxy/proxy_resolver_v8.h" | 29 #include "net/proxy/proxy_resolver_v8.h" |
| 30 #include "url/url_canon.h" |
28 | 31 |
29 // The intent of this class is explained in the design document: | 32 // The intent of this class is explained in the design document: |
30 // https://docs.google.com/a/chromium.org/document/d/16Ij5OcVnR3s0MH4Z5XkhI9VTPo
MJdaBn9rKreAmGOdE/edit | 33 // https://docs.google.com/a/chromium.org/document/d/16Ij5OcVnR3s0MH4Z5XkhI9VTPo
MJdaBn9rKreAmGOdE/edit |
31 // | 34 // |
32 // In a nutshell, PAC scripts are Javascript programs and may depend on | 35 // In a nutshell, PAC scripts are Javascript programs and may depend on |
33 // network I/O, by calling functions like dnsResolve(). | 36 // network I/O, by calling functions like dnsResolve(). |
34 // | 37 // |
35 // This is problematic since functions such as dnsResolve() will block the | 38 // This is problematic since functions such as dnsResolve() will block the |
36 // Javascript execution until the DNS result is availble, thereby stalling the | 39 // Javascript execution until the DNS result is availble, thereby stalling the |
37 // PAC thread, which hurts the ability to process parallel proxy resolves. | 40 // PAC thread, which hurts the ability to process parallel proxy resolves. |
(...skipping 14 matching lines...) Expand all Loading... |
52 // number of DNS resolves, as well as scripts which are misbehaving | 55 // number of DNS resolves, as well as scripts which are misbehaving |
53 // under the tracing optimization. It is not expected to hit this normally. | 56 // under the tracing optimization. It is not expected to hit this normally. |
54 const size_t kMaxUniqueResolveDnsPerExec = 20; | 57 const size_t kMaxUniqueResolveDnsPerExec = 20; |
55 | 58 |
56 // Approximate number of bytes to use for buffering alerts() and errors. | 59 // Approximate number of bytes to use for buffering alerts() and errors. |
57 // This is a failsafe in case repeated executions of the script causes | 60 // This is a failsafe in case repeated executions of the script causes |
58 // too much memory bloat. It is not expected for well behaved scripts to | 61 // too much memory bloat. It is not expected for well behaved scripts to |
59 // hit this. (In fact normal scripts should not even have alerts() or errors). | 62 // hit this. (In fact normal scripts should not even have alerts() or errors). |
60 const size_t kMaxAlertsAndErrorsBytes = 2048; | 63 const size_t kMaxAlertsAndErrorsBytes = 2048; |
61 | 64 |
| 65 // Strips path information from the URL and replaces it with a |
| 66 // recognizable placeholder. |
| 67 // TODO(eroman): Remove when done gathering data for crbug.com/593759 |
| 68 GURL StripUrlForBug593759(const GURL& url) { |
| 69 GURL::Replacements replacements; |
| 70 replacements.SetPathStr("PathIsHiddenForCrbug593759"); |
| 71 replacements.ClearQuery(); |
| 72 replacements.ClearRef(); |
| 73 return url.ReplaceComponents(replacements); |
| 74 } |
| 75 |
| 76 // TODO(eroman): Remove when done gathering data for crbug.com/593759 |
| 77 void LogHistogramForBug593759(PacResultForStrippedUrl value) { |
| 78 UMA_HISTOGRAM_ENUMERATION( |
| 79 kHistogramPacResultForStrippedUrl, static_cast<int>(value), |
| 80 static_cast<int>(PacResultForStrippedUrl::MAX_VALUE)); |
| 81 } |
| 82 |
62 // The Job class is responsible for executing GetProxyForURL() and | 83 // The Job class is responsible for executing GetProxyForURL() and |
63 // creating ProxyResolverV8 instances, since both of these operations share | 84 // creating ProxyResolverV8 instances, since both of these operations share |
64 // similar code. | 85 // similar code. |
65 // | 86 // |
66 // The DNS for these operations can operate in either blocking or | 87 // The DNS for these operations can operate in either blocking or |
67 // non-blocking mode. Blocking mode is used as a fallback when the PAC script | 88 // non-blocking mode. Blocking mode is used as a fallback when the PAC script |
68 // seems to be misbehaving under the tracing optimization. | 89 // seems to be misbehaving under the tracing optimization. |
69 // | 90 // |
70 // Note that this class runs on both the origin thread and a worker | 91 // Note that this class runs on both the origin thread and a worker |
71 // thread. Most methods are expected to be used exclusively on one thread | 92 // thread. Most methods are expected to be used exclusively on one thread |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 void NotifyCaller(int result); | 163 void NotifyCaller(int result); |
143 void NotifyCallerOnOriginLoop(int result); | 164 void NotifyCallerOnOriginLoop(int result); |
144 | 165 |
145 void Start(Operation op, bool blocking_dns, | 166 void Start(Operation op, bool blocking_dns, |
146 const CompletionCallback& callback); | 167 const CompletionCallback& callback); |
147 | 168 |
148 void ExecuteBlocking(); | 169 void ExecuteBlocking(); |
149 void ExecuteNonBlocking(); | 170 void ExecuteNonBlocking(); |
150 int ExecuteProxyResolver(); | 171 int ExecuteProxyResolver(); |
151 | 172 |
| 173 // TODO(eroman): Remove when done gathering data for crbug.com/593759 |
| 174 void LogMetricsForBug593759(int original_error); |
| 175 |
152 // Implementation of ProxyResolverv8::JSBindings | 176 // Implementation of ProxyResolverv8::JSBindings |
153 bool ResolveDns(const std::string& host, | 177 bool ResolveDns(const std::string& host, |
154 ResolveDnsOperation op, | 178 ResolveDnsOperation op, |
155 std::string* output, | 179 std::string* output, |
156 bool* terminate) override; | 180 bool* terminate) override; |
157 void Alert(const base::string16& message) override; | 181 void Alert(const base::string16& message) override; |
158 void OnError(int line_number, const base::string16& error) override; | 182 void OnError(int line_number, const base::string16& error) override; |
159 | 183 |
160 bool ResolveDnsBlocking(const std::string& host, | 184 bool ResolveDnsBlocking(const std::string& host, |
161 ResolveDnsOperation op, | 185 ResolveDnsOperation op, |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 // Sequence of calls made to Alert() or OnError() by this execution. | 292 // Sequence of calls made to Alert() or OnError() by this execution. |
269 std::vector<AlertOrError> alerts_and_errors_; | 293 std::vector<AlertOrError> alerts_and_errors_; |
270 size_t alerts_and_errors_byte_cost_; // Approximate byte cost of the above. | 294 size_t alerts_and_errors_byte_cost_; // Approximate byte cost of the above. |
271 | 295 |
272 // Number of calls made to ResolveDns() by the PREVIOUS execution. | 296 // Number of calls made to ResolveDns() by the PREVIOUS execution. |
273 int last_num_dns_; | 297 int last_num_dns_; |
274 | 298 |
275 // Whether the current execution needs to be restarted in blocking mode. | 299 // Whether the current execution needs to be restarted in blocking mode. |
276 bool should_restart_with_blocking_dns_; | 300 bool should_restart_with_blocking_dns_; |
277 | 301 |
| 302 // TODO(eroman): Remove when done gathering data for crbug.com/593759 |
| 303 bool dont_start_dns_ = false; |
| 304 |
278 // --------------------------------------------------------------------------- | 305 // --------------------------------------------------------------------------- |
279 // State for pending DNS request. | 306 // State for pending DNS request. |
280 // --------------------------------------------------------------------------- | 307 // --------------------------------------------------------------------------- |
281 | 308 |
282 // Handle to the outstanding request in the HostResolver, or NULL. | 309 // Handle to the outstanding request in the HostResolver, or NULL. |
283 // This is mutated and used on the origin thread, however it may be read by | 310 // This is mutated and used on the origin thread, however it may be read by |
284 // the worker thread for some DCHECKS(). | 311 // the worker thread for some DCHECKS(). |
285 HostResolver::RequestHandle pending_dns_; | 312 HostResolver::RequestHandle pending_dns_; |
286 | 313 |
287 // Indicates if the outstanding DNS request completed synchronously. Written | 314 // Indicates if the outstanding DNS request completed synchronously. Written |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 | 553 |
527 // Reset state for the current execution. | 554 // Reset state for the current execution. |
528 abandoned_ = false; | 555 abandoned_ = false; |
529 num_dns_ = 0; | 556 num_dns_ = 0; |
530 alerts_and_errors_.clear(); | 557 alerts_and_errors_.clear(); |
531 alerts_and_errors_byte_cost_ = 0; | 558 alerts_and_errors_byte_cost_ = 0; |
532 should_restart_with_blocking_dns_ = false; | 559 should_restart_with_blocking_dns_ = false; |
533 | 560 |
534 int result = ExecuteProxyResolver(); | 561 int result = ExecuteProxyResolver(); |
535 | 562 |
| 563 // TODO(eroman): Remove when done gathering data for crbug.com/593759 |
| 564 LogMetricsForBug593759(result); |
| 565 |
536 if (should_restart_with_blocking_dns_) { | 566 if (should_restart_with_blocking_dns_) { |
537 DCHECK(!blocking_dns_); | 567 DCHECK(!blocking_dns_); |
538 DCHECK(abandoned_); | 568 DCHECK(abandoned_); |
539 blocking_dns_ = true; | 569 blocking_dns_ = true; |
540 ExecuteBlocking(); | 570 ExecuteBlocking(); |
541 return; | 571 return; |
542 } | 572 } |
543 | 573 |
544 if (abandoned_) | 574 if (abandoned_) |
545 return; | 575 return; |
(...skipping 19 matching lines...) Expand all Loading... |
565 // request were to be cancelled from the origin thread, must guarantee | 595 // request were to be cancelled from the origin thread, must guarantee |
566 // that |user_results_| is not accessed anymore. | 596 // that |user_results_| is not accessed anymore. |
567 &results_, this); | 597 &results_, this); |
568 break; | 598 break; |
569 } | 599 } |
570 } | 600 } |
571 | 601 |
572 return result; | 602 return result; |
573 } | 603 } |
574 | 604 |
| 605 // Gathers data on how often PAC scripts execute differently depending |
| 606 // on the URL path and parameters. |
| 607 // |
| 608 // TODO(eroman): Remove when done gathering data for crbug.com/593759 |
| 609 void Job::LogMetricsForBug593759(int original_error) { |
| 610 CheckIsOnWorkerThread(); |
| 611 |
| 612 DCHECK(!blocking_dns_); |
| 613 |
| 614 if (operation_ != GET_PROXY_FOR_URL || !url_.SchemeIsCryptographic()) { |
| 615 // Only interested in FindProxyForURL() invocations for cryptographic URL |
| 616 // schemes (https:// and wss://). |
| 617 return; |
| 618 } |
| 619 |
| 620 if (should_restart_with_blocking_dns_) { |
| 621 // The current instrumentation is limited to non-blocking DNS mode, for |
| 622 // simplicity. Fallback to blocking mode is possible for unusual PAC |
| 623 // scripts (non-deterministic, or relies on global state). |
| 624 LogHistogramForBug593759( |
| 625 PacResultForStrippedUrl::SKIPPED_FALLBACK_BLOCKING_DNS); |
| 626 return; |
| 627 } |
| 628 |
| 629 if (abandoned_) { |
| 630 // If the FindProxyForURL() attempt was abandoned, it was either cancelled |
| 631 // or it encountered a missing DNS dependency. In the latter case the job |
| 632 // will be re-started once the DNS has been resolved, so just wait until |
| 633 // then. |
| 634 return; |
| 635 } |
| 636 |
| 637 if (original_error != OK) { |
| 638 // Only run the extra diagnostics for successful invocations of |
| 639 // FindProxyForURL(). In other words, the instrumentation will |
| 640 // not check whether the script succeeds when using a stripped |
| 641 // path after having already failed on the original URL. A script error |
| 642 // means the PAC script is already broken, so this would just skew the data. |
| 643 return; |
| 644 } |
| 645 |
| 646 // Save some state variables to compare the original run against the new run |
| 647 // using a stripped URL. |
| 648 auto original_num_dns = num_dns_; |
| 649 auto original_alerts_and_errors_byte_cost_ = alerts_and_errors_byte_cost_; |
| 650 auto original_results = results_.ToPacString(); |
| 651 |
| 652 // Reset state variables used by ExecuteProxyResolver(). |
| 653 // |
| 654 // This is a bit messy, but it keeps the diagnostics code local |
| 655 // to LogMetricsForBug593759() without having to refactor the existing |
| 656 // code. |
| 657 // |
| 658 // The intent is that after returning from this function all of the |
| 659 // internal state is re-set to reflect the original completion of |
| 660 // ExecuteProxyResolver(), not the second diagnostic one. |
| 661 // |
| 662 // Any global modifications made to the script state however are |
| 663 // not undone, since creating a new script context just for this |
| 664 // test would be expensive. |
| 665 // |
| 666 // Lastly, DNS resolution is disabled before calling |
| 667 // ExecuteProxyResolver(), so only previously cached results can be |
| 668 // used. |
| 669 base::AutoReset<GURL> reset_url(&url_, StripUrlForBug593759(url_)); |
| 670 base::AutoReset<int> reset_num_dns(&num_dns_, 0); |
| 671 base::AutoReset<std::vector<AlertOrError>> reset_alerts_and_errors( |
| 672 &alerts_and_errors_, std::vector<AlertOrError>()); |
| 673 base::AutoReset<size_t> reset_alerts_and_errors_byte_cost( |
| 674 &alerts_and_errors_byte_cost_, 0); |
| 675 base::AutoReset<bool> reset_should_restart_with_blocking_dns( |
| 676 &should_restart_with_blocking_dns_, false); |
| 677 base::AutoReset<ProxyInfo> reset_results(&results_, ProxyInfo()); |
| 678 base::AutoReset<bool> reset_dont_start_dns(&dont_start_dns_, true); |
| 679 base::AutoReset<bool> reset_abandoned(&abandoned_, false); |
| 680 |
| 681 // Re-run FindProxyForURL(). |
| 682 auto result = ExecuteProxyResolver(); |
| 683 |
| 684 // Log the result of having run FindProxyForURL() with the modified |
| 685 // URL to an UMA histogram. |
| 686 if (should_restart_with_blocking_dns_) { |
| 687 LogHistogramForBug593759( |
| 688 PacResultForStrippedUrl::FAIL_FALLBACK_BLOCKING_DNS); |
| 689 } else if (abandoned_) { |
| 690 LogHistogramForBug593759(PacResultForStrippedUrl::FAIL_ABANDONED); |
| 691 } else if (result != OK) { |
| 692 LogHistogramForBug593759(PacResultForStrippedUrl::FAIL_ERROR); |
| 693 } else if (original_results != results_.ToPacString()) { |
| 694 LogHistogramForBug593759( |
| 695 PacResultForStrippedUrl::FAIL_DIFFERENT_PROXY_LIST); |
| 696 } else if (original_alerts_and_errors_byte_cost_ != |
| 697 alerts_and_errors_byte_cost_) { |
| 698 // Here alerts_and_errors_byte_cost_ is being used as a cheap (albeit |
| 699 // imprecise) fingerprint for the calls that were made to alert(). |
| 700 LogHistogramForBug593759(PacResultForStrippedUrl::SUCCESS_DIFFERENT_ALERTS); |
| 701 } else if (original_num_dns != num_dns_) { |
| 702 LogHistogramForBug593759( |
| 703 PacResultForStrippedUrl::SUCCESS_DIFFERENT_NUM_DNS); |
| 704 } else { |
| 705 LogHistogramForBug593759(PacResultForStrippedUrl::SUCCESS); |
| 706 } |
| 707 } |
| 708 |
575 bool Job::ResolveDns(const std::string& host, | 709 bool Job::ResolveDns(const std::string& host, |
576 ResolveDnsOperation op, | 710 ResolveDnsOperation op, |
577 std::string* output, | 711 std::string* output, |
578 bool* terminate) { | 712 bool* terminate) { |
579 if (cancelled_.IsSet()) { | 713 if (cancelled_.IsSet()) { |
580 *terminate = true; | 714 *terminate = true; |
581 return false; | 715 return false; |
582 } | 716 } |
583 | 717 |
584 if ((op == DNS_RESOLVE || op == DNS_RESOLVE_EX) && host.empty()) { | 718 if ((op == DNS_RESOLVE || op == DNS_RESOLVE_EX) && host.empty()) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 | 773 |
640 num_dns_ += 1; | 774 num_dns_ += 1; |
641 | 775 |
642 // Check if the DNS result for this host has already been cached. | 776 // Check if the DNS result for this host has already been cached. |
643 bool rv; | 777 bool rv; |
644 if (GetDnsFromLocalCache(host, op, output, &rv)) { | 778 if (GetDnsFromLocalCache(host, op, output, &rv)) { |
645 // Yay, cache hit! | 779 // Yay, cache hit! |
646 return rv; | 780 return rv; |
647 } | 781 } |
648 | 782 |
| 783 // TODO(eroman): Remove when done gathering data for crbug.com/593759 |
| 784 if (dont_start_dns_) { |
| 785 abandoned_ = true; |
| 786 return false; |
| 787 } |
| 788 |
649 if (num_dns_ <= last_num_dns_) { | 789 if (num_dns_ <= last_num_dns_) { |
650 // The sequence of DNS operations is different from last time! | 790 // The sequence of DNS operations is different from last time! |
651 ScheduleRestartWithBlockingDns(); | 791 ScheduleRestartWithBlockingDns(); |
652 *terminate = true; | 792 *terminate = true; |
653 return false; | 793 return false; |
654 } | 794 } |
655 | 795 |
656 if (dns_cache_.size() >= kMaxUniqueResolveDnsPerExec) { | 796 if (dns_cache_.size() >= kMaxUniqueResolveDnsPerExec) { |
657 // Safety net for scripts with unexpectedly many DNS calls. | 797 // Safety net for scripts with unexpectedly many DNS calls. |
658 return false; | 798 return false; |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1092 } | 1232 } |
1093 | 1233 |
1094 } // namespace | 1234 } // namespace |
1095 | 1235 |
1096 // static | 1236 // static |
1097 scoped_ptr<ProxyResolverV8TracingFactory> | 1237 scoped_ptr<ProxyResolverV8TracingFactory> |
1098 ProxyResolverV8TracingFactory::Create() { | 1238 ProxyResolverV8TracingFactory::Create() { |
1099 return make_scoped_ptr(new ProxyResolverV8TracingFactoryImpl()); | 1239 return make_scoped_ptr(new ProxyResolverV8TracingFactoryImpl()); |
1100 } | 1240 } |
1101 | 1241 |
| 1242 const char kHistogramPacResultForStrippedUrl[] = "Net.PacResultForStrippedUrl"; |
| 1243 |
1102 } // namespace net | 1244 } // namespace net |
OLD | NEW |