Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tmsmr committed Oct 17, 2021
0 parents commit 8644e56
Show file tree
Hide file tree
Showing 12 changed files with 361 additions and 0 deletions.
84 changes: 84 additions & 0 deletions .github/workflows/release-chilld.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: release chilld

on:
push:
tags:
- '*'

jobs:
release_chilld:
name: release_chilld
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: set up go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: store tag name
id: store_tag
run: echo "::set-output name=release_tag::${GITHUB_REF/refs\/tags\//}"
- name: create build folder
run: mkdir $GITHUB_WORKSPACE/build
- name: build arm target
run: |
cd $GITHUB_WORKSPACE
GOOS=linux GOARCH=arm go build -v -o build/chilld-${{ steps.store_tag.outputs.release_tag }}.linux-arm
- name: build arm64 target
run: |
cd $GITHUB_WORKSPACE
GOOS=linux GOARCH=arm64 go build -v -o build/chilld-${{ steps.store_tag.outputs.release_tag }}.linux-arm64
- name: compress build artifacts
id: compress_artifacts
run: |
cd $GITHUB_WORKSPACE/build
tar czvf chilld-${{ steps.store_tag.outputs.release_tag }}.linux-arm.tar.gz chilld-${{ steps.store_tag.outputs.release_tag }}.linux-arm
tar czvf chilld-${{ steps.store_tag.outputs.release_tag }}.linux-arm64.tar.gz chilld-${{ steps.store_tag.outputs.release_tag }}.linux-arm64
echo "::set-output name=arm_path::$GITHUB_WORKSPACE/build/chilld-${{ steps.store_tag.outputs.release_tag }}.linux-arm.tar.gz"
echo "::set-output name=arm_name::chilld-${{ steps.store_tag.outputs.release_tag }}.linux-arm.tar.gz"
echo "::set-output name=arm64_path::$GITHUB_WORKSPACE/build/chilld-${{ steps.store_tag.outputs.release_tag }}.linux-arm64.tar.gz"
echo "::set-output name=arm64_name::chilld-${{ steps.store_tag.outputs.release_tag }}.linux-arm64.tar.gz"
- name: create SHA512SUMS.txt
id: create_shasums
run: |
cd $GITHUB_WORKSPACE/build
for f in *.tar.gz; do sha512sum $f >> SHA512SUMS.txt; done
echo "::set-output name=shasums_path::$GITHUB_WORKSPACE/build/SHA512SUMS.txt"
- name: create release
uses: actions/create-release@v1
id: create_release
with:
draft: false
prerelease: false
release_name: ${{ steps.store_tag.outputs.release_tag }}
tag_name: ${{ steps.store_tag.outputs.release_tag }}
body_path: CHANGELOG.md
env:
GITHUB_TOKEN: ${{ github.token }}
- name: upload arm artifact
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ github.token }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ steps.compress_artifacts.outputs.arm_path }}
asset_name: ${{ steps.compress_artifacts.outputs.arm_name }}
asset_content_type: application/gzip
- name: upload arm64 artifact
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ github.token }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ steps.compress_artifacts.outputs.arm64_path }}
asset_name: ${{ steps.compress_artifacts.outputs.arm64_name }}
asset_content_type: application/gzip
- name: upload SHA512SUMS.txt
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ github.token }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ steps.create_shasums.outputs.shasums_path }}
asset_name: SHA512SUMS.txt
asset_content_type: text/plain
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
chilld
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- initial version
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Thomas Maier

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.
52 changes: 52 additions & 0 deletions LICENSES-3D-PARTY
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
chilld uses third-party libraries or other resources that may
be distributed under licenses different than the chilld software.

1) License for https://github.com/go-daq/smbus
---
Copyright ©2016 The go-daq Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the go-daq project nor the names of its authors and
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

2.) License for https://github.com/rs/zerolog
---
MIT License

Copyright (c) 2017 Olivier Poitrey

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.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ChillD

*POC: Thermal controller for the Raspberry Pi Compute Module 4 IO Board.*

**ChillD is a simple *user space* thermal controller for the [CM4IO](https://www.raspberrypi.com/products/compute-module-4-io-board/). It periodically...**
- Reads the BCM2711's temperature using the Generic Thermal Sysfs driver of the linux kernel
- Calculates the target fan speed in a [linear](https://github.com/tmsmr/chilld/blob/main/fancurve/linear.go) manner
- Controls a fan connected to the PWM fan connector on the CM4IO board

I mainly built this as an application example for https://github.com/tmsmr/cm4iofan. Please note [should-i-use-this-for-my-247-running-project](https://github.com/tmsmr/cm4iofan#should-i-use-this-for-my-247-running-project).

## Requirements
See https://github.com/tmsmr/cm4iofan#requirements

## Usage

### Get it
- Clone the Repository and build the `chilld` executable using `go build`, or
- Build the `chilld` executable using `go install github.com/tmsmr/[email protected]` to find it in `$GOPATH/bin`, or
- Download the latest tagged version from the Release page

### Use it
- No configuration needed (nor available at the moment...)
- Execute it: `./chilld`

## Comments

### System service
- Feel free to adjust and use [chilld.service](./chilld.service)
- Scripts for OpenRC, SysVinit shouldn't be a big deal

### User with access to I2C
ChillD needs a user with access to the I2C bus. If you are using Raspian/RaspiOS, the group `i2c` should be available for that purpose.

Otherwise you may:
- Add a user for ChillD: `useradd -r -M -s /bin/false chilld`
- Add the I2C group: `groupadd i2c`
- Add the user to the created group: `usermod -aG i2c chilld`
- Add a udev rule to assign the group: `echo 'KERNEL=="i2c-[0-9]*", GROUP="i2c"' >> /etc/udev/rules.d/10-i2c_group.rules`
72 changes: 72 additions & 0 deletions chilld.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"flag"
"os"
"os/signal"
"syscall"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/tmsmr/chilld/fancurve"
"github.com/tmsmr/cm4iofan"
"github.com/tmsmr/pithermal"
)

const (
ticksPerSecond = 1
)

func main() {
debug := flag.Bool("debug", false, "sets log level to debug")
flag.Parse()
if !*debug {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC1123})

log.Info().Msgf("starting ChillD")

fanctl, err := cm4iofan.New()
if err != nil {
log.Fatal().Str("err", err.Error()).Msg("failed to initialize fan controller")
}

trap := make(chan os.Signal, 1)
signal.Notify(trap, syscall.SIGINT, syscall.SIGTERM)

ticks := time.NewTicker((1000 / ticksPerSecond) * time.Millisecond)
defer ticks.Stop()

currspeed := -1

go func() {
for range ticks.C {
speed := 100
temp, terr := pithermal.GetCpuTemp()
if terr != nil {
log.Error().Str("err", err.Error()).Msg("failed to read cpu temp, using maximum fan speed")
} else {
speed = fancurve.LinearFanSpeedFor(temp)
}
if speed != currspeed {
msg := "setting fan speed to"
if terr == nil {
log.Debug().Msgf("%s %d%% @ %.2f°C", msg, speed, temp)
} else {
log.Warn().Msgf("%s %d%% @ UNKNOWN°C", msg, speed)
}
if err := fanctl.SetDutyCycle(speed); err != nil {
log.Error().Str("err", err.Error()).Msg("failed to to set target speed in fan controller")
} else {
currspeed = speed
}
}
}
}()

<-trap
log.Info().Msg("ChillD terminating, trying to set maximum fan speed")
_ = fanctl.SetDutyCycle(100)
}
12 changes: 12 additions & 0 deletions chilld.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=ChillD - CM4IO thermal controller
After=systemd-modules-load.service

[Service]
ExecStart=/usr/local/sbin/chilld -debug
User=chilld
Group=chilld
Restart=always

[Install]
WantedBy=sysinit.target
3 changes: 3 additions & 0 deletions fancurve/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/tmsmr/chilld/fancurve

go 1.17
26 changes: 26 additions & 0 deletions fancurve/linear.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fancurve

import "math"

const (
coolingStart = 35
)

// LinearFanSpeedFor calculates the fan speed in % for a given CPU temperature.
// For temperatures < coolingStart, 0% is returned. This only makes sense for a fan that is able to stop.
// The target speed is calculated using a simple linear function y = mx + b (Capped to 100%).
// To avoid frequent RPM changes, the result is rounded to the nearest 5%.
func LinearFanSpeedFor(temp float64) int {
if temp < coolingStart {
return 0
}
// 0% @ 20°C
// 20% @ 35°C
// 100% @ 75°C
speed := 2*temp - 50
if speed > 100 {
return 100
}
speed = math.Round(speed/5) * 5
return int(speed)
}
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/tmsmr/chilld

go 1.17

require (
github.com/rs/zerolog v1.25.0
github.com/tmsmr/chilld/fancurve v0.0.0-20211017163227-30f0ba0c4a85
github.com/tmsmr/cm4iofan v0.9.0
github.com/tmsmr/pithermal v0.9.0
)

require github.com/go-daq/smbus v0.0.0-20201216173259-5725b4593606 // indirect
37 changes: 37 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/go-daq/smbus v0.0.0-20201216173259-5725b4593606 h1:1lyGjYJXpYkF6fGQbhAIOdIaeymKQuS6RJVLVlTzc5w=
github.com/go-daq/smbus v0.0.0-20201216173259-5725b4593606/go.mod h1:KDYiP7UvrtHRqnVLE6ziKupbojinMHjFUdo+1VA7r/Q=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.25.0 h1:Rj7XygbUHKUlDPcVdoLyR91fJBsduXj5fRxyqIQj/II=
github.com/rs/zerolog v1.25.0/go.mod h1:7KHcEGe0QZPOm2IE4Kpb5rTh6n1h2hIgS5OOnu1rUaI=
github.com/tmsmr/chilld/fancurve v0.0.0-20211017163227-30f0ba0c4a85 h1:5OTpTxR3+ggCsHtWgbFYsf5KSfmMk8RG5J3IaG6CIpA=
github.com/tmsmr/chilld/fancurve v0.0.0-20211017163227-30f0ba0c4a85/go.mod h1:bYpMfRydebpnrhSO/MZEMpYuDr794fdL6pOm5WEinws=
github.com/tmsmr/cm4iofan v0.9.0 h1:9woqmITJHsTmrCKoRk4K9Pdb1lQJd5kpXhkHoUbaCH0=
github.com/tmsmr/cm4iofan v0.9.0/go.mod h1:bMaIIA2n1G04xWKy4vMAtrcL554vvMhipc29RUUEkIE=
github.com/tmsmr/pithermal v0.9.0 h1:zgNJEAkdYmR6kpqEPTohD6yfZXsvnYX6WPAAhxEQNZk=
github.com/tmsmr/pithermal v0.9.0/go.mod h1:kS+PcnzcpysrNH2WIWdxJPR3rgpEyZICbLG6ojiYHyw=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit 8644e56

Please sign in to comment.