Why CPU usage increase to 100% ?
-
Dear Sirs,
I use RPi3 to run a qt project, basically use UART to collect data as below:
void MainWindow::UART_Receiver2() { UART_Read_Buffer = UART_Read_Buffer + serial->readAll().toHex(); //QByteArray if(UART_Read_Buffer.count()>63) { //-------------------------------------------- elapse_time++; QString s = QString::number(elapse_time); Mychart->axisX()->setTitleText(s); //------------------------------------------- UART_Read_Buffer.clear(); } }
At beginning I also use QChar,QChartView and QSplineSeries to draw diagram, however I found that the CPU usage too high, so I remove almost everything just leave UART as above.
However, CPU usage still increase from 6% to 100% in 20 minutes.Any idea what cause this situation ?
PS. I use pidstat to monitor the CPU usage, everyone can download the log file from:https://www.dropbox.com/s/r7t0256jm4vfe4j/cpustat.txt?dl=0
Any suggestion will very appreciate~
-
Hi,
How fast are you getting these data ?
-
Doesn't looks like that's the culprit then. What else are you doing with that application ?
-
Dear @SGaist ,
I create a new project for the test: https://github.com/hiloshi/CPUTest , I still see the CPU usage increase to 100%.
The CPU usage is https://www.dropbox.com/s/ki42n1ee32vi7r5/UARTCPU.txt?dl=0If I only add UART, the CPU usage will not exceed 1%, however, after I add QChart,QSplineSeries,etc.
(Line 38-63: CPUTest/mainwindow.cpp )
The CPU usage start increase, even I only do axisX()->setTitleText(s), not really draw curve.Is there something wrong with "Mychart->axisX()->setTitleText(s);" ?
or shouldn't draw "QChart" inside the UART slot ?Any suggestions would be very appreciate~
-
You should try to profile your application in order to determine where the hop spots are in your application.
-
Dear @SGaist ,
When MainWindow create, system will also new a serialport instance and connect to UART receiver:
serial = new QSerialPort(this); connect(serial, &QSerialPort::readyRead, this, &MainWindow::UART_Receiver2);
Then I put two widget on the mainwindow by QTCreater, one is pushbutton, another is tabWidget.
Inside the pushbutton, two major task will be done:- UART initialize (ttyUSB0, 115200/8N1N)
- QSplineSeries, QSplineSeries and QChartView initialize. then put this QChartView to a new tab,
MyvLayout->addWidget(MychartView); Mywidget->setLayout(MyvLayout); ui->tabWidget->addTab(Mywidget,"Chart");
In the meanwhile, tab3 will be generated and show a blank diagram.
System waiting UART input, sensor will output 32byes data to RPi3 every 800ms.Inside the UART Receive slot is:
UART_Read_Buffer = UART_Read_Buffer + serial->readAll().toHex(); if(UART_Read_Buffer.count()>63) { //-------------------------------------------- elapse_time++; QString s = QString::number(elapse_time); Mychart->axisX()->setTitleText(s); //------------------------------------------- UART_Read_Buffer.clear(); }
System will keep receive data, I will show numbers of received data and display on axisX().
Therefore, this value will increase by 1 after every 32bytes.Then I can monitor the CPU usage increase to 100%.
If comment out Mychart->axisX()->setTitleText(s); , the CPU usage will not keep increase.
So I think maybe there are something wrong with the way I use QChartView.Any help will be appreciate,
-
- Just do not use QtCharts, use Qwt instead and you will be surprised with performance changing.
- Do not use UART_Read_Buffer, because data already buffered inside of QSP (just use serial->bytesAvailable() to know how much data ready for read).
PS: You can simplify the MainWindow::UART_Receiver2 slot:
void MainWindow::UART_Receiver2() { QByteArray dummy = serial->readAll(); }
to check, how much CPU usage will be in this case..
-
Even if QChart spend 60% CPU power, it should not keep increase to 100%, isn't it ?
Try to do tests without of QtCharts, just with pure UART_Receiver2 slot(), as I wrote before, to look on CPU loading. And then, try to add something other code...
PS: QtCharts - is a bad choose, as it is too slow and also consume many CPU resources. F.e. from my experience, with Qwt I have ~4-5% CPU loading, but with QtCharts I have ~20-50% CPU loading. And I have similar proportions in RAM consuming - QtCharts eat a RAM in ~5-10 times more than Qwt. Besides, has a memory leaks...
-
Dear @kuzulis ,
I make some test as below:
- RPi3+Debian+QSerialPort(800ms)+QTabWidget+QChart-->20mins to 100%
- RPi3+Debian+QSerialPort(800ms)+QChart -->about 20mins to 100%
- RPi3+Debian+QTimer(1000ms)+QChart --> 35ms to 100%
- Corei5+WIN8+QTimer(1000ms)+QChart--> test over 1hr under 20%
My conclusion in the rough is QChart maybe has something wrong on Armv7 or debian....
I will continue to test Qwt.