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 "base/process_util.h" | 5 #include "base/process_util.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 #include <dirent.h> | 8 #include <dirent.h> |
9 #include <dlfcn.h> | 9 #include <dlfcn.h> |
10 #include <errno.h> | 10 #include <errno.h> |
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 // The format of /proc/meminfo is: | 551 // The format of /proc/meminfo is: |
552 // | 552 // |
553 // MemTotal: 8235324 kB | 553 // MemTotal: 8235324 kB |
554 // MemFree: 1628304 kB | 554 // MemFree: 1628304 kB |
555 // Buffers: 429596 kB | 555 // Buffers: 429596 kB |
556 // Cached: 4728232 kB | 556 // Cached: 4728232 kB |
557 // ... | 557 // ... |
558 const size_t kMemTotalIndex = 1; | 558 const size_t kMemTotalIndex = 1; |
559 const size_t kMemFreeIndex = 4; | 559 const size_t kMemFreeIndex = 4; |
560 const size_t kMemBuffersIndex = 7; | 560 const size_t kMemBuffersIndex = 7; |
561 const size_t kMemCacheIndex = 10; | 561 const size_t kMemCachedIndex = 10; |
| 562 const size_t kMemActiveAnonIndex = 22; |
| 563 const size_t kMemInactiveAnonIndex = 25; |
562 | 564 |
563 } // namespace | 565 } // namespace |
564 | 566 |
565 bool GetSystemMemoryInfo(int* mem_total, int* mem_free, int* mem_buffers, | 567 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { |
566 int* mem_cache, int* shmem) { | |
567 // Synchronously reading files in /proc is safe. | 568 // Synchronously reading files in /proc is safe. |
568 base::ThreadRestrictions::ScopedAllowIO allow_io; | 569 base::ThreadRestrictions::ScopedAllowIO allow_io; |
569 | 570 |
570 // Used memory is: total - free - buffers - caches | 571 // Used memory is: total - free - buffers - caches |
571 FilePath meminfo_file("/proc/meminfo"); | 572 FilePath meminfo_file("/proc/meminfo"); |
572 std::string meminfo_data; | 573 std::string meminfo_data; |
573 if (!file_util::ReadFileToString(meminfo_file, &meminfo_data)) { | 574 if (!file_util::ReadFileToString(meminfo_file, &meminfo_data)) { |
574 LOG(WARNING) << "Failed to open /proc/meminfo."; | 575 LOG(WARNING) << "Failed to open /proc/meminfo."; |
575 return false; | 576 return false; |
576 } | 577 } |
577 std::vector<std::string> meminfo_fields; | 578 std::vector<std::string> meminfo_fields; |
578 SplitStringAlongWhitespace(meminfo_data, &meminfo_fields); | 579 SplitStringAlongWhitespace(meminfo_data, &meminfo_fields); |
579 | 580 |
580 if (meminfo_fields.size() < kMemCacheIndex) { | 581 if (meminfo_fields.size() < kMemCachedIndex) { |
581 LOG(WARNING) << "Failed to parse /proc/meminfo. Only found " << | 582 LOG(WARNING) << "Failed to parse /proc/meminfo. Only found " << |
582 meminfo_fields.size() << " fields."; | 583 meminfo_fields.size() << " fields."; |
583 return false; | 584 return false; |
584 } | 585 } |
585 | 586 |
586 DCHECK_EQ(meminfo_fields[kMemTotalIndex-1], "MemTotal:"); | 587 DCHECK_EQ(meminfo_fields[kMemTotalIndex-1], "MemTotal:"); |
587 DCHECK_EQ(meminfo_fields[kMemFreeIndex-1], "MemFree:"); | 588 DCHECK_EQ(meminfo_fields[kMemFreeIndex-1], "MemFree:"); |
588 DCHECK_EQ(meminfo_fields[kMemBuffersIndex-1], "Buffers:"); | 589 DCHECK_EQ(meminfo_fields[kMemBuffersIndex-1], "Buffers:"); |
589 DCHECK_EQ(meminfo_fields[kMemCacheIndex-1], "Cached:"); | 590 DCHECK_EQ(meminfo_fields[kMemCachedIndex-1], "Cached:"); |
| 591 DCHECK_EQ(meminfo_fields[kMemActiveAnonIndex-1], "Active(anon):"); |
| 592 DCHECK_EQ(meminfo_fields[kMemInactiveAnonIndex-1], "Inactive(anon):"); |
590 | 593 |
591 base::StringToInt(meminfo_fields[kMemTotalIndex], mem_total); | 594 base::StringToInt(meminfo_fields[kMemTotalIndex], &meminfo->total); |
592 base::StringToInt(meminfo_fields[kMemFreeIndex], mem_free); | 595 base::StringToInt(meminfo_fields[kMemFreeIndex], &meminfo->free); |
593 base::StringToInt(meminfo_fields[kMemBuffersIndex], mem_buffers); | 596 base::StringToInt(meminfo_fields[kMemBuffersIndex], &meminfo->buffers); |
594 base::StringToInt(meminfo_fields[kMemCacheIndex], mem_cache); | 597 base::StringToInt(meminfo_fields[kMemCachedIndex], &meminfo->cached); |
| 598 base::StringToInt(meminfo_fields[kMemActiveAnonIndex], &meminfo->active_anon); |
| 599 base::StringToInt(meminfo_fields[kMemInactiveAnonIndex], |
| 600 &meminfo->inactive_anon); |
595 #if defined(OS_CHROMEOS) | 601 #if defined(OS_CHROMEOS) |
596 // Chrome OS has a tweaked kernel that allows us to query Shmem, which is | 602 // Chrome OS has a tweaked kernel that allows us to query Shmem, which is |
597 // usually video memory otherwise invisible to the OS. Unfortunately, the | 603 // usually video memory otherwise invisible to the OS. Unfortunately, the |
598 // meminfo format varies on different hardware so we have to search for the | 604 // meminfo format varies on different hardware so we have to search for the |
599 // string. It always appears after "Cached:". | 605 // string. It always appears after "Cached:". |
600 for (size_t i = kMemCacheIndex+2; i < meminfo_fields.size(); i += 3) { | 606 for (size_t i = kMemCachedIndex+2; i < meminfo_fields.size(); i += 3) { |
601 if (meminfo_fields[i] == "Shmem:") { | 607 if (meminfo_fields[i] == "Shmem:") { |
602 base::StringToInt(meminfo_fields[i+1], shmem); | 608 base::StringToInt(meminfo_fields[i+1], &meminfo->shmem); |
603 break; | 609 break; |
604 } | 610 } |
605 } | 611 } |
606 #endif | 612 #endif |
607 return true; | 613 return true; |
608 } | 614 } |
609 | 615 |
610 size_t GetSystemCommitCharge() { | 616 size_t GetSystemCommitCharge() { |
611 int total, free, buffers, cache, shmem; | 617 SystemMemoryInfoKB meminfo; |
612 if (!GetSystemMemoryInfo(&total, &free, &buffers, &cache, &shmem)) | 618 if (!GetSystemMemoryInfo(&meminfo)) |
613 return 0; | 619 return 0; |
614 return total - free - buffers - cache; | 620 return meminfo.total - meminfo.free - meminfo.buffers - meminfo.cached; |
615 } | 621 } |
616 | 622 |
617 namespace { | 623 namespace { |
618 | 624 |
619 void OnNoMemorySize(size_t size) { | 625 void OnNoMemorySize(size_t size) { |
620 if (size != 0) | 626 if (size != 0) |
621 LOG(FATAL) << "Out of memory, size = " << size; | 627 LOG(FATAL) << "Out of memory, size = " << size; |
622 LOG(FATAL) << "Out of memory."; | 628 LOG(FATAL) << "Out of memory."; |
623 } | 629 } |
624 | 630 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 | 740 |
735 if (!file_util::PathExists(oom_adj)) | 741 if (!file_util::PathExists(oom_adj)) |
736 return false; | 742 return false; |
737 | 743 |
738 std::string score_str = base::IntToString(score); | 744 std::string score_str = base::IntToString(score); |
739 return (static_cast<int>(score_str.length()) == | 745 return (static_cast<int>(score_str.length()) == |
740 file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); | 746 file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); |
741 } | 747 } |
742 | 748 |
743 } // namespace base | 749 } // namespace base |
OLD | NEW |