Get ios app memory and cpu usage in testflight

I want to get ios app memory and cpu usage in testflight by some apis, but I'm not sure if these apis are available on testflight, Can some one help me?

methods:

static func currentUsage() -> UInt64? {
    let availableMemory = os_proc_available_memory()
    print("Available memory: \(availableMemory / 1024 / 1024) MB")
    
    let physicalMemory = ProcessInfo.processInfo.physicalMemory
    print("Available memory: \(physicalMemory / 1024 / 1024) MB")
    
    var info = task_vm_info_data_t()
    var count = mach_msg_type_number_t(MemoryLayout<task_vm_info>.size / MemoryLayout<integer_t>.size)
    
    let result = withUnsafeMutablePointer(to: &info) {
        $0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) {
            task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), $0, &count)
        }
    }
    
    guard result == KERN_SUCCESS else { return nil }
    
    return info.phys_footprint
}

static func currentUsage(since lastSampleTime: CFAbsoluteTime) -> Double? {
    var threadList: thread_act_array_t?
    var threadCount = mach_msg_type_number_t(0)
    
    guard task_threads(mach_task_self_, &threadList, &threadCount) == KERN_SUCCESS,
          let threadList = threadList else {
        return nil
    }
    
    defer {
        vm_deallocate(mach_task_self_, vm_address_t(bitPattern: threadList), vm_size_t(threadCount * UInt32(MemoryLayout<thread_act_t>.size)))
    }
    
    var totalUserTime: Double = 0
    var totalSystemTime: Double = 0
    
    for i in 0..<Int(threadCount) {
        var threadInfo = thread_basic_info()
        var count = mach_msg_type_number_t(THREAD_INFO_MAX)
        
        let result = withUnsafeMutablePointer(to: &threadInfo) {
            $0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) {
                thread_info(threadList[i], thread_flavor_t(THREAD_BASIC_INFO), $0, &count)
            }
        }
        
        guard result == KERN_SUCCESS else { continue }
        
        if threadInfo.flags & TH_FLAGS_IDLE == 0 {
            totalUserTime += Double(threadInfo.user_time.seconds) + Double(threadInfo.user_time.microseconds) / 1_000_000.0
            totalSystemTime += Double(threadInfo.system_time.seconds) + Double(threadInfo.system_time.microseconds) / 1_000_000.0
        }
    }
    
    let totalCPUTime = totalUserTime + totalSystemTime
    let timeInterval = CFAbsoluteTimeGetCurrent() - lastSampleTime
    let cpuCount = Double(ProcessInfo.processInfo.activeProcessorCount)
    return totalCPUTime / timeInterval * 100.0 / cpuCount
}
Answered by DTS Engineer in 841960022

The set of APIs available in TestFlight is the same as the set of APIs available on the App Store. The APIs you’re using here fall into two groups:

  • os_proc_available_memory is something we recommend for the use cases described in its doc comments in <os/proc.h>.

  • The low-level Mach APIs, like task_info, are available but they are generally something to avoid if you can.

What are you using these Mach APIs for? If it’s for analytics that should be fine [1], but if you’re making runtime decisions based on the results then we should talk more about what you’re doing and why.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Subject to the App Store privacy constraints, of course.

The set of APIs available in TestFlight is the same as the set of APIs available on the App Store. The APIs you’re using here fall into two groups:

  • os_proc_available_memory is something we recommend for the use cases described in its doc comments in <os/proc.h>.

  • The low-level Mach APIs, like task_info, are available but they are generally something to avoid if you can.

What are you using these Mach APIs for? If it’s for analytics that should be fine [1], but if you’re making runtime decisions based on the results then we should talk more about what you’re doing and why.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Subject to the App Store privacy constraints, of course.

Get ios app memory and cpu usage in testflight
 
 
Q