Skip to content
 
 

Repository files navigation

macmon – Mac Metrics Monitor Library

Sudoless performance monitoring library for Apple Silicon processors.

Motivation

Apple Silicon processors don't provide an easy way to see live power consumption. This library uses a private macOS API to gather metrics (essentially the same as powermetrics) but runs without sudo privileges. πŸŽ‰

🌟 Features

  • 🚫 Works without sudo
  • ⚑ Real-time CPU / GPU / ANE power usage
  • πŸ“Š CPU utilization per cluster
  • πŸ’Ύ RAM / Swap usage
  • 🌑️ CPU / GPU temperature monitoring
  • πŸ¦€ Written in Rust with Swift and C bindings

πŸ“¦ Installation

  1. Clone the repo:
git clone https://github.com/razorback16/macmon.git && cd macmon
  1. Build the library:
cargo build --release

πŸš€ Usage

Swift

import Foundation

// Create MacMon instance
let monitor = try MacMon()

// Get metrics
let metrics = try monitor.getMetrics()

// Convert to JSON and print
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted]

if let jsonData = try? encoder.encode(metrics),
   let jsonString = String(data: jsonData, encoding: .utf8) {
    print(jsonString)
}

C

#include <stdio.h>
#include "macmon.h"

int main() {
    // Create a new sampler
    void *sampler = sampler_new();
    if (!sampler) {
        printf("Failed to create sampler\n");
        return 1;
    }

    // Get metrics
    Metrics *metrics = sampler_get_metrics(sampler);
    if (!metrics) {
        printf("Failed to get metrics\n");
        sampler_free(sampler);
        return 1;
    }

    // Access metrics
    printf("CPU Temperature: %.2fΒ°C\n", metrics->temp.cpu_temp_avg);
    printf("GPU Temperature: %.2fΒ°C\n", metrics->temp.gpu_temp_avg);
    printf("CPU Power: %.2f W\n", metrics->cpu_power);
    printf("GPU Power: %.2f W\n", metrics->gpu_power);

    // Cleanup
    metrics_free(metrics);
    sampler_free(sampler);

    return 0;
}

πŸ“Š Metrics Structure

The library provides the following metrics:

{
  "temp": {
    "cpu_temp_avg": 43.73614,         // Celsius
    "gpu_temp_avg": 36.95167          // Celsius
  },
  "memory": {
    "ram_total": 25769803776,         // Bytes
    "ram_usage": 20985479168,         // Bytes
    "swap_total": 4294967296,         // Bytes
    "swap_usage": 2602434560          // Bytes
  },
  "ecpu_usage": [1181, 0.082656614],  // (Frequency MHz, Usage %)
  "pcpu_usage": [1974, 0.015181795],  // (Frequency MHz, Usage %)
  "gpu_usage": [461, 0.021497859],    // (Frequency MHz, Usage %)
  "cpu_power": 0.20486385,            // Watts
  "gpu_power": 0.017451683,           // Watts
  "ane_power": 0.0,                   // Watts
  "all_power": 0.22231553,            // Watts
  "sys_power": 5.876533,              // Watts
  "ram_power": 0.11635789,            // Watts
  "gpu_ram_power": 0.0009615385       // Watts
}

🀝 Contributing

We love contributions! Whether you have ideas, suggestions, or bug reports, feel free to open an issue or submit a pull request.

πŸ“ License

macmon is distributed under the MIT License. See LICENSE for more details.

πŸ” See also

About

πŸ¦€βš™οΈ Sudoless performance monitoring for Apple Silicon processors. CPU / GPU / RAM usage, power consumption & temperature 🌑️

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages