1

I'm trying to get iOS CPU time programmatically.

I'm creating an application that prints the CPU time on iOS with Swift 3. I'm using two different functions:

var usage = rusage()
getrusage(RUSAGE_SELF, &usage
print(usage.ru_stime) //print system time`

and

  var info = mach_task_basic_info()
  var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4

  let kerr: kern_return_t = withUnsafeMutablePointer(to: &info) {
     $0.withMemoryRebound(to: integer_t.self, capacity: 1) {
        task_info(mach_task_self_,
                  task_flavor_t(MACH_TASK_BASIC_INFO),
                  $0,
                  &count)
     }
  }

  if kerr == KERN_SUCCESS {
     print("System time: \(info.system_time)")
  }
  else {
     print("Error with task_info(): " +
        (String(cString: mach_error_string(kerr), encoding: String.Encoding.ascii) ?? "unknown error"))
  }

When testing on the Xcode simulator, both methods work, giving me the CPU system time, but when I test on an actual device, the time returned is always zero. Using both of these functions, I can get CPU user time, but the CPU system time returned is always zero.

Is there permission that I have to enable for my application? Why do these functions work on the simulator but not on a real device?

Thank you.

0 Answers0