Skip to content

Commit

Permalink
incusd/instance: implmented common.startedAt and called in lcx/qemu r…
Browse files Browse the repository at this point in the history
…ender state

Signed-off-by: Sahaj Bhakta <sahajbhakta@gmail.com>
  • Loading branch information
BajuMcBites committed May 3, 2024
1 parent 0a8574d commit 2cad20e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
13 changes: 13 additions & 0 deletions internal/server/instance/drivers/driver_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strings"
"sync"
"time"
"os"
"syscall"

"github.com/google/uuid"

Expand Down Expand Up @@ -1521,3 +1523,14 @@ func (d *common) setNUMANode() error {

return d.VolatileSet(map[string]string{"volatile.cpu.nodes": fmt.Sprintf("%d", node)})
}

// Gets the time the instance was started at
func (d *common) StartedAt(pid int) (time.Time, error) {
file, err := os.Stat(fmt.Sprintf("/proc/%d", pid))
if err != nil {
return time.Time{}, err
}

linuxInfo := file.Sys().(*syscall.Stat_t)
return time.Unix(linuxInfo.Ctim.Sec, linuxInfo.Ctim.Nsec), nil
}
3 changes: 2 additions & 1 deletion internal/server/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"golang.org/x/sys/unix"
"google.golang.org/protobuf/proto"
yaml "gopkg.in/yaml.v2"

internalInstance "github.com/lxc/incus/v6/internal/instance"
"github.com/lxc/incus/v6/internal/instancewriter"
internalIO "github.com/lxc/incus/v6/internal/io"
Expand Down Expand Up @@ -3411,6 +3411,7 @@ func (d *lxc) renderState(statusCode api.StatusCode, hostInterfaces []net.Interf
status.Network = d.networkState(hostInterfaces)
status.Pid = int64(pid)
status.Processes = processesState
status.StartedAt, _ = d.StartedAt(d.InitPID())
}

status.Disk = d.diskState()
Expand Down
5 changes: 5 additions & 0 deletions internal/server/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7759,6 +7759,11 @@ func (d *qemu) renderState(statusCode api.StatusCode) (*api.InstanceState, error
d.logger.Warn("Error getting disk usage", logger.Ctx{"err": err})
}

status.StartedAt, err = d.StartedAt(d.InitPID())
if err != nil {
return status, err
}

return status, nil
}

Expand Down

0 comments on commit 2cad20e

Please sign in to comment.