Skip to content

Commit

Permalink
Use a pool for write profiles
Browse files Browse the repository at this point in the history
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
  • Loading branch information
kakkoyun committed Apr 12, 2022
1 parent fc5fab9 commit bd9807a
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkg/profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ type CgroupProfiler struct {

target model.LabelSet
profilingDuration time.Duration

profilePool sync.Pool
}

func NewCgroupProfiler(
Expand Down Expand Up @@ -133,6 +135,11 @@ func NewCgroupProfiler(
ConstLabels: map[string]string{"target": target.String()},
},
[]string{"type"}),
profilePool: sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
},
}
}

Expand Down Expand Up @@ -341,8 +348,13 @@ func (p *CgroupProfiler) profileLoop(ctx context.Context, captureTime time.Time)
level.Error(p.logger).Log("msg", "get value and delete batch", "err", err)
}
}
// TODO(kakkoyun): Remove when error handling with eBPF map batch operations is fixed.
if len(values) == 0 {
return err
const msg = "get value and delete batch, nothing received"
if err != nil {
return fmt.Errorf(msg+": %w", err)
}
return errors.New(msg)
}

for i, key := range p.countKeys {
Expand Down Expand Up @@ -573,7 +585,11 @@ func (p *CgroupProfiler) normalizeAddress(m *profile.Mapping, pid uint32, addr u
}

func (p *CgroupProfiler) sendProfile(ctx context.Context, prof *profile.Profile) error {
buf := bytes.NewBuffer(nil)
buf := p.profilePool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
p.profilePool.Put(buf)
}()
if err := prof.Write(buf); err != nil {
return err
}
Expand Down

0 comments on commit bd9807a

Please sign in to comment.