blob: 2d12369e3953a5d42be9a65595ac467bf870038f [file] [log] [blame]
// Copyright 2018 The Fuchsia 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 Google Inc. nor the names of its
// 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
// OWNER 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.
package main
import (
"context"
"encoding/csv"
"flag"
"log"
"os"
"os/signal"
"strconv"
"fuchsia.googlesource.com/zedmon"
"github.com/google/subcommands"
)
type recordCmd struct{}
// Flag-controlled values.
var out string
var writeHeader bool
var resistance float64
func (*recordCmd) Name() string { return "record" }
func (*recordCmd) Synopsis() string {
return "Record data from zedmon. Saves to zedmon.csv or streams to stdout."
}
func (*recordCmd) Usage() string {
return `record
`
}
func (*recordCmd) SetFlags(flags *flag.FlagSet) {
setCommonFlags(flags)
flags.StringVar(&out, "out", "zedmon.csv", "Output file. Specify \"-\" for stdout.")
flags.BoolVar(&writeHeader, "header", false, "Whether to include a header line in the output.")
}
// If a positive resistance and field names including "v_bus" and "v_shunt" are provided, returns a
// function that calculates power from zedmon-provided values. Returns nil otherwise.
func getPowerCalculator(resistance float64, fieldNames []string) func(values []float64) float64 {
if resistance <= 0.0 {
return nil
}
vBusIndex := -1
vShuntIndex := -1
for i, name := range fieldNames {
if name == "v_bus" {
vBusIndex = i
} else if name == "v_shunt" {
vShuntIndex = i
}
}
if vBusIndex < 0 || vShuntIndex < 0 {
return nil
}
return func(values []float64) float64 {
return values[vShuntIndex] * values[vBusIndex] / resistance
}
}
func (*recordCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
zedmon, err := zedmon.OpenZedmon(getConfig())
if err != nil {
log.Printf("Can't open zendmon: %v", err)
return subcommands.ExitFailure
}
defer zedmon.Close()
var f *os.File
if out == "-" {
f = os.Stdout
} else {
f, err = os.OpenFile(out, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
log.Printf("Can't open %s: %v", out, err)
return subcommands.ExitFailure
}
defer f.Close()
}
offset, delta, err := zedmon.GetTimeOffset()
if err != nil {
log.Printf("Get time offset: %v", err)
return subcommands.ExitFailure
}
log.Printf("Time offset: %dns ± %dns\n", offset.Nanoseconds(), delta.Nanoseconds())
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, os.Interrupt)
writer := csv.NewWriter(f)
defer writer.Flush()
log.Println("Starting report recording. Send ^C to stop.")
zedmon.EnableReporting()
fieldNames := zedmon.GetFieldNames()
var calculatePower func(values []float64) float64 = nil
resistance, err := zedmon.GetShuntResistance()
if err != nil {
log.Println("Error fetching resistance:", err)
log.Println("Power will not be calculated.")
} else {
calculatePower = getPowerCalculator(resistance, fieldNames)
}
if calculatePower != nil {
fieldNames = append(fieldNames, "power")
}
if writeHeader {
header := make([]string, 1+len(fieldNames))
header[0] = "timestamp"
copy(header[1:], fieldNames)
writer.Write(header)
}
RecordSampling:
for {
reports, err := zedmon.ReadReports()
if err != nil {
log.Printf("Error reading reports: %v", err)
continue
}
for _, report := range reports {
record := make([]string, 0, len(fieldNames))
record = append(record, strconv.FormatUint(report.Timestamp, 10))
for _, value := range report.Values {
record = append(record, strconv.FormatFloat(value, 'g', -1, 64))
}
if calculatePower != nil {
power := calculatePower(report.Values)
record = append(record, strconv.FormatFloat(power, 'g', -1, 64))
}
writer.Write(record)
}
select {
case <-termChan:
break RecordSampling
default:
}
}
log.Println("Stopping report recording.")
return subcommands.ExitSuccess
}