2014-08-11 18:33:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ProcStats.h"
|
2016-07-27 20:55:26 +00:00
|
|
|
#include "SkTypes.h"
|
2014-08-11 18:33:51 +00:00
|
|
|
|
2015-04-09 14:57:54 +00:00
|
|
|
#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_ANDROID)
|
2014-08-11 18:33:51 +00:00
|
|
|
#include <sys/resource.h>
|
2014-08-19 22:55:55 +00:00
|
|
|
int sk_tools::getMaxResidentSetSizeMB() {
|
2014-08-11 18:33:51 +00:00
|
|
|
struct rusage ru;
|
|
|
|
getrusage(RUSAGE_SELF, &ru);
|
2017-02-06 15:41:11 +00:00
|
|
|
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
|
2014-08-19 22:55:55 +00:00
|
|
|
return static_cast<int>(ru.ru_maxrss / 1024 / 1024); // Darwin reports bytes.
|
2014-08-11 18:33:51 +00:00
|
|
|
#else
|
2014-08-19 22:55:55 +00:00
|
|
|
return static_cast<int>(ru.ru_maxrss / 1024); // Linux reports kilobytes.
|
2014-08-11 18:33:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
2018-01-24 17:42:55 +00:00
|
|
|
#elif defined(SK_BUILD_FOR_WIN)
|
2014-10-21 19:23:12 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <psapi.h>
|
|
|
|
int sk_tools::getMaxResidentSetSizeMB() {
|
|
|
|
PROCESS_MEMORY_COUNTERS info;
|
|
|
|
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
|
|
|
|
return static_cast<int>(info.PeakWorkingSetSize / 1024 / 1024); // Windows reports bytes.
|
|
|
|
}
|
|
|
|
#else
|
2015-03-12 15:24:21 +00:00
|
|
|
int sk_tools::getMaxResidentSetSizeMB() { return -1; }
|
|
|
|
#endif
|
2014-08-11 18:33:51 +00:00
|
|
|
|
2015-04-09 14:57:54 +00:00
|
|
|
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
|
2015-03-12 15:24:21 +00:00
|
|
|
#include <mach/mach.h>
|
|
|
|
int sk_tools::getCurrResidentSetSizeMB() {
|
|
|
|
mach_task_basic_info info;
|
|
|
|
mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
|
|
|
|
if (KERN_SUCCESS !=
|
|
|
|
task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return info.resident_size / 1024 / 1024; // Darwin reports bytes.
|
|
|
|
}
|
2015-03-17 17:24:49 +00:00
|
|
|
#elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) // N.B. /proc is Linux-only.
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
int sk_tools::getCurrResidentSetSizeMB() {
|
|
|
|
const long pageSize = sysconf(_SC_PAGESIZE);
|
2015-04-30 14:35:27 +00:00
|
|
|
long long rssPages = 0;
|
2015-03-17 17:24:49 +00:00
|
|
|
if (FILE* statm = fopen("/proc/self/statm", "r")) {
|
|
|
|
// statm contains: program-size rss shared text lib data dirty, all in page counts.
|
2015-04-30 14:35:27 +00:00
|
|
|
int rc = fscanf(statm, "%*d %lld", &rssPages);
|
2015-03-17 17:24:49 +00:00
|
|
|
fclose(statm);
|
|
|
|
if (rc != 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rssPages * pageSize / 1024 / 1024;
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:42:55 +00:00
|
|
|
#elif defined(SK_BUILD_FOR_WIN)
|
2015-03-12 15:24:21 +00:00
|
|
|
int sk_tools::getCurrResidentSetSizeMB() {
|
|
|
|
PROCESS_MEMORY_COUNTERS info;
|
|
|
|
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
|
|
|
|
return static_cast<int>(info.WorkingSetSize / 1024 / 1024); // Windows reports bytes.
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int sk_tools::getCurrResidentSetSizeMB() { return -1; }
|
2014-08-11 18:33:51 +00:00
|
|
|
#endif
|