Initial work on rapl-daemon. Initial server set up. API to read max power per zone and API to write new power cap have both been written.
This commit is contained in:
parent
3543960689
commit
fb9e88e07d
4 changed files with 164 additions and 0 deletions
49
rapl-daemon/main.go
Normal file
49
rapl-daemon/main.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const powercapDir = "/sys/class/powercap/"
|
||||
|
||||
// Cap is a payload that is expected from Elektron to cap a node
|
||||
type Cap struct {
|
||||
Percentage int
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "Unsupported endpoint %s", html.EscapeString(r.URL.Path))
|
||||
})
|
||||
|
||||
http.HandleFunc("/powercap", powercapEndpoint)
|
||||
log.Fatal(http.ListenAndServe(":9090", nil))
|
||||
}
|
||||
|
||||
// Handler powercapping HTTP API endpoint
|
||||
func powercapEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
var payload Cap
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&payload)
|
||||
if err != nil {
|
||||
http.Error(w, "Error parsing payload: "+err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
if payload.Percentage < 0 || payload.Percentage > 100 {
|
||||
http.Error(w, "Bad payload: percentage must be between 0 and 100", 400)
|
||||
return
|
||||
}
|
||||
|
||||
err = capNode(powercapDir, payload.Percentage)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "Capped node at %d percent", payload.Percentage)
|
||||
}
|
Reference in a new issue