Skip to content

Commit

Permalink
ensure the stats is always 64bit aligned
Browse files Browse the repository at this point in the history
The first word in an allocated struct can be relied upon to be
64-bit aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG.

Signed-off-by: Benjamin Wang <wachao@vmware.com>
  • Loading branch information
ahrtr committed Oct 25, 2023
1 parent 4a17732 commit f9d290f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ const (
// All data access is performed through transactions which can be obtained through the DB.
// All the functions on DB will return a ErrDatabaseNotOpen if accessed before Open() is called.
type DB struct {
// Put `stats` at the first field to ensure it's 64-bit aligned. Note that
// the first word in an allocated struct can be relied upon to be 64-bit
// aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG. Also
// refer to discussion in https://github.com/etcd-io/bbolt/issues/577.
stats Stats

// When enabled, the database will perform a Check() after every commit.
// A panic is issued if the database is in an inconsistent state. This
// flag has a large performance impact so it should only be used for
Expand Down Expand Up @@ -147,7 +153,6 @@ type DB struct {
opened bool
rwtx *Tx
txs []*Tx
stats Stats

freelist *freelist
freelistLoad sync.Once
Expand Down Expand Up @@ -1282,6 +1287,12 @@ var DefaultOptions = &Options{

// Stats represents statistics about the database.
type Stats struct {
// Put `TxStats` at the first field to ensure it's 64-bit aligned. Note
// that the first word in an allocated struct can be relied upon to be
// 64-bit aligned. Refer to https://pkg.go.dev/sync/atomic#pkg-note-BUG.
// Also refer to discussion in https://github.com/etcd-io/bbolt/issues/577.
TxStats TxStats // global, ongoing stats.

// Freelist stats
FreePageN int // total number of free pages on the freelist
PendingPageN int // total number of pending pages on the freelist
Expand All @@ -1291,8 +1302,6 @@ type Stats struct {
// Transaction stats
TxN int // total number of started read transactions
OpenTxN int // number of currently open read transactions

TxStats TxStats // global, ongoing stats.
}

// Sub calculates and returns the difference between two sets of database stats.
Expand Down

0 comments on commit f9d290f

Please sign in to comment.