Accessing Intel® Power Gadget 2.0 library in C++

With the release of Intel® Power Gadget 2.0 there is now externally callable API’s to extract power information within sections of code. The API layer is a set of libraries and dlls that can be called via C/C#/C++ or via JavaScript and offers the flexibility to build the tool within code sections of an application. Intel® Power Gadget 2.0’s main engine is written as ATL COM that supports Javascript, C++, and C#. The gadget itself is a sample javascript code to use the library. In order to look at the source, rename the “CPUPowerData.gadget” to “CPUPowerData.zip” and unzip it. You should be able to see the source to see how the APIs are used. In this article, a sample code is provided to illustrate how to use the library’s API through C++. Another article to use C# is planned in the future. The sample project can be downloaded here.

Initializating COM


The beginning of CppSample.cpp starts with COM initialization as follows so that COM is initialized before _tmain and de-initialized after _tmain is done. EzPwrLibrary.dll itself should be imported as a header.


Setting up parameters



  • Create a smart pointer: create an interface to the library - "IEzPwrCtrlPtr pEzPwrCtl" followed by instantiation -"pEzPwrCtl.CreateInstance(L”EzPwrLibrary.EzPwrCtl”);"

  • Log file: The library takes a log file name and append the time stamp to the log file name. In order to specify the file name, assign the file name to "ExportFileName" property. Usually, full path to the file is specified. In order to get the file name, use "GetExportFileName" or "GetExportFileNameExtended" to get the exact file name with the time stamp appended. In order for GetExportFileNameExtended to succeed, the logging should have been started by setting "Logging" property to 1. Setting "Logging" property to 0 stops the logging.

  • Sampling Resolution: The gadget itself is updated once every second. But you can specify much finer sampling resolution for the logging by calling  "Reset(millisecond)" method. It is not guaranteed to get the specified resolution due to thread scheduling, but it will try to get up to the resolution. It is recommended to set this to 50 ms.



Getting Power and Frequency Information


All the methods in the library are non-blocking calls, but the actual data is updated once per second. The log however, has much finer resolution as specified in the "Reset" method.

  • Multi-socket support: The library supports multi-socket systems and can get the power and frequency information per socket. You can get the number of sockets in the system by calling "GetNumSockets" method.

  • Frequency: In order to get the base frequency (maximum frequency without turbo,) call "GetMaxFreq" method. Be aware that modern processors including Penryn, Nehalem, Sandy Bridge, and Ivy Bridge can not only throttle down the frequency to save power, it can go higher than the base frequency temporarily for performance. It is very important to understand the frequency is not fixed to the base frequency to properly understand power / performance. CPU utilization alone is not going to tell the whole story - x % of CPU utilization really doesn't mean much without the running frequency information at the moment. "GetFrequency" method gives the average frequency from all the sockets, and GetSocketFrequency(int iSocket) gives individual frequency for each socket. If there is only one socket, both methods have the same result.

  • Power: There are 2 ways to get power - total power from all the sockets (GetPackagePower) and individual sockets (GetSocketPower(int iSocket)). GetPackagePower is just a sum of all GetSocketPower.



Clean up


Be sure to stop logging and close & release the interface.

For more complete information about compiler optimizations, see our Optimization Notice.