journald: seek to time when --since is used

Instead of reading the full journal which can be expensive we can seek
based on the time.

If you have a journald with many podman events just compare the time
`time podman events --since 1s --stream=false` with and without this
patch.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2023-01-04 17:46:50 +01:00
parent 5f032256db
commit c674b3dd83
2 changed files with 18 additions and 2 deletions

View File

@ -80,8 +80,15 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
return fmt.Errorf("adding filter to journald logger: %v: %w", match, err)
}
if err := journal.SeekHead(); err != nil {
return err
if options.Since.IsZero() {
if err := journal.SeekHead(); err != nil {
return err
}
} else {
// seek based on time which helps to reduce unnecessary event reads
if err := journal.SeekRealtimeUsec(uint64(options.Since.UnixMicro())); err != nil {
return err
}
}
// API requires Next() immediately after SeekHead().
if _, err := journal.Next(); err != nil {

View File

@ -121,6 +121,15 @@ func (e EventJournalD) Read(ctx context.Context, options ReadOptions) error {
if _, err := j.Previous(); err != nil {
return fmt.Errorf("failed to move journal cursor to previous entry: %w", err)
}
} else if len(options.Since) > 0 {
since, err := util.ParseInputTime(options.Since, true)
if err != nil {
return err
}
// seek based on time which helps to reduce unnecessary event reads
if err := j.SeekRealtimeUsec(uint64(since.UnixMicro())); err != nil {
return err
}
}
for {