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

Refresh boot time #1616

Closed
wants to merge 2 commits into from
Closed

Refresh boot time #1616

wants to merge 2 commits into from

Conversation

braydonk
Copy link

This PR adds a function to process and host to manually refresh the cached boot time value. This is very helpful in scenarios where you are reading all the processes on the system at a given time, want the performance gains of using the boot time cache, but want to update the cached boot time when you read all the processes.

This commit adds the RefreshBootTimeCache function, allowing users to
manually refresh the cached boot time if they are using the
`enableBootTimeCache` feature.
This commit adds the RefreshBootTimeCache function, allowing users to
manually refresh the cached boot time if they are using the
`enableBootTimeCache` feature.
@shirou
Copy link
Owner

shirou commented Mar 31, 2024

I think the same could be accomplished with the following code on a library user side. I don't think it is necessary to add a new API.

host.EnableBootTimeCache(false)
defer host.EnableBootTimeCache(true)

BootTimeWithContext(ctx)

@braydonk
Copy link
Author

I think the same could be accomplished with the following code on a library user side.

That would be better if it was possible, but BootTimeWithContext is internal. So I don't see any way on the user side to only read boot time once.

@braydonk
Copy link
Author

braydonk commented Apr 2, 2024

I will work on an alternative PR that instead factors boot time into the common package and provides entrypoints so that your suggestion is possible.

@shirou
Copy link
Owner

shirou commented Apr 2, 2024

host.BootTimeWithContext is a public function. Here is the complete code.

package main

import (
	"context"
	"fmt"

	"github.com/shirou/gopsutil/v3/host"
)

func main() {
	host.EnableBootTimeCache(false)
	defer host.EnableBootTimeCache(true)

	fmt.Println(host.BootTimeWithContext(context.Background()))
}

@braydonk
Copy link
Author

braydonk commented Apr 2, 2024

Closing in favour of #1618

@braydonk braydonk closed this Apr 2, 2024
@braydonk
Copy link
Author

braydonk commented Apr 2, 2024

Ah okay so using the host function for boot time refreshes the same cache. I'll close the other PR too then.

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.

None yet

2 participants