c++ - High cpu load when reading from /dev/ttyACM0 -
i'm trying implement program read data tactile sensors, have problems high cpu load. need reduce cpu load.
my setup
i'm reading data several (up 16) tacilte sensors. sensors connected via usb pc. in linux these devices create virtual serial ports: /dev/ttyacm0, /dev/ttyacm1, /dev/ttyacm2 ...
this dmesg tells me, when plug in sensors (output 1 sensor):
[ 321.998462] usb 2-2: usb disconnect, device number 3 [ 327.541414] usb 2-2: new full-speed usb device number 5 using ohci_hcd [ 327.998963] usb 2-2: new usb device found, idvendor=0471, idproduct=0889 [ 327.998983] usb 2-2: new usb device strings: mfr=1, product=2, serialnumber=3 [ 327.998997] usb 2-2: product: wts [ 327.999011] usb 2-2: manufacturer: weiss robotics [ 327.999024] usb 2-2: serialnumber: 0001 [ 328.035356] cdc_acm 2-2:1.0: device cannot calls on own. not modem. [ 328.035424] cdc_acm 2-2:1.0: ttyacm0: usb acm device
i use termios termios.h open serial device. sensors have binary protocol set device non-canonical , use raw output. sensors provide periodic sampling feature send 270 frames of data per second.
my program should read , process frame serial device complete frame available.
the problem: high cpu load
this code does:
int fd = open("/dev/ttyacm0"); struct termios settings; settings.xxx = ... // see full code details tcflush( fd, tciflush ); tcsetattr( fd, tcsanow, &settings ); while(1) { frame f = readcompleteframe(fd); processframe(f); }
when put device blocking read mode , execute loop, cpu load goes 15%. have separate thread each sensor. have multiple sensors each thread consume 15% of 1 cpu core.
i profiled program using time command: of cpu time spent in kernel. user time next zero.
what tried fix it
- i put device non-blocking mode , used poll sys/poll.h. cpu load same: 15% per sensor.
- i put device non-blocking mode , used select unistd.h. same result poll.
- i put device non-blocking mode , kept calling read(fd); usleep(1); until got data. uses 50% cpu, independant of number of sensors.
complete test code
the complete code used testing can found here: http://pastebin.com/7dv0u2nn
having examined code, there little can fix in application.
the load caused problem in kernel driver or way usb device talks pc.
Comments
Post a Comment