Skip to content

Commit

Permalink
Add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwin95r committed Jul 25, 2017
1 parent 262d7dd commit cb5117c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compact_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func compactLogIterate(filename string, f func(c *compaction)) error {
if err != nil {
return err
}
if _, err := fd.Read(buf[:1]); err != nil {
if _, err = fd.Read(buf[:1]); err != nil {
return err
}
c.done = buf[0]
Expand Down
4 changes: 4 additions & 0 deletions kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,10 @@ func (s *KV) get(key []byte) (y.ValueStruct, error) {
tables, decr := s.getMemTables() // Lock should be released.
defer decr()

y.NumGets.Add(1)
for i := 0; i < len(tables); i++ {
vs := tables[i].Get(key)
y.NumMemtableGets.Add(1)
if vs.Meta != 0 || vs.Value != nil {
return vs, nil
}
Expand Down Expand Up @@ -698,6 +700,7 @@ func (s *KV) sendToWriteCh(entries []*Entry) []*request {
b.Entries = append(b.Entries, entry)
if size >= s.opt.maxBatchSize {
s.writeCh <- b
y.NumPuts.Add(int64(len(b.Entries)))
reqs = append(reqs, b)
size = 0
b = nil
Expand All @@ -706,6 +709,7 @@ func (s *KV) sendToWriteCh(entries []*Entry) []*request {

if size > 0 {
s.writeCh <- b
y.NumPuts.Add(int64(len(b.Entries)))
reqs = append(reqs, b)
}
return reqs
Expand Down
9 changes: 7 additions & 2 deletions level_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package badger

import (
"bytes"
"fmt"
"sort"

"github.com/dgraph-io/badger/table"
Expand All @@ -37,6 +38,7 @@ type levelHandler struct {

// The following are initialized once and const.
level int
strLevel string
maxTotalSize int64
kv *KV
}
Expand Down Expand Up @@ -138,8 +140,9 @@ func (s *levelHandler) replaceTables(newTables []*table.Table) {

func newLevelHandler(kv *KV, level int) *levelHandler {
return &levelHandler{
level: level,
kv: kv,
level: level,
strLevel: fmt.Sprintf("l%d", level),
kv: kv,
}
}

Expand Down Expand Up @@ -219,12 +222,14 @@ func (s *levelHandler) get(key []byte) (y.ValueStruct, error) {

for _, th := range tables {
if th.DoesNotHave(key) {
y.NumLSMBloomHits.Add(s.strLevel, 1)
continue
}

it := th.NewIterator(false)
defer it.Close()

y.NumLSMGets.Add(s.strLevel, 1)
it.Seek(key)
if !it.Valid() {
continue
Expand Down
3 changes: 1 addition & 2 deletions levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ func (s *levelsController) compactBuildTables(
// Encode the level number as table metadata.
var levelNum [2]byte
binary.BigEndian.PutUint16(levelNum[:], uint16(l+1))
_, err = fd.Write(builder.Finish(levelNum[:]))
if err != nil {
if _, err := fd.Write(builder.Finish(levelNum[:])); err != nil {
che <- errors.Wrapf(err, "Unable to write to file: %d", fileID)
return
}
Expand Down
6 changes: 5 additions & 1 deletion table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ func (t *Table) read(off int, sz int) ([]byte, error) {
}

res := make([]byte, sz)
_, err := t.fd.ReadAt(res, int64(off))
nbr, err := t.fd.ReadAt(res, int64(off))
y.NumReads.Add(1)
y.NumBytesRead.Add(int64(nbr))
return res, err
}

Expand Down Expand Up @@ -363,5 +365,7 @@ func (t *Table) LoadToRAM() error {
if err != nil || read != t.tableSize {
return y.Wrapf(err, "Unable to load file in memory. Table file: %s", t.Filename())
}
y.NumReads.Add(1)
y.NumBytesRead.Add(int64(read))
return nil
}
6 changes: 5 additions & 1 deletion value.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ func (lf *logFile) read(buf []byte, offset int64) error {
lf.RLock()
defer lf.RUnlock()

_, err := lf.fd.ReadAt(buf, offset)
nbr, err := lf.fd.ReadAt(buf, offset)
y.NumReads.Add(1)
y.NumBytesRead.Add(int64(nbr))
return err
}

Expand Down Expand Up @@ -619,6 +621,8 @@ func (l *valueLog) write(reqs []*request) error {
if err != nil {
return errors.Wrapf(err, "Unable to write to value log file: %q", curlf.path)
}
y.NumWrites.Add(1)
y.NumBytesWritten.Add(int64(n))
l.elog.Printf("Done")
curlf.offset += uint32(n)
l.buf.Reset()
Expand Down
46 changes: 46 additions & 0 deletions y/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package y

import (
"expvar"
)

var (
// These are cumulative
NumGets *expvar.Int
NumPuts *expvar.Int
NumReads *expvar.Int
NumWrites *expvar.Int
NumBytesRead *expvar.Int
NumBytesWritten *expvar.Int
NumMemtableGets *expvar.Int
NumLSMGets *expvar.Map
NumLSMBloomHits *expvar.Map
)

func init() {
NumReads = expvar.NewInt("badger_disk_reads_total")
NumWrites = expvar.NewInt("badger_disk_writes_total")
NumBytesRead = expvar.NewInt("badger_read_bytes")
NumBytesWritten = expvar.NewInt("badger_written_bytes")
NumGets = expvar.NewInt("badger_gets_total")
NumPuts = expvar.NewInt("badger_puts_total")
NumMemtableGets = expvar.NewInt("badger_memtable_gets_total")
NumLSMGets = expvar.NewMap("badger_lsm_level_gets_total")
NumLSMBloomHits = expvar.NewMap("badger_lsm_bloom_hits_total")
}

0 comments on commit cb5117c

Please sign in to comment.