title: Node.js 100% CPU - gettimeofday call
body: <p>I've got a long running node.js process that sometimes jumps to 100% CPU and stops responding to requests. The most recent time it did this I attached <code>strace</code> to the process, and here's what I saw:</p>
  
  <pre><code>Process 19523 attached - interrupt to quit
  gettimeofday({1394558574, 234192}, NULL) = 0
  gettimeofday({1394558574, 235381}, NULL) = 0
  gettimeofday({1394558574, 306460}, NULL) = 0
  mmap(0x3edab823a000, 2097152, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x3edab823a000
  munmap(0x3edab823a000, 811008)          = 0
  munmap(0x3edab8400000, 237568)          = 0
  mmap(0x3edab8300000, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x3edab8300000
  gettimeofday({1394558574, 316435}, NULL) = 0
  gettimeofday({1394558574, 316576}, NULL) = 0
  gettimeofday({1394558574, 316677}, NULL) = 0
  gettimeofday({1394558574, 316775}, NULL) = 0
  gettimeofday({1394558574, 316879}, NULL) = 0
  gettimeofday({1394558574, 316978}, NULL) = 0
  gettimeofday({1394558574, 317439}, NULL) = 0
  gettimeofday({1394558574, 317537}, NULL) = 0
  gettimeofday({1394558574, 318139}, NULL) = 0
  gettimeofday({1394558574, 318234}, NULL) = 0
  gettimeofday({1394558574, 318343}, NULL) = 0
  gettimeofday({1394558574, 318437}, NULL) = 0
  gettimeofday({1394558574, 318530}, NULL) = 0
  gettimeofday({1394558574, 318624}, NULL) = 0
  gettimeofday({1394558574, 319135}, NULL) = 0
  gettimeofday({1394558574, 319648}, NULL) = 0
  gettimeofday({1394558574, 319769}, NULL) = 0
  gettimeofday({1394558574, 319975}, NULL) = 0
  futex(0x7f5b380008c8, FUTEX_WAKE_PRIVATE, 1) = 1
  gettimeofday({1394558574, 322266}, NULL) = 0
  gettimeofday({1394558574, 322426}, NULL) = 0
  gettimeofday({1394558574, 322520}, NULL) = 0
  gettimeofday({1394558574, 322759}, NULL) = 0
  gettimeofday({1394558574, 322853}, NULL) = 0
  gettimeofday({1394558574, 322995}, NULL) = 0
  futex(0x7f5b380008c8, FUTEX_WAKE_PRIVATE, 1) = 1
  gettimeofday({1394558574, 417614}, NULL) = 0
  gettimeofday({1394558575, 386566}, NULL) = 0
  gettimeofday({1394558575, 387704}, NULL) = 0
  gettimeofday({1394558575, 463410}, NULL) = 0
  mmap(0x24cfd260f000, 2097152, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0) = 0x24cfd260f000
  munmap(0x24cfd260f000, 987136)          = 0
  munmap(0x24cfd2800000, 61440)           = 0
  mmap(0x24cfd2700000, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x24cfd2700000
  </code></pre>
  
  <p>That's a lot of <code>gettimeofday</code> calls, and not much else! What might cause node.js to get stuck like this?</p>
  
  <p><strong>UPDATE</strong>: I upgraded from an older version of node.js to 10.29 (I think) and this went away. I've just upgraded to 10.33 and the problem has come back. I've made a little more progress on debugging it this time out. First strace:</p>
  
  <pre><code>$ sudo strace -p 11812 -c
  Process 11812 attached - interrupt to quit
  Process 11812 detached
  % time     seconds  usecs/call     calls    errors syscall
  ------ ----------- ----------- --------- --------- ----------------
  100.00    0.000192           0      2780           gettimeofday
    0.00    0.000000           0      1390           getrusage
    0.00    0.000000           0        31           futex
    0.00    0.000000           0      1390           clock_gettime
  ------ ----------- ----------- --------- --------- ----------------
  100.00    0.000192                  5591           total
  </code></pre>
  
  <p>From <a href="https://stackoverflow.com/questions/13052548/node-js-how-to-attach-to-a-running-process-and-to-debug-the-server-with-a-conso">Node.js: How to attach to a running process and to debug the server with a console?</a> I found out about attaching the node debugger to a running process, and here's where I got with that:</p>
  
  <pre><code>$ sudo kill -s SIGUSR1 11812
  $ sudo node debug -p 11812
  connecting... ok
  break in timers.js:79
    77 }
    78
    79 function listOnTimeout() {
    80   var msecs = this.msecs;
    81   var list = this;
  debug&gt; bt
  #0 timers.js:79:23
  </code></pre>
  
  <p>So it looks like it is definitely related to a timer, but I don't know how to move further up the stack to find out where in my code the problem is getting triggered.</p>