halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "ProcStats.h" |
| 9 | |
stephana | 195f62d | 2015-04-09 07:57:54 -0700 | [diff] [blame] | 10 | #if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_ANDROID) |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 11 | #include <sys/resource.h> |
mtklein | afb4379 | 2014-08-19 15:55:55 -0700 | [diff] [blame] | 12 | int sk_tools::getMaxResidentSetSizeMB() { |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 13 | struct rusage ru; |
| 14 | getrusage(RUSAGE_SELF, &ru); |
| 15 | #if defined(SK_BUILD_FOR_MAC) |
mtklein | afb4379 | 2014-08-19 15:55:55 -0700 | [diff] [blame] | 16 | return static_cast<int>(ru.ru_maxrss / 1024 / 1024); // Darwin reports bytes. |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 17 | #else |
mtklein | afb4379 | 2014-08-19 15:55:55 -0700 | [diff] [blame] | 18 | return static_cast<int>(ru.ru_maxrss / 1024); // Linux reports kilobytes. |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 19 | #endif |
| 20 | } |
mtklein | 9abf4f8 | 2014-10-21 12:23:12 -0700 | [diff] [blame] | 21 | #elif defined(SK_BUILD_FOR_WIN32) |
| 22 | #include <windows.h> |
| 23 | #include <psapi.h> |
| 24 | int sk_tools::getMaxResidentSetSizeMB() { |
| 25 | PROCESS_MEMORY_COUNTERS info; |
| 26 | GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info)); |
| 27 | return static_cast<int>(info.PeakWorkingSetSize / 1024 / 1024); // Windows reports bytes. |
| 28 | } |
mtklein | 9abf4f8 | 2014-10-21 12:23:12 -0700 | [diff] [blame] | 29 | #else |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 30 | int sk_tools::getMaxResidentSetSizeMB() { return -1; } |
| 31 | #endif |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 32 | |
stephana | 195f62d | 2015-04-09 07:57:54 -0700 | [diff] [blame] | 33 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 34 | #include <mach/mach.h> |
| 35 | int sk_tools::getCurrResidentSetSizeMB() { |
| 36 | mach_task_basic_info info; |
| 37 | mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT; |
| 38 | if (KERN_SUCCESS != |
| 39 | task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count)) { |
| 40 | return -1; |
| 41 | } |
| 42 | return info.resident_size / 1024 / 1024; // Darwin reports bytes. |
| 43 | } |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 44 | #elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) // N.B. /proc is Linux-only. |
| 45 | #include <unistd.h> |
| 46 | #include <stdio.h> |
| 47 | int sk_tools::getCurrResidentSetSizeMB() { |
| 48 | const long pageSize = sysconf(_SC_PAGESIZE); |
mtklein | f1d6df7 | 2015-04-30 07:35:27 -0700 | [diff] [blame^] | 49 | long long rssPages = 0; |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 50 | if (FILE* statm = fopen("/proc/self/statm", "r")) { |
| 51 | // statm contains: program-size rss shared text lib data dirty, all in page counts. |
mtklein | f1d6df7 | 2015-04-30 07:35:27 -0700 | [diff] [blame^] | 52 | int rc = fscanf(statm, "%*d %lld", &rssPages); |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 53 | fclose(statm); |
| 54 | if (rc != 1) { |
| 55 | return -1; |
| 56 | } |
| 57 | } |
| 58 | return rssPages * pageSize / 1024 / 1024; |
| 59 | } |
| 60 | |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 61 | #elif defined(SK_BUILD_FOR_WIN32) |
| 62 | int sk_tools::getCurrResidentSetSizeMB() { |
| 63 | PROCESS_MEMORY_COUNTERS info; |
| 64 | GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info)); |
| 65 | return static_cast<int>(info.WorkingSetSize / 1024 / 1024); // Windows reports bytes. |
| 66 | } |
| 67 | #else |
| 68 | int sk_tools::getCurrResidentSetSizeMB() { return -1; } |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 69 | #endif |