blob: 51ddf55aa50af7339658f6a239f2e844186b0772 [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
8#include "ProcStats.h"
mtklein25c81d42016-07-27 13:55:26 -07009#include "SkTypes.h"
halcanary0d154ee2014-08-11 11:33:51 -070010
stephana195f62d2015-04-09 07:57:54 -070011#if 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 -070012 #include <sys/resource.h>
mtkleinafb43792014-08-19 15:55:55 -070013 int sk_tools::getMaxResidentSetSizeMB() {
halcanary0d154ee2014-08-11 11:33:51 -070014 struct rusage ru;
15 getrusage(RUSAGE_SELF, &ru);
Mike Kleinef46b2f2017-02-06 10:41:11 -050016 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
mtkleinafb43792014-08-19 15:55:55 -070017 return static_cast<int>(ru.ru_maxrss / 1024 / 1024); // Darwin reports bytes.
halcanary0d154ee2014-08-11 11:33:51 -070018 #else
mtkleinafb43792014-08-19 15:55:55 -070019 return static_cast<int>(ru.ru_maxrss / 1024); // Linux reports kilobytes.
halcanary0d154ee2014-08-11 11:33:51 -070020 #endif
21 }
mtklein9abf4f82014-10-21 12:23:12 -070022#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 }
mtklein9abf4f82014-10-21 12:23:12 -070030#else
mtklein95553d92015-03-12 08:24:21 -070031 int sk_tools::getMaxResidentSetSizeMB() { return -1; }
32#endif
halcanary0d154ee2014-08-11 11:33:51 -070033
stephana195f62d2015-04-09 07:57:54 -070034#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
mtklein95553d92015-03-12 08:24:21 -070035 #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 }
mtkleineec84e32015-03-17 10:24:49 -070045#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);
mtkleinf1d6df72015-04-30 07:35:27 -070050 long long rssPages = 0;
mtkleineec84e32015-03-17 10:24:49 -070051 if (FILE* statm = fopen("/proc/self/statm", "r")) {
52 // statm contains: program-size rss shared text lib data dirty, all in page counts.
mtkleinf1d6df72015-04-30 07:35:27 -070053 int rc = fscanf(statm, "%*d %lld", &rssPages);
mtkleineec84e32015-03-17 10:24:49 -070054 fclose(statm);
55 if (rc != 1) {
56 return -1;
57 }
58 }
59 return rssPages * pageSize / 1024 / 1024;
60 }
61
mtklein95553d92015-03-12 08:24:21 -070062#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; }
halcanary0d154ee2014-08-11 11:33:51 -070070#endif