diff --git a/phaul/client.go b/phaul/client.go index 71a67cd69..50ec3a198 100644 --- a/phaul/client.go +++ b/phaul/client.go @@ -110,7 +110,7 @@ func (pc *Client) Migrate() error { return err } - st, err := criuGetDumpStats(imgDir) + st, err := stats.CriuGetDumpStats(imgDir) if err != nil { return err } diff --git a/phaul/stats.go b/phaul/stats.go deleted file mode 100644 index d3821d314..000000000 --- a/phaul/stats.go +++ /dev/null @@ -1,32 +0,0 @@ -package phaul - -import ( - "os" - - "github.com/checkpoint-restore/go-criu/v5/stats" - "google.golang.org/protobuf/proto" -) - -/* FIXME: report stats from CriuResp */ -func criuGetDumpStats(imgDir *os.File) (*stats.DumpStatsEntry, error) { - stf, err := os.Open(imgDir.Name() + "/stats-dump") - if err != nil { - return nil, err - } - defer stf.Close() - - buf := make([]byte, 2*4096) - sz, err := stf.Read(buf) - if err != nil { - return nil, err - } - - st := &stats.StatsEntry{} - // Skip 2 magic values and entry size - err = proto.Unmarshal(buf[12:sz], st) - if err != nil { - return nil, err - } - - return st.GetDump(), nil -} diff --git a/stats/utils.go b/stats/utils.go new file mode 100644 index 000000000..368b2039e --- /dev/null +++ b/stats/utils.go @@ -0,0 +1,54 @@ +package stats + +import ( + "encoding/binary" + "errors" + "io/ioutil" + "os" + "path/filepath" + + "github.com/checkpoint-restore/go-criu/v5/magic" + "google.golang.org/protobuf/proto" +) + +func readStatisticsFile(imgDir *os.File, fileName string) (*StatsEntry, error) { + buf, err := ioutil.ReadFile(filepath.Join(imgDir.Name(), fileName)) + if err != nil { + return nil, err + } + + if binary.LittleEndian.Uint32(buf[magic.PrimaryMagicOffset:magic.SecondaryMagicOffset]) != magic.ImgServiceMagic { + return nil, errors.New("Primary magic not found") + } + + if binary.LittleEndian.Uint32(buf[magic.SecondaryMagicOffset:magic.SizeOffset]) != magic.StatsMagic { + return nil, errors.New("Secondary magic not found") + } + + payloadSize := binary.LittleEndian.Uint32(buf[magic.SizeOffset:magic.PayloadOffset]) + + st := &StatsEntry{} + if err := proto.Unmarshal(buf[magic.PayloadOffset:magic.PayloadOffset+payloadSize], st); err != nil { + return nil, err + } + + return st, nil +} + +func CriuGetDumpStats(imgDir *os.File) (*DumpStatsEntry, error) { + st, err := readStatisticsFile(imgDir, StatsDump) + if err != nil { + return nil, err + } + + return st.GetDump(), nil +} + +func CriuGetRestoreStats(imgDir *os.File) (*RestoreStatsEntry, error) { + st, err := readStatisticsFile(imgDir, StatsRestore) + if err != nil { + return nil, err + } + + return st.GetRestore(), nil +}