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