OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <errno.h> | 9 #include <errno.h> |
10 #include <fcntl.h> | 10 #include <fcntl.h> |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
443 // are together adding to more than one CPU's worth. | 443 // are together adding to more than one CPU's worth. |
444 int percentage = 100 * (cpu - last_cpu_) / | 444 int percentage = 100 * (cpu - last_cpu_) / |
445 (kHertz * TimeDelta::FromMicroseconds(time_delta).InSecondsF()); | 445 (kHertz * TimeDelta::FromMicroseconds(time_delta).InSecondsF()); |
446 | 446 |
447 last_time_ = time; | 447 last_time_ = time; |
448 last_cpu_ = cpu; | 448 last_cpu_ = cpu; |
449 | 449 |
450 return percentage; | 450 return percentage; |
451 } | 451 } |
452 | 452 |
453 namespace { | |
454 | |
455 // The format of /proc/meminfo is: | |
456 // | |
457 // MemTotal: 8235324 kB | |
458 // MemFree: 1628304 kB | |
459 // Buffers: 429596 kB | |
460 // Cached: 4728232 kB | |
461 // ... | |
462 const size_t kMemTotalIndex = 1; | |
463 const size_t kMemFreeIndex = 4; | |
464 const size_t kMemBuffersIndex = 7; | |
465 const size_t kMemCacheIndex = 10; | |
466 | |
467 } // namespace | |
468 | |
469 size_t GetSystemCommitCharge() { | |
470 // Used memory is: total - free - buffers - caches | |
471 FilePath meminfo_file("/proc/meminfo"); | |
472 std::string meminfo_data; | |
473 if (!file_util::ReadFileToString(meminfo_file, &meminfo_data)) | |
474 LOG(ERROR) << "Failed to open /proc/meminfo."; | |
vandebo (ex-Chrome)
2009/11/09 19:10:29
Should this be ERROR, or just WARNING? It doesn't
| |
475 return 0; | |
476 std::vector<std::string> meminfo_fields; | |
477 SplitStringAlongWhitespace(meminfo_data, &meminfo_fields); | |
478 | |
479 if (meminfo_fields.size() < kMemCacheIndex) { | |
480 LOG(ERROR) << "Failed to parse /proc/meminfo. Only found " << | |
481 meminfo_fields.size() << " fields."; | |
482 return 0; | |
483 } | |
484 | |
485 DCHECK_EQ(meminfo_fields[kMemTotalIndex-1], "MemTotal:"); | |
486 DCHECK_EQ(meminfo_fields[kMemFreeIndex-1], "MemFree:"); | |
487 DCHECK_EQ(meminfo_fields[kMemBuffersIndex-1], "Buffers:"); | |
488 DCHECK_EQ(meminfo_fields[kMemCacheIndex-1], "Cached:"); | |
489 | |
490 size_t result_in_kb; | |
491 result_in_kb = StringToInt(meminfo_fields[kMemTotalIndex]); | |
492 result_in_kb -= StringToInt(meminfo_fields[kMemFreeIndex]); | |
493 result_in_kb -= StringToInt(meminfo_fields[kMemBuffersIndex]); | |
494 result_in_kb -= StringToInt(meminfo_fields[kMemCacheIndex]); | |
495 | |
496 return result_in_kb; | |
497 } | |
498 | |
453 } // namespace base | 499 } // namespace base |
OLD | NEW |