| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package memory | 5 package memory |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/hex" | 8 "encoding/hex" |
| 9 "flag" | 9 "flag" |
| 10 "fmt" | 10 "fmt" |
| 11 "io/ioutil" | 11 "io/ioutil" |
| 12 "os" | 12 "os" |
| 13 "path/filepath" | 13 "path/filepath" |
| 14 "runtime" | 14 "runtime" |
| 15 "strings" | 15 "strings" |
| 16 "sync" | 16 "sync" |
| 17 "sync/atomic" | 17 "sync/atomic" |
| 18 | |
| 19 "github.com/luci/gkvlite" | |
| 20 ) | 18 ) |
| 21 | 19 |
| 22 var logMemCollectionFolder = flag.String( | 20 var logMemCollectionFolder = flag.String( |
| 23 » "luci.gae.gkvlite_trace_folder", "", | 21 » "luci.gae.store_trace_folder", "", |
| 24 "Set to a folder path to enable debugging traces to be dumped there. Set
to '-' to dump to stdout.") | 22 "Set to a folder path to enable debugging traces to be dumped there. Set
to '-' to dump to stdout.") |
| 25 var logMemCollectionFolderTmp string | 23 var logMemCollectionFolderTmp string |
| 26 var logMemCollectionOnce sync.Once | 24 var logMemCollectionOnce sync.Once |
| 27 var logMemCounter uint32 | 25 var logMemCounter uint32 |
| 28 var logMemNameKey = "holds a string indicating the GKVLiteDebuggingTraceName" | 26 var logMemNameKey = "holds a string indicating the StoreDebuggingTraceName" |
| 29 var stdoutLock sync.Mutex | 27 var stdoutLock sync.Mutex |
| 30 | 28 |
| 31 func wrapTracingMemStore(store memStore) memStore { | 29 func wrapTracingMemStore(store memStore) memStore { |
| 32 var writer traceWriter | 30 var writer traceWriter |
| 33 logNum := atomic.AddUint32(&logMemCounter, 1) - 1 | 31 logNum := atomic.AddUint32(&logMemCounter, 1) - 1 |
| 34 collName := fmt.Sprintf("coll%d", logNum) | 32 collName := fmt.Sprintf("coll%d", logNum) |
| 35 | 33 |
| 36 if *logMemCollectionFolder == "-" { | 34 if *logMemCollectionFolder == "-" { |
| 37 writer = func(format string, a ...interface{}) { | 35 writer = func(format string, a ...interface{}) { |
| 38 stdoutLock.Lock() | 36 stdoutLock.Lock() |
| 39 defer stdoutLock.Unlock() | 37 defer stdoutLock.Unlock() |
| 40 fmt.Printf(format+"\n", a...) | 38 fmt.Printf(format+"\n", a...) |
| 41 } | 39 } |
| 42 } else { | 40 } else { |
| 43 logMemCollectionOnce.Do(func() { | 41 logMemCollectionOnce.Do(func() { |
| 44 var err error | 42 var err error |
| 45 » » » logMemCollectionFolderTmp, err = ioutil.TempDir(*logMemC
ollectionFolder, "luci-gae-gkvlite_trace") | 43 » » » logMemCollectionFolderTmp, err = ioutil.TempDir(*logMemC
ollectionFolder, "luci-gae-store_trace") |
| 46 if err != nil { | 44 if err != nil { |
| 47 panic(err) | 45 panic(err) |
| 48 } | 46 } |
| 49 logMemCollectionFolderTmp, err = filepath.Abs(logMemColl
ectionFolderTmp) | 47 logMemCollectionFolderTmp, err = filepath.Abs(logMemColl
ectionFolderTmp) |
| 50 if err != nil { | 48 if err != nil { |
| 51 panic(err) | 49 panic(err) |
| 52 } | 50 } |
| 53 » » » fmt.Fprintf(os.Stderr, "Saving GKVLite trace files to %q
\n", logMemCollectionFolderTmp) | 51 » » » fmt.Fprintf(os.Stderr, "Saving store trace files to %q\n
", logMemCollectionFolderTmp) |
| 54 }) | 52 }) |
| 55 if logMemCollectionFolderTmp == "" { | 53 if logMemCollectionFolderTmp == "" { |
| 56 panic("unable to create folder for tracefiles") | 54 panic("unable to create folder for tracefiles") |
| 57 } | 55 } |
| 58 | 56 |
| 59 lck := sync.Mutex{} | 57 lck := sync.Mutex{} |
| 60 fname := fmt.Sprintf(filepath.Join(logMemCollectionFolderTmp, fm
t.Sprintf("%d.trace", logNum))) | 58 fname := fmt.Sprintf(filepath.Join(logMemCollectionFolderTmp, fm
t.Sprintf("%d.trace", logNum))) |
| 61 fil, err := os.Create(fname) | 59 fil, err := os.Create(fname) |
| 62 if err != nil { | 60 if err != nil { |
| 63 panic(err) | 61 panic(err) |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 t.w("%s := %s.Snapshot()", ret.ident(), t.ident()) | 139 t.w("%s := %s.Snapshot()", ret.ident(), t.ident()) |
| 142 t.snapNum++ | 140 t.snapNum++ |
| 143 return ret | 141 return ret |
| 144 } | 142 } |
| 145 | 143 |
| 146 func (t *tracingMemStoreImpl) IsReadOnly() bool { | 144 func (t *tracingMemStoreImpl) IsReadOnly() bool { |
| 147 return t.i.IsReadOnly() | 145 return t.i.IsReadOnly() |
| 148 } | 146 } |
| 149 | 147 |
| 150 type tracingMemCollectionImpl struct { | 148 type tracingMemCollectionImpl struct { |
| 151 » i memCollection | 149 » i memCollection |
| 152 » w traceWriter | 150 » w traceWriter |
| 153 » visitNumber uint | 151 » iterNumber uint |
| 154 } | 152 } |
| 155 | 153 |
| 156 var _ memCollection = (*tracingMemCollectionImpl)(nil) | 154 var _ memCollection = (*tracingMemCollectionImpl)(nil) |
| 157 | 155 |
| 158 func (t *tracingMemCollectionImpl) Name() string { | 156 func (t *tracingMemCollectionImpl) Name() string { |
| 159 return t.i.Name() | 157 return t.i.Name() |
| 160 } | 158 } |
| 161 | 159 |
| 162 func (t *tracingMemCollectionImpl) Delete(k []byte) bool { | 160 func (t *tracingMemCollectionImpl) Delete(k []byte) { |
| 163 t.w(".Delete(%#v)", k) | 161 t.w(".Delete(%#v)", k) |
| 164 » return t.i.Delete(k) | 162 » t.i.Delete(k) |
| 165 } | 163 } |
| 166 | 164 |
| 167 func (t *tracingMemCollectionImpl) Get(k []byte) []byte { | 165 func (t *tracingMemCollectionImpl) Get(k []byte) []byte { |
| 168 t.w(".Get(%#v)", k) | 166 t.w(".Get(%#v)", k) |
| 169 return t.i.Get(k) | 167 return t.i.Get(k) |
| 170 } | 168 } |
| 171 | 169 |
| 172 func (t *tracingMemCollectionImpl) GetTotals() (numItems, numBytes uint64) { | 170 func (t *tracingMemCollectionImpl) MinItem() *storeEntry { |
| 173 » t.w(".GetTotals()") | 171 » t.w(".MinItem()") |
| 174 » return t.i.GetTotals() | 172 » return t.i.MinItem() |
| 175 } | |
| 176 | |
| 177 func (t *tracingMemCollectionImpl) MinItem(withValue bool) *gkvlite.Item { | |
| 178 » t.w(".MinItem(%t)", withValue) | |
| 179 » return t.i.MinItem(withValue) | |
| 180 } | 173 } |
| 181 | 174 |
| 182 func (t *tracingMemCollectionImpl) Set(k, v []byte) { | 175 func (t *tracingMemCollectionImpl) Set(k, v []byte) { |
| 183 t.w(".Set(%#v, %#v)", k, v) | 176 t.w(".Set(%#v, %#v)", k, v) |
| 184 t.i.Set(k, v) | 177 t.i.Set(k, v) |
| 185 } | 178 } |
| 186 | 179 |
| 187 func (t *tracingMemCollectionImpl) VisitItemsAscend(target []byte, withValue boo
l, visitor gkvlite.ItemVisitor) { | 180 func (t *tracingMemCollectionImpl) Iterator(pivot []byte) memIterator { |
| 188 » vnum := t.visitNumber | 181 » inum := t.iterNumber |
| 189 » t.visitNumber++ | 182 » t.iterNumber++ |
| 190 | 183 |
| 191 » t.w(".VisitItemsAscend(%#v, %t, func(i *gkvlite.Item) bool{ return true
}) // BEGIN VisitItemsAscend(%d)", target, withValue, vnum) | 184 » t.w(".Iterator(%#v) // Created Iterator #%d", pivot, inum) |
| 192 » defer t.w("// END VisitItemsAscend(%d)", vnum) | 185 » return &tracingMemIteratorImpl{t.i.Iterator(pivot), t.w, inum} |
| 193 » t.i.VisitItemsAscend(target, withValue, visitor) | |
| 194 } | 186 } |
| 195 | |
| 196 func (t *tracingMemCollectionImpl) IsReadOnly() bool { | 187 func (t *tracingMemCollectionImpl) IsReadOnly() bool { |
| 197 return t.i.IsReadOnly() | 188 return t.i.IsReadOnly() |
| 198 } | 189 } |
| 190 |
| 191 type tracingMemIteratorImpl struct { |
| 192 i memIterator |
| 193 w traceWriter |
| 194 num uint |
| 195 } |
| 196 |
| 197 func (t *tracingMemIteratorImpl) Next() *storeEntry { |
| 198 t.w(".Next() // Iterator #%d", t.num) |
| 199 return t.i.Next() |
| 200 } |
| OLD | NEW |