38

GitHub - laffra/happymac: A Python Mac app to suspend background processes

 5 years ago
source link: https://github.com/laffra/happymac
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

HappyMac

687474703a2f2f63687269736c61666672612e636f6d2f68617070796d61632f68617070796d61632e77656270

HappyMac features:

  • Automatically suspends background processes
  • Stops your CPU from heating up
  • Preserves battery life
  • Makes your Mac happy again

Visit happymac.app for a ready to install DMG of HappyMac.

How it works

HappyMac is a status bar app for Mac. It looks like this when activated:

687474703a2f2f63687269736c61666672612e636f6d2f68617070796d61632f6d656e75332e706e67

The icon itself changes from happy to unhappy to sweating to mad, depending on the current load of your Mac. In the menu above, the machine is showing as sweating, as the overall CPU usage on the machine, including all available cores, is currently in between 51% and 75%.

The menu shows three categories of processes:

  • Current App Tasks: These are the processes that make up the process family of the currently active application. This includes the process that created the currently active window and all its parent processes up to the Launch process. Also included are all the child processes of the current process, their child processes, etc.

  • Background Tasks: These are tasks that use up CPU even though they run in the background. Literally hundreds of tasks normally run in the background and they only become troublesome when they run hot. In the menu shown below, there are 4 background tasks, that together use up about 30% of one CPU, which is about 8% of the entire machine.

    687474703a2f2f63687269736c61666672612e636f6d2f68617070796d61632f6d656e75342e706e67

    At any point in time you can decide to suspend any background task using the context menu, as is shown above. What happens is that the given task is suspended and moved to the suspended tasks. HappyMac also remembers this decision and will make sure the process is suspended any time it is not part of the foreground task family.

  • Suspended Background Tasks: Suspended tasks are those that are currently suspended by HappyMac. You can resume a suspended task by bringing the process to the foreground, assuming it has a UI. You can also manually resume a task using the context menu:

    687474703a2f2f63687269736c61666672612e636f6d2f68617070796d61632f6d656e75352e706e67

HappyMac Logic

Once a suspended process is activated, by using Cmd+Tab for instance, all the processes in its family are resumed. The logic for suspending and resuming is currently hardwired into HappyMac.

To remove noise, active processes that use up less than 3% of CPU are not shown. Unlike the overall CPU represented by the status bar icon, a given task can use more than 100% of CPU. This is the case when a task uses more than one core of the machine. Most Macbooks have 4 cores and some even 6. It is quite uncommon for a process to use more than 100% of the CPU. Few processes use more than 2 cores.

Just like the task bar icon, each individual task gets a different emoticon for each percentage of CPU it uses. The same scale is used. A happy process uses 25% CPU or less (of one core), an unhappy 50% or less, a sweating one 75% or less, and a mad one more than 75%.

A Practical Example

Say we are developing an Android app and start up the Intellij IDE. From it, we launch the Android emulator. CPU goes up, as we can tell as it will not take long for the Macbook's fan to turn on. When we switch to another application, both the idea and qemu-system-i386 tasks keep using CPU:

687474703a2f2f63687269736c61666672612e636f6d2f68617070796d61632f6d656e75362e706e67

Together, they use up more than two cores. Eventually, idea will slow down and not use any CPU anymore, but the second task (which is the Android emulator) keeps on using CPU, even when it is "not doing anything". With HappyMac, such processes can be automatically suspended. This is what the machine looks like with both processes suspended:

687474703a2f2f63687269736c61666672612e636f6d2f68617070796d61632f6d656e75372e706e67

Shown in blue above, my CPU is still kind of "unhappy". This is because CrashPlan and Carbon like to use up 30% of the CPU on average on my machine and this tends to go up higher when doing a large build. If I am low on battery, or connected over tether on my phone, you could decide to suspend things like backups temporarily and make your Mac really happy again:

687474703a2f2f63687269736c61666672612e636f6d2f68617070796d61632f7461736b6261722d68617070792e706e67

Admin Tasks

Sometimes, you may want to suspend a process that does not belong to your current user account. In such a case, HappyMac will ask you to provide an admin or root password for your machine, so it can "sudo" suspend the process. The dialogue will look like this:

687474703a2f2f63687269736c61666672612e636f6d2f68617070796d61632f61646d696e2e706e67

Critical Tasks

Some Mac tasks are part of the operating system and have a critical function. One such example is WindowServer. It is the process that draws your display and handles events. If you suspend that process, the only recovery would be to shut down your mac by pressing the power button for 5 seconds. Processes like WindowServer will not be suspended by HappyMac:

687474703a2f2f63687269736c61666672612e636f6d2f68617070796d61632f77696e646f777365727665722e706e67

Terminating Tasks

The context menu on processes listed in HappyMac have a menu item to Google for the meaning of the task. In addition to that, it also has a Terminate menu item. We strongly recommend you do not terminate processes. HappyMac will show a warning and ask you to confirm you really want to terminate the process, rather than suspend it.

Implementation

HappyMac is written in Python and uses psutil to list all processes every two seconds, compute their CPU, compute the current foreground task family and resume it, if needed. Any previously suspended process that is now running in the background will be suspended automatically as well. If the status bar menu is open, it is redrawn to show the current state.

To draw the status bar menu and the dialogs shown above, rumps is used.

The distribution, i.e., the DMG file, is created using PyInstaller.

Development

Want to contribute? Great! Simply download this git repo and send a pull request.

What is next?

It would be great to add more refined policies for suspending processes. Policies to consider:

  • Only suspend a background process when it uses more than 35%
  • Only suspend process X when I am on battery
  • When doing a build, don't do backups
  • When I am tethered to my phone, suspend process that use the network for more than X MB per minute
  • When the CPU is below 30% for a while, it is OK to run backups

Rules of Engagement

When you actually run HappyMac, be aware you agree with the Privacy Policy and Terms & Conditions.

Furthermore, please make sure you are in compliance with your company's IT policy when using HappyMac on your work laptop.

License

MIT

Build HappyMac Yourself

You can build HappyMac yourself, or run "python src/happymac.py" from a local repo.

However, for daily use, we suggest you visit happymac.app and download the ready to install DMG of HappyMac.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK