Skip to content

High CPU usage since 9 version #140

Closed

Description

@SkeLLLa

Hi. I've noticed high CPU usage by prom-client since version 9 (10 also affected). Version 8 works fine.

Code is pretty simple:

const client = require('prom-client');
client.collectDefaultMetrics({ timeout: 1000 });

With default metrics enabled app eats ~60% of CPU. Without - ~0.1%. Node version 8.1.X.
If I have some time to look, I'll provide some additional info.

UPD:
It seems that problem is somewhere in general code, not specific metric, because even "version" metric eats 6-8% CPU. If I enable more metrics, it eats CPU proportionally.

Activity

  1. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    Oh, that's interesting. The only thing that really changed is timestamp support (#69 and #103). Other than that, just how metrics are constructed and ability to merge them, neither of which should impact runtime performance.

    Do you see the same thing with metrics without timestamps? Or if you remove timestamp support entirely?

  2. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor
  3. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    Just removing timestamp from the default metrics made no difference

  4. SkeLLLa commented on Jul 7, 2017

    @SkeLLLa
    ContributorAuthor

    @SimenB I've run with CPU profile and it shown that most of time it spends in "idle" state (aka (program)), so nothing unusual. It shouldn't eat so much CPU.

  5. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    hmm, I just see the issue on v10, not v9.

    sup.js:

    var m = require('.');
    m.collectDefaultMetrics()
    
    setInterval(() => console.log(m.register.metrics()), 1000)

    Then git checkout v9.1.1 && node sup.js in one terminal, and git checkout v10.0.0 && node sup.js in the other.

    image

    Top one is v10

  6. SkeLLLa commented on Jul 7, 2017

    @SkeLLLa
    ContributorAuthor

    @SimenB for me it reproduces on both: ^9.0.0 and ^10.0.0

  7. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    Running on node@8 puts v10 at 12% instead of 16%, but v9.1.1 stays at the same (0.1%).

    @SimenB for me it reproduces on both: ^9.0.0 and ^10.0.0

    Odd!

  8. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    v10 --prof output

     [Shared libraries]:
       ticks  total  nonlib   name
       5060   89.8%          /usr/lib/system/libsystem_kernel.dylib
          7    0.1%          /usr/lib/system/libsystem_pthread.dylib
          6    0.1%          /usr/lib/system/libsystem_c.dylib
          5    0.1%          /usr/lib/system/libsystem_platform.dylib
          1    0.0%          /usr/lib/system/libsystem_malloc.dylib
    

    v9

     [Shared libraries]:
       ticks  total  nonlib   name
         12   12.9%          /usr/lib/system/libsystem_kernel.dylib
          2    2.2%          /usr/lib/system/libsystem_pthread.dylib
          2    2.2%          /usr/lib/system/libsystem_platform.dylib
          1    1.1%          /usr/lib/system/libsystem_malloc.dylib
          1    1.1%          /usr/lib/system/libsystem_c.dylib
    
  9. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    Weird that we're getting different results...

    This commit is what drove cpu usage through the roof on my machine: c8a32d9

  10. SkeLLLa commented on Jul 7, 2017

    @SkeLLLa
    ContributorAuthor

    Probably that's because I use linux :).

    v9 results:

     [Shared libraries]:
       ticks  total  nonlib   name
        298    3.2%          /usr/bin/node
        254    2.7%          /lib64/libpthread-2.23.so
        115    1.2%          /lib64/libc-2.23.so
         10    0.1%          [vdso]
          9    0.1%          /usr/lib64/libuv.so.1.0.0
          6    0.1%          /usr/lib64/gcc/x86_64-pc-linux-gnu/5.3.0/libstdc++.so.6.0.21
    
     [Summary]:
       ticks  total  nonlib   name
        991   10.7%   11.5%  JavaScript
       7224   77.8%   84.1%  C++
         69    0.7%    0.8%  GC
        692    7.5%          Shared libraries
        371    4.0%          Unaccounted
    
     [C++]:
       ticks  total  nonlib   name
       5038   54.3%   58.7%  syscall
        269    2.9%    3.1%  __pthread_cond_signal
        184    2.0%    2.1%  __write
    

    v8 results:

     [Shared libraries]:
       ticks  total  nonlib   name
         62   30.2%          /usr/bin/node
          9    4.4%          /lib64/libc-2.23.so
          1    0.5%          /lib64/libpthread-2.23.so
    
     [Summary]:
       ticks  total  nonlib   name
          9    4.4%    6.8%  JavaScript
        105   51.2%   78.9%  C++
          3    1.5%    2.3%  GC
         72   35.1%          Shared libraries
         18    8.8%          Unaccounted
    
     [C++]:
       ticks  total  nonlib   name
         15    7.3%   11.3%  syscall
         14    6.8%   10.5%  __write
          7    3.4%    5.3%  _IO_vfprintf
    
    
  11. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    I don't get JavaScript. The diff fixing the crazy CPU usage for me is this

    diff --git i/lib/defaultMetrics.js w/lib/defaultMetrics.js
    index 077ce2f..cd737bd 100644
    --- i/lib/defaultMetrics.js
    +++ w/lib/defaultMetrics.js
    @@ -69,7 +69,7 @@ module.exports = function startDefaultMetrics(config) {
     
     	existingInterval = setInterval(
     		updateAllMetrics,
    -		normalizedConfig.interval
    +		10000
     	).unref();
     
     	init = false;
  12. SkeLLLa commented on Jul 7, 2017

    @SkeLLLa
    ContributorAuthor

    @SimenB

    normalizedConfig.interval
    

    doesn't equal

    normalizedConfig.timeout
    

    :)

  13. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    Oh, hahaha. So we passed undefined....

  14. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    Well, then... 10.0.2 published! Thanks for spotting it. /cc @siimon (I deprecated 10.0.0 and 10.0.1)

    But that still leaves your issue, @SkeLLLa. I can't reproduce on v9. Do you have a script I could run to test it? Which OS are you on?

    EDIT: Oh, Linux. I'm on a mac

  15. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    Are you able to bisect which commit causes the issue?

  16. SkeLLLa commented on Jul 7, 2017

    @SkeLLLa
    ContributorAuthor

    My code is in the first comment :). Just add setTimeout or setInterval to prevent node process to exit. I'll try to find out which commit causing this a bit later.

  17. SkeLLLa commented on Jul 7, 2017

    @SkeLLLa
    ContributorAuthor

    @SimenB With 9 version - it just my bad, because I used code from v10 with v9. So in v9 it received object instead of number for interval.

  18. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    Oh, so v10.0.2 fixed the problem?

  19. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    If not, I've created this Dockerfile and I'm ready to start bisecting

    FROM node:8
    
    USER node
    
    WORKDIR /home/node
    
    RUN git clone https://github.com/siimon/prom-client.git
    
    WORKDIR /home/node/prom-client
    
    RUN git checkout v8.0.0
    
    RUN npm install --only=production
    
    RUN echo "var m = require('./');\nm.collectDefaultMetrics && m.collectDefaultMetrics();\n\nsetInterval(() => console.log(m.register.metrics()), 1000);\n" > testFile.js
    
    CMD ["bash"]
  20. SkeLLLa commented on Jul 7, 2017

    @SkeLLLa
    ContributorAuthor

    @SimenB 10.0.2 is fine.

  21. SimenB commented on Jul 7, 2017

    @SimenB
    Contributor

    Perfect, thanks for pointing it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions