Skip to content

Commit

Permalink
aviod race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
austinhmh committed Dec 12, 2024
1 parent 65726f6 commit 2792451
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions util/rdma/rdma_benchmark/common/perf_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package common

import (
"fmt"
"sync/atomic"
"time"
)

type NetPerfStat struct {
num int64
size int64
start int64
num int64
size int64
start int64
tmCost int64
}

Expand All @@ -18,23 +19,25 @@ func Stat() *NetPerfStat {
return &stat
}

func (s *NetPerfStat)AddSum(size int) {
s.num++
s.size += 2*int64(size) //send and recv
if s.start == 0 {
s.start = time.Now().Unix()
func (s *NetPerfStat) AddSum(size int) {
atomic.AddInt64(&s.num, 1)
atomic.AddInt64(&s.size, int64(size)*2) // send and recv
if atomic.LoadInt64(&s.start) == 0 { // Although there may be a potential race condition, it only occurs when start = 0, so I won't implement a lock here.
atomic.StoreInt64(&s.start, time.Now().Unix())
}
}

func (s *NetPerfStat)AddSumTime(size int, tm int64) {
func (s *NetPerfStat) AddSumTime(size int, tm int64) {
s.AddSum(size)
s.tmCost += tm
atomic.AddInt64(&s.tmCost, tm)
}
func (s *NetPerfStat)Print() {
tmCost := time.Now().Unix() - s.start
func (s *NetPerfStat) Print() {
tmCost := time.Now().Unix() - atomic.LoadInt64(&s.start)
//io ps band w
fmt.Printf("IOPS=[%.2f], TOTAL_BPS=[%.2fM], AVG_TM=[%.2fus]\n", float64(s.num)/float64(tmCost),
float64(s.size)/(1024*1024)/float64(tmCost), float64(s.tmCost)/float64(s.num))
}

num := atomic.LoadInt64(&s.num)
totaltmCost := atomic.LoadInt64(&s.tmCost)
size := atomic.LoadInt64(&s.size)

fmt.Printf("IOPS=[%.2f], TOTAL_BPS=[%.2fM], AVG_TM=[%.2fus]\n", float64(num)/float64(tmCost),
float64(size)/(1024*1024)/float64(tmCost), float64(totaltmCost)/float64(s.num))
}

0 comments on commit 2792451

Please sign in to comment.