Chromium Code Reviews| 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 // Decay coef. | |
| 41 double g_tcp_socket_estimated_cwnd_decay_coef = 0; | |
| 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, current_pair; | |
| 376 | |
| 377 std::string debug_log(request->handle()->group_name()); | |
| 378 double rtt_ms = 0; | |
| 366 | 379 |
| 367 // Iterate through the idle sockets forwards (oldest to newest) | 380 // Iterate through the idle sockets forwards (oldest to newest) |
| 368 // * Delete any disconnected ones. | 381 // * Delete any disconnected ones. |
| 369 // * If we find a used idle socket, assign to |idle_socket|. At the end, | 382 // * 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. | 383 // the |idle_socket_it| will be set to the newest used idle socket. |
| 371 for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); | 384 for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); |
| 372 it != idle_sockets->end();) { | 385 it != idle_sockets->end();) { |
| 373 if (!it->socket->IsConnectedAndIdle()) { | 386 if (!it->socket->IsConnectedAndIdle()) { |
| 374 DecrementIdleCount(); | 387 DecrementIdleCount(); |
| 375 delete it->socket; | 388 delete it->socket; |
| 376 it = idle_sockets->erase(it); | 389 it = idle_sockets->erase(it); |
| 377 continue; | 390 continue; |
| 378 } | 391 } |
| 379 | 392 |
| 380 if (it->socket->WasEverUsed()) { | 393 if (it->socket->WasEverUsed()) { |
| 381 // We found one we can reuse! | 394 // We found one we can reuse! |
| 382 idle_socket_it = it; | 395 double benefit = 0; |
|
Mike Belshe
2011/06/10 18:11:42
My thoughts on this function:
* You're calculatin
Gagan
2011/06/12 20:11:44
This function penalizes num_kb lesser than the old
| |
| 396 int64 bytes_read = it->socket->NumBytesRead(); | |
| 397 double num_kb = 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 | |
| 403 if (g_tcp_socket_estimated_cwnd_decay_coef > 0) { | |
| 404 double decay = pow(g_tcp_socket_estimated_cwnd_decay_coef, | |
| 405 idle_time_mins); | |
| 406 | |
| 407 debug_log = StringPrintf("%s\n%f\t%f\t%f", debug_log.data(), | |
| 408 num_kb, idle_time_mins, decay); | |
| 409 // Cwnd of a socket is estimated as the number of kilobytes | |
| 410 // transferred on it minus a decay function which is estimated as decay | |
| 411 // coef exponentiated by idle time in minutes. | |
| 412 // decay coef = 1 simulates no decay (cwnd validation disabled). | |
| 413 // decay coef = 1.83 simulates a decay of 20KB in 5 minutes. | |
| 414 benefit = num_kb - decay; | |
| 415 } | |
| 416 | |
| 417 // Equality to prefer recently used connection. | |
| 418 if (benefit >= max_benefit) { | |
| 419 idle_socket_it = it; | |
| 420 max_benefit = benefit; | |
| 421 best_pair = std::make_pair(bytes_read, idle_time); | |
| 422 rtt_ms = it->socket->GetConnectTimeMicros(); | |
| 423 } | |
| 383 } | 424 } |
| 384 | 425 |
| 385 ++it; | 426 ++it; |
| 386 } | 427 } |
| 428 debug_log = StringPrintf("%s\nChose socket <%d %ld>\nRtt = %f", | |
| 429 debug_log.data(), | |
| 430 best_pair.first, best_pair.second, | |
| 431 rtt_ms); | |
| 432 LOG(ERROR) << "\n\n----------------------------------------" << debug_log; | |
| 387 | 433 |
| 388 // If we haven't found an idle socket, that means there are no used idle | 434 // If we haven't found an idle socket, that means there are no used idle |
| 389 // sockets. Pick the oldest (first) idle socket (FIFO). | 435 // sockets. Pick the oldest (first) idle socket (FIFO). |
| 390 | 436 |
| 391 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) | 437 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) |
| 392 idle_socket_it = idle_sockets->begin(); | 438 idle_socket_it = idle_sockets->begin(); |
| 393 | 439 |
| 394 if (idle_socket_it != idle_sockets->end()) { | 440 if (idle_socket_it != idle_sockets->end()) { |
| 395 DecrementIdleCount(); | 441 DecrementIdleCount(); |
| 396 base::TimeDelta idle_time = | 442 base::TimeDelta idle_time = |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 642 return g_connect_backup_jobs_enabled; | 688 return g_connect_backup_jobs_enabled; |
| 643 } | 689 } |
| 644 | 690 |
| 645 // static | 691 // static |
| 646 bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) { | 692 bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) { |
| 647 bool old_value = g_connect_backup_jobs_enabled; | 693 bool old_value = g_connect_backup_jobs_enabled; |
| 648 g_connect_backup_jobs_enabled = enabled; | 694 g_connect_backup_jobs_enabled = enabled; |
| 649 return old_value; | 695 return old_value; |
| 650 } | 696 } |
| 651 | 697 |
| 698 // static | |
| 699 double ClientSocketPoolBaseHelper::SetSocketReusePolicy( | |
| 700 ClientSocketReusePolicy policy) { | |
| 701 double old_value = g_tcp_socket_estimated_cwnd_decay_coef; | |
| 702 g_tcp_socket_estimated_cwnd_decay_coef = | |
| 703 policy == USE_WARMEST_SOCKET ? 1 : | |
| 704 (policy == USE_WARM_SOCKET ? 1.83841629 : 0); | |
| 705 DLOG(INFO) << "Setting g_tcp_socket_estimated_cwnd_decay_coef = " | |
| 706 << g_tcp_socket_estimated_cwnd_decay_coef; | |
| 707 // TODO(gagan): Remove this. | |
| 708 LOG(ERROR) << "Setting g_tcp_socket_estimated_cwnd_decay_coef = " | |
| 709 << g_tcp_socket_estimated_cwnd_decay_coef; | |
| 710 return old_value; | |
| 711 } | |
| 712 | |
| 713 // static | |
| 714 double ClientSocketPoolBaseHelper::set_tcp_socket_estimated_cwnd_decay_coef( | |
| 715 double coef) { | |
| 716 DLOG(INFO) << "Setting g_tcp_socket_estimated_cwnd_decay_coef = " << coef; | |
| 717 LOG(ERROR) << "Setting g_tcp_socket_estimated_cwnd_decay_coef = " << coef; | |
| 718 double old_value = g_tcp_socket_estimated_cwnd_decay_coef; | |
| 719 g_tcp_socket_estimated_cwnd_decay_coef = coef; | |
| 720 return old_value; | |
| 721 } | |
| 722 | |
| 652 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { | 723 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { |
| 653 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; | 724 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; |
| 654 } | 725 } |
| 655 | 726 |
| 656 void ClientSocketPoolBaseHelper::IncrementIdleCount() { | 727 void ClientSocketPoolBaseHelper::IncrementIdleCount() { |
| 657 if (++idle_socket_count_ == 1) | 728 if (++idle_socket_count_ == 1) |
| 658 timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this, | 729 timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this, |
| 659 &ClientSocketPoolBaseHelper::OnCleanupTimerFired); | 730 &ClientSocketPoolBaseHelper::OnCleanupTimerFired); |
| 660 } | 731 } |
| 661 | 732 |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1081 // Delete active jobs. | 1152 // Delete active jobs. |
| 1082 STLDeleteElements(&jobs_); | 1153 STLDeleteElements(&jobs_); |
| 1083 | 1154 |
| 1084 // Cancel pending backup job. | 1155 // Cancel pending backup job. |
| 1085 method_factory_.RevokeAll(); | 1156 method_factory_.RevokeAll(); |
| 1086 } | 1157 } |
| 1087 | 1158 |
| 1088 } // namespace internal | 1159 } // namespace internal |
| 1089 | 1160 |
| 1090 } // namespace net | 1161 } // namespace net |
| OLD | NEW |