设为首页 加入收藏

TOP

Go 语言的分布式读写互斥(二)
2015-07-16 12:56:51 来源: 作者: 【 】 浏览:8
Tags:语言 分布式 读写
00, "Amount of work for each reader")
? ? readers := flag.Int("n", runtime.GOMAXPROCS(0), "Total number of readers")
? ? checkcpu := flag.Uint64("c", 100, "Update CPU estimate every n iterations")
? ? flag.Parse()
?
? ? var o *os.File
? ? if *cpuprofile {
? ? ? ? o, _ := os.Create("rw.out")
? ? ? ? pprof.StartCPUProfile(o)
? ? }
?
? ? readers_per_core := *readers / runtime.GOMAXPROCS(0)
?
? ? var wg sync.WaitGroup
?
? ? var mx1 sync.RWMutex
?
? ? start1 := time.Now()
? ? for n := 0; n < runtime.GOMAXPROCS(0); n++ {
? ? ? ? for r := 0; r < readers_per_core; r++ {
? ? ? ? ? ? wg.Add(1)
? ? ? ? ? ? go func() {
? ? ? ? ? ? ? ? defer wg.Done()
? ? ? ? ? ? ? ? r := rand.New(rand.NewSource(rand.Int63()))
? ? ? ? ? ? ? ? for n := uint64(0); n < *locks; n++ {
? ? ? ? ? ? ? ? ? ? if r.Float64() < *write {
? ? ? ? ? ? ? ? ? ? ? ? mx1.Lock()
? ? ? ? ? ? ? ? ? ? ? ? x := 0
? ? ? ? ? ? ? ? ? ? ? ? for i := 0; i < *wwork; i++ {
? ? ? ? ? ? ? ? ? ? ? ? ? ? x++
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? _ = x
? ? ? ? ? ? ? ? ? ? ? ? mx1.Unlock()
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? mx1.RLock()
? ? ? ? ? ? ? ? ? ? ? ? x := 0
? ? ? ? ? ? ? ? ? ? ? ? for i := 0; i < *rwork; i++ {
? ? ? ? ? ? ? ? ? ? ? ? ? ? x++
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? _ = x
? ? ? ? ? ? ? ? ? ? ? ? mx1.RUnlock()
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }()
? ? ? ? }
? ? }
? ? wg.Wait()
? ? end1 := time.Now()
?
? ? t1 := end1.Sub(start1)
? ? fmt.Println("mx1", runtime.GOMAXPROCS(0), *readers, *locks, *write, *wwork, *rwork, *checkcpu, t1.Seconds(), t1)
?
? ? if *cpuprofile {
? ? ? ? pprof.StopCPUProfile()
? ? ? ? o.Close()
?
? ? ? ? o, _ = os.Create("rw2.out")
? ? ? ? pprof.StartCPUProfile(o)
? ? }
?
? ? mx2 := make(RWMutex2, len(cpus))
?
? ? start2 := time.Now()
? ? for n := 0; n < runtime.GOMAXPROCS(0); n++ {
? ? ? ? for r := 0; r < readers_per_core; r++ {
? ? ? ? ? ? wg.Add(1)
? ? ? ? ? ? go func() {
? ? ? ? ? ? ? ? defer wg.Done()
? ? ? ? ? ? ? ? c := cpus[cpu()]
? ? ? ? ? ? ? ? r := rand.New(rand.NewSource(rand.Int63()))
? ? ? ? ? ? ? ? for n := uint64(0); n < *locks; n++ {
? ? ? ? ? ? ? ? ? ? if *checkcpu != 0 && n%*checkcpu == 0 {
? ? ? ? ? ? ? ? ? ? ? ? c = cpus[cpu()]
? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? if r.Float64() < *write {
? ? ? ? ? ? ? ? ? ? ? ? mx2.Lock()
? ? ? ? ? ? ? ? ? ? ? ? x := 0
? ? ? ? ? ? ? ? ? ? ? ? for i := 0; i < *wwork; i++ {
? ? ? ? ? ? ? ? ? ? ? ? ? ? x++
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? _ = x
? ? ? ? ? ? ? ? ? ? ? ? mx2.Unlock()
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? mx2[c].RLock()
? ? ? ? ? ? ? ? ? ? ? ? x := 0
? ? ? ? ? ? ? ? ? ? ? ? for i := 0; i < *rwork; i++ {
? ? ? ? ? ? ? ? ? ? ? ? ? ? x++
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? _ = x
? ? ? ? ? ? ? ? ? ? ? ? mx2[c].RUnlock()
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }()
? ? ? ? }
? ? }
? ? wg.Wait()
? ? end2 := time.Now()
?
? ? pprof.StopCPUProfile()
? ? o.Close()
?
? ? t2 := end2.Sub(start2)
? ? fmt.Println("mx2", runtime.GOMAXPROCS(0), *readers, *locks, *write, *wwork, *rwork, *checkcpu, t2.Seconds(), t2)
}


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Lua执行字节码的过程介绍 下一篇PHP中处理浮点数的一些方法记录

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: