xref: /aosp_15_r20/external/skia/tools/ProcStats.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2014 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker  *
4*c8dee2aaSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker  * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker  */
7*c8dee2aaSAndroid Build Coastguard Worker 
8*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkTypes.h"
9*c8dee2aaSAndroid Build Coastguard Worker #include "tools/ProcStats.h"
10*c8dee2aaSAndroid Build Coastguard Worker 
11*c8dee2aaSAndroid Build Coastguard Worker #if defined(__Fuchsia__)
12*c8dee2aaSAndroid Build Coastguard Worker     #include <zircon/process.h>
13*c8dee2aaSAndroid Build Coastguard Worker     #include <zircon/syscalls.h>
14*c8dee2aaSAndroid Build Coastguard Worker     #include <zircon/syscalls/object.h>
15*c8dee2aaSAndroid Build Coastguard Worker     #include <zircon/types.h>
16*c8dee2aaSAndroid Build Coastguard Worker 
getMaxResidentSetSizeBytes()17*c8dee2aaSAndroid Build Coastguard Worker     int64_t sk_tools::getMaxResidentSetSizeBytes() {
18*c8dee2aaSAndroid Build Coastguard Worker       zx_info_task_stats_t task_stats;
19*c8dee2aaSAndroid Build Coastguard Worker       zx_handle_t process = zx_process_self();
20*c8dee2aaSAndroid Build Coastguard Worker       zx_status_t status = zx_object_get_info(
21*c8dee2aaSAndroid Build Coastguard Worker       process, ZX_INFO_TASK_STATS, &task_stats, sizeof(task_stats), nullptr, nullptr);
22*c8dee2aaSAndroid Build Coastguard Worker       if (status != ZX_OK) {
23*c8dee2aaSAndroid Build Coastguard Worker         return -1;
24*c8dee2aaSAndroid Build Coastguard Worker       }
25*c8dee2aaSAndroid Build Coastguard Worker       return (task_stats.mem_private_bytes + task_stats.mem_shared_bytes);
26*c8dee2aaSAndroid Build Coastguard Worker     }
27*c8dee2aaSAndroid Build Coastguard Worker #elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_ANDROID)
28*c8dee2aaSAndroid Build Coastguard Worker     #include <sys/resource.h>
getMaxResidentSetSizeBytes()29*c8dee2aaSAndroid Build Coastguard Worker     int64_t sk_tools::getMaxResidentSetSizeBytes() {
30*c8dee2aaSAndroid Build Coastguard Worker         struct rusage ru;
31*c8dee2aaSAndroid Build Coastguard Worker         getrusage(RUSAGE_SELF, &ru);
32*c8dee2aaSAndroid Build Coastguard Worker     #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
33*c8dee2aaSAndroid Build Coastguard Worker         return ru.ru_maxrss;         // Darwin reports bytes.
34*c8dee2aaSAndroid Build Coastguard Worker     #else
35*c8dee2aaSAndroid Build Coastguard Worker         return ru.ru_maxrss * 1024;  // Linux reports kilobytes.
36*c8dee2aaSAndroid Build Coastguard Worker     #endif
37*c8dee2aaSAndroid Build Coastguard Worker     }
38*c8dee2aaSAndroid Build Coastguard Worker #elif defined(SK_BUILD_FOR_WIN)
39*c8dee2aaSAndroid Build Coastguard Worker     #include <windows.h>
40*c8dee2aaSAndroid Build Coastguard Worker     #include <psapi.h>
getMaxResidentSetSizeBytes()41*c8dee2aaSAndroid Build Coastguard Worker     int64_t sk_tools::getMaxResidentSetSizeBytes() {
42*c8dee2aaSAndroid Build Coastguard Worker         PROCESS_MEMORY_COUNTERS info;
43*c8dee2aaSAndroid Build Coastguard Worker         GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
44*c8dee2aaSAndroid Build Coastguard Worker         return info.PeakWorkingSetSize;
45*c8dee2aaSAndroid Build Coastguard Worker     }
46*c8dee2aaSAndroid Build Coastguard Worker #else
getMaxResidentSetSizeBytes()47*c8dee2aaSAndroid Build Coastguard Worker     int64_t sk_tools::getMaxResidentSetSizeBytes() { return -1; }
48*c8dee2aaSAndroid Build Coastguard Worker #endif
49*c8dee2aaSAndroid Build Coastguard Worker 
50*c8dee2aaSAndroid Build Coastguard Worker #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
51*c8dee2aaSAndroid Build Coastguard Worker     #include <mach/mach.h>
getCurrResidentSetSizeBytes()52*c8dee2aaSAndroid Build Coastguard Worker     int64_t sk_tools::getCurrResidentSetSizeBytes() {
53*c8dee2aaSAndroid Build Coastguard Worker         mach_task_basic_info info;
54*c8dee2aaSAndroid Build Coastguard Worker         mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
55*c8dee2aaSAndroid Build Coastguard Worker         if (KERN_SUCCESS !=
56*c8dee2aaSAndroid Build Coastguard Worker                 task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count)) {
57*c8dee2aaSAndroid Build Coastguard Worker             return -1;
58*c8dee2aaSAndroid Build Coastguard Worker         }
59*c8dee2aaSAndroid Build Coastguard Worker         return info.resident_size;
60*c8dee2aaSAndroid Build Coastguard Worker     }
61*c8dee2aaSAndroid Build Coastguard Worker #elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID)  // N.B. /proc is Linux-only.
62*c8dee2aaSAndroid Build Coastguard Worker     #include <unistd.h>
63*c8dee2aaSAndroid Build Coastguard Worker     #include <stdio.h>
getCurrResidentSetSizeBytes()64*c8dee2aaSAndroid Build Coastguard Worker     int64_t sk_tools::getCurrResidentSetSizeBytes() {
65*c8dee2aaSAndroid Build Coastguard Worker         const long pageSize = sysconf(_SC_PAGESIZE);
66*c8dee2aaSAndroid Build Coastguard Worker         long long rssPages = 0;
67*c8dee2aaSAndroid Build Coastguard Worker         if (FILE* statm = fopen("/proc/self/statm", "r")) {
68*c8dee2aaSAndroid Build Coastguard Worker             // statm contains: program-size rss shared text lib data dirty, all in page counts.
69*c8dee2aaSAndroid Build Coastguard Worker             int rc = fscanf(statm, "%*d %lld", &rssPages);
70*c8dee2aaSAndroid Build Coastguard Worker             fclose(statm);
71*c8dee2aaSAndroid Build Coastguard Worker             if (rc != 1) {
72*c8dee2aaSAndroid Build Coastguard Worker                 return -1;
73*c8dee2aaSAndroid Build Coastguard Worker             }
74*c8dee2aaSAndroid Build Coastguard Worker         }
75*c8dee2aaSAndroid Build Coastguard Worker         return rssPages * pageSize;
76*c8dee2aaSAndroid Build Coastguard Worker     }
77*c8dee2aaSAndroid Build Coastguard Worker #elif defined(SK_BUILD_FOR_WIN)
getCurrResidentSetSizeBytes()78*c8dee2aaSAndroid Build Coastguard Worker     int64_t sk_tools::getCurrResidentSetSizeBytes() {
79*c8dee2aaSAndroid Build Coastguard Worker         PROCESS_MEMORY_COUNTERS info;
80*c8dee2aaSAndroid Build Coastguard Worker         GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
81*c8dee2aaSAndroid Build Coastguard Worker         return info.WorkingSetSize;
82*c8dee2aaSAndroid Build Coastguard Worker     }
83*c8dee2aaSAndroid Build Coastguard Worker #else
getCurrResidentSetSizeBytes()84*c8dee2aaSAndroid Build Coastguard Worker     int64_t sk_tools::getCurrResidentSetSizeBytes() { return -1; }
85*c8dee2aaSAndroid Build Coastguard Worker #endif
86*c8dee2aaSAndroid Build Coastguard Worker 
getMaxResidentSetSizeMB()87*c8dee2aaSAndroid Build Coastguard Worker int sk_tools::getMaxResidentSetSizeMB() {
88*c8dee2aaSAndroid Build Coastguard Worker     int64_t bytes = sk_tools::getMaxResidentSetSizeBytes();
89*c8dee2aaSAndroid Build Coastguard Worker     return bytes < 0 ? -1 : static_cast<int>(bytes / 1024 / 1024);
90*c8dee2aaSAndroid Build Coastguard Worker }
91*c8dee2aaSAndroid Build Coastguard Worker 
getCurrResidentSetSizeMB()92*c8dee2aaSAndroid Build Coastguard Worker int sk_tools::getCurrResidentSetSizeMB() {
93*c8dee2aaSAndroid Build Coastguard Worker     int64_t bytes = sk_tools::getCurrResidentSetSizeBytes();
94*c8dee2aaSAndroid Build Coastguard Worker     return bytes < 0 ? -1 : static_cast<int>(bytes / 1024 / 1024);
95*c8dee2aaSAndroid Build Coastguard Worker }
96