mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
Add internals for stackdiff tool (#3286)
Motivation: Our alloc regression workflows are somewhat disparate: on macOS we have dtrace and a script to diff two ouputs. On Linux we have heaptrack and bpftrace but no way to analyze their outputs. Diffing output from these tools can be quite tedious if the stacks vary even a little (because the compiler decided to not inline a function, for example). Most of the tedium in this workflow is from pairing up equivalent stacks across the two inputs. We can make this easier to do by ranking suggestions based on how similar they are and letting the user decide whether to accept the match or not. Modifications: - Add a subpackage with some internals for parsing heaptrack, aggregating stacks, and measuring the similarity between them - The CLI took is currently a no-op, it'll be added in subsequent PRs Result: Easier to diagnose alloc regressions
This commit is contained in:
1 parent
09030f291f
commit
e84b2ce05a
9 files changed
+766
No files matched your search
@@ -0,0 +1,131 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Stacks
|
||||
import Testing
|
||||
|
||||
struct AggregateStackTests {
|
||||
@Test
|
||||
func netAllocations() {
|
||||
var aggregate = AggregateStacks()
|
||||
#expect(aggregate.netAllocations == 0)
|
||||
|
||||
aggregate.groupWeightedStack(
|
||||
WeightedStack(lines: ["a"], allocations: 100),
|
||||
by: PartialStack(["a"])
|
||||
)
|
||||
|
||||
#expect(aggregate.netAllocations == 100)
|
||||
|
||||
aggregate.groupWeightedStack(
|
||||
WeightedStack(lines: ["a"], allocations: -10),
|
||||
by: PartialStack(["a"])
|
||||
)
|
||||
|
||||
#expect(aggregate.netAllocations == 90)
|
||||
}
|
||||
|
||||
@Test
|
||||
func groupWeightedStack() {
|
||||
var aggregate = AggregateStacks()
|
||||
|
||||
let partial = PartialStack(["a"])
|
||||
let weighted = ["a", "b", "c"].map { line in
|
||||
WeightedStack(lines: ["a"] + [line], allocations: 10)
|
||||
}
|
||||
|
||||
for weightedStack in weighted {
|
||||
aggregate.groupWeightedStack(weightedStack, by: partial)
|
||||
}
|
||||
|
||||
aggregate.groupWeightedStack(
|
||||
WeightedStack(lines: [], allocations: 0),
|
||||
by: PartialStack(["b"])
|
||||
)
|
||||
|
||||
#expect(aggregate.netAllocations(groupedBy: partial) == 30)
|
||||
|
||||
let grouped = aggregate.weightedStacks(groupedBy: partial)
|
||||
#expect(grouped == weighted)
|
||||
}
|
||||
|
||||
@Test
|
||||
func removeWeightedStacks() {
|
||||
var aggregate = AggregateStacks()
|
||||
let partial = PartialStack(["a"])
|
||||
|
||||
// Nothing to remove.
|
||||
#expect(aggregate.removeWeightedStacks(groupedUnder: partial) == nil)
|
||||
|
||||
// Add then remove.
|
||||
let weighted = WeightedStack(lines: ["a"], allocations: 100)
|
||||
aggregate.groupWeightedStack(weighted, by: partial)
|
||||
#expect(aggregate.removeWeightedStacks(groupedUnder: partial) == [weighted])
|
||||
}
|
||||
|
||||
@Test
|
||||
func filter() {
|
||||
var aggregate = AggregateStacks()
|
||||
|
||||
let partialA = PartialStack(["a"])
|
||||
let partialB = PartialStack(["b"])
|
||||
aggregate.groupWeightedStack(WeightedStack(lines: ["a"], allocations: 10), by: partialA)
|
||||
aggregate.groupWeightedStack(WeightedStack(lines: ["b"], allocations: -10), by: partialB)
|
||||
#expect(aggregate.netAllocations == 0)
|
||||
|
||||
let filtered = aggregate.filter { $0.key.lines == ["a"] }
|
||||
#expect(filtered.netAllocations == 10)
|
||||
}
|
||||
|
||||
@Test
|
||||
func removeAll() {
|
||||
var aggregate = AggregateStacks()
|
||||
|
||||
let partialA = PartialStack(["a"])
|
||||
let partialB = PartialStack(["b"])
|
||||
aggregate.groupWeightedStack(WeightedStack(lines: ["a"], allocations: 10), by: partialA)
|
||||
aggregate.groupWeightedStack(WeightedStack(lines: ["b"], allocations: -10), by: partialB)
|
||||
#expect(aggregate.netAllocations == 0)
|
||||
|
||||
aggregate.removeAll { $0.key.lines == ["a"] }
|
||||
#expect(aggregate.netAllocations == -10)
|
||||
}
|
||||
|
||||
@Test
|
||||
func subtract() {
|
||||
let weighted = WeightedStack(lines: [], allocations: 10)
|
||||
|
||||
var aggregateA = AggregateStacks()
|
||||
aggregateA.groupWeightedStack(weighted, by: PartialStack(["m"]))
|
||||
aggregateA.groupWeightedStack(weighted, by: PartialStack(["n"]))
|
||||
|
||||
var aggregateB = AggregateStacks()
|
||||
aggregateB.groupWeightedStack(weighted, by: PartialStack(["m"]))
|
||||
aggregateB.groupWeightedStack(weighted, by: PartialStack(["o"]))
|
||||
|
||||
let diff = aggregateA.subtracting(aggregateB)
|
||||
|
||||
// m=10 in A and B
|
||||
#expect(diff.netAllocations(groupedBy: PartialStack(["m"])) == 0)
|
||||
#expect(diff.weightedStacks(groupedBy: PartialStack(["m"])).count == 2)
|
||||
|
||||
// n=10 in A and not present in B
|
||||
#expect(diff.netAllocations(groupedBy: PartialStack(["n"])) == 10)
|
||||
#expect(diff.weightedStacks(groupedBy: PartialStack(["n"])).count == 1)
|
||||
|
||||
// o=10 in B and not present in A
|
||||
#expect(diff.netAllocations(groupedBy: PartialStack(["o"])) == -10)
|
||||
#expect(diff.weightedStacks(groupedBy: PartialStack(["o"])).count == 1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Stacks
|
||||
import Testing
|
||||
|
||||
struct HeaptrackParserTests {
|
||||
@Test
|
||||
func parsing() throws {
|
||||
// Real input but modified to be a lot shorter.
|
||||
let input = """
|
||||
reading file "heaptrack.simple-handshake.a.gz" - please wait, this might take some time...
|
||||
Debuggee command was: ./.build/release/simple-handshake
|
||||
finished reading file, now analyzing data:
|
||||
|
||||
MOST CALLS TO ALLOCATION FUNCTIONS
|
||||
488872 calls to allocation functions with 28.16K peak consumption from
|
||||
CNIOBoringSSL_OPENSSL_malloc
|
||||
at Sources/CNIOBoringSSL/crypto/mem.cc:249
|
||||
in /code/.build/aarch64-unknown-linux-gnu/release/simple-handshake
|
||||
3000 calls with 80B peak consumption from:
|
||||
CNIOBoringSSL_OPENSSL_zalloc
|
||||
at Sources/CNIOBoringSSL/crypto/mem.cc:266
|
||||
in /code/.build/aarch64-unknown-linux-gnu/release/simple-handshake
|
||||
CNIOBoringSSL_ASN1_item_ex_new
|
||||
at Sources/CNIOBoringSSL/crypto/asn1/tasn_new.cc:153
|
||||
in /code/.build/aarch64-unknown-linux-gnu/release/simple-handshake
|
||||
ASN1_template_new(ASN1_VALUE_st**, ASN1_TEMPLATE_st const*)
|
||||
at Sources/CNIOBoringSSL/crypto/asn1/tasn_new.cc:236
|
||||
in /code/.build/aarch64-unknown-linux-gnu/release/simple-handshake
|
||||
CNIOBoringSSL_ASN1_item_ex_new
|
||||
at Sources/CNIOBoringSSL/crypto/asn1/tasn_new.cc:161
|
||||
asn1_item_ex_d2i(ASN1_VALUE_st**, unsigned char const**, long, ASN1_ITEM_st const*, int, int, char, crypto_buffer_st*, int)
|
||||
at Sources/CNIOBoringSSL/crypto/asn1/tasn_dec.cc:378
|
||||
in /code/.build/aarch64-unknown-linux-gnu/release/simple-handshake
|
||||
static simple_handshake.SimpleHandshake.$main() throws -> ()
|
||||
at //<compiler-generated>:40
|
||||
simple_handshake_main
|
||||
at /code/Sources/simple-handshake/SimpleHandshake.swift:0
|
||||
0xffff935b73fa
|
||||
in /lib/aarch64-linux-gnu/libc.so.6
|
||||
__libc_start_main
|
||||
in /lib/aarch64-linux-gnu/libc.so.6
|
||||
2000 calls with 96B peak consumption from:
|
||||
CNIOBoringSSL_ASN1_STRING_type_new
|
||||
at Sources/CNIOBoringSSL/crypto/asn1/asn1_lib.cc:329
|
||||
in /code/.build/aarch64-unknown-linux-gnu/release/simple-handshake
|
||||
ASN1_primitive_new(ASN1_VALUE_st**, ASN1_ITEM_st const*)
|
||||
at Sources/CNIOBoringSSL/crypto/asn1/tasn_new.cc:294
|
||||
in /code/.build/aarch64-unknown-linux-gnu/release/simple-handshake
|
||||
static simple_handshake.SimpleHandshake.main() throws -> ()
|
||||
at /code/Sources/simple-handshake/SimpleHandshake.swift:43
|
||||
in /code/.build/aarch64-unknown-linux-gnu/release/simple-handshake
|
||||
static simple_handshake.SimpleHandshake.$main() throws -> ()
|
||||
at //<compiler-generated>:40
|
||||
simple_handshake_main
|
||||
at /code/Sources/simple-handshake/SimpleHandshake.swift:0
|
||||
0xffff935b73fa
|
||||
in /lib/aarch64-linux-gnu/libc.so.6
|
||||
__libc_start_main
|
||||
in /lib/aarch64-linux-gnu/libc.so.6
|
||||
|
||||
|
||||
total runtime: 0.44s.
|
||||
calls to allocation functions: 638104 (1440415/s)
|
||||
temporary memory allocations: 56039 (126498/s)
|
||||
peak heap memory consumption: 190.22K
|
||||
peak RSS (including heaptrack overhead): 21.92M
|
||||
total memory leaked: 5.54K
|
||||
"""
|
||||
|
||||
let lines = input.split(separator: "\n").map { String($0) }
|
||||
let stacks = HeaptrackParser.parse(lines: lines)
|
||||
|
||||
try #require(stacks.count == 2)
|
||||
|
||||
let stack0 = stacks[0]
|
||||
let expected0 = WeightedStack(
|
||||
lines: [
|
||||
"CNIOBoringSSL_OPENSSL_zalloc",
|
||||
"CNIOBoringSSL_ASN1_item_ex_new",
|
||||
"ASN1_template_new(ASN1_VALUE_st**, ASN1_TEMPLATE_st const*)",
|
||||
"CNIOBoringSSL_ASN1_item_ex_new",
|
||||
"asn1_item_ex_d2i(ASN1_VALUE_st**, unsigned char const**, long, ASN1_ITEM_st const*, int, int, char, crypto_buffer_st*, int)",
|
||||
"static simple_handshake.SimpleHandshake.$main() throws -> ()",
|
||||
"simple_handshake_main",
|
||||
"__libc_start_main",
|
||||
],
|
||||
allocations: 3000
|
||||
)
|
||||
#expect(stack0 == expected0)
|
||||
|
||||
let stack1 = stacks[1]
|
||||
let expected1 = WeightedStack(
|
||||
lines: [
|
||||
"CNIOBoringSSL_ASN1_STRING_type_new",
|
||||
"ASN1_primitive_new(ASN1_VALUE_st**, ASN1_ITEM_st const*)",
|
||||
"static simple_handshake.SimpleHandshake.main() throws -> ()",
|
||||
"static simple_handshake.SimpleHandshake.$main() throws -> ()",
|
||||
"simple_handshake_main",
|
||||
"__libc_start_main",
|
||||
],
|
||||
allocations: 2000
|
||||
)
|
||||
#expect(stack1 == expected1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Stacks
|
||||
import Testing
|
||||
|
||||
struct SimilarityTests {
|
||||
@Test(arguments: ["", "foo", "Foo"])
|
||||
func perfectMatch(_ input: String) {
|
||||
let (distance, score) = Similarity.levenshtein(input, input)
|
||||
#expect(distance == 0)
|
||||
#expect(score == 1)
|
||||
}
|
||||
|
||||
@Test(arguments: [("", "a"), ("a", ""), ("a", "b"), ("A", "a")])
|
||||
func maxDistance(_ lhs: String, _ rhs: String) {
|
||||
let (distance, score) = Similarity.levenshtein(lhs, rhs)
|
||||
#expect(distance == 1)
|
||||
#expect(score == 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
func kittenSitting() {
|
||||
let (distance, _) = Similarity.levenshtein("kitten", "sitting")
|
||||
// k -> s
|
||||
// e -> i
|
||||
// +g
|
||||
#expect(distance == 3)
|
||||
}
|
||||
|
||||
@Test
|
||||
func similarityScore() {
|
||||
let (distance, similarity) = Similarity.levenshtein("aa", "bbaa")
|
||||
// +b
|
||||
// +b
|
||||
#expect(distance == 2)
|
||||
// = 1 - lev(aa, bbaa) / max(len(aa), len(bbaa))
|
||||
// = 1 - (2) / max(2, 4)
|
||||
// = 1 - 2 / 4
|
||||
// = 1 - 0.5
|
||||
// = 0.5
|
||||
#expect(similarity == 0.5)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user