Files
kopia/cli/command_content_stats.go
Jarek Kowalski fa7976599c repo: refactored repository interfaces (#780)
- `repo.Repository` is now read-only and only has methods that can be supported over kopia server
- `repo.RepositoryWriter` has read-write methods that can be supported over kopia server
- `repo.DirectRepository` is read-only and contains all methods of `repo.Repository` plus some low-level methods for data inspection
- `repo.DirectRepositoryWriter` contains write methods for `repo.DirectRepository`

- `repo.Reader` removed and merged with `repo.Repository`
- `repo.Writer` became `repo.RepositoryWriter`
- `*repo.DirectRepository` struct became `repo.DirectRepository`
  interface

Getting `{Direct}RepositoryWriter` requires using `NewWriter()` or `NewDirectWriter()` on a read-only repository and multiple simultaneous writers are supported at the same time, each writing to their own indexes and pack blobs.

`repo.Open` returns `repo.Repository` (which is also `repo.RepositoryWriter`).

* content: removed implicit flush on content manager close
* repo: added tests for WriteSession() and implicit flush behavior
* invalidate manifest manager after write session

* cli: disable maintenance in 'kopia server start'
  Server will close the repository before completing.

* repo: unconditionally close RepositoryWriter in {Direct,}WriteSession
* repo: added panic in case somebody tries to create RepositoryWriter after closing repository
  - used atomic to manage SharedManager.closed

* removed stale example
* linter: fixed spurious failures

Co-authored-by: Julio López <julio+gh@kasten.io>
2021-01-20 11:41:47 -08:00

91 lines
2.0 KiB
Go

package cli
import (
"context"
"fmt"
"strconv"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/units"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content"
)
var (
contentStatsCommand = contentCommands.Command("stats", "Content statistics")
contentStatsRaw = contentStatsCommand.Flag("raw", "Raw numbers").Short('r').Bool()
)
func runContentStatsCommand(ctx context.Context, rep repo.DirectRepository) error {
var sizeThreshold uint32 = 10
countMap := map[uint32]int{}
totalSizeOfContentsUnder := map[uint32]int64{}
var sizeThresholds []uint32
for i := 0; i < 8; i++ {
sizeThresholds = append(sizeThresholds, sizeThreshold)
countMap[sizeThreshold] = 0
sizeThreshold *= 10
}
var totalSize, count int64
if err := rep.ContentReader().IterateContents(
ctx,
content.IterateOptions{
Range: contentIDRange(),
},
func(b content.Info) error {
totalSize += int64(b.Length)
count++
for s := range countMap {
if b.Length < s {
countMap[s]++
totalSizeOfContentsUnder[s] += int64(b.Length)
}
}
return nil
}); err != nil {
return errors.Wrap(err, "error iterating contents")
}
sizeToString := units.BytesStringBase10
if *contentStatsRaw {
sizeToString = func(l int64) string { return strconv.FormatInt(l, 10) }
}
fmt.Println("Count:", count)
fmt.Println("Total:", sizeToString(totalSize))
if count == 0 {
return nil
}
fmt.Println("Average:", sizeToString(totalSize/count))
fmt.Printf("Histogram:\n\n")
var lastSize uint32
for _, size := range sizeThresholds {
fmt.Printf("%9v between %v and %v (total %v)\n",
countMap[size]-countMap[lastSize],
sizeToString(int64(lastSize)),
sizeToString(int64(size)),
sizeToString(totalSizeOfContentsUnder[size]-totalSizeOfContentsUnder[lastSize]),
)
lastSize = size
}
return nil
}
func init() {
contentStatsCommand.Action(directRepositoryReadAction(runContentStatsCommand))
setupContentIDRangeFlags(contentStatsCommand)
}