| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/socket/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.h" |
| 6 | 6 |
| 7 #include <math.h> |
| 8 #include <utility> |
| 9 #include "base/command_line.h" |
| 7 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 8 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 12 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 10 #include "base/metrics/stats_counters.h" | 14 #include "base/metrics/stats_counters.h" |
| 11 #include "base/stl_util-inl.h" | 15 #include "base/stl_util-inl.h" |
| 16 #include "base/string_number_conversions.h" |
| 12 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 13 #include "base/time.h" | 18 #include "base/time.h" |
| 14 #include "base/values.h" | 19 #include "base/values.h" |
| 15 #include "net/base/net_log.h" | 20 #include "net/base/net_log.h" |
| 16 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 17 #include "net/socket/client_socket_handle.h" | 22 #include "net/socket/client_socket_handle.h" |
| 18 | 23 |
| 19 using base::TimeDelta; | 24 using base::TimeDelta; |
| 20 | 25 |
| 21 namespace { | 26 namespace { |
| 22 | 27 |
| 23 // The timeout value, in seconds, used to clean up idle sockets that can't be | 28 // The timeout value, in seconds, used to clean up idle sockets that can't be |
| 24 // reused. | 29 // reused. |
| 25 // | 30 // |
| 26 // Note: It's important to close idle sockets that have received data as soon | 31 // Note: It's important to close idle sockets that have received data as soon |
| 27 // as possible because the received data may cause BSOD on Windows XP under | 32 // as possible because the received data may cause BSOD on Windows XP under |
| 28 // some conditions. See http://crbug.com/4606. | 33 // some conditions. See http://crbug.com/4606. |
| 29 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. | 34 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. |
| 30 | 35 |
| 31 // Indicate whether or not we should establish a new transport layer connection | 36 // Indicate whether or not we should establish a new transport layer connection |
| 32 // after a certain timeout has passed without receiving an ACK. | 37 // after a certain timeout has passed without receiving an ACK. |
| 33 bool g_connect_backup_jobs_enabled = true; | 38 bool g_connect_backup_jobs_enabled = true; |
| 34 | 39 |
| 40 // Alpha. |
| 41 double g_bytes_read_vs_last_accessed_alpha = 1000000; |
| 42 |
| 35 } // namespace | 43 } // namespace |
| 36 | 44 |
| 37 namespace net { | 45 namespace net { |
| 38 | 46 |
| 39 ConnectJob::ConnectJob(const std::string& group_name, | 47 ConnectJob::ConnectJob(const std::string& group_name, |
| 40 base::TimeDelta timeout_duration, | 48 base::TimeDelta timeout_duration, |
| 41 Delegate* delegate, | 49 Delegate* delegate, |
| 42 const BoundNetLog& net_log) | 50 const BoundNetLog& net_log) |
| 43 : group_name_(group_name), | 51 : group_name_(group_name), |
| 44 timeout_duration_(timeout_duration), | 52 timeout_duration_(timeout_duration), |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 } | 364 } |
| 357 } | 365 } |
| 358 | 366 |
| 359 return rv; | 367 return rv; |
| 360 } | 368 } |
| 361 | 369 |
| 362 bool ClientSocketPoolBaseHelper::AssignIdleSocketToGroup( | 370 bool ClientSocketPoolBaseHelper::AssignIdleSocketToGroup( |
| 363 const Request* request, Group* group) { | 371 const Request* request, Group* group) { |
| 364 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); | 372 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); |
| 365 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); | 373 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); |
| 374 double max_benefit = -100000000; |
| 375 std::pair<int, int64> best_pair; |
| 376 |
| 377 std::string debug_log(request->handle()->group_name()); |
| 378 std::string candidates; |
| 379 double rtt_ms = 0; |
| 366 | 380 |
| 367 // Iterate through the idle sockets forwards (oldest to newest) | 381 // Iterate through the idle sockets forwards (oldest to newest) |
| 368 // * Delete any disconnected ones. | 382 // * Delete any disconnected ones. |
| 369 // * If we find a used idle socket, assign to |idle_socket|. At the end, | 383 // * If we find a used idle socket, assign to |idle_socket|. At the end, |
| 370 // the |idle_socket_it| will be set to the newest used idle socket. | 384 // the |idle_socket_it| will be set to the newest used idle socket. |
| 371 for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); | 385 for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); |
| 372 it != idle_sockets->end();) { | 386 it != idle_sockets->end();) { |
| 373 if (!it->socket->IsConnectedAndIdle()) { | 387 if (!it->socket->IsConnectedAndIdle()) { |
| 374 DecrementIdleCount(); | 388 DecrementIdleCount(); |
| 375 delete it->socket; | 389 delete it->socket; |
| 376 it = idle_sockets->erase(it); | 390 it = idle_sockets->erase(it); |
| 377 continue; | 391 continue; |
| 378 } | 392 } |
| 379 | 393 |
| 380 if (it->socket->WasEverUsed()) { | 394 if (it->socket->WasEverUsed()) { |
| 381 // We found one we can reuse! | 395 // We found one we can reuse! |
| 382 idle_socket_it = it; | 396 int bytes_read = it->socket->NumBytesRead(); |
| 397 double B = static_cast<double>(bytes_read) / 1024.0; |
| 398 |
| 399 int64 idle_time = (base::TimeTicks::Now() - it->start_time) |
| 400 .InMicroseconds(); |
| 401 double idle_time_mins = static_cast<double>(idle_time) / 60000000.0; |
| 402 double T = pow(g_bytes_read_vs_last_accessed_alpha, idle_time_mins); |
| 403 |
| 404 debug_log = StringPrintf("%s\n%f\t%f\t%f", |
| 405 debug_log.data(), B, idle_time_mins, T); |
| 406 double benefit = B - T; |
| 407 |
| 408 // Equality to prefer recently used connection. |
| 409 if (benefit >= max_benefit) { |
| 410 idle_socket_it = it; |
| 411 max_benefit = benefit; |
| 412 best_pair = std::make_pair(bytes_read, idle_time); |
| 413 rtt_ms = it->socket->GetRTTInMs(); |
| 414 } |
| 383 } | 415 } |
| 384 | 416 |
| 385 ++it; | 417 ++it; |
| 386 } | 418 } |
| 419 debug_log = StringPrintf("%s\nChose socket <%d %lld>\nRtt = %f", |
| 420 debug_log.data(), |
| 421 best_pair.first, best_pair.second, |
| 422 rtt_ms); |
| 423 LOG(ERROR) << "\n\n----------------------------------------" << debug_log; |
| 387 | 424 |
| 388 // If we haven't found an idle socket, that means there are no used idle | 425 // If we haven't found an idle socket, that means there are no used idle |
| 389 // sockets. Pick the oldest (first) idle socket (FIFO). | 426 // sockets. Pick the oldest (first) idle socket (FIFO). |
| 390 | 427 |
| 391 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) | 428 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) |
| 392 idle_socket_it = idle_sockets->begin(); | 429 idle_socket_it = idle_sockets->begin(); |
| 393 | 430 |
| 394 if (idle_socket_it != idle_sockets->end()) { | 431 if (idle_socket_it != idle_sockets->end()) { |
| 395 DecrementIdleCount(); | 432 DecrementIdleCount(); |
| 396 base::TimeDelta idle_time = | 433 base::TimeDelta idle_time = |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 return g_connect_backup_jobs_enabled; | 679 return g_connect_backup_jobs_enabled; |
| 643 } | 680 } |
| 644 | 681 |
| 645 // static | 682 // static |
| 646 bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) { | 683 bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) { |
| 647 bool old_value = g_connect_backup_jobs_enabled; | 684 bool old_value = g_connect_backup_jobs_enabled; |
| 648 g_connect_backup_jobs_enabled = enabled; | 685 g_connect_backup_jobs_enabled = enabled; |
| 649 return old_value; | 686 return old_value; |
| 650 } | 687 } |
| 651 | 688 |
| 689 // static |
| 690 double ClientSocketPoolBaseHelper::set_bytes_read_vs_last_accessed_alpha( |
| 691 double alpha) { |
| 692 LOG(ERROR) << "Setting g_bytes_read_vs_last_accessed_alpha = " << alpha; |
| 693 double old_value = g_bytes_read_vs_last_accessed_alpha; |
| 694 g_bytes_read_vs_last_accessed_alpha = alpha; |
| 695 return old_value; |
| 696 } |
| 697 |
| 652 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { | 698 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { |
| 653 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; | 699 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; |
| 654 } | 700 } |
| 655 | 701 |
| 656 void ClientSocketPoolBaseHelper::IncrementIdleCount() { | 702 void ClientSocketPoolBaseHelper::IncrementIdleCount() { |
| 657 if (++idle_socket_count_ == 1) | 703 if (++idle_socket_count_ == 1) |
| 658 timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this, | 704 timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this, |
| 659 &ClientSocketPoolBaseHelper::OnCleanupTimerFired); | 705 &ClientSocketPoolBaseHelper::OnCleanupTimerFired); |
| 660 } | 706 } |
| 661 | 707 |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1081 // Delete active jobs. | 1127 // Delete active jobs. |
| 1082 STLDeleteElements(&jobs_); | 1128 STLDeleteElements(&jobs_); |
| 1083 | 1129 |
| 1084 // Cancel pending backup job. | 1130 // Cancel pending backup job. |
| 1085 method_factory_.RevokeAll(); | 1131 method_factory_.RevokeAll(); |
| 1086 } | 1132 } |
| 1087 | 1133 |
| 1088 } // namespace internal | 1134 } // namespace internal |
| 1089 | 1135 |
| 1090 } // namespace net | 1136 } // namespace net |
| OLD | NEW |