Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: content/browser/renderer_host/render_process_host_impl.cc

Issue 2929113002: Enable spare RenderProcessHost to be preinitialized. (Closed)
Patch Set: tweaks Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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 // Represents the browser side of the browser <--> renderer communication 5 // Represents the browser side of the browser <--> renderer communication
6 // channel. There will be one RenderProcessHost per renderer process. 6 // channel. There will be one RenderProcessHost per renderer process.
7 7
8 #include "content/browser/renderer_host/render_process_host_impl.h" 8 #include "content/browser/renderer_host/render_process_host_impl.h"
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 void Release(int old_route_id) { 456 void Release(int old_route_id) {
457 session_storage_namespaces_awaiting_close_->erase(old_route_id); 457 session_storage_namespaces_awaiting_close_->erase(old_route_id);
458 } 458 }
459 459
460 private: 460 private:
461 std::unique_ptr<std::map<int, SessionStorageNamespaceMap>> 461 std::unique_ptr<std::map<int, SessionStorageNamespaceMap>>
462 session_storage_namespaces_awaiting_close_; 462 session_storage_namespaces_awaiting_close_;
463 DISALLOW_COPY_AND_ASSIGN(SessionStorageHolder); 463 DISALLOW_COPY_AND_ASSIGN(SessionStorageHolder);
464 }; 464 };
465 465
466 // This class manages spare RenderProcessHosts.
467 //
468 // There is a singleton instance of this class which manages a single spare
469 // renderer (g_spare_render_process_host_manager, below). This class
470 // encapsulates the implementation of
471 // RenderProcessHost::WarmupSpareRenderProcessHost()
472 //
473 // RenderProcessHostImpl should call
474 // SpareRenderProcessHostManager::MaybeTakeSpareRenderProcessHost when creating
475 // a new RPH. In this implementation, the spare renderer is bound to a
476 // BrowserContext and its default StoragePartition. If
477 // MaybeTakeSpareRenderProcessHost is called with a BrowserContext and
478 // StoragePartition that does not match, the spare renderer is discarded. In
479 // particular, only the default StoragePartition will be able to use a spare
480 // renderer. The spare renderer will also not be used as a guest renderer
481 // (is_for_guests_ == true).
482 //
483 // It is safe to call WarmupSpareRenderProcessHost multiple times, although if
484 // called in a context where the spare renderer is not likely to be used
485 // performance may suffer due to the unnecessary RPH creation.
486 class SpareRenderProcessHostManager : public RenderProcessHostObserver {
487 public:
488 SpareRenderProcessHostManager() {}
489
490 void WarmupSpareRenderProcessHost(BrowserContext* browser_context) {
491 StoragePartitionImpl* current_partition =
492 static_cast<StoragePartitionImpl*>(
493 BrowserContext::GetStoragePartition(browser_context, nullptr));
494
495 if (spare_render_process_host_ &&
496 matching_browser_context_ == browser_context &&
497 matching_storage_partition_ == current_partition)
498 return; // Nothing to warm up.
499
500 CleanupSpareRenderProcessHost();
501
502 // Don't create a spare renderer if we're running a single renderer or if
Charlie Reis 2017/06/29 21:09:17 nit: s/running a single renderer/using --single-pr
mattcary 2017/06/30 12:37:56 Done.
503 // we've got too many processes. See also ShouldTryToUseExistingProcessHost
504 // in this file.
505 if (RenderProcessHost::run_renderer_in_process() ||
506 g_all_hosts.Get().size() >=
507 RenderProcessHostImpl::GetMaxRendererProcessCount())
508 return;
509
510 matching_browser_context_ = browser_context;
511 matching_storage_partition_ = current_partition;
512
513 spare_render_process_host_ = RenderProcessHostImpl::CreateRenderProcessHost(
514 browser_context, current_partition, false /* is_for_guests_only */);
515 spare_render_process_host_->AddObserver(this);
516 spare_render_process_host_->Init();
517 }
518
519 RenderProcessHost* MaybeTakeSpareRenderProcessHost(
520 const BrowserContext* browser_context,
521 const StoragePartition* partition,
522 bool is_for_guests_only) {
523 if (!spare_render_process_host_ ||
524 browser_context != matching_browser_context_ ||
525 partition != matching_storage_partition_ || is_for_guests_only) {
526 // As a new RenderProcessHost will almost certainly be created, we cleanup
527 // the non-matching one so as not to waste resources.
528 CleanupSpareRenderProcessHost();
529 return nullptr;
530 }
531
532 RenderProcessHost* rph = spare_render_process_host_;
Charlie Reis 2017/06/29 21:09:16 As I mentioned earlier, I think we should include
mattcary 2017/06/30 12:37:56 Done. I had thought that was in the context of not
533 DropSpareRenderProcessHost(spare_render_process_host_);
534 return rph;
535 }
536
537 // Remove |host| as a possible spare renderer. Does not shut it down cleanly;
538 // the assumption is that the host was shutdown somewhere else and has
539 // notifying the SpareRenderProcessHostManager.
540 void DropSpareRenderProcessHost(RenderProcessHost* host) {
541 if (spare_render_process_host_ && spare_render_process_host_ == host) {
542 spare_render_process_host_->RemoveObserver(this);
543 spare_render_process_host_ = nullptr;
544 }
545 }
546
547 RenderProcessHost* spare_render_process_host() {
548 return spare_render_process_host_;
549 }
550
551 private:
552 // RenderProcessHostObserver
553 void RenderProcessWillExit(RenderProcessHost* host) override {
554 DropSpareRenderProcessHost(host);
555 }
556
557 void RenderProcessExited(RenderProcessHost* host,
558 base::TerminationStatus unused_status,
559 int unused_exit_code) override {
560 DropSpareRenderProcessHost(host);
561 }
562
563 void RenderProcessHostDestroyed(RenderProcessHost* host) override {
564 DropSpareRenderProcessHost(host);
565 }
566
567 void CleanupSpareRenderProcessHost() {
568 if (spare_render_process_host_) {
569 spare_render_process_host_->Cleanup();
570 DropSpareRenderProcessHost(spare_render_process_host_);
571 }
572 }
573
574 // This is a bare pointer, because RenderProcessHost manages the lifetime of
575 // all its instances; see g_all_hosts, above.
576 RenderProcessHost* spare_render_process_host_;
577
578 // Used only to check if a creation request matches the spare, and not
579 // accessed.
580 const BrowserContext* matching_browser_context_ = nullptr;
581 const StoragePartition* matching_storage_partition_ = nullptr;
582
583 DISALLOW_COPY_AND_ASSIGN(SpareRenderProcessHostManager);
584 };
585
586 base::LazyInstance<SpareRenderProcessHostManager>::Leaky
587 g_spare_render_process_host_manager = LAZY_INSTANCE_INITIALIZER;
588
466 const void* const kDefaultSubframeProcessHostHolderKey = 589 const void* const kDefaultSubframeProcessHostHolderKey =
467 &kDefaultSubframeProcessHostHolderKey; 590 &kDefaultSubframeProcessHostHolderKey;
468 591
469 class DefaultSubframeProcessHostHolder : public base::SupportsUserData::Data, 592 class DefaultSubframeProcessHostHolder : public base::SupportsUserData::Data,
470 public RenderProcessHostObserver { 593 public RenderProcessHostObserver {
471 public: 594 public:
472 explicit DefaultSubframeProcessHostHolder(BrowserContext* browser_context) 595 explicit DefaultSubframeProcessHostHolder(BrowserContext* browser_context)
473 : browser_context_(browser_context) {} 596 : browser_context_(browser_context) {}
474 ~DefaultSubframeProcessHostHolder() override {} 597 ~DefaultSubframeProcessHostHolder() override {}
475 598
476 // Gets the correct render process to use for this SiteInstance. 599 // Gets the correct render process to use for this SiteInstance.
477 RenderProcessHost* GetProcessHost(SiteInstance* site_instance, 600 RenderProcessHost* GetProcessHost(SiteInstance* site_instance,
478 bool is_for_guests_only) { 601 bool is_for_guests_only) {
479 StoragePartition* default_partition = 602 StoragePartitionImpl* default_partition =
480 BrowserContext::GetDefaultStoragePartition(browser_context_); 603 static_cast<StoragePartitionImpl*>(
481 StoragePartition* partition = 604 BrowserContext::GetDefaultStoragePartition(browser_context_));
482 BrowserContext::GetStoragePartition(browser_context_, site_instance); 605 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
606 BrowserContext::GetStoragePartition(browser_context_, site_instance));
483 607
484 // Is this the default storage partition? If it isn't, then just give it its 608 // Is this the default storage partition? If it isn't, then just give it its
485 // own non-shared process. 609 // own non-shared process.
486 if (partition != default_partition || is_for_guests_only) { 610 if (partition != default_partition || is_for_guests_only) {
487 RenderProcessHostImpl* host = new RenderProcessHostImpl( 611 RenderProcessHost* host = RenderProcessHostImpl::CreateRenderProcessHost(
488 browser_context_, static_cast<StoragePartitionImpl*>(partition), 612 browser_context_, partition, is_for_guests_only);
489 is_for_guests_only);
490 host->SetIsNeverSuitableForReuse(); 613 host->SetIsNeverSuitableForReuse();
491 return host; 614 return host;
492 } 615 }
493 616
494 // If we already have a shared host for the default storage partition, use 617 // If we already have a shared host for the default storage partition, use
495 // it. 618 // it.
496 if (host_) 619 if (host_)
497 return host_; 620 return host_;
498 621
499 host_ = new RenderProcessHostImpl( 622 host_ = RenderProcessHostImpl::CreateOrUseSpareRenderProcessHost(
500 browser_context_, static_cast<StoragePartitionImpl*>(partition), 623 browser_context_, partition, false /* is for guests only */);
501 false /* for guests only */);
502 host_->SetIsNeverSuitableForReuse(); 624 host_->SetIsNeverSuitableForReuse();
503 host_->AddObserver(this); 625 host_->AddObserver(this);
504 626
505 return host_; 627 return host_;
506 } 628 }
507 629
508 // Implementation of RenderProcessHostObserver. 630 // Implementation of RenderProcessHostObserver.
509 void RenderProcessHostDestroyed(RenderProcessHost* host) override { 631 void RenderProcessHostDestroyed(RenderProcessHost* host) override {
510 DCHECK_EQ(host_, host); 632 DCHECK_EQ(host_, host);
511 host_->RemoveObserver(this); 633 host_->RemoveObserver(this);
512 host_ = nullptr; 634 host_ = nullptr;
513 } 635 }
514 636
515 private: 637 private:
516 BrowserContext* browser_context_; 638 BrowserContext* browser_context_;
517 639
518 // The default subframe render process used for the default storage partition 640 // The default subframe render process used for the default storage partition
519 // of this BrowserContext. 641 // of this BrowserContext.
520 RenderProcessHostImpl* host_ = nullptr; 642 RenderProcessHost* host_ = nullptr;
521 }; 643 };
522 644
523 void CreateMemoryCoordinatorHandle( 645 void CreateMemoryCoordinatorHandle(
524 int render_process_id, 646 int render_process_id,
525 const service_manager::BindSourceInfo& source_info, 647 const service_manager::BindSourceInfo& source_info,
526 mojom::MemoryCoordinatorHandleRequest request) { 648 mojom::MemoryCoordinatorHandleRequest request) {
527 MemoryCoordinatorImpl::GetInstance()->CreateHandle(render_process_id, 649 MemoryCoordinatorImpl::GetInstance()->CreateHandle(render_process_id,
528 std::move(request)); 650 std::move(request));
529 } 651 }
530 652
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
1022 max_count = std::min(max_count, kMaxRendererProcessCount); 1144 max_count = std::min(max_count, kMaxRendererProcessCount);
1023 } 1145 }
1024 return max_count; 1146 return max_count;
1025 } 1147 }
1026 1148
1027 // static 1149 // static
1028 void RenderProcessHost::SetMaxRendererProcessCount(size_t count) { 1150 void RenderProcessHost::SetMaxRendererProcessCount(size_t count) {
1029 g_max_renderer_count_override = count; 1151 g_max_renderer_count_override = count;
1030 } 1152 }
1031 1153
1154 // static
1155 RenderProcessHost* RenderProcessHostImpl::CreateRenderProcessHost(
1156 BrowserContext* browser_context,
1157 StoragePartitionImpl* storage_partition_impl,
1158 bool is_for_guests_only) {
1159 if (g_render_process_host_factory_) {
1160 return g_render_process_host_factory_->CreateRenderProcessHost(
1161 browser_context);
1162 }
1163
1164 return new RenderProcessHostImpl(browser_context, storage_partition_impl,
1165 is_for_guests_only);
1166 }
1167
1168 // static
1169 RenderProcessHost* RenderProcessHostImpl::CreateOrUseSpareRenderProcessHost(
1170 BrowserContext* browser_context,
1171 StoragePartitionImpl* storage_partition_impl,
1172 bool is_for_guests_only) {
1173 RenderProcessHost* render_process_host =
1174 g_spare_render_process_host_manager.Get().MaybeTakeSpareRenderProcessHost(
1175 browser_context, storage_partition_impl, is_for_guests_only);
1176
1177 if (!render_process_host) {
1178 render_process_host = CreateRenderProcessHost(
1179 browser_context, storage_partition_impl, is_for_guests_only);
1180 }
1181
1182 DCHECK(render_process_host);
1183 return render_process_host;
1184 }
1185
1032 RenderProcessHostImpl::RenderProcessHostImpl( 1186 RenderProcessHostImpl::RenderProcessHostImpl(
1033 BrowserContext* browser_context, 1187 BrowserContext* browser_context,
1034 StoragePartitionImpl* storage_partition_impl, 1188 StoragePartitionImpl* storage_partition_impl,
1035 bool is_for_guests_only) 1189 bool is_for_guests_only)
1036 : fast_shutdown_started_(false), 1190 : fast_shutdown_started_(false),
1037 deleting_soon_(false), 1191 deleting_soon_(false),
1038 #ifndef NDEBUG 1192 #ifndef NDEBUG
1039 is_self_deleted_(false), 1193 is_self_deleted_(false),
1040 #endif 1194 #endif
1041 pending_views_(0), 1195 pending_views_(0),
(...skipping 1064 matching lines...) Expand 10 before | Expand all | Expand 10 after
2106 SiteProcessCountTracker* tracker = static_cast<SiteProcessCountTracker*>( 2260 SiteProcessCountTracker* tracker = static_cast<SiteProcessCountTracker*>(
2107 browser_context->GetUserData(kPendingSiteProcessCountTrackerKey)); 2261 browser_context->GetUserData(kPendingSiteProcessCountTrackerKey));
2108 if (!tracker) { 2262 if (!tracker) {
2109 tracker = new SiteProcessCountTracker(); 2263 tracker = new SiteProcessCountTracker();
2110 browser_context->SetUserData(kPendingSiteProcessCountTrackerKey, 2264 browser_context->SetUserData(kPendingSiteProcessCountTrackerKey,
2111 base::WrapUnique(tracker)); 2265 base::WrapUnique(tracker));
2112 } 2266 }
2113 tracker->DecrementSiteProcessCount(site_url, render_process_host->GetID()); 2267 tracker->DecrementSiteProcessCount(site_url, render_process_host->GetID());
2114 } 2268 }
2115 2269
2270 // static
2271 RenderProcessHost*
2272 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting() {
2273 return g_spare_render_process_host_manager.Get().spare_render_process_host();
2274 }
2275
2116 bool RenderProcessHostImpl::IsForGuestsOnly() const { 2276 bool RenderProcessHostImpl::IsForGuestsOnly() const {
2117 return is_for_guests_only_; 2277 return is_for_guests_only_;
2118 } 2278 }
2119 2279
2120 StoragePartition* RenderProcessHostImpl::GetStoragePartition() const { 2280 StoragePartition* RenderProcessHostImpl::GetStoragePartition() const {
2121 return storage_partition_impl_; 2281 return storage_partition_impl_;
2122 } 2282 }
2123 2283
2124 static void AppendCompositorCommandLineFlags(base::CommandLine* command_line) { 2284 static void AppendCompositorCommandLineFlags(base::CommandLine* command_line) {
2125 command_line->AppendSwitchASCII( 2285 command_line->AppendSwitchASCII(
(...skipping 934 matching lines...) Expand 10 before | Expand all | Expand 10 after
3060 // Otherwise, if this process has been used to host any other content, it 3220 // Otherwise, if this process has been used to host any other content, it
3061 // cannot be reused if the destination site indeed requires a dedicated 3221 // cannot be reused if the destination site indeed requires a dedicated
3062 // process and can be locked to just that site. 3222 // process and can be locked to just that site.
3063 return false; 3223 return false;
3064 } 3224 }
3065 3225
3066 return GetContentClient()->browser()->IsSuitableHost(host, site_url); 3226 return GetContentClient()->browser()->IsSuitableHost(host, site_url);
3067 } 3227 }
3068 3228
3069 // static 3229 // static
3230 void RenderProcessHost::WarmupSpareRenderProcessHost(
3231 content::BrowserContext* browser_context) {
3232 g_spare_render_process_host_manager.Get().WarmupSpareRenderProcessHost(
3233 browser_context);
3234 }
3235
3236 // static
3070 bool RenderProcessHost::run_renderer_in_process() { 3237 bool RenderProcessHost::run_renderer_in_process() {
3071 return g_run_renderer_in_process_; 3238 return g_run_renderer_in_process_;
3072 } 3239 }
3073 3240
3074 // static 3241 // static
3075 void RenderProcessHost::SetRunRendererInProcess(bool value) { 3242 void RenderProcessHost::SetRunRendererInProcess(bool value) {
3076 g_run_renderer_in_process_ = value; 3243 g_run_renderer_in_process_ = value;
3077 3244
3078 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 3245 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
3079 if (value) { 3246 if (value) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
3137 browser_context, site_url)) { 3304 browser_context, site_url)) {
3138 suitable_renderers.push_back(iter.GetCurrentValue()); 3305 suitable_renderers.push_back(iter.GetCurrentValue());
3139 } 3306 }
3140 iter.Advance(); 3307 iter.Advance();
3141 } 3308 }
3142 3309
3143 // Now pick a random suitable renderer, if we have any. 3310 // Now pick a random suitable renderer, if we have any.
3144 if (!suitable_renderers.empty()) { 3311 if (!suitable_renderers.empty()) {
3145 int suitable_count = static_cast<int>(suitable_renderers.size()); 3312 int suitable_count = static_cast<int>(suitable_renderers.size());
3146 int random_index = base::RandInt(0, suitable_count - 1); 3313 int random_index = base::RandInt(0, suitable_count - 1);
3314 g_spare_render_process_host_manager.Get().DropSpareRenderProcessHost(
3315 suitable_renderers[random_index]);
Charlie Reis 2017/06/29 21:09:16 Sure, that seems like an ok way to handle it. Ple
mattcary 2017/06/30 12:37:56 Done.
3147 return suitable_renderers[random_index]; 3316 return suitable_renderers[random_index];
3148 } 3317 }
3149 3318
3150 return NULL; 3319 return NULL;
3151 } 3320 }
3152 3321
3153 // static 3322 // static
3154 bool RenderProcessHost::ShouldUseProcessPerSite(BrowserContext* browser_context, 3323 bool RenderProcessHost::ShouldUseProcessPerSite(BrowserContext* browser_context,
3155 const GURL& url) { 3324 const GURL& url) {
3156 // Returns true if we should use the process-per-site model. This will be 3325 // Returns true if we should use the process-per-site model. This will be
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
3263 render_process_host = UnmatchedServiceWorkerProcessTracker::MatchWithSite( 3432 render_process_host = UnmatchedServiceWorkerProcessTracker::MatchWithSite(
3264 browser_context, site_url); 3433 browser_context, site_url);
3265 } 3434 }
3266 3435
3267 // If not (or if none found), see if we should reuse an existing process. 3436 // If not (or if none found), see if we should reuse an existing process.
3268 if (!render_process_host && 3437 if (!render_process_host &&
3269 ShouldTryToUseExistingProcessHost(browser_context, site_url)) { 3438 ShouldTryToUseExistingProcessHost(browser_context, site_url)) {
3270 render_process_host = GetExistingProcessHost(browser_context, site_url); 3439 render_process_host = GetExistingProcessHost(browser_context, site_url);
3271 } 3440 }
3272 3441
3273 // Otherwise (or if that fails), create a new one. 3442 // Before creating a new process, try to use a spare process if one is found.
3443 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
3444 BrowserContext::GetStoragePartition(browser_context, site_instance));
3274 if (!render_process_host) { 3445 if (!render_process_host) {
3275 if (g_render_process_host_factory_) { 3446 render_process_host = CreateOrUseSpareRenderProcessHost(
3276 render_process_host = 3447 browser_context, partition, is_for_guests_only);
3277 g_render_process_host_factory_->CreateRenderProcessHost(
3278 browser_context);
3279 } else {
3280 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
3281 BrowserContext::GetStoragePartition(browser_context, site_instance));
3282 render_process_host = new RenderProcessHostImpl(
3283 browser_context, partition, is_for_guests_only);
3284 }
3285 } 3448 }
3286 3449
3287 if (is_unmatched_service_worker) { 3450 if (is_unmatched_service_worker) {
3288 UnmatchedServiceWorkerProcessTracker::Register( 3451 UnmatchedServiceWorkerProcessTracker::Register(
3289 browser_context, render_process_host, site_url); 3452 browser_context, render_process_host, site_url);
3290 } 3453 }
3291 return render_process_host; 3454 return render_process_host;
3292 } 3455 }
3293 3456
3294 void RenderProcessHostImpl::CreateSharedRendererHistogramAllocator() { 3457 void RenderProcessHostImpl::CreateSharedRendererHistogramAllocator() {
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
3787 LOG(ERROR) << "Terminating render process for bad Mojo message: " << error; 3950 LOG(ERROR) << "Terminating render process for bad Mojo message: " << error;
3788 3951
3789 // The ReceivedBadMessage call below will trigger a DumpWithoutCrashing. 3952 // The ReceivedBadMessage call below will trigger a DumpWithoutCrashing.
3790 // Capture the error message in a crash key value. 3953 // Capture the error message in a crash key value.
3791 base::debug::ScopedCrashKey error_key_value("mojo-message-error", error); 3954 base::debug::ScopedCrashKey error_key_value("mojo-message-error", error);
3792 bad_message::ReceivedBadMessage(render_process_id, 3955 bad_message::ReceivedBadMessage(render_process_id,
3793 bad_message::RPH_MOJO_PROCESS_ERROR); 3956 bad_message::RPH_MOJO_PROCESS_ERROR);
3794 } 3957 }
3795 3958
3796 } // namespace content 3959 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698