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

Side by Side Diff: components/tracing/common/process_metrics_memory_dump_provider.cc

Issue 2839733004: Fill in PlatformPrivateFootprint on Linux (Closed)
Patch Set: remove extra } Created 3 years, 7 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
« no previous file with comments | « components/tracing/common/process_metrics_memory_dump_provider.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/tracing/common/process_metrics_memory_dump_provider.h" 5 #include "components/tracing/common/process_metrics_memory_dump_provider.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <map> 10 #include <map>
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 pmm->AddVMRegion(region); 173 pmm->AddVMRegion(region);
174 ++num_valid_regions; 174 ++num_valid_regions;
175 should_add_current_region = false; 175 should_add_current_region = false;
176 } 176 }
177 } 177 }
178 } 178 }
179 } 179 }
180 return num_valid_regions; 180 return num_valid_regions;
181 } 181 }
182 182
183 bool GetResidentSizeFromStatmFile(int fd, uint64_t* resident_pages) { 183 bool GetResidentAndSharedPagesFromStatmFile(int fd,
184 uint64_t* resident_pages,
185 uint64_t* shared_pages) {
184 lseek(fd, 0, SEEK_SET); 186 lseek(fd, 0, SEEK_SET);
185 char line[kMaxLineSize]; 187 char line[kMaxLineSize];
186 int res = read(fd, line, kMaxLineSize - 1); 188 int res = read(fd, line, kMaxLineSize - 1);
187 if (res <= 0) 189 if (res <= 0)
188 return false; 190 return false;
189 line[res] = '\0'; 191 line[res] = '\0';
190 int num_scanned = sscanf(line, "%*s %" SCNu64, resident_pages); 192 int num_scanned =
191 return num_scanned == 1; 193 sscanf(line, "%*s %" SCNu64 " %" SCNu64, resident_pages, shared_pages);
194 return num_scanned == 2;
192 } 195 }
193 196
194 #endif // defined(OS_LINUX) || defined(OS_ANDROID) 197 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
195 198
196 std::unique_ptr<base::ProcessMetrics> CreateProcessMetrics( 199 std::unique_ptr<base::ProcessMetrics> CreateProcessMetrics(
197 base::ProcessId process) { 200 base::ProcessId process) {
198 if (process == base::kNullProcessId) 201 if (process == base::kNullProcessId)
199 return base::ProcessMetrics::CreateCurrentProcessMetrics(); 202 return base::ProcessMetrics::CreateCurrentProcessMetrics();
200 #if defined(OS_LINUX) || defined(OS_ANDROID) 203 #if defined(OS_LINUX) || defined(OS_ANDROID)
201 // Just pass ProcessId instead of handle since they are the same in linux and 204 // Just pass ProcessId instead of handle since they are the same in linux and
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 #endif // defined(OS_MACOSX) 629 #endif // defined(OS_MACOSX)
627 if (rss_bytes_for_testing) 630 if (rss_bytes_for_testing)
628 rss_bytes = rss_bytes_for_testing; 631 rss_bytes = rss_bytes_for_testing;
629 632
630 // rss_bytes will be 0 if the process ended while dumping. 633 // rss_bytes will be 0 if the process ended while dumping.
631 if (!rss_bytes) 634 if (!rss_bytes)
632 return false; 635 return false;
633 636
634 uint64_t peak_rss_bytes = 0; 637 uint64_t peak_rss_bytes = 0;
635 638
639 #if defined(OS_LINUX) || defined(OS_ANDROID)
640 auto& footprint = pmd->process_totals()->GetPlatformPrivateFootprint();
641
642 base::ScopedFD autoclose;
643 int statm_fd = fast_polling_statm_fd_.get();
644 if (statm_fd == -1) {
645 autoclose = OpenStatm();
646 statm_fd = autoclose.get();
647 }
648 if (statm_fd == -1)
649 return false;
650 const static size_t page_size = base::GetPageSize();
651 uint64_t resident_pages;
652 uint64_t shared_pages;
653 bool success = GetResidentAndSharedPagesFromStatmFile(
654 statm_fd, &resident_pages, &shared_pages);
655 if (!success)
656 return false;
657
658 // TODO(hjd): Implement swap in the next CL.
659 footprint.rss_anon_bytes = (resident_pages - shared_pages) * page_size;
660 #endif // defined(OS_LINUX)
661
636 #if !defined(OS_IOS) 662 #if !defined(OS_IOS)
637 peak_rss_bytes = process_metrics_->GetPeakWorkingSetSize(); 663 peak_rss_bytes = process_metrics_->GetPeakWorkingSetSize();
638 #if defined(OS_LINUX) || defined(OS_ANDROID) 664 #if defined(OS_LINUX) || defined(OS_ANDROID)
639 if (is_rss_peak_resettable_) { 665 if (is_rss_peak_resettable_) {
640 std::string clear_refs_file = 666 std::string clear_refs_file =
641 "/proc/" + 667 "/proc/" +
642 (process_ == base::kNullProcessId ? "self" 668 (process_ == base::kNullProcessId ? "self"
643 : base::IntToString(process_)) + 669 : base::IntToString(process_)) +
644 "/clear_refs"; 670 "/clear_refs";
645 int clear_refs_fd = open(clear_refs_file.c_str(), O_WRONLY); 671 int clear_refs_fd = open(clear_refs_file.c_str(), O_WRONLY);
(...skipping 23 matching lines...) Expand all
669 #endif // !defined(OS_IOS) 695 #endif // !defined(OS_IOS)
670 696
671 pmd->process_totals()->set_resident_set_bytes(rss_bytes); 697 pmd->process_totals()->set_resident_set_bytes(rss_bytes);
672 pmd->set_has_process_totals(); 698 pmd->set_has_process_totals();
673 pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes); 699 pmd->process_totals()->set_peak_resident_set_bytes(peak_rss_bytes);
674 700
675 // Returns true even if other metrics failed, since rss is reported. 701 // Returns true even if other metrics failed, since rss is reported.
676 return true; 702 return true;
677 } 703 }
678 704
705 base::ScopedFD ProcessMetricsMemoryDumpProvider::OpenStatm() {
706 std::string name =
707 "/proc/" +
708 (process_ == base::kNullProcessId ? "self"
709 : base::IntToString(process_)) +
710 "/statm";
711 base::ScopedFD fd = base::ScopedFD(open(name.c_str(), O_RDONLY));
712 DCHECK(fd.is_valid());
713 return fd;
714 }
715
679 void ProcessMetricsMemoryDumpProvider::PollFastMemoryTotal( 716 void ProcessMetricsMemoryDumpProvider::PollFastMemoryTotal(
680 uint64_t* memory_total) { 717 uint64_t* memory_total) {
681 *memory_total = 0; 718 *memory_total = 0;
682 #if defined(OS_LINUX) || defined(OS_ANDROID) 719 #if defined(OS_LINUX) || defined(OS_ANDROID)
720
683 int statm_fd = fast_polling_statm_fd_for_testing; 721 int statm_fd = fast_polling_statm_fd_for_testing;
684 if (statm_fd == -1) { 722 if (statm_fd == -1) {
685 if (!fast_polling_statm_fd_.is_valid()) { 723 if (!fast_polling_statm_fd_.is_valid())
686 std::string name = "/proc/" + (process_ == base::kNullProcessId 724 fast_polling_statm_fd_ = OpenStatm();
687 ? "self"
688 : base::IntToString(process_)) +
689 "/statm";
690 fast_polling_statm_fd_.reset(open(name.c_str(), O_RDONLY));
691 DCHECK(fast_polling_statm_fd_.is_valid());
692 }
693 statm_fd = fast_polling_statm_fd_.get(); 725 statm_fd = fast_polling_statm_fd_.get();
726 if (statm_fd == -1)
727 return;
694 } 728 }
695 if (statm_fd == -1)
696 return;
697 729
698 uint64_t rss_pages = 0; 730 uint64_t rss_pages = 0;
Primiano Tucci (use gerrit) 2017/05/04 11:11:16 at this point call this "private_pages" for consis
hjd 2017/05/05 14:59:35 thanks, Used resident_pages to match arg name
699 if (!GetResidentSizeFromStatmFile(statm_fd, &rss_pages)) 731 uint64_t shared_pages = 0;
Primiano Tucci (use gerrit) 2017/05/04 11:11:16 and I'd call this "ignored" (or ignored_shared_pag
hjd 2017/05/05 14:59:35 Done.
732 if (!GetResidentAndSharedPagesFromStatmFile(statm_fd, &rss_pages,
733 &shared_pages))
700 return; 734 return;
701 735
702 static size_t page_size = base::GetPageSize(); 736 static size_t page_size = base::GetPageSize();
703 *memory_total = rss_pages * page_size; 737 *memory_total = rss_pages * page_size;
704 #else 738 #else
705 *memory_total = process_metrics_->GetWorkingSetSize(); 739 *memory_total = process_metrics_->GetWorkingSetSize();
706 #endif 740 #endif
707 } 741 }
708 742
709 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() { 743 void ProcessMetricsMemoryDumpProvider::SuspendFastMemoryPolling() {
710 #if defined(OS_LINUX) || defined(OS_ANDROID) 744 #if defined(OS_LINUX) || defined(OS_ANDROID)
711 fast_polling_statm_fd_.reset(); 745 fast_polling_statm_fd_.reset();
712 #endif 746 #endif
713 } 747 }
714 748
715 } // namespace tracing 749 } // namespace tracing
OLDNEW
« no previous file with comments | « components/tracing/common/process_metrics_memory_dump_provider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698