Skip to content

Commit

Permalink
test block sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
CheranMahalingam committed Apr 25, 2024
1 parent 8ff3428 commit 7ce83ae
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 18 deletions.
15 changes: 3 additions & 12 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1674,20 +1674,11 @@ def go_deps():
)
go_repository(
name = "com_github_cockroachdb_pebble",
build_directives = [
"gazelle:build_tags invariants",
],
build_file_proto_mode = "disable_global",
importpath = "github.com/cockroachdb/pebble",
patch_args = ["-p1"],
patches = [
"@com_github_cockroachdb_cockroach//build/patches:com_github_cockroachdb_pebble.patch",
],
sha256 = "8cca4abbe3e4d4c758c27e500e2c4bc33d92343e97985f1b815d262283f1df64",
strip_prefix = "github.com/cockroachdb/pebble@v0.0.0-20240412230519-6a4bcf5f1aec",
urls = [
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/pebble/com_github_cockroachdb_pebble-v0.0.0-20240412230519-6a4bcf5f1aec.zip",
],
vcs = "git",
remote = "https://github.com/CheranMahalingam/pebble",
commit = "43a74eec18ef063492005a0129f0ce97ad5c9154",
)
go_repository(
name = "com_github_cockroachdb_redact",
Expand Down
20 changes: 17 additions & 3 deletions pkg/server/status/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ var (
Measurement: "Memory",
Unit: metric.Unit_BYTES,
}
metaCgoMetadataBytes = metric.Metadata{
Name: "sys.cgo.metadatabytes",
Help: "Current bytes of metadata allocated by cgo",
Measurement: "Memory",
Unit: metric.Unit_BYTES,
}
metaCgoTotalBytes = metric.Metadata{
Name: "sys.cgo.totalbytes",
Help: "Total bytes of memory allocated by cgo, but not released",
Expand Down Expand Up @@ -327,7 +333,7 @@ var diskMetricsIgnoredDevices = envutil.EnvOrDefaultString("COCKROACH_DISK_METRI
// allocated uint: bytes allocated by application
// total uint: total bytes requested from system
// error : any issues fetching stats. This should be a warning only.
var getCgoMemStats func(context.Context) (uint, uint, error)
var getCgoMemStats func(context.Context) (uint, uint, uint, error)

// Estimated total CPU time goroutines spent performing GC tasks to assist the
// GC and prevent it from falling behind the application. This metric is an
Expand Down Expand Up @@ -520,6 +526,7 @@ type RuntimeStatSampler struct {
GoHeapReleasedBytes *metric.Gauge
GoTotalAllocBytes *metric.Gauge
CgoAllocBytes *metric.Gauge
CgoMetadataBytes *metric.Gauge
CgoTotalBytes *metric.Gauge
GcCount *metric.Gauge
GcPauseNS *metric.Gauge
Expand Down Expand Up @@ -613,6 +620,7 @@ func NewRuntimeStatSampler(ctx context.Context, clock hlc.WallClock) *RuntimeSta
GoHeapReleasedBytes: metric.NewGauge(metaGoHeapReleasedBytes),
GoTotalAllocBytes: metric.NewGauge(metaGoTotalAllocBytes),
CgoAllocBytes: metric.NewGauge(metaCgoAllocBytes),
CgoMetadataBytes: metric.NewGauge(metaCgoMetadataBytes),
CgoTotalBytes: metric.NewGauge(metaCgoTotalBytes),
GcCount: metric.NewGauge(metaGCCount),
GcPauseNS: metric.NewGauge(metaGCPauseNS),
Expand Down Expand Up @@ -661,22 +669,24 @@ func NewRuntimeStatSampler(ctx context.Context, clock hlc.WallClock) *RuntimeSta
type CGoMemStats struct {
// CGoAllocated represents allocated bytes.
CGoAllocatedBytes uint64
CGoMetadataBytes uint64
// CGoTotal represents total bytes (allocated + metadata etc).
CGoTotalBytes uint64
}

// GetCGoMemStats collects non-Go memory statistics.
func GetCGoMemStats(ctx context.Context) *CGoMemStats {
var cgoAllocated, cgoTotal uint
var cgoAllocated, cgoMetadata, cgoTotal uint
if getCgoMemStats != nil {
var err error
cgoAllocated, cgoTotal, err = getCgoMemStats(ctx)
cgoAllocated, cgoMetadata, cgoTotal, err = getCgoMemStats(ctx)
if err != nil {
log.Warningf(ctx, "problem fetching CGO memory stats: %s; CGO stats will be empty.", err)
}
}
return &CGoMemStats{
CGoAllocatedBytes: uint64(cgoAllocated),
CGoMetadataBytes: uint64(cgoMetadata),
CGoTotalBytes: uint64(cgoTotal),
}
}
Expand Down Expand Up @@ -855,7 +865,11 @@ func (rsr *RuntimeStatSampler) SampleEnvironment(ctx context.Context, cs *CGoMem
rsr.CgoCalls.Update(numCgoCall)
rsr.Goroutines.Update(int64(numGoroutine))
rsr.RunnableGoroutinesPerCPU.Update(runnableAvg)
fmt.Printf("CGO A %d\n", cs.CGoAllocatedBytes)
fmt.Printf("CGO M %d\n", cs.CGoMetadataBytes)
fmt.Printf("CGO T %d\n", cs.CGoTotalBytes)
rsr.CgoAllocBytes.Update(int64(cs.CGoAllocatedBytes))
rsr.CgoMetadataBytes.Update(int64(cs.CGoMetadataBytes))
rsr.CgoTotalBytes.Update(int64(cs.CGoTotalBytes))
rsr.GcCount.Update(gc.NumGC)
rsr.GcPauseNS.Update(int64(gc.PauseTotal))
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/status/runtime_jemalloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ func init() {
getCgoMemStats = getJemallocStats
}

func getJemallocStats(ctx context.Context) (uint, uint, error) {
func getJemallocStats(ctx context.Context) (uint, uint, uint, error) {
var js C.JemallocStats
// TODO(marc): should we panic here? Failure on fetching the stats may be a problem.
if _, err := C.jemalloc_get_stats(&js); err != nil {
return 0, 0, err
return 0, 0, 0, err
}

if log.V(2) {
Expand All @@ -118,7 +118,7 @@ func getJemallocStats(ctx context.Context) (uint, uint, error) {
C.je_malloc_stats_print(nil, nil, nil)
}

return uint(js.Allocated), uint(js.Resident), nil
return uint(js.Allocated), uint(js.Metadata), uint(js.Resident), nil
}

// Used to force allocation in tests. 'import "C"' is not supported in tests.
Expand Down
6 changes: 6 additions & 0 deletions pkg/storage/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,12 @@ func DefaultPebbleOptions() *pebble.Options {
}
l.EnsureDefaults()
}
opts.AllocatorSizeClasses = []int{
16384,
20480, 24576, 28672, 32768,
40960, 49152, 57344, 65536,
81920, 98304, 114688, 131072,
}

return opts
}
Expand Down
45 changes: 45 additions & 0 deletions stats.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

num_allocs=$(cat flush | grep "ALLOC" | wc -l)
avg_bytes=$(($(cat flush | grep "ALLOC" | awk '{SUM+=$3}END{print SUM}')/$num_allocs))

num_64k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 57344 && $3 <= 65536) print $3}' | wc -l)
num_56k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 49152 && $3 <= 57344) print $3}' | wc -l)
num_48k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 40960 && $3 <= 49152) print $3}' | wc -l)
num_40k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 32768 && $3 <= 40960) print $3}' | wc -l)
num_32k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 28672 && $3 <= 32768) print $3}' | wc -l)
num_28k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 24576 && $3 <= 28672) print $3}' | wc -l)
num_24k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 20480 && $3 <= 24576) print $3}' | wc -l)
num_20k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 16384 && $3 <= 20480) print $3}' | wc -l)
num_16k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 14336 && $3 <= 16384) print $3}' | wc -l)

frag_bytes_64k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 57344 && $3 <= 65536) SUM+=(65536 - $3)}END{print SUM}')
frag_bytes_56k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 49152 && $3 <= 57344) SUM+=(57344 - $3)}END{print SUM}')
frag_bytes_48k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 40960 && $3 <= 49152) SUM+=(49152 - $3)}END{print SUM}')
frag_bytes_40k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 32768 && $3 <= 40960) SUM+=(40960 - $3)}END{print SUM}')
frag_bytes_32k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 28672 && $3 <= 32768) SUM+=(32768 - $3)}END{print SUM}')
frag_bytes_28k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 24576 && $3 <= 28672) SUM+=(28672- $3)}END{print SUM}')
frag_bytes_24k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 20480 && $3 <= 24576) SUM+=(24576 - $3)}END{print SUM}')
frag_bytes_20k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 16384 && $3 <= 20480) SUM+=(20480 - $3)}END{print SUM}')
frag_bytes_16k=$(cat flush | grep "ALLOC" | awk '{if ($3 > 14336 && $3 <= 16384) SUM+=(16384 - $3)}END{print SUM}')

avg_frag_bytes_64k=$(($frag_bytes_64k/$num_64k))
avg_frag_bytes_56k=$(($frag_bytes_56k/$num_56k))
avg_frag_bytes_48k=$(($frag_bytes_48k/$num_48k))
avg_frag_bytes_40k=$(($frag_bytes_40k/$num_40k))
avg_frag_bytes_32k=$(($frag_bytes_32k/$num_32k))
avg_frag_bytes_28k=$(($frag_bytes_28k/$num_28k))
avg_frag_bytes_24k=$(($frag_bytes_24k/$num_24k))
avg_frag_bytes_20k=$(($frag_bytes_20k/$num_20k))
avg_frag_bytes_16k=$(($frag_bytes_16k/$num_16k))

num_bounded_allocs=$(($num_64k + $num_56k + $num_48k + $num_40k + $num_32k + $num_28k + $num_24k + $num_20k + $num_16k))
avg_frag_bytes=$((($avg_frag_bytes_64k*$num_64k + $avg_frag_bytes_56k*$num_56k + $avg_frag_bytes_48k*$num_48k + $avg_frag_bytes_40k*$num_40k + $avg_frag_bytes_32k*$num_32k + $avg_frag_bytes_28k*$num_28k + $avg_frag_bytes_24k*$num_24k + $avg_frag_bytes_20k*$num_20k + $avg_frag_bytes_16k*$num_16k)/$num_bounded_allocs))

# 48 was manually selected since it took ~8 minutes for the cache to be saturated
jemalloc_alloc=$(cat flush | grep "CGO A" | tail -n +48 | awk '{SUM+=$3}END{printf "%f\n", SUM/NR/(1024*1024*1024)}')
jemalloc_metadata=$(cat flush | grep "CGO M" | tail -n +48 | awk '{SUM+=$3}END{printf "%f\n", SUM/NR/(1024*1024)}')
jemalloc_resident=$(cat flush | grep "CGO T" | tail -n +48 | awk '{SUM+=$3}END{printf "%f\n", SUM/NR/(1024*1024*1024)}')

echo -e "Allocations: $num_allocs\nAverage Allocation Size (B): $avg_bytes\nBins\n\tSize(KiB)\tCount\tFragmentation(B/Alloc)\n\t64\t$num_64k\t$avg_frag_bytes_64k\n\t56\t$num_56k\t$avg_frag_bytes_56k\n\t48\t$num_48k\t$avg_frag_bytes_48k\n\t40\t$num_40k\t$avg_frag_bytes_40k\n\t32\t$num_32k\t$avg_frag_bytes_32k\n\t28\t$num_28k\t$avg_frag_bytes_28k\n\t24\t$num_24k\t$avg_frag_bytes_24k\n\t20\t$num_20k\t$avg_frag_bytes_20k\n\t16\t$num_16k\t$avg_frag_bytes_16k\n\navg(Fragmentation): $avg_frag_bytes\n"
echo -e "Jemalloc Stats\nAverage Allocated Bytes (GiB): $jemalloc_alloc\nAverage Resident Bytes (GiB): $jemalloc_resident\nAverage Metadata Bytes (MiB): $jemalloc_metadata"

0 comments on commit 7ce83ae

Please sign in to comment.