Daniel Herr
Daniel Herr

Reputation: 20438

Chrome.system.cpu API Usage

Is it possible to use the chrome.system.cpu api to get the current CPU load? I specifically can't figure out how to convert the returned numbers to a percent of the total current load.

I found this, but not quite sure how to implement: "Callers can compute load fractions by making two calls, subtracting the times, and dividing by the difference in totalTime."

https://developer.chrome.com/apps/system_cpu

Answer: An example of getting CPU usage is here: https://github.com/beaufortfrancois/cog-chrome-app/blob/master/src/main.js

Upvotes: 5

Views: 8907

Answers (2)

Marc Rochkind
Marc Rochkind

Reputation: 3740

If you want to compute the load on the processor(s) -- that is, the percentage of total available CPU time that's non-idle -- I would make the API call at regular intervals (say, once every 5 seconds), and, for each interval, compute difference in user + kernel from start to the end of the interval, divided by difference in total. That's the load for that interval. Then, if you like, you can plot these on a moving graph.

Sorry, no code to show, just a sketch of how I would do it.

Upvotes: 2

Richard Chassereau
Richard Chassereau

Reputation: 468

Yes, I believe it is possible to get the current CPU load. You may need to come up with an algorithm to find a better way to display it, and possibly refresh it using something like setInterval().

I ran this bit of code:

chrome.system.cpu.getInfo(function(info){       
   console.log(JSON.stringify(info));   
});

Here is what returns for me:

    {
   "archName":"x86_64",
   "features":[
      "mmx",
      "sse",
      "sse2",
      "sse3",
      "ssse3",
      "sse4_1",
      "sse4_2",
      "avx"
   ],
   "modelName":"Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz",
   "numOfProcessors":4,
   "processors":[
      {
         "usage":{
            "idle":1651683051644,
            "kernel":78195033247,
            "total":1886069562112,
            "user":156191477221
         }
      },
      {
         "usage":{
            "idle":1829832137618,
            "kernel":16966512759,
            "total":1886065818088,
            "user":39267167711
         }
      },
      {
         "usage":{
            "idle":1651957145401,
            "kernel":60555064171,
            "total":1886064570080,
            "user":173552360508
         }
      },
      {
         "usage":{
            "idle":1854233814038,
            "kernel":9238067218,
            "total":1886063166071,
            "user":22591284815
         }
      }
   ]
}

Upvotes: 1

Related Questions