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
}
Topic:
App & System Services
SubTopic:
Core OS