Skip to content

Commit

Permalink
Uses efficient way to get the timestamp of now
Browse files Browse the repository at this point in the history
Uses vDSO based gettimeofday(2) to get current timestamp [0].

A side-effect is we lose nanosecond resolution [1], the patch changes
the points where is affordable and microsecond resolution is enough.

[0]
goos: linux
goarch: amd64
BenchmarkTimeNow-32            	 5000000	       333 ns/op
BenchmarkNowGettimeofday-32    	10000000	       167 ns/op

[1]
time.Now().UnixNano():  1513660132018156236 syscall.Gettimeofday-UnixNano: 1513660132018158000
  • Loading branch information
zhiyanliu authored and Jack47 committed Dec 19, 2017
1 parent 0172056 commit 419ddd9
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 92 deletions.
60 changes: 30 additions & 30 deletions src/cli/cluster_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"strings"
"time"

"common"

"github.com/hexdecteam/easegateway-go-client/rest/1.0/cluster/admin/v1/pdu"
"github.com/urfave/cli"

"common"
)

func setLocalOperationSequence(group string, seq uint64) error {
Expand Down Expand Up @@ -147,9 +147,9 @@ func ClusterCreatePlugin(c *cli.Context) error {
continue
}

startTime := time.Now()
startTime := common.Now()
seq, err := getOperationSequence(group, uint16(timeout.Seconds()))
expiredTime := time.Now().Sub(startTime)
expiredTime := common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s: %v", file, err))
Expand All @@ -164,9 +164,9 @@ func ClusterCreatePlugin(c *cli.Context) error {

seq++

startTime = time.Now()
startTime = common.Now()
do(file, seq, data, uint16(timeout.Seconds()))
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

setLocalOperationSequence(group, seq)

Expand Down Expand Up @@ -215,9 +215,9 @@ func ClusterDeletePlugin(c *cli.Context) error {
timeout := time.Duration(timeoutSec) * time.Second

for i, pluginName := range args {
startTime := time.Now()
startTime := common.Now()
seq, err := getOperationSequence(group, uint16(timeout.Seconds()))
expiredTime := time.Now().Sub(startTime)
expiredTime := common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s: %v", pluginName, err))
Expand All @@ -232,9 +232,9 @@ func ClusterDeletePlugin(c *cli.Context) error {

seq++

startTime = time.Now()
startTime = common.Now()
do(pluginName, seq, uint16(timeout.Seconds()))
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

setLocalOperationSequence(group, seq+1)

Expand Down Expand Up @@ -312,9 +312,9 @@ func ClusterRetrievePlugins(c *cli.Context) error {
doAll(uint16(timeout.Seconds()))
} else {
for i, pluginName := range args {
startTime := time.Now()
startTime := common.Now()
do(pluginName, uint16(timeout.Seconds()))
expiredTime := time.Now().Sub(startTime)
expiredTime := common.Since(startTime)

if timeout <= expiredTime {
if i < len(args)-1 {
Expand Down Expand Up @@ -375,9 +375,9 @@ func ClusterUpdatePlugin(c *cli.Context) error {
continue
}

startTime := time.Now()
startTime := common.Now()
seq, err := getOperationSequence(group, uint16(timeout.Seconds()))
expiredTime := time.Now().Sub(startTime)
expiredTime := common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s: %v", file, err))
Expand All @@ -392,9 +392,9 @@ func ClusterUpdatePlugin(c *cli.Context) error {

seq++

startTime = time.Now()
startTime = common.Now()
do(file, seq, data, uint16(timeout.Seconds()))
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

setLocalOperationSequence(group, seq)

Expand Down Expand Up @@ -453,9 +453,9 @@ func ClusterCreatePipeline(c *cli.Context) error {
continue
}

startTime := time.Now()
startTime := common.Now()
seq, err := getOperationSequence(group, uint16(timeout.Seconds()))
expiredTime := time.Now().Sub(startTime)
expiredTime := common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s: %v", file, err))
Expand All @@ -470,9 +470,9 @@ func ClusterCreatePipeline(c *cli.Context) error {

seq++

startTime = time.Now()
startTime = common.Now()
do(file, seq, data, uint16(timeout.Seconds()))
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

setLocalOperationSequence(group, seq)

Expand Down Expand Up @@ -520,13 +520,13 @@ func ClusterDeletePipeline(c *cli.Context) error {
timeout := time.Duration(timeoutSec) * time.Second

for i, pipelineName := range args {
startTime := time.Now()
startTime := common.Now()
seq, err := getOperationSequence(group, uint16(timeout.Seconds()))
if err != nil {
errs.append(fmt.Errorf("%s: %v", pipelineName, err))
break
}
expiredTime := time.Now().Sub(startTime)
expiredTime := common.Since(startTime)
if timeout <= expiredTime {
errs.append(fmt.Errorf("timeout: skip to handle [%s]", strings.Join(args[i:], ", ")))
break
Expand All @@ -535,9 +535,9 @@ func ClusterDeletePipeline(c *cli.Context) error {

seq++

startTime = time.Now()
startTime = common.Now()
do(pipelineName, seq, uint16(timeout.Seconds()))
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

setLocalOperationSequence(group, seq+1)

Expand Down Expand Up @@ -615,9 +615,9 @@ func ClusterRetrievePipelines(c *cli.Context) error {
doAll(uint16(timeout.Seconds()))
} else {
for i, pipelineName := range args {
startTime := time.Now()
startTime := common.Now()
do(pipelineName, uint16(timeout.Seconds()))
expiredTime := time.Now().Sub(startTime)
expiredTime := common.Since(startTime)

if timeout <= expiredTime {
if i < len(args)-1 {
Expand Down Expand Up @@ -678,9 +678,9 @@ func ClusterUpdatePipeline(c *cli.Context) error {
continue
}

startTime := time.Now()
startTime := common.Now()
seq, err := getOperationSequence(group, uint16(timeout.Seconds()))
expiredTime := time.Now().Sub(startTime)
expiredTime := common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s: %v", file, err))
Expand All @@ -695,9 +695,9 @@ func ClusterUpdatePipeline(c *cli.Context) error {

seq++

startTime = time.Now()
startTime = common.Now()
do(file, seq, data, uint16(timeout.Seconds()))
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

setLocalOperationSequence(group, seq)

Expand Down
40 changes: 20 additions & 20 deletions src/cli/cluster_statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strings"
"time"

"common"

"github.com/hexdecteam/easegateway-go-client/rest/1.0/cluster/statistics/v1/pdu"
"github.com/urfave/cli"

"common"
)

func ClusterRetrievePluginIndicatorNames(c *cli.Context) error {
Expand Down Expand Up @@ -43,10 +43,10 @@ func ClusterRetrievePluginIndicatorNames(c *cli.Context) error {
}
timeout -= expiredTime

startTime := time.Now()
startTime := common.Now()
retrieveResp, apiResp, err := clusterStatApi().GetPluginIndicatorNames(
group, pipelineName, pluginName, req)
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s-%s: %v", pipelineName, pluginName, err))
Expand Down Expand Up @@ -104,10 +104,10 @@ func ClusterGetPluginIndicatorValue(c *cli.Context) error {
}
timeout -= expiredTime

startTime := time.Now()
startTime := common.Now()
value, apiResp, err := clusterStatApi().GetPluginIndicatorValue(
group, pipelineName, pluginName, indicatorName, req)
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s-%s-%s: %v",
Expand Down Expand Up @@ -169,10 +169,10 @@ func ClusterGetPluginIndicatorDesc(c *cli.Context) error {
}
timeout -= expiredTime

startTime := time.Now()
startTime := common.Now()
desc, apiResp, err := clusterStatApi().GetPluginIndicatorDesc(
group, pipelineName, pluginName, indicatorName, req)
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s-%s-%s: %v",
Expand Down Expand Up @@ -225,9 +225,9 @@ func ClusterRetrievePipelineIndicatorNames(c *cli.Context) error {
}
timeout -= expiredTime

startTime := time.Now()
startTime := common.Now()
retrieveResp, apiResp, err := clusterStatApi().GetPipelineIndicatorNames(group, pipelineName, req)
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s: %v", pipelineName, err))
Expand Down Expand Up @@ -281,10 +281,10 @@ func ClusterGetPipelineIndicatorValue(c *cli.Context) error {
}
timeout -= expiredTime

startTime := time.Now()
startTime := common.Now()
value, apiResp, err := clusterStatApi().GetPipelineIndicatorValue(
group, pipelineName, indicatorName, req)
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s-%s: %v", pipelineName, indicatorName, err))
Expand Down Expand Up @@ -338,10 +338,10 @@ func ClusterGetPipelineIndicatorDesc(c *cli.Context) error {
}
timeout -= expiredTime

startTime := time.Now()
startTime := common.Now()
desc, apiResp, err := clusterStatApi().GetPipelineIndicatorDesc(
group, pipelineName, indicatorName, req)
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s-%s: %v", pipelineName, indicatorName, err))
Expand Down Expand Up @@ -391,9 +391,9 @@ func ClusterRetrieveTaskIndicatorNames(c *cli.Context) error {
}
timeout -= expiredTime

startTime := time.Now()
startTime := common.Now()
retrieveResp, apiResp, err := clusterStatApi().GetTaskIndicatorNames(group, pipelineName, req)
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s: %v", pipelineName, err))
Expand Down Expand Up @@ -447,10 +447,10 @@ func ClusterGetTaskIndicatorValue(c *cli.Context) error {
}
timeout -= expiredTime

startTime := time.Now()
startTime := common.Now()
value, apiResp, err := clusterStatApi().GetTaskIndicatorValue(
group, pipelineName, indicatorName, req)
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s-%s: %v", pipelineName, indicatorName, err))
Expand Down Expand Up @@ -504,9 +504,9 @@ func ClusterGetTaskIndicatorDesc(c *cli.Context) error {
}
timeout -= expiredTime

startTime := time.Now()
startTime := common.Now()
desc, apiResp, err := clusterStatApi().GetTaskIndicatorDesc(group, pipelineName, indicatorName, req)
expiredTime = time.Now().Sub(startTime)
expiredTime = common.Since(startTime)

if err != nil {
errs.append(fmt.Errorf("%s-%s: %v", pipelineName, indicatorName, err))
Expand Down
4 changes: 2 additions & 2 deletions src/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (c *Cluster) leaveNode(node *memberlist.Node) {
}

var event EventType
now := time.Now()
now := common.Now()

switch ms.Status {
case MemberLeaving:
Expand Down Expand Up @@ -918,7 +918,7 @@ func (c *Cluster) cleanupMember() {
for {
select {
case <-ticker.C:
now := time.Now()
now := common.Now()

c.membersLock.Lock()
removedMembers := c.failedMembers.cleanup(now)
Expand Down
4 changes: 3 additions & 1 deletion src/cluster/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"math/rand"
"net"
"time"

"common"
)

func init() {
Expand Down Expand Up @@ -124,7 +126,7 @@ func (mob *memberOperationBook) save(msgType messageType, nodeName string, msgTi
mob.operations[nodeName] = &memberOperation{
msgType: msgType,
messageTime: msgTime,
receiveTime: time.Now(),
receiveTime: common.Now(),
}
return true
}
Expand Down
3 changes: 2 additions & 1 deletion src/cluster/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"logger"
"common"
)

type RequestParam struct {
Expand Down Expand Up @@ -61,7 +62,7 @@ func createFuture(requestId uint64, requestTime logicalTime, memberCount int,
future := &Future{
requestId: requestId,
requestTime: requestTime,
requestDeadline: time.Now().Add(param.Timeout),
requestDeadline: common.Now().Add(param.Timeout),
responseBook: make(map[string]struct{}),
responseStream: make(chan *MemberResponse, memberCount*2),
ackStream: make(chan string, memberCount*2),
Expand Down
6 changes: 3 additions & 3 deletions src/common/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (t *timeIO) Elapse() time.Duration {
}

func (t *timeIO) timeTrack(start time.Time) {
t.duration += time.Since(start)
t.duration += Since(start)
}

////
Expand All @@ -115,7 +115,7 @@ func NewTimeReader(r io.Reader) *TimeReader {
}

func (tr *TimeReader) Read(p []byte) (n int, err error) { // io.Reader stub
defer tr.timeTrack(time.Now())
defer tr.timeTrack(Now())
return tr.r.Read(p)
}

Expand All @@ -133,7 +133,7 @@ func NewTimeWriter(w io.Writer) *TimeWriter {
}

func (tw *TimeWriter) Write(p []byte) (n int, err error) { // io.Write stub
defer tw.timeTrack(time.Now())
defer tw.timeTrack(Now())
return tw.w.Write(p)
}

Expand Down
Loading

0 comments on commit 419ddd9

Please sign in to comment.