VectorLinux

Please login or register.

Login with username, password and session length
Advanced search  

News:

Visit our home page for VL info. For support and documentation, visit the Vector Linux Knowledge Center or search the Knowledge Center and this Forum using the search box above.

Author Topic: USB programming: ioctl connect call has random delays  (Read 468 times)

Rytz

  • Member
  • *
  • Posts: 90
USB programming: ioctl connect call has random delays
« on: October 19, 2016, 11:13:52 am »

VL Version: VL64-7.1 Standard
Kernel: 3.8.16 #2

Preface: I've been poking around the internet looking for more information on ioctl connect delays but haven't really found much.  Any suggestions or feedback appreciated.

I have a C program that calibrates joysticks using ioctl calls.  The code in question looks like below:

Code: [Select]
usb_ioctl.ioctl_code = USBDEVFS_DISCONNECT;
ioctl(fd, USBDEVFS_IOCTL, &usb_ioctl);

usb_ioctl.ioctl_code = USBDEVFS_CONNECT;
ioctl(fd, USBDEVFS_IOCTL, &usb_ioctl);

The 2nd ioctl call with USBDEVFS_CONNECT is the culprit.  I'd say 90% of the time, this call takes about 5ms.  The other 10% of the calls can take anywhere up to 10 seconds.  The delay time is random, so sometimes it is less.

What I have tried so far is to put the calibrator in a loop and force it to retry if execution time goes past 50ms.  So far this doesn't seem to help, as the newly spawn calibrators just keep delaying as well.

I have some code to manually reset the USB ports (to simulate a physical reconnect) but haven't tried it out yet.  I was trying to avoid the resetting of ports just in case that leads to other problems.  Other than these random delays in the ioctl call, I haven't experienced any other hardware related problems.

Any info or help on debugging the problem or fixing it would be great.  Thanks!
« Last Edit: October 19, 2016, 11:18:45 am by Rytz »
Logged
~ Rytz

rbistolfi

  • Packager
  • Vectorian
  • ****
  • Posts: 2372
Re: USB programming: ioctl connect call has random delays
« Reply #1 on: October 25, 2016, 08:39:23 am »

Hi Rytz,

I would try the easiest first. USB devices are not great for latency, event a long cable can cause a lot of problems. Did you try a different cable? Are you using an USB hub? I recently experienced a lot of problems installing a weather station and it turned out to be the USB hub.

HTH, Rodrigo
Logged
"There is a concept which corrupts and upsets all others. I refer not to Evil, whose limited realm is that of ethics; I refer to the infinite."
Jorge Luis Borges, Avatars of the Tortoise.

--
Jumalauta!!

Rytz

  • Member
  • *
  • Posts: 90
Re: USB programming: ioctl connect call has random delays
« Reply #2 on: October 26, 2016, 11:14:21 am »

Hi Rytz,

I would try the easiest first. USB devices are not great for latency, event a long cable can cause a lot of problems. Did you try a different cable? Are you using an USB hub? I recently experienced a lot of problems installing a weather station and it turned out to be the USB hub.

HTH, Rodrigo
Hi Rodrigo - thanks for the response.

There are four USB joysticks, and all four have been hit with this delay (I have logs that record the process).  Which device is affected when this occurs seems random.  I could swap out the cables, but I doubt all four cables are bad. 

I'm not using a USB hub.  I do think that I am using all USB ports on the machine, though (8-10 ports).

Are you reprogramming your weather station dynamically in a similar fashion?
Logged
~ Rytz

rbistolfi

  • Packager
  • Vectorian
  • ****
  • Posts: 2372
Re: USB programming: ioctl connect call has random delays
« Reply #3 on: October 27, 2016, 11:06:09 am »

Nop, we are just reading on the serial port as data from the station arrives.
I agree about the cables, it would be weird if all 4 cables were bad.
Did you try disabling power saving on usb ports?
Logged
"There is a concept which corrupts and upsets all others. I refer not to Evil, whose limited realm is that of ethics; I refer to the infinite."
Jorge Luis Borges, Avatars of the Tortoise.

--
Jumalauta!!

rbistolfi

  • Packager
  • Vectorian
  • ****
  • Posts: 2372
Re: USB programming: ioctl connect call has random delays
« Reply #4 on: October 27, 2016, 11:11:54 am »

What about this?

Code: [Select]
# setserial /dev/<tty_name> low_latency
Worth the try :)
Logged
"There is a concept which corrupts and upsets all others. I refer not to Evil, whose limited realm is that of ethics; I refer to the infinite."
Jorge Luis Borges, Avatars of the Tortoise.

--
Jumalauta!!

Rytz

  • Member
  • *
  • Posts: 90
Re: USB programming: ioctl connect call has random delays
« Reply #5 on: November 01, 2016, 03:54:16 pm »

What about this?

Code: [Select]
# setserial /dev/<tty_name> low_latency
Worth the try :)
I haven't done anything with USB power saving.  Thanks for the suggestion - I'll look into it.

Isn't setserial specific to serial ports only?
Logged
~ Rytz

retired1af

  • Administrator
  • Vectorian
  • *****
  • Posts: 1425
Re: USB programming: ioctl connect call has random delays
« Reply #6 on: November 01, 2016, 05:38:43 pm »

Logged
ASUS K73 Intel i3 Dual Core 2.3GHz

rbistolfi

  • Packager
  • Vectorian
  • ****
  • Posts: 2372
Re: USB programming: ioctl connect call has random delays
« Reply #7 on: November 01, 2016, 09:03:44 pm »

What about this?

Code: [Select]
# setserial /dev/<tty_name> low_latency
Worth the try :)
I haven't done anything with USB power saving.  Thanks for the suggestion - I'll look into it.

Isn't setserial specific to serial ports only?

AFAIK USB ports are serial ports.
Logged
"There is a concept which corrupts and upsets all others. I refer not to Evil, whose limited realm is that of ethics; I refer to the infinite."
Jorge Luis Borges, Avatars of the Tortoise.

--
Jumalauta!!

retired1af

  • Administrator
  • Vectorian
  • *****
  • Posts: 1425
Re: USB programming: ioctl connect call has random delays
« Reply #8 on: November 02, 2016, 03:05:50 am »

USB - Universal Serial Bus   :)
Logged
ASUS K73 Intel i3 Dual Core 2.3GHz

Rytz

  • Member
  • *
  • Posts: 90
Re: USB programming: ioctl connect call has random delays
« Reply #9 on: November 02, 2016, 08:43:06 am »

Thanks fellas.  My brain is dumb sometimes. :)  I was thinking of classic serial ports.

I'll do some testing and report back.  I can't manually reproduce the problem, so testing may take some time.
« Last Edit: November 02, 2016, 08:44:52 am by Rytz »
Logged
~ Rytz

Rytz

  • Member
  • *
  • Posts: 90
Re: USB programming: ioctl connect call has random delays
« Reply #10 on: November 11, 2016, 11:51:50 am »

I just set up the system to disable USB autosuspend.  I used the kernel options approach (usbcore.autosuspend).  Going to do some testing this weekend to see if that makes a difference.

I did try the low_latency approach but I still have the occasional ioctl() call delays.

In the shell script that executes the calibration program (which calls the delayed ioctl), I added a while loop after the kill command and have it wait until the program has actually been killed.  What I've now noticed is that the kills are never immediate, so there is something going on under the hood which is ignoring my kill signal.  I would guess at the kernel level.

I really hope that the disabling of the autosuspend works, because I'm not looking forward to digging into and debugging kernel code :/
Logged
~ Rytz

Rytz

  • Member
  • *
  • Posts: 90
Re: USB programming: ioctl connect call has random delays
« Reply #11 on: November 13, 2016, 01:24:50 pm »

Disabling USB autosuspend did not prevent the ioctl delays.  Looking for any further suggestions.  Thanks.
Logged
~ Rytz