mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-14 01:04:14 +00:00
f1a7dd766e
Per the sync/atomic bug note: > On ARM, x86-32, and 32-bit MIPS, it is the caller's > responsibility to arrange for 64-bit alignment of 64-bit words > accessed atomically. The first word in a variable or in an > allocated struct, array, or slice can be relied upon to be > 64-bit aligned. All atomic accesses of 64-bit variables in syncthing code base are currently ok (i.e they are all 64-bit aligned). Generally, the bug is triggered because of incorrect alignement of struct fields. Free variables (declared in a function) are guaranteed to be 64-bit aligned by the Go compiler. To ensure the code remains correct upon further addition/removal of fields, which would change the currently correct alignment, I added the following comment where required: // atomic, must remain 64-bit aligned See https://golang.org/pkg/sync/atomic/#pkg-note-BUG.
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
// Copyright (C) 2014 The Protocol Authors.
|
|
|
|
package protocol
|
|
|
|
import (
|
|
"io"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
type countingReader struct {
|
|
io.Reader
|
|
tot int64 // bytes (atomic, must remain 64-bit aligned)
|
|
last int64 // unix nanos (atomic, must remain 64-bit aligned)
|
|
}
|
|
|
|
var (
|
|
totalIncoming int64
|
|
totalOutgoing int64
|
|
)
|
|
|
|
func (c *countingReader) Read(bs []byte) (int, error) {
|
|
n, err := c.Reader.Read(bs)
|
|
atomic.AddInt64(&c.tot, int64(n))
|
|
atomic.AddInt64(&totalIncoming, int64(n))
|
|
atomic.StoreInt64(&c.last, time.Now().UnixNano())
|
|
return n, err
|
|
}
|
|
|
|
func (c *countingReader) Tot() int64 {
|
|
return atomic.LoadInt64(&c.tot)
|
|
}
|
|
|
|
func (c *countingReader) Last() time.Time {
|
|
return time.Unix(0, atomic.LoadInt64(&c.last))
|
|
}
|
|
|
|
type countingWriter struct {
|
|
io.Writer
|
|
tot int64 // bytes (atomic, must remain 64-bit aligned)
|
|
last int64 // unix nanos (atomic, must remain 64-bit aligned)
|
|
}
|
|
|
|
func (c *countingWriter) Write(bs []byte) (int, error) {
|
|
n, err := c.Writer.Write(bs)
|
|
atomic.AddInt64(&c.tot, int64(n))
|
|
atomic.AddInt64(&totalOutgoing, int64(n))
|
|
atomic.StoreInt64(&c.last, time.Now().UnixNano())
|
|
return n, err
|
|
}
|
|
|
|
func (c *countingWriter) Tot() int64 {
|
|
return atomic.LoadInt64(&c.tot)
|
|
}
|
|
|
|
func (c *countingWriter) Last() time.Time {
|
|
return time.Unix(0, atomic.LoadInt64(&c.last))
|
|
}
|
|
|
|
func TotalInOut() (int64, int64) {
|
|
return atomic.LoadInt64(&totalIncoming), atomic.LoadInt64(&totalOutgoing)
|
|
}
|