-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add comments and test for 64-bit field alignment (#418)
* Add comments on needed filed alignment Add comment about alignment requirements to all struct fields who's values are passed to 64-bit atomic operations. Update any struct's field ordering if one or more of those fields has alignment requirements to support 64-bit atomic operations. * Add 64-bit alignment tests Most `struct` that have field alignment requirements are now statically validated prior to testing. The only `struct`s not validated that have these requirements are ones defined in tests themselves where multiple `TestMain` functions would be needed to test them. Given the fields are already identified with comments specifying the alignment requirements and they are in the test themselves, this seems like an OK omission. Co-authored-by: Liz Fong-Jones <[email protected]>
- Loading branch information
1 parent
aefc49c
commit 91091b4
Showing
21 changed files
with
330 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package metric | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"unsafe" | ||
|
||
ottest "go.opentelemetry.io/otel/internal/testing" | ||
) | ||
|
||
// Ensure struct alignment prior to running tests. | ||
func TestMain(m *testing.M) { | ||
fields := []ottest.FieldOffset{ | ||
{ | ||
Name: "Measurement.number", | ||
Offset: unsafe.Offsetof(Measurement{}.number), | ||
}, | ||
} | ||
if !ottest.Aligned8Byte(fields, os.Stderr) { | ||
os.Exit(1) | ||
} | ||
|
||
os.Exit(m.Run()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package metric | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"unsafe" | ||
|
||
ottest "go.opentelemetry.io/otel/internal/testing" | ||
) | ||
|
||
// Ensure struct alignment prior to running tests. | ||
func TestMain(m *testing.M) { | ||
fields := []ottest.FieldOffset{ | ||
{ | ||
Name: "Batch.Measurments", | ||
Offset: unsafe.Offsetof(Batch{}.Measurements), | ||
}, | ||
{ | ||
Name: "Measurement.Number", | ||
Offset: unsafe.Offsetof(Measurement{}.Number), | ||
}, | ||
} | ||
if !ottest.Aligned8Byte(fields, os.Stderr) { | ||
os.Exit(1) | ||
} | ||
|
||
os.Exit(m.Run()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package testing | ||
|
||
/* | ||
This file contains common utilities and objects to validate memory alignment | ||
of Go types. The primary use of this functionality is intended to ensure | ||
`struct` fields that need to be 64-bit aligned so they can be passed as | ||
arguments to 64-bit atomic operations. | ||
The common workflow is to define a slice of `FieldOffset` and pass them to the | ||
`Aligned8Byte` function from within a `TestMain` function from a package's | ||
tests. It is important to make this call from the `TestMain` function prior | ||
to running the rest of the test suit as it can provide useful diagnostics | ||
about field alignment instead of ambiguous nil pointer dereference and runtime | ||
panic. | ||
For more information: | ||
https://github.com/open-telemetry/opentelemetry-go/issues/341 | ||
*/ | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// FieldOffset is a preprocessor representation of a struct field alignment. | ||
type FieldOffset struct { | ||
// Name of the field. | ||
Name string | ||
|
||
// Offset of the field in bytes. | ||
// | ||
// To compute this at compile time use unsafe.Offsetof. | ||
Offset uintptr | ||
} | ||
|
||
// Aligned8Byte returns if all fields are aligned modulo 8-bytes. | ||
// | ||
// Error messaging is printed to out for any fileds determined misaligned. | ||
func Aligned8Byte(fields []FieldOffset, out io.Writer) bool { | ||
misaligned := make([]FieldOffset, 0) | ||
for _, f := range fields { | ||
if f.Offset%8 != 0 { | ||
misaligned = append(misaligned, f) | ||
} | ||
} | ||
|
||
if len(misaligned) == 0 { | ||
return true | ||
} | ||
|
||
fmt.Fprintln(out, "struct fields not aligned for 64-bit atomic operations:") | ||
for _, f := range misaligned { | ||
fmt.Fprintf(out, " %s: %d-byte offset\n", f.Name, f.Offset) | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package trace | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"unsafe" | ||
|
||
ottest "go.opentelemetry.io/otel/internal/testing" | ||
) | ||
|
||
// Ensure struct alignment prior to running tests. | ||
func TestMain(m *testing.M) { | ||
fields := []ottest.FieldOffset{ | ||
{ | ||
Name: "MockTracer.StartSpanID", | ||
Offset: unsafe.Offsetof(MockTracer{}.StartSpanID), | ||
}, | ||
} | ||
if !ottest.Aligned8Byte(fields, os.Stderr) { | ||
os.Exit(1) | ||
} | ||
|
||
os.Exit(m.Run()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.