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

Side by Side Diff: base/process/process_metrics_linux.cc

Issue 1197243004: Replace some Tokenize calls with SplitString. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix android Created 5 years, 6 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 | « base/process/process_iterator_openbsd.cc ('k') | base/strings/string_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "base/process/process_metrics.h" 5 #include "base/process/process_metrics.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <sys/time.h> 10 #include <sys/time.h>
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 // Buffers: 429596 kB 556 // Buffers: 429596 kB
557 // Cached: 4728232 kB 557 // Cached: 4728232 kB
558 // ... 558 // ...
559 // There is no guarantee on the ordering or position 559 // There is no guarantee on the ordering or position
560 // though it doesn't appear to change very often 560 // though it doesn't appear to change very often
561 561
562 // As a basic sanity check, let's make sure we at least get non-zero 562 // As a basic sanity check, let's make sure we at least get non-zero
563 // MemTotal value 563 // MemTotal value
564 meminfo->total = 0; 564 meminfo->total = 0;
565 565
566 std::vector<std::string> meminfo_lines; 566 for (const StringPiece& line : SplitStringPiece(
567 Tokenize(meminfo_data, "\n", &meminfo_lines); 567 meminfo_data, "\n", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY)) {
568 for (std::vector<std::string>::iterator it = meminfo_lines.begin(); 568 std::vector<StringPiece> tokens = SplitStringPiece(
569 it != meminfo_lines.end(); ++it) { 569 line, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
570 std::vector<std::string> tokens;
571 SplitStringAlongWhitespace(*it, &tokens);
572 // HugePages_* only has a number and no suffix so we can't rely on 570 // HugePages_* only has a number and no suffix so we can't rely on
573 // there being exactly 3 tokens. 571 // there being exactly 3 tokens.
574 if (tokens.size() <= 1) { 572 if (tokens.size() <= 1) {
575 DLOG(WARNING) << "meminfo: tokens: " << tokens.size() 573 DLOG(WARNING) << "meminfo: tokens: " << tokens.size()
576 << " malformed line: " << *it; 574 << " malformed line: " << line.as_string();
577 continue; 575 continue;
578 } 576 }
579 577
580 int* target = NULL; 578 int* target = NULL;
581 if (tokens[0] == "MemTotal:") 579 if (tokens[0] == "MemTotal:")
582 target = &meminfo->total; 580 target = &meminfo->total;
583 else if (tokens[0] == "MemFree:") 581 else if (tokens[0] == "MemFree:")
584 target = &meminfo->free; 582 target = &meminfo->free;
585 else if (tokens[0] == "Buffers:") 583 else if (tokens[0] == "Buffers:")
586 target = &meminfo->buffers; 584 target = &meminfo->buffers;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 // 621 //
624 // nr_free_pages 299878 622 // nr_free_pages 299878
625 // nr_inactive_anon 239863 623 // nr_inactive_anon 239863
626 // nr_active_anon 1318966 624 // nr_active_anon 1318966
627 // nr_inactive_file 2015629 625 // nr_inactive_file 2015629
628 // ... 626 // ...
629 // 627 //
630 // We iterate through the whole file because the position of the 628 // We iterate through the whole file because the position of the
631 // fields are dependent on the kernel version and configuration. 629 // fields are dependent on the kernel version and configuration.
632 630
633 std::vector<std::string> vmstat_lines; 631 for (const StringPiece& line : SplitStringPiece(
634 Tokenize(vmstat_data, "\n", &vmstat_lines); 632 vmstat_data, "\n", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY)) {
635 for (std::vector<std::string>::iterator it = vmstat_lines.begin(); 633 std::vector<StringPiece> tokens = SplitStringPiece(
636 it != vmstat_lines.end(); ++it) { 634 line, " ", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
637 std::vector<std::string> tokens;
638 SplitString(*it, ' ', &tokens);
639 if (tokens.size() != 2) 635 if (tokens.size() != 2)
640 continue; 636 continue;
641 637
642 if (tokens[0] == "pswpin") { 638 if (tokens[0] == "pswpin") {
643 StringToInt(tokens[1], &meminfo->pswpin); 639 StringToInt(tokens[1], &meminfo->pswpin);
644 } else if (tokens[0] == "pswpout") { 640 } else if (tokens[0] == "pswpout") {
645 StringToInt(tokens[1], &meminfo->pswpout); 641 StringToInt(tokens[1], &meminfo->pswpout);
646 } else if (tokens[0] == "pgmajfault") { 642 } else if (tokens[0] == "pgmajfault") {
647 StringToInt(tokens[1], &meminfo->pgmajfault); 643 StringToInt(tokens[1], &meminfo->pgmajfault);
648 } 644 }
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 // Synchronously reading files in /proc does not hit the disk. 781 // Synchronously reading files in /proc does not hit the disk.
786 ThreadRestrictions::ScopedAllowIO allow_io; 782 ThreadRestrictions::ScopedAllowIO allow_io;
787 783
788 FilePath diskinfo_file("/proc/diskstats"); 784 FilePath diskinfo_file("/proc/diskstats");
789 std::string diskinfo_data; 785 std::string diskinfo_data;
790 if (!ReadFileToString(diskinfo_file, &diskinfo_data)) { 786 if (!ReadFileToString(diskinfo_file, &diskinfo_data)) {
791 DLOG(WARNING) << "Failed to open " << diskinfo_file.value(); 787 DLOG(WARNING) << "Failed to open " << diskinfo_file.value();
792 return false; 788 return false;
793 } 789 }
794 790
795 std::vector<std::string> diskinfo_lines; 791 std::vector<StringPiece> diskinfo_lines = SplitStringPiece(
796 size_t line_count = Tokenize(diskinfo_data, "\n", &diskinfo_lines); 792 diskinfo_data, "\n", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
797 if (line_count == 0) { 793 if (diskinfo_lines.size() == 0) {
798 DLOG(WARNING) << "No lines found"; 794 DLOG(WARNING) << "No lines found";
799 return false; 795 return false;
800 } 796 }
801 797
802 diskinfo->reads = 0; 798 diskinfo->reads = 0;
803 diskinfo->reads_merged = 0; 799 diskinfo->reads_merged = 0;
804 diskinfo->sectors_read = 0; 800 diskinfo->sectors_read = 0;
805 diskinfo->read_time = 0; 801 diskinfo->read_time = 0;
806 diskinfo->writes = 0; 802 diskinfo->writes = 0;
807 diskinfo->writes_merged = 0; 803 diskinfo->writes_merged = 0;
808 diskinfo->sectors_written = 0; 804 diskinfo->sectors_written = 0;
809 diskinfo->write_time = 0; 805 diskinfo->write_time = 0;
810 diskinfo->io = 0; 806 diskinfo->io = 0;
811 diskinfo->io_time = 0; 807 diskinfo->io_time = 0;
812 diskinfo->weighted_io_time = 0; 808 diskinfo->weighted_io_time = 0;
813 809
814 uint64 reads = 0; 810 uint64 reads = 0;
815 uint64 reads_merged = 0; 811 uint64 reads_merged = 0;
816 uint64 sectors_read = 0; 812 uint64 sectors_read = 0;
817 uint64 read_time = 0; 813 uint64 read_time = 0;
818 uint64 writes = 0; 814 uint64 writes = 0;
819 uint64 writes_merged = 0; 815 uint64 writes_merged = 0;
820 uint64 sectors_written = 0; 816 uint64 sectors_written = 0;
821 uint64 write_time = 0; 817 uint64 write_time = 0;
822 uint64 io = 0; 818 uint64 io = 0;
823 uint64 io_time = 0; 819 uint64 io_time = 0;
824 uint64 weighted_io_time = 0; 820 uint64 weighted_io_time = 0;
825 821
826 for (size_t i = 0; i < line_count; i++) { 822 for (const StringPiece& line : diskinfo_lines) {
827 std::vector<std::string> disk_fields; 823 std::vector<StringPiece> disk_fields = SplitStringPiece(
828 SplitStringAlongWhitespace(diskinfo_lines[i], &disk_fields); 824 line, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
829 825
830 // Fields may have overflowed and reset to zero. 826 // Fields may have overflowed and reset to zero.
831 if (IsValidDiskName(disk_fields[kDiskDriveName])) { 827 if (IsValidDiskName(disk_fields[kDiskDriveName].as_string())) {
832 StringToUint64(disk_fields[kDiskReads], &reads); 828 StringToUint64(disk_fields[kDiskReads], &reads);
833 StringToUint64(disk_fields[kDiskReadsMerged], &reads_merged); 829 StringToUint64(disk_fields[kDiskReadsMerged], &reads_merged);
834 StringToUint64(disk_fields[kDiskSectorsRead], &sectors_read); 830 StringToUint64(disk_fields[kDiskSectorsRead], &sectors_read);
835 StringToUint64(disk_fields[kDiskReadTime], &read_time); 831 StringToUint64(disk_fields[kDiskReadTime], &read_time);
836 StringToUint64(disk_fields[kDiskWrites], &writes); 832 StringToUint64(disk_fields[kDiskWrites], &writes);
837 StringToUint64(disk_fields[kDiskWritesMerged], &writes_merged); 833 StringToUint64(disk_fields[kDiskWritesMerged], &writes_merged);
838 StringToUint64(disk_fields[kDiskSectorsWritten], &sectors_written); 834 StringToUint64(disk_fields[kDiskSectorsWritten], &sectors_written);
839 StringToUint64(disk_fields[kDiskWriteTime], &write_time); 835 StringToUint64(disk_fields[kDiskWriteTime], &write_time);
840 StringToUint64(disk_fields[kDiskIO], &io); 836 StringToUint64(disk_fields[kDiskIO], &io);
841 StringToUint64(disk_fields[kDiskIOTime], &io_time); 837 StringToUint64(disk_fields[kDiskIOTime], &io_time);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 #if defined(OS_LINUX) 903 #if defined(OS_LINUX)
908 int ProcessMetrics::GetIdleWakeupsPerSecond() { 904 int ProcessMetrics::GetIdleWakeupsPerSecond() {
909 uint64 wake_ups; 905 uint64 wake_ups;
910 const char kWakeupStat[] = "se.statistics.nr_wakeups"; 906 const char kWakeupStat[] = "se.statistics.nr_wakeups";
911 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? 907 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ?
912 CalculateIdleWakeupsPerSecond(wake_ups) : 0; 908 CalculateIdleWakeupsPerSecond(wake_ups) : 0;
913 } 909 }
914 #endif // defined(OS_LINUX) 910 #endif // defined(OS_LINUX)
915 911
916 } // namespace base 912 } // namespace base
OLDNEW
« no previous file with comments | « base/process/process_iterator_openbsd.cc ('k') | base/strings/string_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698