★ TouchArcade needs your help. Click here to support us on Patreon.

Dealing with iPhone OS background processes

07-23-2009, 08:58 PM
#1
Joined: Jul 2009
Posts: 61
Dealing with iPhone OS background processes

Hi folks,

I'm getting ready to submit my first game to the App Store, but I'm hung up on maintaining a smooth framerate on the first-gen iPhone using OS 3.0. There are occasional hiccups that occur about once per minute, undoubtedly due to OS background processes kicking in (this will happen even when there's not much going on in the game). Otherwise, the game runs at a steady 60 fps.

Implementation-wise, I'm creating a separate thread to handle the game logic and rendering, which relies entirely on VBL sync blocking to prevent it from going faster than 60fps. I've also got Compile for Thumb turned off, -ffast-math and O3 optimization enabled (also tested with O2), as well as auto-vectorization. All the game logic is done in straight C, minimizing the use of Objective-C objects and even C function calls. CocosDenshion is being used to handle the audio (although this is the only part of Cocos2D that I'm using), with music in MP4 format (mono, 96kbps), small .wavs for sound effects, which are used sparingly. Calls to OpenGL are also minimized, including state changes, and I'm using interleaved data wherever possible.

I've tried using frame skipping as described in iPhone Games Projects, but this doesn't appear to have helped. The only thing I can think of doing at this point is to calculate everything's position based on elapsed time, but the hiccups seem long enough (say, a quarter to a half second) that this wouldn't effectively counter them.

I've been using PewPew as a basis for comparison, and it doesn't seem to suffer from background process hiccups nearly as much. Are there any techniques I should be using to deal with them? Should I force the thread to sleep until it's time to process the next tick? Any suggestions would be appreciated.

Last edited by emitcollect; 07-23-2009 at 09:19 PM.
07-23-2009, 10:01 PM
#2
Joined: Mar 2009
Location: Boston, mass
Posts: 39
Quote:
Originally Posted by emitcollect View Post
Hi folks,

I'm getting ready to submit my first game to the App Store, but I'm hung up on maintaining a smooth framerate on the first-gen iPhone using OS 3.0. There are occasional hiccups that occur about once per minute, undoubtedly due to OS background processes kicking in (this will happen even when there's not much going on in the game). Otherwise, the game runs at a steady 60 fps.

Implementation-wise, I'm creating a separate thread to handle the game logic and rendering, which relies entirely on VBL sync blocking to prevent it from going faster than 60fps. I've also got Compile for Thumb turned off, -ffast-math and O3 optimization enabled (also tested with O2), as well as auto-vectorization. All the game logic is done in straight C, minimizing the use of Objective-C objects and even C function calls. CocosDenshion is being used to handle the audio (although this is the only part of Cocos2D that I'm using), with music in MP4 format (mono, 96kbps), small .wavs for sound effects, which are used sparingly. Calls to OpenGL are also minimized, including state changes, and I'm using interleaved data wherever possible.

I've tried using frame skipping as described in iPhone Games Projects, but this doesn't appear to have helped. The only thing I can think of doing at this point is to calculate everything's position based on elapsed time, but the hiccups seem long enough (say, a quarter to a half second) that this wouldn't effectively counter them.

I've been using PewPew as a basis for comparison, and it doesn't seem to suffer from background process hiccups nearly as much. Are there any techniques I should be using to deal with them? Should I force the thread to sleep until it's time to process the next tick? Any suggestions would be appreciated.
I've noticed small hickups in frame rates on all iphone games if you haven't rebooted in a while. However, if they're more than a few frames you have something else going on. I'd run things in shark and see if it turns up any particular culprit. Also, put a break point in the app delegate function that's called when the phone needs to free up memory - if you're pushing your memory usage, it might be getting triggered and could definitely cause hickups.

Also, it's always wise to code things in a frame rate independent manner regardless of if your hitting frame rate on what you expect to be your low end device. I've also noticed that spawning many different threads can have a performance impact; so only create new threads when you really need to run something asynchronously.

__________________________________
Splat; shoot stuff and make art, what more could you ask for? Currently sporting a 4.78 rating in the app store!

07-24-2009, 02:20 AM
#3
Joined: Jul 2009
Posts: 61
Jason, thanks for the tips. I did some profiling and decided to experiment with forcing the thread to sleep. I've got to play around with the numbers some more, but so far this seems to have greatly reduced the incidence and severity of the hiccups. Now they only tend to occur when the action gets really hairy, which is what I'd expect. I'm thinking I can track the overage on long-running ticks to dial down the framerate so there's still time leftover for background processes.
07-31-2009, 07:11 PM
#4
Joined: Jul 2009
Posts: 61
Revisiting this, it seems that the thread sleeping strategy gave illusory results, as hiccups would eventually start up again, regardless of how I configured the sleeping intervals. So I went back to using an NSTimer on the main thread (at a 1.0/66.0 interval) and, so far, it appears to play better with background processes. However, that screws things up on a 3GS, so I'm using Erica Sadun's UIDevice-hardware code to start the game with timers on older hardware and threads on newer hardware (3GS, iPod touch 2G).

Edit: Scratch that. It behaved smoothly for a good while but then started hiccuping again. Totally baffling, because the degradation doesn't depend on how long the device has been on or anything.

Last edited by emitcollect; 07-31-2009 at 08:18 PM.
08-01-2009, 01:37 AM
#5
Joined: Feb 2009
Location: Sweden
Posts: 1,634
The framerate hickups started on 3.0, before that we had no problems at all. We're using timers as well.

I hope Apple will release 3.1 sometime soon and fix all those bugs 3.0 came with (which we devs usually are blamed for when the games behave oddly).
08-01-2009, 02:18 AM
#6
Joined: Jul 2009
Posts: 61
Ah, that's a relief to hear. I've spent a good portion of my life now trying to get rid of them, thinking I must have deficient knowledge of threads and such. Glad to know it isn't my fault!
08-01-2009, 02:28 AM
#7
Joined: Feb 2009
Location: Sweden
Posts: 1,634
Quote:
Originally Posted by emitcollect View Post
Ah, that's a relief to hear. I've spent a good portion of my life now trying to get rid of them, thinking I must have deficient knowledge of threads and such. Glad to know it isn't my fault!
Well it could depend on other things too! Atleast on my device 3.0 is behaving very oddly, and it was fine before. I try to ignore the performance issues for our next game right now and take care of if when 3.1 is out, cause right now you will have to go the blind route trying to fix things (runs at 60+ fps full 3D with the occasional stutters as you mentioned, then it goes down to about 30).

You're using OpenGL right?
08-01-2009, 03:15 AM
#8
Joined: Jul 2009
Posts: 61
Yup, OpenGL! And yeah, I'm sure something else could be contributing to the performance issues, because the stutters seem a bit more noticeable in my game than in others. Part of that is probably because my updates aren't framerate independent, but I implemented that briefly and it didn't make that much of a difference.

Have you only noticed stutters on an iPhone 1G? I haven't been able to personally observe my game running on a 3G, but a friend who owns one said he hadn't noticed any issues. I'm also guessing the iPod touch 1G doesn't run the same background processes as the iPhone, and would be less prone to stutters.
08-01-2009, 03:25 AM
#9
Joined: Feb 2009
Location: Sweden
Posts: 1,634
Quote:
Originally Posted by emitcollect View Post
Yup, OpenGL! And yeah, I'm sure something else could be contributing to the performance issues, because the stutters seem a bit more noticeable in my game than in others. Part of that is probably because my updates aren't framerate independent, but I implemented that briefly and it didn't make that much of a difference.

Have you only noticed stutters on an iPhone 1G? I haven't been able to personally observe my game running on a 3G, but a friend who owns one said he hadn't noticed any issues. I'm also guessing the iPod touch 1G doesn't run the same background processes as the iPhone, and would be less prone to stutters.
Yup, you really should do it framerate independent. It should help with stutters, but more importantly it will make it run fine on all devices, which is important since the performance difference between the iPod Touch 1G and the 3GS is huge!

Dunno. We don't have that many devices, and we would need the double if we would have each device pre-loaded with each software version (i.e. 1.x, 2.x, and 3.x). I don't think we have an iPhone 1G with 3.0 (I think we have 2.2.1 on that one). Is it possible to downgrade the software btw?
08-01-2009, 03:38 AM
#10
Joined: Jul 2009
Posts: 61
Quote:
Originally Posted by Anders View Post
Yup, you really should do it framerate independent. It should help with stutters, but more importantly it will make it run fine on all devices, which is important since the performance difference between the iPod Touch 1G and the 3GS is huge!
I'll give it a shot for the next point release. Aside from the hiccups, it runs at a solid 60fps on an iPhone 1G, so I'm hoping it'll run fine on an iPod touch 1G. The only reason why I'm reluctant to make it framerate independent is because it might create subtle errors with the collision code. (Maybe not, but I didn't want to risk it this close to release.)

Quote:
Dunno. We don't have that many devices, and we would need the double if we would have each device pre-loaded with each software version (i.e. 1.x, 2.x, and 3.x). I don't think we have an iPhone 1G with 3.0 (I think we have 2.2.1 on that one). Is it possible to downgrade the software btw?
Ah, so which device with 3.0 have you noticed the stutters on? The 3G?

I've looked into downgrading the software, but couldn't get it working. There's a technique where you put the device in a certain mode and then Option-Restore with older firmware, but I think this requires an older version of iTunes.