-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from adrianreber/2021-10-29-stats-functions
Move statistics read funtion to stats/
- Loading branch information
Showing
6 changed files
with
73 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package magic | ||
|
||
const ( | ||
ImgCommonMagic = 0x54564319 /* Sarov (a.k.a. Arzamas-16) */ | ||
ImgServiceMagic = 0x55105940 /* Zlatoust */ | ||
StatsMagic = 0x57093306 /* Ostashkov */ | ||
|
||
PrimaryMagicOffset = 0x0 | ||
SecondaryMagicOffset = 0x4 | ||
SizeOffset = 0x8 | ||
PayloadOffset = 0xC | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package stats | ||
|
||
const ( | ||
StatsDump = "stats-dump" | ||
StatsRestore = "stats-restore" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |