This repository has been archived on 2024-04-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
elektron/rapl-daemon/main.go
Renan I. Del Valle e76c1ae972 Rapl node capping daemon ()
* 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.

* Removing python script since this has been ported to go code now.

* Adding test for happy path retrieving max power.

* Change some data types around to avoid too much conversion.

* Add happy path test for cap zone

* Removing uncessary print statement.

* Change cap node to use a temporary setup.

* Renaming arguments to be more descriptive.

* Changing todo message.

* Changing test structure to only set up mock subsystem once and allowing functions to test on it later.

* Adding some more coverage for unhappy paths and fixing some values to reflect they are no longer floats.

* Keeping the old script around as it should be removed in a different PR.

* Delegating percentage check to capNode function.

* Fixing typo.

* Fixing typos.

* Changing shortWindow to longWindow as constraint_0 actually points to the long window.

* Renaming variable in test.

* capping funciton now returns which zones were sucessfully capped and which zones could not be capped. This information is now returned to the caller of the HTTP api.
2020-01-19 14:52:30 -05:00

60 lines
1.4 KiB
Go

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
}
// CapResponse is the payload sent with information about the capping call
type CapResponse struct {
CappedZones []string `json:"cappedZones"`
FailedZones []string `json:"failedZones"`
Error *string `json:"error"`
}
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 for the powercapping HTTP API endpoint.
func powercapEndpoint(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var payload Cap
var response CapResponse
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&payload)
if err != nil {
errorMsg := "error parsing payload: " + err.Error()
response.Error = &errorMsg
json.NewEncoder(w).Encode(response)
return
}
cappedZones, failedZones, err := capNode(powercapDir, payload.Percentage)
if err != nil {
errorMsg := err.Error()
response.Error = &errorMsg
}
response.CappedZones = cappedZones
response.FailedZones = failedZones
json.NewEncoder(w).Encode(response)
}