Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/function-equal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pdata/pprofile

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Introduce `Equal` method on the `Function` type

# One or more tracking issues or pull requests related to the change
issues: [13222]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
25 changes: 25 additions & 0 deletions .chloggen/set-function.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pdata/pprofile

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add new helper method `SetFunction` to set a new function on a line.

# One or more tracking issues or pull requests related to the change
issues: [13222]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
12 changes: 12 additions & 0 deletions pdata/pprofile/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"

// Equal checks equality with another Function
func (fn Function) Equal(val Function) bool {
return fn.NameStrindex() == val.NameStrindex() &&
fn.SystemNameStrindex() == val.SystemNameStrindex() &&
fn.FilenameStrindex() == val.FilenameStrindex() &&
fn.StartLine() == val.StartLine()
}
73 changes: 73 additions & 0 deletions pdata/pprofile/function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pprofile

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFunctionEqual(t *testing.T) {
for _, tt := range []struct {
name string
orig Function
dest Function
want bool
}{
{
name: "empty functions",
orig: NewFunction(),
dest: NewFunction(),
want: true,
},
{
name: "non-empty identical functions",
orig: buildFunction(1, 2, 3, 4),
dest: buildFunction(1, 2, 3, 4),
want: true,
},
{
name: "with different name",
orig: buildFunction(1, 2, 3, 4),
dest: buildFunction(2, 2, 3, 4),
want: false,
},
{
name: "with different system name",
orig: buildFunction(1, 2, 3, 4),
dest: buildFunction(1, 3, 3, 4),
want: false,
},
{
name: "with different file name",
orig: buildFunction(1, 2, 3, 4),
dest: buildFunction(1, 2, 4, 4),
want: false,
},
{
name: "with different start line",
orig: buildFunction(1, 2, 3, 4),
dest: buildFunction(1, 2, 3, 5),
want: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
if tt.want {
assert.True(t, tt.orig.Equal(tt.dest))
} else {
assert.False(t, tt.orig.Equal(tt.dest))
}
})
}
}

func buildFunction(name, sName, fileName int32, startLine int64) Function {
f := NewFunction()
f.SetNameStrindex(name)
f.SetSystemNameStrindex(sName)
f.SetFilenameStrindex(fileName)
f.SetStartLine(startLine)
return f
}
47 changes: 47 additions & 0 deletions pdata/pprofile/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"

import (
"errors"
"fmt"
"math"
)

var errTooManyFunctionTableEntries = errors.New("too many entries in FunctionTable")

// SetFunction updates a FunctionTable and a Line's FunctionIndex to
// add or update a function.
func SetFunction(table FunctionSlice, record Line, fn Function) error {
idx := int(record.FunctionIndex())
if idx > 0 {
if idx >= table.Len() {
return fmt.Errorf("index value %d out of range for FunctionIndex", idx)
}
mapAt := table.At(idx)
if mapAt.Equal(fn) {
// Function already exists, nothing to do.
return nil
}
}

for j, m := range table.All() {
if m.Equal(fn) {
if j > math.MaxInt32 {
return errTooManyFunctionTableEntries
}
// Add the index of the existing function to the indices.
record.SetFunctionIndex(int32(j)) //nolint:gosec // G115 overflow checked
return nil
}
}

if table.Len() >= math.MaxInt32 {
return errTooManyFunctionTableEntries
}

fn.CopyTo(table.AppendEmpty())
record.SetFunctionIndex(int32(table.Len() - 1)) //nolint:gosec // G115 overflow checked
return nil
}
123 changes: 123 additions & 0 deletions pdata/pprofile/functions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pprofile

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSetFunction(t *testing.T) {
table := NewFunctionSlice()
f := NewFunction()
f.SetNameStrindex(1)
f2 := NewFunction()
f2.SetNameStrindex(2)
li := NewLine()

// Put a first function
require.NoError(t, SetFunction(table, li, f))
assert.Equal(t, 1, table.Len())
assert.Equal(t, int32(0), li.FunctionIndex())

// Put the same function
// This should be a no-op.
require.NoError(t, SetFunction(table, li, f))
assert.Equal(t, 1, table.Len())
assert.Equal(t, int32(0), li.FunctionIndex())

// Set a new function
// This sets the index and adds to the table.
require.NoError(t, SetFunction(table, li, f2))
assert.Equal(t, 2, table.Len())
assert.Equal(t, int32(table.Len()-1), li.FunctionIndex()) //nolint:gosec // G115

// Set an existing function
require.NoError(t, SetFunction(table, li, f))
assert.Equal(t, 2, table.Len())
assert.Equal(t, int32(0), li.FunctionIndex())
// Set another existing function
require.NoError(t, SetFunction(table, li, f2))
assert.Equal(t, 2, table.Len())
assert.Equal(t, int32(table.Len()-1), li.FunctionIndex()) //nolint:gosec // G115
}

func TestSetFunctionCurrentTooHigh(t *testing.T) {
table := NewFunctionSlice()
li := NewLine()
li.SetFunctionIndex(42)

err := SetFunction(table, li, NewFunction())
require.Error(t, err)
assert.Equal(t, 0, table.Len())
assert.Equal(t, int32(42), li.FunctionIndex())
}

func BenchmarkSetFunction(b *testing.B) {
for _, bb := range []struct {
name string
fn Function

runBefore func(*testing.B, FunctionSlice, Line)
}{
{
name: "with a new function",
fn: NewFunction(),
},
{
name: "with an existing function",
fn: func() Function {
f := NewFunction()
f.SetNameStrindex(1)
return f
}(),

runBefore: func(_ *testing.B, table FunctionSlice, _ Line) {
f := table.AppendEmpty()
f.SetNameStrindex(1)
},
},
{
name: "with a duplicate function",
fn: NewFunction(),

runBefore: func(_ *testing.B, table FunctionSlice, obj Line) {
require.NoError(b, SetFunction(table, obj, NewFunction()))
},
},
{
name: "with a hundred functions to loop through",
fn: func() Function {
f := NewFunction()
f.SetNameStrindex(1)
return f
}(),

runBefore: func(_ *testing.B, table FunctionSlice, _ Line) {
for i := range 100 {
f := table.AppendEmpty()
f.SetNameStrindex(int32(i)) //nolint:gosec // overflow checked
}
},
},
} {
b.Run(bb.name, func(b *testing.B) {
table := NewFunctionSlice()
obj := NewLine()

if bb.runBefore != nil {
bb.runBefore(b, table, obj)
}

b.ResetTimer()
b.ReportAllocs()

for range b.N {
_ = SetFunction(table, obj, bb.fn)
}
})
}
}
Loading