shirou / gopsutil

psutil for golang
Other
10.36k stars 1.57k forks source link

Refresh boot time #1616

Closed braydonk closed 4 months ago

braydonk commented 4 months ago

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.

shirou commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

Closing in favour of #1618

braydonk commented 4 months ago

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