|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +//go:build darwin && cgo |
| 15 | + |
| 16 | +#include <mach/mach_init.h> |
| 17 | +#include <mach/task.h> |
| 18 | +// Compiler warns that shared_memory_server.h is deprecated, use this instead. |
| 19 | +// But this doesn't define SHARED_DATA_REGION_SIZE or SHARED_TEXT_REGION_SIZE. |
| 20 | +//#include <mach/shared_region.h> |
| 21 | +#include <mach/shared_memory_server.h> |
| 22 | +#include <mach/mach_vm.h> |
| 23 | + |
| 24 | + |
| 25 | +int get_memory_info(unsigned long long *rss, unsigned long long *vsize) |
| 26 | +{ |
| 27 | + // This is lightly adapted from how ps(1) obtains its memory info. |
| 28 | + // https://github.com/apple-oss-distributions/adv_cmds/blob/8744084ea0ff41ca4bb96b0f9c22407d0e48e9b7/ps/tasks.c#L109 |
| 29 | + |
| 30 | + kern_return_t error; |
| 31 | + task_t task = MACH_PORT_NULL; |
| 32 | + mach_task_basic_info_data_t info; |
| 33 | + mach_msg_type_number_t info_count = MACH_TASK_BASIC_INFO_COUNT; |
| 34 | + |
| 35 | + error = task_info( |
| 36 | + mach_task_self(), |
| 37 | + MACH_TASK_BASIC_INFO, |
| 38 | + (task_info_t) &info, |
| 39 | + &info_count ); |
| 40 | + |
| 41 | + if( error != KERN_SUCCESS ) |
| 42 | + { |
| 43 | + return error; |
| 44 | + } |
| 45 | + |
| 46 | + *rss = info.resident_size; |
| 47 | + *vsize = info.virtual_size; |
| 48 | + |
| 49 | + { |
| 50 | + vm_region_basic_info_data_64_t b_info; |
| 51 | + mach_vm_address_t address = GLOBAL_SHARED_TEXT_SEGMENT; |
| 52 | + mach_vm_size_t size; |
| 53 | + mach_port_t object_name; |
| 54 | + |
| 55 | + /* |
| 56 | + * try to determine if this task has the split libraries |
| 57 | + * mapped in... if so, adjust its virtual size down by |
| 58 | + * the 2 segments that are used for split libraries |
| 59 | + */ |
| 60 | + info_count = VM_REGION_BASIC_INFO_COUNT_64; |
| 61 | + |
| 62 | + error = mach_vm_region( |
| 63 | + mach_task_self(), |
| 64 | + &address, |
| 65 | + &size, |
| 66 | + VM_REGION_BASIC_INFO_64, |
| 67 | + (vm_region_info_t) &b_info, |
| 68 | + &info_count, |
| 69 | + &object_name); |
| 70 | + |
| 71 | + if (error == KERN_SUCCESS) { |
| 72 | + if (b_info.reserved && size == (SHARED_TEXT_REGION_SIZE) && |
| 73 | + *vsize > (SHARED_TEXT_REGION_SIZE + SHARED_DATA_REGION_SIZE)) { |
| 74 | + *vsize -= (SHARED_TEXT_REGION_SIZE + SHARED_DATA_REGION_SIZE); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
0 commit comments