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

Stub out library when not built with cgo enabled #11

Closed
wants to merge 1 commit into from

Conversation

lorenz
Copy link
Contributor

@lorenz lorenz commented May 12, 2019

GoNVML depends on C code so it (and everything that depends on it) is currently not buildable with cgo disabled. Since its API design assumes that everything can fail anyways if the NVML is not available this makes it possible to just always fail if cgo is disabled. That also makes sense since systems without C standard library will not be able to run NVML anyways.

I've modified the errLibraryNotLoaded message in the bindings_unsupported.go file, that way it is clear to the user when he runs into an initialization error that the error is caused by not using cgo to build his binary.

@carlosedp
Copy link
Contributor

Hi @mindprince , any chance of accepting this PR. It would help-out building cadvisor (that depends on this lib) without CGO.

Copy link
Member

@rohitagarwal003 rohitagarwal003 left a comment

Choose a reason for hiding this comment

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

cc @dashpole for LGTM

szUUID = 0
)

var errLibraryNotLoaded = errors.New("this binary is built without CGO, NVML is disabled")
Copy link
Member

Choose a reason for hiding this comment

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

Let's rename this to errNoCgo?

Copy link
Contributor

Choose a reason for hiding this comment

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

That would be fine. I think maybe the file name could be renamed to bindings_nocgo.go

return "", errLibraryNotLoaded
}

// DeviceCount returns the number of nvidia devices on the system.
Copy link
Member

Choose a reason for hiding this comment

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

What's the best practice for built-constraint files? Do we repeat the method comments in all such files? Or do we keep it only at one place?

Copy link
Contributor

Choose a reason for hiding this comment

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

Usually the libs implement this to make clear the function use. Even in stubs.

Choose a reason for hiding this comment

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

For different build-constraint you should have comments on each method. They don't need to be the same (e.g. this could say "returns an error to indicate NVML is disabled"), but they should be there.

Copy link

@dashpole dashpole left a comment

Choose a reason for hiding this comment

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

Looks mostly good.

"time"
)

const (

Choose a reason for hiding this comment

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

are these constants still required?

return "", errLibraryNotLoaded
}

// DeviceCount returns the number of nvidia devices on the system.

Choose a reason for hiding this comment

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

For different build-constraint you should have comments on each method. They don't need to be the same (e.g. this could say "returns an error to indicate NVML is disabled"), but they should be there.

@carlosedp
Copy link
Contributor

@dashpole @mindprince I've been testing this PR and it works pretty fine. Any news on it getting merged?

@rohitagarwal003
Copy link
Member

rohitagarwal003 commented Aug 28, 2019 via email

@carlosedp
Copy link
Contributor

@mindprince I've added most recommendations, would you mind if I submit a new PR with reference to this?

@rohitagarwal003
Copy link
Member

rohitagarwal003 commented Aug 28, 2019 via email

@carlosedp carlosedp mentioned this pull request Aug 28, 2019
@lorenz
Copy link
Contributor Author

lorenz commented Aug 28, 2019

I've had this in my K8s tree for some time (as part of cadvisor/kubelet), it works fine. @carlosedp Thanks for integrating the comments, I've seen them but had no time to deal with this.

@carlosedp
Copy link
Contributor

Oh, no probs @lorenz ... I didn't knew how involved with it you were so I forked the PR. Hope you don't mind but I added you too as the author. :)

rohitagarwal003 pushed a commit that referenced this pull request Aug 28, 2019
Co-authored-by: lorenz <lorenz@dolansoft.org>
Co-authored-by: Carlos <me@carlosedp.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.

None yet

4 participants