Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. app using "excessive" CPU
Forum Updated to NodeBB v4.3 + New Features

app using "excessive" CPU

Scheduled Pinned Locked Moved Solved General and Desktop
42 Posts 10 Posters 5.8k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mzimmers
    wrote on 20 May 2020, 22:14 last edited by
    #1

    Hi all -

    I have a small app whose display looks like this:
    discovery.PNG

    The model grows to about 50 columns (most of which are hidden) and 20 rows -- certainly not huge. And in the first few seconds, the model rarely gets updated.

    Multiple users have complained about it using too much CPU (about 11% on my i3 when seemingly idle).

    Given that this is running on Windows, I can't use the profiler built into Creator. Any tips for trying to determine where all this CPU is going?

    Thanks...

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fcarney
      wrote on 20 May 2020, 22:36 last edited by
      #2

      I would assume that for some reason it is receiving a lot of events or a thread is busy doing "something". Possibly a combination. Is there a way to tap into and print out the events the app is receiving while idle?

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mzimmers
        wrote on 20 May 2020, 22:49 last edited by
        #3

        Good assumption - I'm getting thousands of events per second, all like this:

        15466 "15:46:02" QEvent::Paint
        15467 "15:46:02" QEvent::UpdateRequest
        15468 "15:46:02" QEvent::Paint
        15469 "15:46:02" QEvent::UpdateRequest
        15470 "15:46:02" QEvent::Paint
        15471 "15:46:02" QEvent::UpdateRequest
        15472 "15:46:02" QEvent::Paint
        15473 "15:46:02" QEvent::UpdateRequest
        15474 "15:46:02" QEvent::Paint
        15475 "15:46:02" QEvent::UpdateRequest
        

        But what could be causing this? My model isn't changing (certainly not that quickly) and there's no user input, or resizing going on. The app, from the user's perspective, is truly idle.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fcarney
          wrote on 20 May 2020, 22:57 last edited by
          #4

          This is guessing time. I am good, but not that good. But I will hazard a guess. Something is "wiggling" and it most likely caused by something that controls layout. Something adjusts, something else adjusts it back.

          I do most of my work in QML and when this happens it is usually detected as a binding loop. I am unsure if there is anything like that in QWidgets.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          1
          • G Offline
            G Offline
            gde23
            wrote on 21 May 2020, 09:21 last edited by
            #5

            You can start the debugger and after start randomly click on pause.
            Since the program will be inside Paint or UpdateRequest with a high probability you will get there quickly and then you can follow the call-stack to see what is causing it.

            M 1 Reply Last reply 21 May 2020, 13:33
            0
            • G gde23
              21 May 2020, 09:21

              You can start the debugger and after start randomly click on pause.
              Since the program will be inside Paint or UpdateRequest with a high probability you will get there quickly and then you can follow the call-stack to see what is causing it.

              M Offline
              M Offline
              mzimmers
              wrote on 21 May 2020, 13:33 last edited by
              #6

              @gde23 that's a clever idea, and I tried it. Unfortunately, whenever I pause the debugger, the call stack only displays routines pertinent to handling the pause:pause.PNG
              Great idea, though.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 21 May 2020, 14:48 last edited by
                #7

                Hi,

                Are the columns automatically resized to content ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply 21 May 2020, 15:25
                0
                • S SGaist
                  21 May 2020, 14:48

                  Hi,

                  Are the columns automatically resized to content ?

                  M Offline
                  M Offline
                  mzimmers
                  wrote on 21 May 2020, 15:25 last edited by
                  #8

                  @SGaist not explicitly. I do this:

                      qhv->setSectionResizeMode(QHeaderView::Stretch);
                  

                  But disabling that line doesn't reduce CPU noticably.

                  J 1 Reply Last reply 21 May 2020, 16:35
                  0
                  • M mzimmers
                    21 May 2020, 15:25

                    @SGaist not explicitly. I do this:

                        qhv->setSectionResizeMode(QHeaderView::Stretch);
                    

                    But disabling that line doesn't reduce CPU noticably.

                    J Offline
                    J Offline
                    JonB
                    wrote on 21 May 2020, 16:35 last edited by JonB
                    #9

                    @mzimmers
                    Sit down, grab a coffee & a pack of cigs. Start binary-chopping-commenting-out half your code at a time. It can't be that hard to get to the point where these update requests do not happen... :) For example, first get rid of all hidden columns. Then reduce to fewer columns than the width of the window. With 2 rows. Then no rows and no columns --- does it still happen then?!

                    What are you using which gives you those events printed out?

                    M 1 Reply Last reply 21 May 2020, 16:43
                    0
                    • J JonB
                      21 May 2020, 16:35

                      @mzimmers
                      Sit down, grab a coffee & a pack of cigs. Start binary-chopping-commenting-out half your code at a time. It can't be that hard to get to the point where these update requests do not happen... :) For example, first get rid of all hidden columns. Then reduce to fewer columns than the width of the window. With 2 rows. Then no rows and no columns --- does it still happen then?!

                      What are you using which gives you those events printed out?

                      M Offline
                      M Offline
                      mzimmers
                      wrote on 21 May 2020, 16:43 last edited by
                      #10

                      @JonB OK, I'll belly up to removing the hidden rows (I was hoping to avoid that).

                      I've already sized my widget so that everything is fully displayed.

                      I even disconnected the signal that tells the model to update. My tableView is completely empty. And I'm still getting the CPU usage.

                      Here's what I use to show me the events:

                      bool KeyPress::eventFilter(QObject *obj, QEvent *ev)
                      {
                          bool rc = false;
                          QEvent::Type type;
                          static int count = 0;
                      
                          type = ev->type();
                          qDebug() << count++ << QTime::currentTime().toString() << type;
                      
                          rc = QObject::eventFilter(obj, ev);
                          return rc;
                      }
                      

                      It may be noteworthy that disabling this filter entirely doesn't help.

                      J 1 Reply Last reply 21 May 2020, 17:17
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 21 May 2020, 17:12 last edited by
                        #11

                        Hi
                        Could you not add a paintEvent handler where you simply call base class but
                        then add a break point and that way maybe see how its triggered ?

                        M 1 Reply Last reply 21 May 2020, 17:27
                        1
                        • M mzimmers
                          21 May 2020, 16:43

                          @JonB OK, I'll belly up to removing the hidden rows (I was hoping to avoid that).

                          I've already sized my widget so that everything is fully displayed.

                          I even disconnected the signal that tells the model to update. My tableView is completely empty. And I'm still getting the CPU usage.

                          Here's what I use to show me the events:

                          bool KeyPress::eventFilter(QObject *obj, QEvent *ev)
                          {
                              bool rc = false;
                              QEvent::Type type;
                              static int count = 0;
                          
                              type = ev->type();
                              qDebug() << count++ << QTime::currentTime().toString() << type;
                          
                              rc = QObject::eventFilter(obj, ev);
                              return rc;
                          }
                          

                          It may be noteworthy that disabling this filter entirely doesn't help.

                          J Offline
                          J Offline
                          JonB
                          wrote on 21 May 2020, 17:17 last edited by JonB
                          #12

                          @mzimmers said in app using "excessive" CPU:

                          My tableView is completely empty. And I'm still getting the CPU usage.

                          Which is the sort of thing which is interesting, so you don't want to waste time looking at resizes. Nonetheless, the events indicate widget updating?

                          I had forgotten about your KeyPress and its eventFilter. Get rid of it completely from your code! You don't want to be messing about with your key press stuff from your other thread. Now what's the speed? It ought be still slow, but you never know.

                          Then get rid of all your own "event filters". Reintroduce only one at the very top level to debug monitor whatever you need to.

                          M 1 Reply Last reply 21 May 2020, 17:36
                          0
                          • mrjjM mrjj
                            21 May 2020, 17:12

                            Hi
                            Could you not add a paintEvent handler where you simply call base class but
                            then add a break point and that way maybe see how its triggered ?

                            M Offline
                            M Offline
                            mzimmers
                            wrote on 21 May 2020, 17:27 last edited by
                            #13

                            @mrjj I can do something like this:

                            void Widget::paintEvent(QPaintEvent *event)
                            {
                                QWidget::paintEvent(event);
                            }
                            

                            But I don't know what kind of meaningful information I can extract from the event argument. (According to the docs, paintEvent() isn't a slot.)

                            1 Reply Last reply
                            0
                            • J JonB
                              21 May 2020, 17:17

                              @mzimmers said in app using "excessive" CPU:

                              My tableView is completely empty. And I'm still getting the CPU usage.

                              Which is the sort of thing which is interesting, so you don't want to waste time looking at resizes. Nonetheless, the events indicate widget updating?

                              I had forgotten about your KeyPress and its eventFilter. Get rid of it completely from your code! You don't want to be messing about with your key press stuff from your other thread. Now what's the speed? It ought be still slow, but you never know.

                              Then get rid of all your own "event filters". Reintroduce only one at the very top level to debug monitor whatever you need to.

                              M Offline
                              M Offline
                              mzimmers
                              wrote on 21 May 2020, 17:36 last edited by
                              #14

                              @JonB I've gotten rid of all filters, all hidden columns, and have disconnect model updates after I create two rows. I also disabled stretch mode, and expanding policy. Still using 11% CPU.

                              This might be a good time to point out that I "stole" the code for this app from another project, which doesn't have this problem. For the life of me, I can't see what I added that causes this.

                              J 1 Reply Last reply 21 May 2020, 18:19
                              0
                              • M mzimmers
                                21 May 2020, 17:36

                                @JonB I've gotten rid of all filters, all hidden columns, and have disconnect model updates after I create two rows. I also disabled stretch mode, and expanding policy. Still using 11% CPU.

                                This might be a good time to point out that I "stole" the code for this app from another project, which doesn't have this problem. For the life of me, I can't see what I added that causes this.

                                J Offline
                                J Offline
                                JonB
                                wrote on 21 May 2020, 18:19 last edited by JonB
                                #15

                                @mzimmers
                                Try not running your application, or switch the machine off ;-)

                                Given that this is running on Windows, I can't use the profiler built into Creator

                                Does your MSVC/MinGW compiler come with a usable profiler, nothing to do with Qt?

                                In your paintEvent() override @mrjj had in mind for you to try a debugger breakpoint there and look at the stack trace. You will doubtless need to perhaps put a delay/count on the breakpoint, or set it while the app is in the middle of running. I don't actually know whether paint events will show anything interesting on the stack.

                                You can use the top-level eventFilter() to examine most of what's going on, if you have to :( Debug every event's type to file/debug output window for a couple of seconds into your "idle". Search the output, you're probably interested in what you see just before the updating starts, or as it goes along.

                                M 1 Reply Last reply 21 May 2020, 18:27
                                0
                                • J JonB
                                  21 May 2020, 18:19

                                  @mzimmers
                                  Try not running your application, or switch the machine off ;-)

                                  Given that this is running on Windows, I can't use the profiler built into Creator

                                  Does your MSVC/MinGW compiler come with a usable profiler, nothing to do with Qt?

                                  In your paintEvent() override @mrjj had in mind for you to try a debugger breakpoint there and look at the stack trace. You will doubtless need to perhaps put a delay/count on the breakpoint, or set it while the app is in the middle of running. I don't actually know whether paint events will show anything interesting on the stack.

                                  You can use the top-level eventFilter() to examine most of what's going on, if you have to :( Debug every event's type to file/debug output window for a couple of seconds into your "idle". Search the output, you're probably interested in what you see just before the updating starts, or as it goes along.

                                  M Offline
                                  M Offline
                                  mzimmers
                                  wrote on 21 May 2020, 18:27 last edited by
                                  #16

                                  @JonB using MinGW, and I don't think it has a profiler. I could look at 3rd party products, but it's probably easier just to have a co-worker build this on Linux, and then run the profiler. (Hopefully the problem will occur on Linux!)

                                  Here's the stack trace you mentioned:
                                  stack.PNG
                                  Maybe you can see something useful in it; I can't.

                                  J 1 Reply Last reply 21 May 2020, 18:33
                                  0
                                  • M mzimmers
                                    21 May 2020, 18:27

                                    @JonB using MinGW, and I don't think it has a profiler. I could look at 3rd party products, but it's probably easier just to have a co-worker build this on Linux, and then run the profiler. (Hopefully the problem will occur on Linux!)

                                    Here's the stack trace you mentioned:
                                    stack.PNG
                                    Maybe you can see something useful in it; I can't.

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 21 May 2020, 18:33 last edited by JonB
                                    #17

                                    @mzimmers
                                    An expert may have something to say about the traceback. All I know is it would be nice to know which the QWidget is. Wait, this code is yours for Widget? So which of your widgets is it? If you break more than once, is it always the same widget?

                                    I can see QCoreApplication::sendSpontaneousEvent(). You sure you're not "wiggling"? :)

                                    (Hopefully the problem will occur on Linux!)

                                    Nope... ;-)

                                    M 1 Reply Last reply 21 May 2020, 18:39
                                    0
                                    • J JonB
                                      21 May 2020, 18:33

                                      @mzimmers
                                      An expert may have something to say about the traceback. All I know is it would be nice to know which the QWidget is. Wait, this code is yours for Widget? So which of your widgets is it? If you break more than once, is it always the same widget?

                                      I can see QCoreApplication::sendSpontaneousEvent(). You sure you're not "wiggling"? :)

                                      (Hopefully the problem will occur on Linux!)

                                      Nope... ;-)

                                      M Offline
                                      M Offline
                                      mzimmers
                                      wrote on 21 May 2020, 18:39 last edited by
                                      #18

                                      @JonB yes Widget is my oh-so-creative name for my main QWidget class, which is the only QWidget the app uses (unless you push one of the buttons). So yeah, it's always the same Widget.

                                      Someone else mentioned wiggling, but I'm not sure I know what it means. The problem occurs even when the app loses focus, though.

                                      And, if you're confident this won't happen on Linux, then maybe the problem isn't in my code space...

                                      J mrjjM 2 Replies Last reply 21 May 2020, 18:51
                                      0
                                      • M mzimmers
                                        21 May 2020, 18:39

                                        @JonB yes Widget is my oh-so-creative name for my main QWidget class, which is the only QWidget the app uses (unless you push one of the buttons). So yeah, it's always the same Widget.

                                        Someone else mentioned wiggling, but I'm not sure I know what it means. The problem occurs even when the app loses focus, though.

                                        And, if you're confident this won't happen on Linux, then maybe the problem isn't in my code space...

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 21 May 2020, 18:51 last edited by
                                        #19

                                        @mzimmers said in app using "excessive" CPU:

                                        And, if you're confident this won't happen on Linux, then maybe the problem isn't in my code space...

                                        Noooo, I put a wink --> ;-) <--
                                        I absolutely do not know whether it will repro under Linux, if you're lucky it will, I just meant sod's law it won't!

                                        1 Reply Last reply
                                        0
                                        • M mzimmers
                                          21 May 2020, 18:39

                                          @JonB yes Widget is my oh-so-creative name for my main QWidget class, which is the only QWidget the app uses (unless you push one of the buttons). So yeah, it's always the same Widget.

                                          Someone else mentioned wiggling, but I'm not sure I know what it means. The problem occurs even when the app loses focus, though.

                                          And, if you're confident this won't happen on Linux, then maybe the problem isn't in my code space...

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 21 May 2020, 19:30 last edited by
                                          #20

                                          @mzimmers
                                          Hi
                                          That stack trace is for

                                          1. starting app
                                            2: add break point
                                            3: let it loose focus to see the issue

                                          and not just set at startup so we ssee the first paint when it becomes visible?
                                          Just asking to be sure. Not seeing anything special besides maybe the sendSpontaneousEvent

                                          M 1 Reply Last reply 21 May 2020, 19:47
                                          0

                                          1/42

                                          20 May 2020, 22:14

                                          • Login

                                          • Login or register to search.
                                          1 out of 42
                                          • First post
                                            1/42
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved