Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf add timeout argument #736

Merged
merged 5 commits into from
Aug 10, 2022
Merged

perf add timeout argument #736

merged 5 commits into from
Aug 10, 2022

Conversation

xpu22
Copy link
Contributor

@xpu22 xpu22 commented Jul 14, 2022

No description provided.

xpu22 added 2 commits July 14, 2022 19:26
Signed-off-by: Tonghao Zhang <xiangxia.m.yue@gmail.com>
Signed-off-by: Tonghao Zhang <xiangxia.m.yue@gmail.com>
perf/reader.go Outdated
@@ -162,6 +163,7 @@ type ReaderOptions struct {
// Read will process data. Must be smaller than PerCPUBuffer.
// The default is to start processing as soon as data is available.
Watermark int
TimeOut int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this option should have at least document what it is used for so users can reason about its impact. Also if there is a default value, it should be mentioned here as well I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With SetDeadline this Option isn't used anymore.

@lmb
Copy link
Collaborator

lmb commented Jul 14, 2022

Hi @xpu22, thanks for your PR!

There was an earlier proposal for this in #523 The gist of the discussion is:

Please also add some unit tests and take a look at the failing CI: https://ebpf.semaphoreci.com/jobs/b80f08ab-6ffd-4473-8887-27ad0c4c0508#L572

@xpu22
Copy link
Contributor Author

xpu22 commented Jul 15, 2022

Hi @xpu22, thanks for your PR!

There was an earlier proposal for this in #523 The gist of the discussion is:

Please also add some unit tests and take a look at the failing CI: https://ebpf.semaphoreci.com/jobs/b80f08ab-6ffd-4473-8887-27ad0c4c0508#L572
Ok

Signed-off-by: Tonghao Zhang <xiangxia.m.yue@gmail.com>
@lmb lmb self-requested a review July 18, 2022 15:53
@lmb
Copy link
Collaborator

lmb commented Jul 18, 2022

https://ebpf.semaphoreci.com/jobs/690fd5ce-e7d0-4400-9f58-3da6f915e3a0#L573 is still failing. Please ping me explicitly when you're ready for another round of review.

Signed-off-by: Tonghao Zhang <xiangxia.m.yue@gmail.com>
@xpu22
Copy link
Contributor Author

xpu22 commented Jul 19, 2022

https://ebpf.semaphoreci.com/jobs/690fd5ce-e7d0-4400-9f58-3da6f915e3a0#L573 is still failing. Please ping me explicitly when you're ready for another round of review.

Hi @lmb please review

Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for following up so quickly!

n, err := unix.EpollWait(p.epollFd, events, -1)
msec := int(-1)
if !deadline.IsZero() {
msec = int(time.Until(deadline).Milliseconds())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug in my own code: this doesn't handle deadlines in the past or so far in the future that msec is overflowed.

I'll fix that up in a follow up.

perf/reader.go Outdated
ErrClosed = os.ErrClosed
errEOR = errors.New("end of ring")
ErrClosed = os.ErrClosed
ErrTimeOut = os.ErrDeadlineExceeded
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This alias isn't necessary. Just document that os.ErrDeadlineExceeded is returned.

perf/reader.go Outdated
@@ -162,6 +163,7 @@ type ReaderOptions struct {
// Read will process data. Must be smaller than PerCPUBuffer.
// The default is to start processing as soon as data is available.
Watermark int
TimeOut int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With SetDeadline this Option isn't used anymore.

perf/reader.go Outdated
@@ -133,7 +135,8 @@ func readRawSample(rd io.Reader, buf, sampleBuf []byte) ([]byte, error) {
// Reader allows reading bpf_perf_event_output
// from user space.
type Reader struct {
poller *epoll.Poller
poller *epoll.Poller
timeOut time.Time
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: deadline time.Time.

perf/reader.go Outdated
@@ -299,14 +316,15 @@ func (pr *Reader) Read() (Record, error) {
func (pr *Reader) ReadInto(rec *Record) error {
pr.mu.Lock()
defer pr.mu.Unlock()
defer func() { pr.timeOut = time.Time{} }()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So net.Conn does not automatically clear the deadline: https://pkg.go.dev/net#Conn Unless there is a strong reason to do this here I'd suggest that we also don't clear it.

perf/reader.go Outdated
@@ -280,6 +285,18 @@ func (pr *Reader) Close() error {
return nil
}

// SetDeadline the Read timeout
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// SetDeadline the Read timeout
// SetDeadline controls how long Read and ReadInto will block waiting for samples.
//
// Passing a zero time.Time will remove the deadline.

perf/reader.go Outdated
// SetDeadline the Read timeout
//
// If this function was not invoked, the Read, ReadInto
// will be blocked until there are at least Watermark
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is slightly misleading: adding a timeout / deadline doesn't change how Watermark works.

perf/reader.go Outdated Show resolved Hide resolved
@@ -58,6 +58,10 @@ func TestPerfReader(t *testing.T) {
if record.CPU < 0 {
t.Error("Record has invalid CPU number")
}

rd.SetDeadline(time.Now().Add(100 * time.Millisecond))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: you can make the timeout very short, like a millisecond or so. It'll save us a bit of time when running ht test.

@@ -138,6 +144,10 @@ func (p *Poller) Wait(events []unix.EpollEvent) (int, error) {
return 0, err
}

if n == 0 && msec != -1 {
return 0, os.ErrDeadlineExceeded
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should wrap the error here, otherwise we return a naked ErrDeadlineExceeded.

Suggested change
return 0, os.ErrDeadlineExceeded
return 0, fmt.Errorf("poll: %w", os.ErrDeadlineExceeded)

Signed-off-by: Tonghao Zhang <xiangxia.m.yue@gmail.com>
@nashuiliang
Copy link

Cool, adding the timeout of ebpf-perf is very necessary!
Hi, @lmb @florianl @xpu22, have you considered using context.Context WithCancel/WithDeadline for this?

The new Read() API is as follows:

func (pr *Reader) ReadWithContext(ctx context.Context) (Record, error)

@ti-mo
Copy link
Collaborator

ti-mo commented Jul 28, 2022

have you considered using context.Context WithCancel/WithDeadline for this?

We have, but IMO the performance characteristics of context.Context aren't very well-suited to this kind of API. Granted, maybe that's not our call to make and we could just offer the functionality. If the ring isn't that busy, the overhead of Context could be acceptable if it makes it much more ergonomic to use.

Could you give a practical use case for it? In any case, I would suggest we take this discussion to a dedicated issue.

Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finishing this work, I was waiting for you to ping me for another review. Sorry it took a while.

@lmb lmb merged commit 9f500f3 into cilium:master Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants