Skip to content

Commit

Permalink
Fix to get boot time with nonosecond
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuya9786 committed Nov 2, 2023
1 parent 02422da commit e14d70a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ require (
github.com/cilium/ebpf v0.11.0
github.com/google/gopacket v1.1.19
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys v0.10.0
gopkg.in/yaml.v3 v3.0.1
)

require (
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/sys v0.10.0 // indirect
)
require golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
22 changes: 4 additions & 18 deletions pkg/client/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ import (
"context"
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/netip"
"strconv"
"strings"
"sync"
"time"
"unsafe"
Expand All @@ -27,6 +23,7 @@ import (
"github.com/nttcom/fluvia/pkg/ipfix"
"github.com/nttcom/fluvia/pkg/packet"
"golang.org/x/sync/errgroup"
"golang.org/x/sys/unix"
)

type Stats struct {
Expand Down Expand Up @@ -241,20 +238,9 @@ func (m *Meter) Close() error {
}

func getSystemBootTime() (time.Time, error) {
data, err := ioutil.ReadFile("/proc/uptime")
if err != nil {
return time.Time{}, err
}

parts := strings.Split(string(data), " ")
if len(parts) == 0 {
return time.Time{}, fmt.Errorf("unexpected /proc/uptime format")
}

uptime, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
var ts unix.Timespec
if err := unix.ClockGettime(unix.CLOCK_MONOTONIC, &ts); err != nil {
return time.Time{}, err
}

return time.Now().Add(-time.Duration(uptime) * time.Second), nil
return time.Now().Add(-time.Duration(ts.Nano())), nil
}

0 comments on commit e14d70a

Please sign in to comment.