Debian Codegraph @

storage/probe.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // blame: jnml, labs.nic.cz
6
7 package storage // import "modernc.org/fileutil/storage"
8
9 import "sync/atomic"
10
11 // Probe collects usage statistics of the embeded Accessor.
12 // Probe itself IS an Accessor.
13 type Probe struct {
14 Accessor
15 Chain *Probe
16 OpsRd int64
17 OpsWr int64
18 BytesRd int64
19 BytesWr int64
20 SectorsRd int64 // Assuming 512 byte sector size
21 SectorsWr int64
22 }
23
24 // NewProbe returns a newly created probe which embedes the src Accessor.
25 // The retuned *Probe satisfies Accessor. if chain != nil then Reset()
26 // is cascaded down the chained Probes.
27 func NewProbe(src Accessor, chain *Probe) *Probe {
28 return &Probe{Accessor: src, Chain: chain}
29 }
30
31 func reset(n *int64) {
32 atomic.AddInt64(n, -atomic.AddInt64(n, 0))
33 }
34
35 // Reset zeroes the collected statistics of p.
36 func (p *Probe) Reset() {
37 if p.Chain != nil {
38 p.Chain.Reset()
39 }
40 reset(&p.OpsRd)
41 reset(&p.OpsWr)
42 reset(&p.BytesRd)
43 reset(&p.BytesWr)
44 reset(&p.SectorsRd)
45 reset(&p.SectorsWr)
46 }
47
48 func (p *Probe) ReadAt(b []byte, off int64) (n int, err error) {
49 n, err = p.Accessor.ReadAt(b, off)
50 atomic.AddInt64(&p.OpsRd, 1)
51 atomic.AddInt64(&p.BytesRd, int64(n))
52 if n <= 0 {
53 return
54 }
55
56 sectorFirst := off >> 9
57 sectorLast := (off + int64(n) - 1) >> 9
58 atomic.AddInt64(&p.SectorsRd, sectorLast-sectorFirst+1)
59 return
60 }
61
62 func (p *Probe) WriteAt(b []byte, off int64) (n int, err error) {
63 n, err = p.Accessor.WriteAt(b, off)
64 atomic.AddInt64(&p.OpsWr, 1)
65 atomic.AddInt64(&p.BytesWr, int64(n))
66 if n <= 0 {
67 return
68 }
69
70 sectorFirst := off >> 9
71 sectorLast := (off + int64(n) - 1) >> 9
72 atomic.AddInt64(&p.SectorsWr, sectorLast-sectorFirst+1)
73 return
74 }