Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metric): Add dgraph_memory_alloc_bytes to track jemalloc memory. #7051

Merged
merged 4 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dgraph/cmd/alpha/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestMetrics(t *testing.T) {

// Dgraph Memory Metrics
"dgraph_memory_idle_bytes", "dgraph_memory_inuse_bytes", "dgraph_memory_proc_bytes",
"dgraph_memory_alloc_bytes",
// Dgraph Activity Metrics
"dgraph_active_mutations_total", "dgraph_pending_proposals_total",
"dgraph_pending_queries_total",
Expand Down
21 changes: 21 additions & 0 deletions x/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ var (
// PendingProposals records the current number of pending RAFT proposals.
PendingProposals = stats.Int64("pending_proposals_total",
"Number of pending proposals", stats.UnitDimensionless)
// MemoryAlloc records the amount of memory allocated via jemalloc
MemoryAlloc = stats.Int64("memory_alloc_bytes",
"Amount of memory allocated", stats.UnitBytes)
// MemoryInUse records the current amount of used memory by Dgraph.
MemoryInUse = stats.Int64("memory_inuse_bytes",
"Amount of memory in use", stats.UnitBytes)
Expand Down Expand Up @@ -192,6 +195,13 @@ var (
Aggregation: view.LastValue(),
TagKeys: nil,
},
{
Name: MemoryAlloc.Name(),
Measure: MemoryAlloc,
Description: MemoryAlloc.Description(),
Aggregation: view.LastValue(),
TagKeys: allTagKeys,
},
{
Name: MemoryInUse.Name(),
Measure: MemoryInUse,
Expand Down Expand Up @@ -447,8 +457,13 @@ func MonitorMemoryMetrics(lc *z.Closer) {
defer lc.Done()
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
fastTicker := time.NewTicker(time.Second)
defer fastTicker.Stop()

update := func() {
// ReadMemStats stops the world which is expensive especially when the
// heap is large. So don't call it too frequently. Calling it every
// minute is OK.
var ms runtime.MemStats
runtime.ReadMemStats(&ms)

Expand All @@ -467,14 +482,20 @@ func MonitorMemoryMetrics(lc *z.Closer) {
MemoryIdle.M(int64(idle)),
MemoryProc.M(int64(getMemUsage())))
}
updateAlloc := func() {
ostats.Record(context.Background(), MemoryAlloc.M(z.NumAllocBytes()))
}
// Call update immediately so that Dgraph reports memory stats without
// having to wait for the first tick.
update()
updateAlloc()

for {
select {
case <-lc.HasBeenClosed():
return
case <-fastTicker.C:
updateAlloc()
case <-ticker.C:
update()
}
Expand Down