|
| 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 | +package prometheus |
| 15 | + |
| 16 | +import ( |
| 17 | + "bytes" |
| 18 | + "encoding/binary" |
| 19 | + "fmt" |
| 20 | + "github.com/ebitengine/purego" |
| 21 | +) |
| 22 | + |
| 23 | +func init() { |
| 24 | + if lib, err := purego.Dlopen("/usr/lib/system/libsystem_kernel.dylib", |
| 25 | + purego.RTLD_NOW|purego.RTLD_GLOBAL); err == nil { |
| 26 | + |
| 27 | + // purego.RegisterLibFunc() panics if the symbol is missing. Ignore any error, |
| 28 | + // and the metric will simply be unavailable when it is queried, instead of |
| 29 | + // bringing down the whole process. |
| 30 | + defer func() { |
| 31 | + if err := recover(); err != nil { |
| 32 | + // TODO: Log this somehow |
| 33 | + } |
| 34 | + }() |
| 35 | + |
| 36 | + purego.RegisterLibFunc(&machTaskSelf, lib, "mach_task_self") |
| 37 | + purego.RegisterLibFunc(&taskInfo, lib, "task_info") |
| 38 | + purego.RegisterLibFunc(&machVmRegion, lib, "mach_vm_region") |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +const ( |
| 43 | + // The task_info() flavor MACH_TASK_BASIC_INFO for retrieving machTaskBasicInfo. |
| 44 | + mach_task_basic_info task_flavor_t = 20 /* always 64-bit basic info */ |
| 45 | + |
| 46 | + // The MACH_TASK_BASIC_INFO_COUNT value, which is passed to the Mach API as the size |
| 47 | + // of the payload for MACH_TASK_BASIC_INFO commands. |
| 48 | + machTaskBasicInfoCount mach_msg_type_number_t = machTaskBasicInfoSizeOf / 4 |
| 49 | +) |
| 50 | + |
| 51 | +// Defined in xnu/osfmk/mach/policy.h, xnu/iokit/IOKit/IORPC.h, and xnu/osfmk/mach/message.h |
| 52 | +// respectively. policy_t is explicitly 32-bit here to help decoding into a structure, |
| 53 | +// where 'int' types are not supported. |
| 54 | +type ( |
| 55 | + policy_t = int32 // typedef int policy_t; |
| 56 | + mach_port_t = natural_t // typedef natural_t mach_port_t; |
| 57 | + mach_msg_type_number_t = natural_t // typedef natural_t mach_msg_type_number_t; |
| 58 | +) |
| 59 | + |
| 60 | +// Defined in xnu/osfmk/mach/task_info.h. Note that task_info_t is actually defined as |
| 61 | +// integer_t* and cast from the address of the structure in C. Define it as []byte to |
| 62 | +// keep the type system happy. |
| 63 | +type ( |
| 64 | + task_flavor_t = natural_t // typedef natural_t task_flavor_t |
| 65 | + task_info_t = []byte // typedef integer_t *task_info_t /* varying array of int */ |
| 66 | +) |
| 67 | + |
| 68 | +// time_value_t is the type for kernel time values, defined in xnu/osfmk/mach/time_value.h |
| 69 | +type time_value_t struct { |
| 70 | + Seconds integer_t |
| 71 | + MicroSeconds integer_t |
| 72 | +} |
| 73 | + |
| 74 | +var machTaskSelf func() mach_port_t |
| 75 | + |
| 76 | +var taskInfo func( |
| 77 | + mach_port_t, |
| 78 | + task_flavor_t, |
| 79 | + task_info_t, |
| 80 | + *mach_msg_type_number_t, |
| 81 | +) kern_return_t |
| 82 | + |
| 83 | +// machTaskBasicInfo is the representation of `struct mach_task_basic_info` defined in |
| 84 | +// xnu/osfmk/mach/task_info.h, which is the architecture independent payload for fetching |
| 85 | +// certain task values. |
| 86 | +type machTaskBasicInfo struct { |
| 87 | + VirtualSize mach_vm_size_t // virtual memory size (bytes) |
| 88 | + ResidentSize mach_vm_size_t // resident memory size (bytes) |
| 89 | + ResidentSizeMax mach_vm_size_t // maximum resident memory size (bytes) |
| 90 | + UserTime time_value_t // total user run time for terminated threads |
| 91 | + SystemTime time_value_t // total system run time for terminated threads |
| 92 | + Policy policy_t // default policy for new threads |
| 93 | + SuspendCount integer_t // suspend count for task |
| 94 | +} |
| 95 | + |
| 96 | +func getBasicTaskInfo() (*machTaskBasicInfo, error) { |
| 97 | + var info machTaskBasicInfo |
| 98 | + |
| 99 | + if taskInfo == nil { |
| 100 | + return nil, fmt.Errorf("task_info() is not supported") |
| 101 | + } |
| 102 | + |
| 103 | + var count = machTaskBasicInfoCount |
| 104 | + buf := make([]byte, machTaskBasicInfoSizeOf) |
| 105 | + |
| 106 | + if ret := taskInfo(machTaskSelf(), mach_task_basic_info, buf, &count); ret != 0 { |
| 107 | + return nil, fmt.Errorf("task_info() returned %d", ret) |
| 108 | + } |
| 109 | + |
| 110 | + if err := loadStruct(buf, &info); err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + return &info, nil |
| 115 | +} |
| 116 | + |
| 117 | +// Defined in xnu/osfmk/mach/memory_object_types.h, xnu/osfmk/mach/vm_behavior.h, |
| 118 | +// defined in xnu/osfmk/mach/vm_inherit.h, and defined in xnu/osfmk/mach/vm_prot.h |
| 119 | +// respectively. These types are not identical to the native definitions, because the |
| 120 | +// struct decoding requires primitives with a specific width. The widths here are the |
| 121 | +// same as the native types. |
| 122 | +type ( |
| 123 | + memory_object_offset_t = uint64 // typedef unsigned long long memory_object_offset_t; |
| 124 | + vm_behavior_t = int32 // typedef int vm_behavior_t |
| 125 | + vm_inherit_t = uint32 // typedef unsigned int vm_inherit_t; |
| 126 | + vm_prot_t = int32 // typedef int vm_prot_t; |
| 127 | +) |
| 128 | + |
| 129 | +// These are defined in xnu/osfmk/mach/vm_types.h. |
| 130 | +type ( |
| 131 | + vm_map_t = mach_port_t // typedef mach_port_t vm_map_t; |
| 132 | +) |
| 133 | + |
| 134 | +// Defined in xnu/osfmk/mach/vm_region.h. vm_region_flavor_t is explicitly 32-bit here to |
| 135 | +// help decoding into a structure, where 'int' types are not supported, and vm_region_info_t |
| 136 | +// is []byte to keep the type system happy. |
| 137 | +type ( |
| 138 | + vm_region_flavor_t = int32 // typedef int vm_region_flavor_t; |
| 139 | + vm_region_info_t = []byte // typedef int *vm_region_info_t; |
| 140 | +) |
| 141 | + |
| 142 | +const ( |
| 143 | + // The mach_vm_region() flavor VM_REGION_BASIC_INFO_64 for retrieving |
| 144 | + // vmRegionBasicInfo64. |
| 145 | + vm_region_basic_info_64 vm_region_flavor_t = 9 |
| 146 | + |
| 147 | + // The VM_REGION_BASIC_INFO_COUNT_64 value, which is passed to the Mach API as the |
| 148 | + // size of the payload for VM_REGION_BASIC_INFO_64 commands. |
| 149 | + vmRegionBasicInfoCount64 mach_msg_type_number_t = vmRegionBasicInfo64SizeOf / 4 |
| 150 | +) |
| 151 | + |
| 152 | +// vmRegionBasicInfo64 is the representation of `struct vm_region_basic_info_64` defined |
| 153 | +// in xnu/osfmk/mach/vm_region.h. This is enclosed in `#pragma pack(push, 4)` in C, so |
| 154 | +// unsafe.SizeOf() won't match the actual sizeof(vm_region_basic_info_64). |
| 155 | +type vmRegionBasicInfo64 struct { |
| 156 | + Protection vm_prot_t |
| 157 | + MaxProtection vm_prot_t |
| 158 | + Inheritance vm_inherit_t |
| 159 | + Shared boolean_t |
| 160 | + Reserved boolean_t |
| 161 | + Offset memory_object_offset_t |
| 162 | + Behavior vm_behavior_t |
| 163 | + UserWiredCount uint16 |
| 164 | +} |
| 165 | + |
| 166 | +var machVmRegion func( |
| 167 | + vm_map_t, |
| 168 | + *mach_vm_offset_t, /* IN/OUT */ |
| 169 | + *mach_vm_size_t, /* OUT */ |
| 170 | + vm_region_flavor_t, /* IN */ |
| 171 | + vm_region_info_t, /* OUT */ |
| 172 | + *mach_msg_type_number_t, /* IN/OUT */ |
| 173 | + *mach_port_t, /* OUT */ |
| 174 | +) kern_return_t |
| 175 | + |
| 176 | +func getMemoryUsage() (*machTaskBasicInfo, error) { |
| 177 | + // The logic in here follows how the ps(1) utility determines the memory values. The |
| 178 | + // basic_task_info command used here is a more modern, cross-architecture one that is |
| 179 | + // suggested in the kernel header files. |
| 180 | + // |
| 181 | + // https://github.com/apple-oss-distributions/adv_cmds/blob/8744084ea0ff41ca4bb96b0f9c22407d0e48e9b7/ps/tasks.c#L132 |
| 182 | + |
| 183 | + info, err := getBasicTaskInfo() |
| 184 | + |
| 185 | + if err != nil { |
| 186 | + return nil, err |
| 187 | + } else if machVmRegion != nil { |
| 188 | + |
| 189 | + var textInfo vmRegionBasicInfo64 |
| 190 | + |
| 191 | + buf := make([]byte, vmRegionBasicInfo64SizeOf) |
| 192 | + address := globalSharedTextSegment |
| 193 | + var size mach_vm_size_t |
| 194 | + var objectName mach_port_t |
| 195 | + |
| 196 | + cmd := vm_region_basic_info_64 |
| 197 | + count := vmRegionBasicInfoCount64 |
| 198 | + |
| 199 | + ret := machVmRegion(machTaskSelf(), &address, &size, cmd, buf, &count, &objectName) |
| 200 | + |
| 201 | + if ret == 0 { |
| 202 | + if err := loadStruct(buf, &textInfo); err == nil { |
| 203 | + adjustment := sharedTextRegionSize + sharedDataRegionSize |
| 204 | + if textInfo.Reserved != 0 && size == sharedTextRegionSize && info.VirtualSize > adjustment { |
| 205 | + info.VirtualSize -= adjustment |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + return info, nil |
| 212 | +} |
| 213 | + |
| 214 | +func loadStruct(buffer []byte, data any) error { |
| 215 | + r := bytes.NewReader(buffer) |
| 216 | + |
| 217 | + // TODO: NativeEndian was added in go 1.21 |
| 218 | + return binary.Read(r, binary.LittleEndian, data) |
| 219 | +} |
0 commit comments