Browse Source

device: move stats fields back down and add test diagnostics

This reverts the movement of fields from d49f4e9.
That commit was cherry-picked from another branch where a field
had changed and misaligned the atomic fields. After cherry-picking,
moving the fields was no longer necessary but got dragged along.

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
David Crawshaw 1 month ago
parent
commit
a4657f996d
2 changed files with 22 additions and 8 deletions
  1. 8
    8
      device/peer.go
  2. 14
    0
      device/peer_test.go

+ 8
- 8
device/peer.go View File

@@ -21,6 +21,14 @@ const (
21 21
 )
22 22
 
23 23
 type Peer struct {
24
+	// Mostly protects endpoint, but is generally taken whenever we modify peer
25
+	sync.RWMutex
26
+	keypairs                    Keypairs
27
+	handshake                   Handshake
28
+	device                      *Device
29
+	endpoint                    conn.Endpoint
30
+	persistentKeepaliveInterval uint16
31
+
24 32
 	// These fields are accessed with atomic operations, which must be
25 33
 	// 64-bit aligned even on 32-bit platforms. Go guarantees that an
26 34
 	// allocated struct will be 64-bit aligned. So we place
@@ -35,14 +43,6 @@ type Peer struct {
35 43
 	// bits. Don't place other atomic fields after this one.
36 44
 	isRunning AtomicBool
37 45
 
38
-	// Mostly protects endpoint, but is generally taken whenever we modify peer
39
-	sync.RWMutex
40
-	keypairs                    Keypairs
41
-	handshake                   Handshake
42
-	device                      *Device
43
-	endpoint                    conn.Endpoint
44
-	persistentKeepaliveInterval uint16
45
-
46 46
 	timers struct {
47 47
 		retransmitHandshake     *Timer
48 48
 		sendKeepalive           *Timer

+ 14
- 0
device/peer_test.go View File

@@ -6,6 +6,7 @@
6 6
 package device
7 7
 
8 8
 import (
9
+	"reflect"
9 10
 	"testing"
10 11
 	"unsafe"
11 12
 )
@@ -24,6 +25,19 @@ func checkAlignment(t *testing.T, name string, offset uintptr) {
24 25
 // hard segfault at runtime.
25 26
 func TestPeerAlignment(t *testing.T) {
26 27
 	var p Peer
28
+
29
+	typ := reflect.TypeOf(p)
30
+	t.Logf("Peer type size: %d, with fields:", typ.Size())
31
+	for i := 0; i < typ.NumField(); i++ {
32
+		field := typ.Field(i)
33
+		t.Logf("\t%30s\toffset=%3v\t(type size=%3d, align=%d)",
34
+			field.Name,
35
+			field.Offset,
36
+			field.Type.Size(),
37
+			field.Type.Align(),
38
+		)
39
+	}
40
+
27 41
 	checkAlignment(t, "Peer.stats", unsafe.Offsetof(p.stats))
28 42
 	checkAlignment(t, "Peer.isRunning", unsafe.Offsetof(p.isRunning))
29 43
 }

Loading…
Cancel
Save