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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
| 9 | #include "tools/ProcStats.h" |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 10 | |
John Rosasco | 24cbdab | 2019-09-25 14:14:35 -0700 | [diff] [blame] | 11 | #if defined(__Fuchsia__) |
| 12 | #include <zircon/process.h> |
| 13 | #include <zircon/syscalls.h> |
| 14 | #include <zircon/syscalls/object.h> |
| 15 | #include <zircon/types.h> |
| 16 | |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 17 | int64_t sk_tools::getMaxResidentSetSizeBytes() { |
John Rosasco | 24cbdab | 2019-09-25 14:14:35 -0700 | [diff] [blame] | 18 | zx_info_task_stats_t task_stats; |
| 19 | zx_handle_t process = zx_process_self(); |
| 20 | zx_status_t status = zx_object_get_info( |
| 21 | process, ZX_INFO_TASK_STATS, &task_stats, sizeof(task_stats), NULL, NULL); |
| 22 | if (status != ZX_OK) { |
| 23 | return -1; |
| 24 | } |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 25 | return (task_stats.mem_private_bytes + task_stats.mem_shared_bytes); |
John Rosasco | 24cbdab | 2019-09-25 14:14:35 -0700 | [diff] [blame] | 26 | } |
| 27 | #elif 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] | 28 | #include <sys/resource.h> |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 29 | int64_t sk_tools::getMaxResidentSetSizeBytes() { |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 30 | struct rusage ru; |
| 31 | getrusage(RUSAGE_SELF, &ru); |
Mike Klein | ef46b2f | 2017-02-06 10:41:11 -0500 | [diff] [blame] | 32 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 33 | return ru.ru_maxrss; // Darwin reports bytes. |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 34 | #else |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 35 | return ru.ru_maxrss * 1024; // Linux reports kilobytes. |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 36 | #endif |
| 37 | } |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 38 | #elif defined(SK_BUILD_FOR_WIN) |
mtklein | 9abf4f8 | 2014-10-21 12:23:12 -0700 | [diff] [blame] | 39 | #include <windows.h> |
| 40 | #include <psapi.h> |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 41 | int64_t sk_tools::getMaxResidentSetSizeBytes() { |
mtklein | 9abf4f8 | 2014-10-21 12:23:12 -0700 | [diff] [blame] | 42 | PROCESS_MEMORY_COUNTERS info; |
| 43 | GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info)); |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 44 | return info.PeakWorkingSetSize; |
mtklein | 9abf4f8 | 2014-10-21 12:23:12 -0700 | [diff] [blame] | 45 | } |
mtklein | 9abf4f8 | 2014-10-21 12:23:12 -0700 | [diff] [blame] | 46 | #else |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 47 | int64_t sk_tools::getMaxResidentSetSizeBytes() { return -1; } |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 48 | #endif |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 49 | |
stephana | 195f62d | 2015-04-09 07:57:54 -0700 | [diff] [blame] | 50 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 51 | #include <mach/mach.h> |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 52 | int64_t sk_tools::getCurrResidentSetSizeBytes() { |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 53 | mach_task_basic_info info; |
| 54 | mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT; |
| 55 | if (KERN_SUCCESS != |
| 56 | task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count)) { |
| 57 | return -1; |
| 58 | } |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 59 | return info.resident_size; |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 60 | } |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 61 | #elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) // N.B. /proc is Linux-only. |
| 62 | #include <unistd.h> |
| 63 | #include <stdio.h> |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 64 | int64_t sk_tools::getCurrResidentSetSizeBytes() { |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 65 | const long pageSize = sysconf(_SC_PAGESIZE); |
mtklein | f1d6df7 | 2015-04-30 07:35:27 -0700 | [diff] [blame] | 66 | long long rssPages = 0; |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 67 | if (FILE* statm = fopen("/proc/self/statm", "r")) { |
| 68 | // 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] | 69 | int rc = fscanf(statm, "%*d %lld", &rssPages); |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 70 | fclose(statm); |
| 71 | if (rc != 1) { |
| 72 | return -1; |
| 73 | } |
| 74 | } |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 75 | return rssPages * pageSize; |
mtklein | eec84e3 | 2015-03-17 10:24:49 -0700 | [diff] [blame] | 76 | } |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 77 | #elif defined(SK_BUILD_FOR_WIN) |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 78 | int64_t sk_tools::getCurrResidentSetSizeBytes() { |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 79 | PROCESS_MEMORY_COUNTERS info; |
| 80 | GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info)); |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 81 | return info.WorkingSetSize; |
mtklein | 95553d9 | 2015-03-12 08:24:21 -0700 | [diff] [blame] | 82 | } |
| 83 | #else |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 84 | int64_t sk_tools::getCurrResidentSetSizeBytes() { return -1; } |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 85 | #endif |
Brian Osman | 68870aa | 2020-07-08 14:12:43 -0400 | [diff] [blame] | 86 | |
| 87 | int sk_tools::getMaxResidentSetSizeMB() { |
| 88 | int64_t bytes = sk_tools::getMaxResidentSetSizeBytes(); |
| 89 | return bytes < 0 ? -1 : static_cast<int>(bytes / 1024 / 1024); |
| 90 | } |
| 91 | |
| 92 | int sk_tools::getCurrResidentSetSizeMB() { |
| 93 | int64_t bytes = sk_tools::getCurrResidentSetSizeBytes(); |
| 94 | return bytes < 0 ? -1 : static_cast<int>(bytes / 1024 / 1024); |
| 95 | } |