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

pci: add and use LookupDevice #240

Merged
merged 1 commit into from
Apr 12, 2021
Merged

Conversation

ffromani
Copy link
Collaborator

Add a simple helper to find a device among the pci.Info.Devices.
Up until now, client code had to reimplement the find function
every time, or to call GetDevice. Problem is:
GetDevice access the system every time.

In most cases, besides a tiny performance hit, this is no issue.
When consuming snapshots, however, this may lead to surprising behaviour
because the caller need to ensure the snapshot is available when
GetDevice is called, or it will return unpredictable result (!!!).

The wrong code looks like that:

  1. PCI info is created supplying snapshot data with automatic management
    1.a ghw unpacks the snapshot on a temporary dir
    1.b ghw loads all the info from the snapshot. info.Devices contains
    correct data
    1.c ghw cleans up the snapshot (see:
    context: Fix directory leakage for snapshots contexts #236)
  2. the client (test) code calls GetDevice
    2.a GetDevice will try to access the unpacked snapshot data, which is
    gone
    2.b GetDevice will fail

Signed-off-by: Francesco Romani fromani@redhat.com

Copy link
Owner

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

See inline comments @fromanirh; I'm concerned about introducing another method instead of just modifying the implementation of the existing PCIInfo.GetDevice method.

README.md Outdated

The difference between the two methods is that `LookupDevice` will use
cached data, while `GetDevice` will attempt every time to get informations
from the system.
Copy link
Owner

Choose a reason for hiding this comment

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

The caching behaviour of any ghw struct should be entirely transparent to the user, so I'm going to push back on this particular change. The user should not need to know the internal implementation details in order to know to call a different method of ghw.PCIInfo depending on whether an underlying sysfs/filesystem mount is present.

In other words, just change the PCIInfo.GetDevice method implementation to look up the device in PCIInfo.Devices by address before searching a filesystem :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

very good point.

Comment on lines +178 to +181
for _, dev := range info.Devices {
if dev.Address == address {
return dev
}
}
return nil
Copy link
Owner

Choose a reason for hiding this comment

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

just move this into GetDevice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sure thing!

Add a simple helper to find a device among the `pci.Info.Devices`.
This is needed because `GetDevice` accesses the system every time.

In most cases, besides a tiny performance hit, this is no issue.
When consuming snapshots, however, this may lead to surprising behaviour
because the caller need to ensure the snapshot is available when
`GetDevice` is called, or it will return unpredictable result (!!!).

The wrong code looks like that:

1. PCI info is created supplying snapshot data with automatic management
1.a ghw unpacks the snapshot on a temporary directory.
1.b ghw loads all the info from the snapshot. info.Devices contains
    correct data.
1.c ghw cleans up the snapshot (see:
  jaypipes#236)
2. the client (test) code calls GetDevice
2.a GetDevice will try to access the unpacked snapshot data, which is
    gone.
2.b GetDevice will fail.

Signed-off-by: Francesco Romani <fromani@redhat.com>
Copy link
Owner

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

👍

@jaypipes jaypipes merged commit f38192d into jaypipes:master Apr 12, 2021
ffromani added a commit to ffromani/performance-addon-operators that referenced this pull request Apr 19, 2021
update to the tip of ghe main branch to pull in the latest fixes,
most notably to avoid to leak temporary directories when consuming
snapshots:
jaypipes/ghw#236
jaypipes/ghw#240

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/performance-addon-operators that referenced this pull request Apr 20, 2021
update to the tip of ghe main branch to pull in the latest fixes,
most notably to avoid to leak temporary directories when consuming
snapshots:
jaypipes/ghw#236
jaypipes/ghw#240

Signed-off-by: Francesco Romani <fromani@redhat.com>
ffromani added a commit to ffromani/performance-addon-operators that referenced this pull request Apr 29, 2021
update to the tip of ghe main branch to pull in the latest fixes,
most notably to avoid to leak temporary directories when consuming
snapshots:
jaypipes/ghw#236
jaypipes/ghw#240

Signed-off-by: Francesco Romani <fromani@redhat.com>
@ffromani ffromani deleted the pci-lookup-device branch May 24, 2021 06:52
cynepco3hahue pushed a commit to openshift-kni/performance-addon-operators that referenced this pull request Jun 3, 2021
update to the tip of ghe main branch to pull in the latest fixes,
most notably to avoid to leak temporary directories when consuming
snapshots:
jaypipes/ghw#236
jaypipes/ghw#240

Signed-off-by: Francesco Romani <fromani@redhat.com>
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.

2 participants