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" |
mtklein | 25c81d4 | 2016-07-27 13:55:26 -0700 | [diff] [blame] | 9 | #include "SkTypes.h" |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 10 | |
stephana | 195f62d | 2015-04-09 07:57:54 -0700 | [diff] [blame] | 11 | #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] | 12 | #include <sys/resource.h> |
mtklein | afb4379 | 2014-08-19 15:55:55 -0700 | [diff] [blame] | 13 | int sk_tools::getMaxResidentSetSizeMB() { |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 14 | struct rusage ru; |
| 15 | getrusage(RUSAGE_SELF, &ru); |
Mike Klein | ef46b2f | 2017-02-06 10:41:11 -0500 | [diff] [blame] | 16 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
mtklein | afb4379 | 2014-08-19 15:55:55 -0700 | [diff] [blame] | 17 | return static_cast<int>(ru.ru_maxrss / 1024 / 1024); // Darwin reports bytes. |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 18 | #else |
mtklein | afb4379 | 2014-08-19 15:55:55 -0700 | [diff] [blame] | 19 | return static_cast<int>(ru.ru_maxrss / 1024); // Linux reports kilobytes. |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 20 | #endif |
| 21 | } |
mtklein | 9abf4f8 | 2014-10-21 12:23:12 -0700 | [diff] [blame] | 22 | #elif defined(SK_BUILD_FOR_WIN32) |
| 23 | #include <windows.h> |
| 24 | #include <psapi.h> |
| 25 | int sk_tools::getMaxResidentSetSizeMB() { |
| 26 | PROCESS_MEMORY_COUNTERS info; |
| 27 | GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info)); |
| 28 | return static_cast<int>(info.PeakWorkingSetSize / 1024 / 1024); // Windows reports bytes. |
| 29 | } |
mtklein | 9abf4f8 | 2014-10-21 12:23:12 -0700 | [diff] [blame] | 30 | #else |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 31 | int sk_tools::getMaxResidentSetSizeMB() { return -1; } |
| 32 | #endif |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 33 | |
stephana | 195f62d | 2015-04-09 07:57:54 -0700 | [diff] [blame] | 34 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 35 | #include <mach/mach.h> |
| 36 | int sk_tools::getCurrResidentSetSizeMB() { |
| 37 | mach_task_basic_info info; |
| 38 | mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT; |
| 39 | if (KERN_SUCCESS != |
| 40 | task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count)) { |
| 41 | return -1; |
| 42 | } |
| 43 | return info.resident_size / 1024 / 1024; // Darwin reports bytes. |
| 44 | } |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 45 | #elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) // N.B. /proc is Linux-only. |
| 46 | #include <unistd.h> |
| 47 | #include <stdio.h> |
| 48 | int sk_tools::getCurrResidentSetSizeMB() { |
| 49 | const long pageSize = sysconf(_SC_PAGESIZE); |
mtklein | f1d6df7 | 2015-04-30 07:35:27 -0700 | [diff] [blame] | 50 | long long rssPages = 0; |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 51 | if (FILE* statm = fopen("/proc/self/statm", "r")) { |
| 52 | // 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] | 53 | int rc = fscanf(statm, "%*d %lld", &rssPages); |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 54 | fclose(statm); |
| 55 | if (rc != 1) { |
| 56 | return -1; |
| 57 | } |
| 58 | } |
| 59 | return rssPages * pageSize / 1024 / 1024; |
| 60 | } |
| 61 | |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 62 | #elif defined(SK_BUILD_FOR_WIN32) |
| 63 | int sk_tools::getCurrResidentSetSizeMB() { |
| 64 | PROCESS_MEMORY_COUNTERS info; |
| 65 | GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info)); |
| 66 | return static_cast<int>(info.WorkingSetSize / 1024 / 1024); // Windows reports bytes. |
| 67 | } |
| 68 | #else |
| 69 | int sk_tools::getCurrResidentSetSizeMB() { return -1; } |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 70 | #endif |