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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "tools/ProcStats.h"
|
2014-08-11 18:33:51 +00:00
|
|
|
|
2019-09-25 21:14:35 +00:00
|
|
|
#if defined(__Fuchsia__)
|
|
|
|
#include <zircon/process.h>
|
|
|
|
#include <zircon/syscalls.h>
|
|
|
|
#include <zircon/syscalls/object.h>
|
|
|
|
#include <zircon/types.h>
|
|
|
|
|
2020-07-08 18:12:43 +00:00
|
|
|
int64_t sk_tools::getMaxResidentSetSizeBytes() {
|
2019-09-25 21:14:35 +00:00
|
|
|
zx_info_task_stats_t task_stats;
|
|
|
|
zx_handle_t process = zx_process_self();
|
|
|
|
zx_status_t status = zx_object_get_info(
|
|
|
|
process, ZX_INFO_TASK_STATS, &task_stats, sizeof(task_stats), NULL, NULL);
|
|
|
|
if (status != ZX_OK) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-07-08 18:12:43 +00:00
|
|
|
return (task_stats.mem_private_bytes + task_stats.mem_shared_bytes);
|
2019-09-25 21:14:35 +00:00
|
|
|
}
|
|
|
|
#elif 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>
|
2020-07-08 18:12:43 +00:00
|
|
|
int64_t sk_tools::getMaxResidentSetSizeBytes() {
|
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)
|
2020-07-08 18:12:43 +00:00
|
|
|
return ru.ru_maxrss; // Darwin reports bytes.
|
2014-08-11 18:33:51 +00:00
|
|
|
#else
|
2020-07-08 18:12:43 +00:00
|
|
|
return 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>
|
2020-07-08 18:12:43 +00:00
|
|
|
int64_t sk_tools::getMaxResidentSetSizeBytes() {
|
2014-10-21 19:23:12 +00:00
|
|
|
PROCESS_MEMORY_COUNTERS info;
|
|
|
|
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
|
2020-07-08 18:12:43 +00:00
|
|
|
return info.PeakWorkingSetSize;
|
2014-10-21 19:23:12 +00:00
|
|
|
}
|
|
|
|
#else
|
2020-07-08 18:12:43 +00:00
|
|
|
int64_t sk_tools::getMaxResidentSetSizeBytes() { return -1; }
|
2015-03-12 15:24:21 +00:00
|
|
|
#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>
|
2020-07-08 18:12:43 +00:00
|
|
|
int64_t sk_tools::getCurrResidentSetSizeBytes() {
|
2015-03-12 15:24:21 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-07-08 18:12:43 +00:00
|
|
|
return info.resident_size;
|
2015-03-12 15:24:21 +00:00
|
|
|
}
|
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>
|
2020-07-08 18:12:43 +00:00
|
|
|
int64_t sk_tools::getCurrResidentSetSizeBytes() {
|
2015-03-17 17:24:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 18:12:43 +00:00
|
|
|
return rssPages * pageSize;
|
2015-03-17 17:24:49 +00:00
|
|
|
}
|
2018-01-24 17:42:55 +00:00
|
|
|
#elif defined(SK_BUILD_FOR_WIN)
|
2020-07-08 18:12:43 +00:00
|
|
|
int64_t sk_tools::getCurrResidentSetSizeBytes() {
|
2015-03-12 15:24:21 +00:00
|
|
|
PROCESS_MEMORY_COUNTERS info;
|
|
|
|
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
|
2020-07-08 18:12:43 +00:00
|
|
|
return info.WorkingSetSize;
|
2015-03-12 15:24:21 +00:00
|
|
|
}
|
|
|
|
#else
|
2020-07-08 18:12:43 +00:00
|
|
|
int64_t sk_tools::getCurrResidentSetSizeBytes() { return -1; }
|
2014-08-11 18:33:51 +00:00
|
|
|
#endif
|
2020-07-08 18:12:43 +00:00
|
|
|
|
|
|
|
int sk_tools::getMaxResidentSetSizeMB() {
|
|
|
|
int64_t bytes = sk_tools::getMaxResidentSetSizeBytes();
|
|
|
|
return bytes < 0 ? -1 : static_cast<int>(bytes / 1024 / 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sk_tools::getCurrResidentSetSizeMB() {
|
|
|
|
int64_t bytes = sk_tools::getCurrResidentSetSizeBytes();
|
|
|
|
return bytes < 0 ? -1 : static_cast<int>(bytes / 1024 / 1024);
|
|
|
|
}
|