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" 9 10 #if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_ANDROID) 11 #include <sys/resource.h> 12 int sk_tools::getMaxResidentSetSizeMB() { 13 struct rusage ru; 14 getrusage(RUSAGE_SELF, &ru); 15 #if defined(SK_BUILD_FOR_MAC) 16 return static_cast<int>(ru.ru_maxrss / 1024 / 1024); // Darwin reports bytes. 17 #else 18 return static_cast<int>(ru.ru_maxrss / 1024); // Linux reports kilobytes. 19 #endif 20 } 21 #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 } 29 #else 30 int sk_tools::getMaxResidentSetSizeMB() { return -1; } 31 #endif 32 33 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) 34 #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 } 44 #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); 49 long long rssPages = 0; 50 if (FILE* statm = fopen("/proc/self/statm", "r")) { 51 // statm contains: program-size rss shared text lib data dirty, all in page counts. 52 int rc = fscanf(statm, "%*d %lld", &rssPages); 53 fclose(statm); 54 if (rc != 1) { 55 return -1; 56 } 57 } 58 return rssPages * pageSize / 1024 / 1024; 59 } 60 61 #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; } 69 #endif 70