Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

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~


  • Lifetime Qt Champion

    Hi,

    How fast are you getting these data ?



  • Dear @SGaist ,

    Baud rate is 115200, the sensor will send 32bytes data to RPi3 for every 800ms automatically.

    Thanks,


  • Lifetime Qt Champion

    Doesn't looks like that's the culprit then. What else are you doing with that application ?



  • Dear @SGaist ,

    It should be nothing else in the application. But in order to make sure everything is under control, I will make a new project and put UART communication only then test again.

    Thanks,



  • 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=0

    If 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~


  • Lifetime Qt Champion

    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:

    1. UART initialize (ttyUSB0, 115200/8N1N)
    2. 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,


  • Qt Champions 2018

    @Hiloshi You could try to increase less often - for example after every 128 bytes.



    1. Just do not use QtCharts, use Qwt instead and you will be surprised with performance changing.
    2. 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..



  • Dear @jsulm ,

    Thank you very much.

    I change 128 to decrease draw speed of the QChart. The result is become 65minutes to 100%.
    https://www.dropbox.com/s/dmi8d84p48qjug1/UARTCPU2.txt?dl=0

    Hmmm....



  • Dear @kuzulis ,

    Thanks for the suggestions, I will test Qwt and change uart read method.

    I was wondering why CPU usage keep increase. Even if QChart spend 60% CPU power, it should not keep increase to 100%, isn't it ?



  • 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:

    1. RPi3+Debian+QSerialPort(800ms)+QTabWidget+QChart-->20mins to 100%
    2. RPi3+Debian+QSerialPort(800ms)+QChart -->about 20mins to 100%
    3. RPi3+Debian+QTimer(1000ms)+QChart --> 35ms to 100%
    4. 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.



  • Dear @kuzulis ,

    After testing Qwt for the same project, the CPU usage stable under 3%.
    It is huge difference compare to the QChart performance.

    Thanks for the suggestion.


Log in to reply