Intel CPU Web API Documentation and Examples

The Intel CPU Web API provides web developers with JavaScript access to information about the CPU and current CPU utilization.

Contents

Overview

When web apps have the ability to learn about some of the characteristics of the device's CPU and the current CPU load they are able to make use of that information to create a better experience for the user.

For instance, in combination with other information about the device, a web app might use CPU information to request a video stream which is approprate for the device's CPU decode capability. A web app may determine that current CPU utilization is too high for the app to function appropriatly for the desired user experience - and may prompt the user to shut down some applicaitons before proceeding.

Browser and Device Compatibility

The table below lists some compatability notes for browser and operating system combinations. This API is currently beta quality.

If you are interested in seeing these APIs available on other device types and operating systems please let us know by leaving comments.

BrowserOperating SystemNotes
Firefox 3.5.x, 3.6.x Windows (XP, Vista, Win 7) Seems to be working well.
Chrome 4.0.x.y Windows (XP, Vista, Win 7) Seems to be working well.
Safari 4.0.x Windows (XP, Vista, Win 7) Seems to be working well.
Opera v 10.x Windows (XP, Vista, Win 7) Not currently working. It's on the debug todo list.
Internet Explorer 8.x Windows (XP, Vista, Win 7) Not supported yet. Its on the todo list though.
Firefox, Chrome, Safari Linux, MacOS Not supported yet. Linux and Mac OS porting is underway :)

A code sample to get you started

This HTML & JavaScript shows you how to embed the Intel CPU Web API into your app.

  <div id="IntelWebAPIs_sample_code_status_div" 
    style="border:1px dotted red;width:640px;height:480px;overflow:auto;"></div>
  <script 
    type="text/javascript" 
    src="http://software.intel.com/sites/whatif/webapis/intelwebapis.js">
  </script>
  <script type="text/javascript">

    // this outer closure just to keep the sample code from cluttering up
    // the namespace with the function names etc being used
    (function() {
      
      //kick off IntelWebAPIs.init() and IntelWebAPIs.require() when
      //the page is loaded
      add_onload_handler( function() {

        IntelWebAPIs.init(init_result_func);
        
        //called by IntelWebAPIs.init()
        function init_result_func(result) {
          if(result.success) {
            // now bring in Intel Web APIs required by your app...
            output_status("IntelWebAPIs Connector init success");
            IntelWebAPIs.require(["cpu"], require_completion_func, require_progress_func);
          }
          else {
            output_status("IntelWebAPIs Connector init error: " + result.error.msg);
            //handle error condition
            //result.error.msg contains an error message
            //result.connectorInstUrl contains the url to the Intel Web API connector installer
            output_status("you may need to download the Intel Web API connector from here: " + 
              result.connectorInstUrl);
          }
        }
    
        //called by IntelWebAPIs.require()
        function require_completion_func(result) {
          if(result.success){
            //you can now use IntelWebAPIs.cpu properties and functions
            output_status("IntelWebAPIs." + result.installInfo.plugins[0].name + " ready for use!");
            output_status("Your CPU Name is: " + IntelWebAPIs.cpu.name);
            setInterval(function(){
              // send a status message to our div every second
              var cpuLoad = Math.round((IntelWebAPIs.cpu.load * 100) * 100)/100;
              output_status("CPU Load =" + cpuLoad + " %");
            },1000);
          }
          else {
            //check other result properties and handle error condition
            output_status("IntelWebAPIs error: " + result.installInfo.msg);
          }
        }
        
        //called by IntelWebAPIs.require()
        function require_progress_func(statusMsg){
          //statusMsg is a string - send this wherever
          output_status("IntelWebAPIs connector status: " + statusMsg);
        }
      
      });
      
      /* Its recommended that you wait until the document is loaded 
         before calling IntelWebAPIs.init() and IntelWebAPIs.require() 
         This utility helps with that...*/
      function add_onload_handler(addfunc, altwindow) {
        var targwindow = window;
        if(typeof(altwindow) != 'undefined'){
          targwindow = altwindow;
        }
        
        /* chain the onload functions together */
        var prevf = targwindow.onload;
        var newf  = addfunc;
        targwindow.onload = function(){
            newf();
            if(typeof(prevf) == 'function'){
              prevf();
            }
          };
      }
      
      /* utility that outputs status messages to a div*/
      function output_status(str) {
        var divout = document.getElementById("IntelWebAPIs_sample_code_status_div");
        divout.innerHTML += str + "<br/>";
        divout.scrollTop = divout.scrollHeight;
      }
    })(); // end of outer closure
  </script>

This is the result Object that is passed into init_result_func(result).
If the Intel Web API Connector is not yet installed on a user's device you can use the result.connectorInstUrl to prompt the user, in the context of your app, to install it.
Once the Intel Web API Connector is installed other apis are installed by the connector, with minimal user interruption, when your app calls IntelWebAPIs.require().

  result = { 
    success           : boolean,
    
    // these fields are valid if success != true
    connectorInstUrl  : string 
    error             : {
      msg             : string 
    } 
  }

This is the result Object that is passed into require_completion_func(result).

  result = { 
    success       : boolean,
    
    installInfo   : {
      msg         : string,
      
      plugins     : [ { 
        name      : string,
        installed : boolean,
        details   : string
        }, 
        //...
      ], 
    }
  }  

JavaScript API Details
These are the properties exposed by the CPU Web API:

  IntelWebAPIs.cpu = {
    'version'     : string,
    'name'        : string,
    'load'        : float
  };

IntelWebAPIs.cpu.version
This is the API's version string. It is formatted as "major.minor.patch" where major and minor are numbers and patch is a string.

IntelWebAPIs.cpu.name
The IntelWebAPIs.cpu.name property provides the device's CPU name string. This is useful for applications that want to determine which app features should run on the client device and which features should run in the cloud.

IntelWebAPIs.cpu.load
The IntelWebAPIs.cpu.load property provides the device's CPU load. 0.0 is the minimum possible load and 1.0 is maximum possible load. Multiplying by 100 gives converts the value to a percentage.


These JavaScript functions demonstrate accessing the cpu information:

  function output_cpu_name(){
    alert("cpu name = " + IntelWebAPIs.cpu.name);
  }
  
  function output_cpu_load(){
    var currLoad = IntelWebAPIs.cpu.load * 100;
    alert("cpu load = " + currLoad + " %");
  }



See it working in this page!
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.