From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
Yang Jihong <yangjihong1@huawei.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v1 1/2] tools api: Add simple timeout to io read
Date: Wed, 14 Jun 2023 15:44:16 -0300 [thread overview]
Message-ID: <ZIoKgEO+pz3cDgEH@kernel.org> (raw)
In-Reply-To: <CAP-5=fX1X=A7gkExsV917EntTdfgytNA8LBvHwTCsXfq1zHixw@mail.gmail.com>
Em Wed, Jun 14, 2023 at 10:36:47AM -0700, Ian Rogers escreveu:
> On Wed, Jun 7, 2023 at 11:20 PM Ian Rogers <irogers@google.com> wrote:
> >
> > In situations like reading from a pipe it can be useful to have a
> > timeout so that the caller doesn't block indefinitely. Implement a
> > simple one based on poll.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
>
> There is overlap in what these patches aim to fix with the 2 submitted
> patches making addr2line more robust:
> https://lore.kernel.org/all/20230613034817.1356114-2-irogers@google.com/
>
> I think it could be pragmatic to take both of them. Be robust but
> timeout if addr2line doesn't respond for 1s. What do you think?
Agreed, fixed up a minor conflict and applied.
- Arnaldo
> Thanks,
> Ian
>
> > ---
> > tools/lib/api/io.h | 28 +++++++++++++++++++++++++++-
> > 1 file changed, 27 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/lib/api/io.h b/tools/lib/api/io.h
> > index d5e8cf0dada0..9fc429d2852d 100644
> > --- a/tools/lib/api/io.h
> > +++ b/tools/lib/api/io.h
> > @@ -8,6 +8,7 @@
> > #define __API_IO__
> >
> > #include <errno.h>
> > +#include <poll.h>
> > #include <stdlib.h>
> > #include <string.h>
> > #include <unistd.h>
> > @@ -23,6 +24,8 @@ struct io {
> > char *end;
> > /* Currently accessed data pointer. */
> > char *data;
> > + /* Read timeout, 0 implies no timeout. */
> > + int timeout_ms;
> > /* Set true on when the end of file on read error. */
> > bool eof;
> > };
> > @@ -35,6 +38,7 @@ static inline void io__init(struct io *io, int fd,
> > io->buf = buf;
> > io->end = buf;
> > io->data = buf;
> > + io->timeout_ms = 0;
> > io->eof = false;
> > }
> >
> > @@ -47,7 +51,29 @@ static inline int io__get_char(struct io *io)
> > return -1;
> >
> > if (ptr == io->end) {
> > - ssize_t n = read(io->fd, io->buf, io->buf_len);
> > + ssize_t n;
> > +
> > + if (io->timeout_ms != 0) {
> > + struct pollfd pfds[] = {
> > + {
> > + .fd = io->fd,
> > + .events = POLLIN,
> > + },
> > + };
> > +
> > + n = poll(pfds, 1, io->timeout_ms);
> > + if (n == 0)
> > + errno = ETIMEDOUT;
> > + if (n > 0 && !(pfds[0].revents & POLLIN)) {
> > + errno = EIO;
> > + n = -1;
> > + }
> > + if (n <= 0) {
> > + io->eof = true;
> > + return -1;
> > + }
> > + }
> > + n = read(io->fd, io->buf, io->buf_len);
> >
> > if (n <= 0) {
> > io->eof = true;
> > --
> > 2.41.0.rc0.172.g3f132b7071-goog
> >
--
- Arnaldo
prev parent reply other threads:[~2023-06-14 18:44 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-08 6:18 [PATCH v1 1/2] tools api: Add simple timeout to io read Ian Rogers
2023-06-08 6:18 ` [PATCH v1 2/2] perf srcline: Add a timeout to reading from addr2line Ian Rogers
2023-06-14 17:36 ` [PATCH v1 1/2] tools api: Add simple timeout to io read Ian Rogers
2023-06-14 18:44 ` Arnaldo Carvalho de Melo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZIoKgEO+pz3cDgEH@kernel.org \
--to=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=yangjihong1@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).