skylarmt / android-xserver

Automatically exported from code.google.com/p/android-xserver
0 stars 0 forks source link

Repeatable ANR: starvation in font metrics loading #21

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
This issue is 100% repeatable on Android 4.2.1 (Nexus 7).

To reproduce:
1. Start the XServer.
2. Run urxvt.  I'm using the binary from the `rxvt-unicode` package in Ubuntu 
12.04, armhf port.  (The issue also appears with xterm, but it's far more 
obvious with urxvt, since it requests a Unicode font.)

This causes an ANR dialog (trace attached).

The problem is the implementation of Font.processQueryFontRequest.  For fonts 
with non-ASCII characters, it will make up to 65K calls to Android's 
Paint.getTextBounds, which in turn calls a native method.  This native method 
does not appear to be fast.  For the first few calls on an ASCII font it 
returns in 26 µs (on a Tegra3), but eventually ramps up to the 1-2ms range for 
a Unicode font.  At even 1ms/call it'll take 65 seconds to transmit font 
metrics.

(It *almost* looks O(n) in the character you request!  Haven't inspected the 
code to verify this.)

While the XServer is collecting this font data, it's holding a lock to the 
client I/O object.  The UI thread also takes this lock.  Thus, the font 
handling will starve the UI thread.

I'm poking around in the Android API trying to find a more efficient way of 
computing metrics for large fonts, but haven't seen anything obvious just yet.

Original issue reported on code.google.com by cbif...@gmail.com on 26 Dec 2012 at 11:41

Attachments:

GoogleCodeExporter commented 8 years ago
After analysis, the locking situation is more complex.  I modified Font to 
reduce the critical section on the InputOutput object, and noticed that the 
entire routine is in a larger critical section up in Client.doComms, which 
locks the XServer.  This blocks onTouchEvent in ScreenView.

Original comment by cbif...@gmail.com on 26 Dec 2012 at 11:55

GoogleCodeExporter commented 8 years ago
In case anyone's curious, I've worked around this on my Nexus 7 by disabling 
(in the source code) all iso10646 fonts.

Original comment by cbif...@gmail.com on 28 Dec 2012 at 4:43