Skip to content

pkg/platform: cleanup, and deprecate OSType#45298

Merged
thaJeztah merged 6 commits intomoby:masterfrom
thaJeztah:pkg_pkatform_cleanup
Apr 27, 2023
Merged

pkg/platform: cleanup, and deprecate OSType#45298
thaJeztah merged 6 commits intomoby:masterfrom
thaJeztah:pkg_pkatform_cleanup

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

pkg/platform: rename files for consistency

pkg/platform: move package doc to platform-agnostic file

Make sure the package's documentation is available for any platform,
not just "unix".

pkg/platform: replace use of deprecated syscall.Syscall

pkg/platform: use const for OSType, improve GoDoc

It was not immediately clear why we were not using runtime.GOARCH for
these (with a conversion to other formats, such as x86_64). These docs
are based on comments that were posted when implementing this package;

pkg/platform: un-export consts that are only for internal use

These consts are only used internally, and never returned to the user.
Un-export to make it clear these are not for external consumption.

While looking at the code, I also noticed that we may be using the wrong
Windows API to collect this information (and found an implementation elsewhere
that does use the correct API). I did not yet update the code, in cases there
are specific reasons.

pkg/platform: deprecate OSType in favor or runtime.GOOS

This const looks to only be there for "convenience", or possibly was created
with future normalization or special handling in mind.

In either case, currently it is just a direct copy (alias) for runtime.GOOS,
and defining our own type for this gives the impression that it's more than
that. It's only used in a single place, and there's no external consumers, so
let's deprecate this const, and use runtime.GOOS instead.

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

Make sure the package's documentation is available for any platform,
not just "unix".

Signed-off-by: Sebastiaan van Stijn <[email protected]>
It was not immediately clear why we were not using runtime.GOARCH for
these (with a conversion to other formats, such as x86_64). These docs
are based on comments that were posted when implementing this package;

- moby#13921 (comment)
- moby#13921 (comment)

Some links were now redirecting to a new location, so updated them to
not depend on the redirect.

While at it, also updated a call to logrus to use structured formatting
(WithError()).

Signed-off-by: Sebastiaan van Stijn <[email protected]>
These consts are only used internally, and never returned to the user.
Un-export to make it clear these are not for external consumption.

While looking at the code, I also noticed that we may be using the wrong
Windows API to collect this information (and found an implementation elsewhere
that does use the correct API). I did not yet update the code, in cases there
are specific reasons.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
This const looks to only be there for "convenience", or _possibly_ was created
with future normalization or special handling in mind.

In either case, currently it is just a direct copy (alias) for runtime.GOOS,
and defining our own type for this gives the impression that it's more than
that. It's only used in a single place, and there's no external consumers, so
let's deprecate this const, and use runtime.GOOS instead.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Comment thread pkg/platform/platform.go
Comment on lines -20 to +30
logrus.Errorf("Could not read system architecture info: %v", err)
logrus.WithError(err).Error("Could not read system architecture info")
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

FWIW, I wonder if we should consider replacing var Architecture with a func Architecture() (with a sync.Once). Currently this is part of an init(), which means that we'll be loading "kernel32.dll" on Windows (and a syscall), and we may be logging some error just by importing this package 🤔

Comment on lines +66 to +71
// NumProcs returns the number of processors on the system
func NumProcs() uint32 {
var sysinfo systeminfo
_, _, _ = syscall.SyscallN(procGetSystemInfo.Addr(), uintptr(unsafe.Pointer(&sysinfo)))
return sysinfo.dwNumberOfProcessors
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Related to the above; I could use some thoughts on this part. Do we know how costly it is to get this information? ❓ I see that this function is called (on Windows) as part of daemon.GetContainerStats() -> daemon.stats()

for _, pair := range pairs {
stats, err := s.supervisor.GetContainerStats(pair.container)

moby/daemon/stats.go

Lines 148 to 149 in dd3b71d

func (daemon *Daemon) GetContainerStats(container *container.Container) (*types.StatsJSON, error) {
stats, err := daemon.stats(container)

// Start with an empty structure
s := &types.StatsJSON{}
s.Stats.Read = stats.Read
s.Stats.NumProcs = platform.NumProcs()

That means that we're calling this function for every container, and at least once every second.

I think what we're collecting here is the hardware information (i.e. number of physical processors), and if that's true, we could assume that to not change at runtime, but I wasn't 100% sure if this could have "hot-pluggable" CPUs 🤔.

If that's not the case, and if it's "costly", we could use a sync.Once for this?

var (
	numProcsOnce sync.Once
	numProcs     uint32
)

// NumProcs returns the number of processors on the system
func NumProcs() uint32 {
	numProcsOnce.Do(func() {
		var si systeminfo
		_, _, _ = syscall.SyscallN(procGetSystemInfo.Addr(), uintptr(unsafe.Pointer(&si)))
		numProcs = si.dwNumberOfProcessors
	})
	return numProcs
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have any idea whether this is costly or not, but if you wanted to write some way to measure it, I'd be happy to run a test on my Windows system 😅

@thaJeztah
Copy link
Copy Markdown
Member Author

@rumpl @tianon ptal

@rumpl rumpl modified the milestones: 24.0.0, v-future Apr 24, 2023
@thaJeztah thaJeztah modified the milestones: v-future, 24.0.0 Apr 27, 2023
@thaJeztah thaJeztah merged commit c80f205 into moby:master Apr 27, 2023
@thaJeztah thaJeztah deleted the pkg_pkatform_cleanup branch April 27, 2023 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants