Real-time scheduling class pthread on iOS

Hi,


We're writing code to decode a real-time network RTSP video stream (using ffmpeg + ************) on iOS, we've experienced issues that *seems* to be related to our work-queue loop getting throttled/paused/whatever during the parsing+decoding process. Under load (and noticably more on weaker devices) we experience "gaps" - times that the thread seems to be paused so the video stream jumps - we've boiled the code down to the basics of just parsing the rtsp stream and decoding h264 frames and still get "gaps" (packet loss is minimal from what we've checked with rvictl and wireshark)


I've stumbled upon Technical Note TN2169 - High Precision Timers in iOS / OS X which explains how to set the pthread to real-time scheduling but want to make sure it actually works on iOS (on the comments of this article from 2011 someone stated that Apple disabled real-time scheduling of pthreads on iOS) and if it's the correct solution to the issue.


Running the code with real-time pthread gives the impression that there are less gaps (we're printing the times of the gaps while streaming) but we're working on this for so long that it might be a lie.


Thanks,

Nimrod

Accepted Reply

If you’re doing a real time task that the real-time thread policy (

THREAD_TIME_CONSTRAINT_POLICY
) is a reasonable option.

IMPORTANT This policy is very deliberately structured to force you to state, in advance, how much CPU you need. If you can’t come up with reasonable values for the

period
,
computation
and
constraint
values, you probably need to reconsider whether you should be using this policy.

(we're printing the times of the gaps while streaming)

I hope you’re not printing from the real-time thread itself. That would represent a massive priority inversion. I recommend that you gather statistics in your real-time thread, record them in memory, and then print them from a normal-priority thread.

If you see anything unexpected you can investigate using the System Trace instrument. That’ll show you how long your thread is on the CPU and, when it get kicked off, why.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

If you’re doing a real time task that the real-time thread policy (

THREAD_TIME_CONSTRAINT_POLICY
) is a reasonable option.

IMPORTANT This policy is very deliberately structured to force you to state, in advance, how much CPU you need. If you can’t come up with reasonable values for the

period
,
computation
and
constraint
values, you probably need to reconsider whether you should be using this policy.

(we're printing the times of the gaps while streaming)

I hope you’re not printing from the real-time thread itself. That would represent a massive priority inversion. I recommend that you gather statistics in your real-time thread, record them in memory, and then print them from a normal-priority thread.

If you see anything unexpected you can investigate using the System Trace instrument. That’ll show you how long your thread is on the CPU and, when it get kicked off, why.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi there,


I am looking for real time since we got bug on system time user can go into device and can change time.I found similar to what nimrodgutman found


mach_timebase_info(&timebaseInfo).:-- question is what is timebaseInfo

startTime = machAbsoluteToSeconds()


func machAbsoluteToSeconds(machAbsolute: UInt64 = mach_absolute_time()) -> Double {

let nanos = Double(machAbsolute * UInt64(timebaseInfo.numer)) / Double(timebaseInfo.denom)

return nanos / 1.0e9;

// This method converts nanoseconds into seconds


let elapsed = self.machAbsoluteToSeconds() - self.startTime:-- question is elapsed time giving me difference of calcuation of seconds to nanoseconds ?? then time should be same correct??


Important Question machAbsoluteToSeconds does it give time when application is on background or when on sleep ???

what is

timebaseInfo
timebaseInfo
holds the information necessary to convert Mach absolute time to a meaningful value. The actual values you get back can vary by CPU and OS version. See QA1398 Mach Absolute Time Units for more.

is elapsed time giving me difference of calcuation of seconds to nanoseconds ?? then time should be same correct??

I don’t understand what you’re asking here.

machAbsoluteToSeconds
does it give time when application is on background or when on sleep ???

Mach absolute time will, in general, continue to count if your process is suspended in the background. However, if will stop counting if the CPU goes to sleep.

If you’re looking for lower-level times with easier semantics, conside

clock_gettime
man page.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"