Package: pyJoules
PyJoulesAboutpyJoulesis a software toolkit to measure the energy footprint of a host machine along the execution of a piece of Python code.
It monitors the energy consumed by specific device of the host machine such as :intel CPU socket packageRAM (for intel server architectures)intel integrated GPU (for client architectures)nvidia GPULimitationCPU, RAM and integrated GPUpyJoulesuses the Intel "Running Average Power Limit" (RAPL) technology that estimates power consumption of the CPU, ram and integrated GPU.
This technology is available on Intel CPU since theSandy Bridge generation(2010).Nvidia GPUpyJoulesuses the nvidia "Nvidia Management Library" technology to measure energy consumption of nvidia devices. The energy measurement API is only available on nvidia GPU withVolta architecture(2018)InstallationMeasurement frequencyPyJoule use hardware measurement tools (intel RAPL, nvidia GPU tools, ...) to measure device energy consumption. Theses tools have a mesasurement frequency that depend of the device. Thus, you can't use Pyjoule to measure energy consumption during a period shorter than the device energy measurement frequency. Pyjoule will return null values if the measurement period is to short.Requirementspython >= 3.7nvml(if you want nvidia GPU support)InstallationYou can installpyJouleswith pip:pip install pyJoulesif you want to use pyJoule to also measure nvidia GPU energy consumption, you have to install it with nvidia driver support using this command :pip install pyJoules[nvidia].Basic usageThis Readme describe basic usage of pyJoules. For more in depth description, read the documentationhereHere are some basic usages ofpyJoules. Please note that the reported energy consumption is not only the energy consumption of the code you are running. This includes theglobal energy consumptionof all the process running on the machine during this period, thus including the operating system and other applications.
That is why we recommend to eliminate any extra programs that may alter the energy consumption of the machine hosting experiments and to keeponlythe code under measurement (i.e., no extra applications, such as graphical interface, background running task...). This will give the closest measure to the real energy consumption of the measured code.Decorate a function to measure its energy consumptionTo measure the energy consumed by the machine during the execution of the functionfoo()run the following code:frompyJoules.energy_meterimportmeasure_energy@measure_energydeffoo():# Instructions to be evaluated.foo()This will print on the console the recorded energy consumption of all the monitorable devices during the execution of functionfoo.Output descriptiondecorator basic usage will print iformation with this format :begin timestamp : XXX; tag : YYY; duration : ZZZ;device_name: AAAAwith :begin timestamp: monitored function launching timetag: tag of the measure, if nothing is specified, this will be the function nameduration: function execution durationdevice_name: power consumption of the devicedevice_namein uJfor cpu and ram devices, device_name match the RAPL domain described on the image below plus the CPU socket id. Rapl domain are describedhereConfigure the decorator specifying the device to monitorYou can easily configure which device to monitor using the parameters of themeasureitdecorator.
For example, the following example only monitors the CPU power consumption on the CPU socket1and the Nvidia GPU0.
By default,pyJoulesmonitors all the available devices of the CPU sockets.frompyJoules.energy_meterimportmeasure_energyfrompyJoules.device.rapl_deviceimportRaplPackageDomainfrompyJoules.device.nvidia_deviceimportNvidiaGPUDomain@measure_energy(domains=[RaplPackageDomain(1),NvidiaGPUDomain(0)])deffoo():# Instructions to be evaluated.foo()You can append the following domain list to monitor them :pyJoules.device.rapl_device.RaplPackageDomain: CPU (specify the socket id in parameter)pyJoules.device.rapl_device.RaplDramDomain: RAM (specify the socket id in parameter)pyJoules.device.rapl_device.RaplUncoreDomain: integrated GPU (specify the socket id in parameter)pyJoules.device.rapl_device.RaplCoreDomain: RAPL Core domain (specify the socket id in parameter)pyJoules.device.nvidia_device.NvidiaGPUDomain: Nvidia GPU (specify the socket id in parameter)to understand which par of the cpu each RAPL domain monitor, see thissectionConfigure the output of the decoratorIf you want to handle data with different output than the standard one, you can configure the decorator with anEnergyHandlerinstance from thepyJoules.handlermodule.As an example, if you want to write the recorded energy consumption in a .csv file:frompyJoules.energy_meterimportmeasure_energyfrompyJoules.handler.csv_handlerimportCSVHandlercsv_handler=CSVHandler('result.csv')@measure_energy(handler=csv_handler)deffoo():# Instructions to be evaluated.for_inrange(100):foo()csv_handler.save_data()This will produce a csv file of 100 lines. Each line containing the energy
consumption recorded during one execution of the functionfoo.
Other predefinedHandlerclasses exist to export data toMongoDBandPandadataframe.Use a context manager to add tagged "breakpoint" in your measurmentIf you want to know where is the "hot spots" where your python code consume the
most energy you can add "breakpoints" during the measurement process and tag
them to know amount of energy consumed between this breakpoints.For this, you have to use a context manager to measure the energy
consumption. It is configurable as the decorator. For example, here we use anEnergyContextto measure the power consumption of CPU1and nvidia gpu0and report it in a csv file :frompyJoules.energy_meterimportEnergyContextfrompyJoules.device.rapl_deviceimportRaplPackageDomainfrompyJoules.device.nvidia_deviceimportNvidiaGPUDomainfrompyJoules.handler.csv_handlerimportCSVHandlercsv_handler=CSVHandler('result.csv')withEnergyContext(handler=csv_handler,domains=[RaplPackageDomain(1),NvidiaGPUDomain(0)],start_tag='foo')asctx:foo()ctx.record(tag='bar')bar()csv_handler.save_data()This will record the energy consumed :between the beginning of theEnergyContextand the call of thectx.recordmethodbetween the call of thectx.recordmethod and the end of theEnergyContextEach measured part will be written in the csv file. One line per part.RAPL domain descriptionRAPL domains match part of the cpu socket as described in this image :Package : correspond to the wall cpu energy consumptioncore : correpond to the sum of all cpu core energy consumptionuncore : correspond to the integrated GPUMiscellaneousAboutpyJoulesis an open-source project developed by theSpirals research group(University of Lille and Inria) that is part of thePowerAPIinitiative.The documentation is availablehere.Mailing listYou can follow the latest news and asks questions by subscribing to ourmailing list.ContributingIf you would like to contribute code, you can do so via GitHub by forking the repository and sending a pull request.When submitting code, please make every effort to follow existing coding conventions and style in order to keep the code as readable as possible.MIT LicenseCopyright (c) 2019, INRIA
Copyright (c) 2019, University of Lille
All rights reserved.Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.