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