From cb5117ce1779c6112c1ecc9008ef4466ae374f7b Mon Sep 17 00:00:00 2001 From: Ashwin Ramesh Date: Tue, 25 Jul 2017 17:37:27 +1000 Subject: [PATCH] Add metrics --- compact_log.go | 2 +- kv.go | 4 ++++ level_handler.go | 9 +++++++-- levels.go | 3 +-- table/table.go | 6 +++++- value.go | 6 +++++- y/metrics.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 y/metrics.go diff --git a/compact_log.go b/compact_log.go index c9ba32eb5..1c90e0c3f 100644 --- a/compact_log.go +++ b/compact_log.go @@ -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] diff --git a/kv.go b/kv.go index 71dfc21c9..b46b1b78e 100644 --- a/kv.go +++ b/kv.go @@ -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 } @@ -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 @@ -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 diff --git a/level_handler.go b/level_handler.go index e6bac99ee..de3d97df4 100644 --- a/level_handler.go +++ b/level_handler.go @@ -18,6 +18,7 @@ package badger import ( "bytes" + "fmt" "sort" "github.com/dgraph-io/badger/table" @@ -37,6 +38,7 @@ type levelHandler struct { // The following are initialized once and const. level int + strLevel string maxTotalSize int64 kv *KV } @@ -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, } } @@ -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 diff --git a/levels.go b/levels.go index 761c8e7c4..b9b3f6e5b 100644 --- a/levels.go +++ b/levels.go @@ -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 } diff --git a/table/table.go b/table/table.go index d0d81d924..61fe511ed 100644 --- a/table/table.go +++ b/table/table.go @@ -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 } @@ -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 } diff --git a/value.go b/value.go index d349c160d..03f7c7d7b 100644 --- a/value.go +++ b/value.go @@ -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 } @@ -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() diff --git a/y/metrics.go b/y/metrics.go new file mode 100644 index 000000000..aedb76e5d --- /dev/null +++ b/y/metrics.go @@ -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") +}