GitHub - green-coding-solutions/hog: A tool suit that collects data of programs that use a lot of energy (original) (raw)

The Power Hog

The power hog is a tool that periodically collects energy statistics of your mac and makes them available to you.

There are two main aims:

  1. Identify which apps are using a lot of energy on your machine.
  2. Collecting the data from as many machines as possible to identify wasteful apps globally.

We provide a website for detailed analytics of your data. The hog by default uploads your measurement data to ourGreen Metrics Tool backend. We put in a lot of effort to make sure that no confidential information is exposed but please refer to the settings section if you want to disable the upload or submit the data to your own backend.

The hog consists of 2 apps that run on your local system. You need to power logger but not the app!

Power logger

The background process power_logger.py which saves the power statists to the database. We use the mac internalpowermetrics tool to collect the data. Because the powermetrics tool needs to be run as root so does the power_logger script. The tool accepts one argument -d to run the tool in debug mode. It can also be sent the SIGINFO command to give some statistics. You can either call it by hand and send it to the background with & or define it an agent. For development purposes we recommend to always first run the program in the foreground and see if everything works fine and then use the launch agent.

If you want to avoid running the desktop app you can call the power_logger.py script with -w which will give you the details url.

You can also run the powermetrics process yourself and then use power_logger.py to process the data and upload it. You can use the -f parameter with a filename. Please submit the data in the plist format. You can use the following call string:powermetrics --show-all -i 5000 -f plist -o FILENAME and to run the powermetrics process yourself.

Parameter list

Setup of the power collection script

This is a description on how to set everything up if you did a git clone. You can also just do

sudo bash -c "$(curl -fsSL https://raw.githubusercontent.com/green-coding-solutions/hog/main/install.sh)"

which will do the whole install for you.

Do it manually

Make the power_logger.py script executable with chmod a+x power_logger.py

Please modify the io.green-coding.hogger.plist file to reference the right path. There is a script below that does everything for you.

Place the .plist file in the /Library/LaunchDaemons directory. For security reasons, files in /Library/LaunchDaemons/ should have their permissions set to be owned by root:wheel and should not be writable by others.

sed -i.bak "s|PATH_PLEASE_CHANGE|$(pwd)|g" io.green-coding.hogger.plist sudo cp io.green-coding.hogger.plist /Library/LaunchDaemons/

sudo chown root:wheel /Library/LaunchDaemons/io.green-coding.hogger.plist sudo chmod 644 /Library/LaunchDaemons/io.green-coding.hogger.plist

After placing the .plist file in the right directory, you need to tell launchd to load the new configuration:

sudo launchctl bootstrap system /Library/LaunchDaemons/io.green-coding.hogger.plist

You can check if your service is loaded with:

sudo launchctl list | grep io.green-coding

If you want to unload or stop the service:

sudo launchctl bootout system /Library/LaunchDaemons/io.green-coding.hogger.plist

Settings

It is possible to configure your own settings by using a settings.ini file in the same directory as the power_logger.pyscript. Or adding a hogger_settings.ini file to /etc/. This file in etc will be prioritized.

Following keys are currently used:

The desktop App

The hog desktop app gives you analytics of the data that was recorded. Please move this into your app folder.

Description of the headings

Database

All data is saved in an sqlite database that is located under:

/Library/Application Support/io.green-coding.hogger/db.db

Updating

We currently don't support an automatic update. You will have to:

sudo mv /etc/hogger_settings.ini /etc/hogger_settings.ini.back
sudo bash -c "$(curl -fsSL https://raw.githubusercontent.com/green-coding-solutions/hog/main/install.sh)"

Contributing

PRs are always welcome. Feel free to drop us an email or look into the issues.

The hog is developed to not need any dependencies.

Debugging

It sometimes help to enable debugging for the logger process you can do this by editing the /Library/LaunchDaemons/io.green-coding.hogger.plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>io.green-coding.hogger</string>

    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/hog/power_logger.py</string>
        <string>-v</string>
        <string>debug</string>
        <string>-o</string>
        <string>/tmp/hog.log</string>
    </array>

    <key>RunAtLoad</key>
    <true/>

    <key>KeepAlive</key>
    <true/>
</dict>

Please remember that the log file can become quite big. The hog does not use logrotate or similar out of the box.

Tests

You can run some simple tests by running the ./run_test.sh script the in the test folder. This is very basic!

Screenshots

Sources

Misc