Rapl node capping daemon (#21)

* 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.
This commit is contained in:
Renan I. Del Valle 2020-01-19 11:52:30 -08:00 committed by PRADYUMNA KAUSHIK
parent 3543960689
commit e76c1ae972
5 changed files with 311 additions and 0 deletions

60
rapl-daemon/main.go Normal file
View file

@ -0,0 +1,60 @@
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)
}