blob: 5c7bb4670bb76e5a692e96e34a7bda29493f3ec5 [file] [log] [blame]
halcanary0d154ee2014-08-11 11:33:51 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
9#include "tools/ProcStats.h"
halcanary0d154ee2014-08-11 11:33:51 -070010
John Rosasco24cbdab2019-09-25 14:14:35 -070011#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
17 int sk_tools::getMaxResidentSetSizeMB() {
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 }
25 return (task_stats.mem_private_bytes + task_stats.mem_shared_bytes) / (1 << 20);
26 }
27#elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_ANDROID)
halcanary0d154ee2014-08-11 11:33:51 -070028 #include <sys/resource.h>
mtkleinafb43792014-08-19 15:55:55 -070029 int sk_tools::getMaxResidentSetSizeMB() {
halcanary0d154ee2014-08-11 11:33:51 -070030 struct rusage ru;
31 getrusage(RUSAGE_SELF, &ru);
Mike Kleinef46b2f2017-02-06 10:41:11 -050032 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
mtkleinafb43792014-08-19 15:55:55 -070033 return static_cast<int>(ru.ru_maxrss / 1024 / 1024); // Darwin reports bytes.
halcanary0d154ee2014-08-11 11:33:51 -070034 #else
mtkleinafb43792014-08-19 15:55:55 -070035 return static_cast<int>(ru.ru_maxrss / 1024); // Linux reports kilobytes.
halcanary0d154ee2014-08-11 11:33:51 -070036 #endif
37 }
Mike Klein8f11d4d2018-01-24 12:42:55 -050038#elif defined(SK_BUILD_FOR_WIN)
mtklein9abf4f82014-10-21 12:23:12 -070039 #include <windows.h>
40 #include <psapi.h>
41 int sk_tools::getMaxResidentSetSizeMB() {
42 PROCESS_MEMORY_COUNTERS info;
43 GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
44 return static_cast<int>(info.PeakWorkingSetSize / 1024 / 1024); // Windows reports bytes.
45 }
mtklein9abf4f82014-10-21 12:23:12 -070046#else
mtklein95553d92015-03-12 08:24:21 -070047 int sk_tools::getMaxResidentSetSizeMB() { return -1; }
48#endif
halcanary0d154ee2014-08-11 11:33:51 -070049
stephana195f62d2015-04-09 07:57:54 -070050#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
mtklein95553d92015-03-12 08:24:21 -070051 #include <mach/mach.h>
52 int sk_tools::getCurrResidentSetSizeMB() {
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 }
59 return info.resident_size / 1024 / 1024; // Darwin reports bytes.
60 }
mtkleineec84e32015-03-17 10:24:49 -070061#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>
64 int sk_tools::getCurrResidentSetSizeMB() {
65 const long pageSize = sysconf(_SC_PAGESIZE);
mtkleinf1d6df72015-04-30 07:35:27 -070066 long long rssPages = 0;
mtkleineec84e32015-03-17 10:24:49 -070067 if (FILE* statm = fopen("/proc/self/statm", "r")) {
68 // statm contains: program-size rss shared text lib data dirty, all in page counts.
mtkleinf1d6df72015-04-30 07:35:27 -070069 int rc = fscanf(statm, "%*d %lld", &rssPages);
mtkleineec84e32015-03-17 10:24:49 -070070 fclose(statm);
71 if (rc != 1) {
72 return -1;
73 }
74 }
75 return rssPages * pageSize / 1024 / 1024;
76 }
77
Mike Klein8f11d4d2018-01-24 12:42:55 -050078#elif defined(SK_BUILD_FOR_WIN)
mtklein95553d92015-03-12 08:24:21 -070079 int sk_tools::getCurrResidentSetSizeMB() {
80 PROCESS_MEMORY_COUNTERS info;
81 GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
82 return static_cast<int>(info.WorkingSetSize / 1024 / 1024); // Windows reports bytes.
83 }
84#else
85 int sk_tools::getCurrResidentSetSizeMB() { return -1; }
halcanary0d154ee2014-08-11 11:33:51 -070086#endif