Aha, thanks for finding that. For a bit of background, that was added because the signal-based notification of URBs never worked (or at least, not when I was trying to make it work). So hence, the polling loop was added, and the FIXME comment. The reason it uses 0 is because back then, when the kernel had a hardcoded 10ms scheduler interval, any sleep was guaranteed at least 10ms of downtime. However, later kernels change that to much smaller intervals, and in some cases the scheduler is not even timer-based, so the assumption no longer applies.
The reason for the counter to 20 is because when there are many URBs piling up in the kernel waiting for javax.usb to read them, we don't want to sleep, as that will greatly reduce throughput. So the 20-count was added to only sleep when there really was nothing happening on the USB bus. Modified patch (using only 10000) is applied in CVS, thanks. On Nov 28, 2007 6:37 PM, Mark Scott <[EMAIL PROTECTED]> wrote: > Hi, > > I just started playing with javax.usb and ran into a situation where my > CPU is reproducibly pegged at 100%. This thread: > > <http://sourceforge.net/mailarchive/forum.php?thread_name=012601c7c6a6%2446545070%24c200010a%40Homer&forum_name=javax-usb-devel> > > reports a similar-sounding problem where "the machine grinds to a halt", > but unfortunately doesn't describe any solution. It's the only other > report I can find of such high CPU usage with javax.usb. > > Profiling my test class shows that the problem is caused (for me) by the > while loop in the native function > Java_com_ibm_jusb_os_linux_JavaxUsb_nativeDeviceProxy (defined in > JavaxUsbDeviceProxy.c) - I see millions of JNI calls to the Java method > com.ibm.jusb.os.linux.LinuxRequestProxy#isRequestWaiting() occurring. > > I'm no C hacker by any means, but the 'FIXME' comment is a bit of a > giveaway and the argument of 0 passed to the usleep function seems to > make the call pointless. Applying this diff makes the CPU more or less > idle for me: > > $ cvs diff -cw JavaxUsbDeviceProxy.c > Index: JavaxUsbDeviceProxy.c > =================================================================== > RCS file: /cvsroot/javax-usb/javax-usb-ri-linux/jni/JavaxUsbDeviceProxy.c,v > retrieving revision 1.5 > diff -c -w -r1.5 JavaxUsbDeviceProxy.c > *** JavaxUsbDeviceProxy.c 19 Jul 2004 17:40:02 -0000 1.5 > --- JavaxUsbDeviceProxy.c 28 Nov 2007 23:07:42 -0000 > *************** > *** 65,71 **** > while (1) { > /* FIXME - stop using polling! */ > if ( loop_count > 20 ) { > ! usleep( 0 ); > loop_count = 0; > } > loop_count ++; > --- 65,71 ---- > while (1) { > /* FIXME - stop using polling! */ > if ( loop_count > 20 ) { > ! usleep( 100000 ); > loop_count = 0; > } > loop_count ++; > > I'm not quite sure why the function calls further down the while loop > are executed 20 times for each call to usleep, nor what might be an > optimal number of microseconds to sleep, but my first guess of 100000 > seems OK. > > I'm also not sure why this wouldn't be a problem for _every_ javax.usb > user, so I'm wondering if I'm only hitting it because of something I'm > (not?) doing in my Java code or because of some problem with the USB > device I'm talking to. But before heading off down either of those > paths I'm wondering if anyone can offer an opinion on whether or not > there is indeed a bug in the native code that's fixed by the diff above. > > -- > Mark Scott > [EMAIL PROTECTED] > > ------------------------------------------------------------------------- > SF.Net email is sponsored by: The Future of Linux Business White Paper > from Novell. From the desktop to the data center, Linux is going > mainstream. Let it simplify your IT future. > http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4 > _______________________________________________ > javax-usb-devel mailing list > javax-usb-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/javax-usb-devel > ------------------------------------------------------------------------- SF.Net email is sponsored by: The Future of Linux Business White Paper from Novell. From the desktop to the data center, Linux is going mainstream. Let it simplify your IT future. http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4 _______________________________________________ javax-usb-devel mailing list javax-usb-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/javax-usb-devel