Skip to content

Commit

Permalink
Move stats related function to stats/
Browse files Browse the repository at this point in the history
This also adds a function to read restore stats and verifies the magic
numbers in the stats files.

Signed-off-by: Adrian Reber <areber@redhat.com>
  • Loading branch information
adrianreber committed Oct 29, 2021
1 parent a8c94b2 commit 130a18c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion phaul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
32 changes: 0 additions & 32 deletions phaul/stats.go

This file was deleted.

54 changes: 54 additions & 0 deletions stats/utils.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 130a18c

Please sign in to comment.