Giter VIP home page Giter VIP logo

cpufreq-swift's Introduction

CPUFreq - Swift 3 and 4 bindings to CPUFreq in Linux sysfs

The project provides the CPUFreq Swift package - a thin wrapper around the CPUFreq Linux interface in sysfs for getting and setting DVFS-related configurations. Its purpose is to abstract programs from file I/O and data parsing/conversion. It does not provide core/domain/socket discovery or DVFS management logic.

For Linux kernel documentation on CPUFreq, see: https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt

Building

To build, run:

swift build

Usage

A simple example of setting DVFS frequencies with the userspace governor:

import CPUFreq

// in practice, core IDs could be discovered dynamically using other means...
let bindings: [CPUFreq] = [ CPUFreq(cpu: 0), CPUFreq(cpu: 1) ]

// populate "availableFreqs" array
let availableFreqs: [UInt32] = bindings[0].getScalingAvailableFrequencies()!
// won't use that file again, so we can manually close it to free up resources
bindings[0].closeFile(file: CPUFreqFile.scalingAvailableFrequencies)

// set "userspace" governor on all cores
for cpu in bindings {
    let _ = cpu.setScalingGovernor(governor: "userspace")
    // again, close the file manually to free up resources
    cpu.closeFile(file: CPUFreqFile.scalingGovernor)
}

// do application work, breaking from loop when finished...
while (true) {
    // in practice, a new frequency would be determined dynamically by some scheduling logic...
    let freq = availableFreqs[0];
    for cpu in bindings {
        let _ = cpu.setScalingSetspeed(freq: freq);
    }
}

// once bindings go out of scope, any open files (e.g. CPUFreqFile.scalingSetspeed) are closed automatically

The files scaling_governor, scaling_min_freq, and scaling_max_freq are opened as read/write by default, but may be opened as read-only by specifying readOnly: True as a function parameter when they are first accessed. The file scaling_setspeed is opened as write-only. All other files are opened as read-only. Users are responsible for ensuring that their programs have correct read/write privileges.

Project Source

Find this and related project sources at the powercap organization on GitHub.
This project originates at: https://github.com/powercap/CPUFreq-Swift

Bug reports and pull requests are welcome.

cpufreq-swift's People

Contributors

connorimes avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.