blob: a772db422649b84483a6a17e2bf8284cd4993afe [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"
9
stephana195f62d2015-04-09 07:57:54 -070010#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 -070011 #include <sys/resource.h>
mtkleinafb43792014-08-19 15:55:55 -070012 int sk_tools::getMaxResidentSetSizeMB() {
halcanary0d154ee2014-08-11 11:33:51 -070013 struct rusage ru;
14 getrusage(RUSAGE_SELF, &ru);
15 #if defined(SK_BUILD_FOR_MAC)
mtkleinafb43792014-08-19 15:55:55 -070016 return static_cast<int>(ru.ru_maxrss / 1024 / 1024); // Darwin reports bytes.
halcanary0d154ee2014-08-11 11:33:51 -070017 #else
mtkleinafb43792014-08-19 15:55:55 -070018 return static_cast<int>(ru.ru_maxrss / 1024); // Linux reports kilobytes.
halcanary0d154ee2014-08-11 11:33:51 -070019 #endif
20 }
mtklein9abf4f82014-10-21 12:23:12 -070021#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 }
mtklein9abf4f82014-10-21 12:23:12 -070029#else
mtklein95553d92015-03-12 08:24:21 -070030 int sk_tools::getMaxResidentSetSizeMB() { return -1; }
31#endif
halcanary0d154ee2014-08-11 11:33:51 -070032
stephana195f62d2015-04-09 07:57:54 -070033#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
mtklein95553d92015-03-12 08:24:21 -070034 #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 }
mtkleineec84e32015-03-17 10:24:49 -070044#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);
mtkleinf1d6df72015-04-30 07:35:27 -070049 long long rssPages = 0;
mtkleineec84e32015-03-17 10:24:49 -070050 if (FILE* statm = fopen("/proc/self/statm", "r")) {
51 // statm contains: program-size rss shared text lib data dirty, all in page counts.
mtkleinf1d6df72015-04-30 07:35:27 -070052 int rc = fscanf(statm, "%*d %lld", &rssPages);
mtkleineec84e32015-03-17 10:24:49 -070053 fclose(statm);
54 if (rc != 1) {
55 return -1;
56 }
57 }
58 return rssPages * pageSize / 1024 / 1024;
59 }
60
mtklein95553d92015-03-12 08:24:21 -070061#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; }
halcanary0d154ee2014-08-11 11:33:51 -070069#endif