Code Monkey home page Code Monkey logo

Comments (5)

mochi-co avatar mochi-co commented on May 22, 2024

Interesting. If I had to guess it's because ARMv7 is a 32bit chip, whereas the atomic counters in the broker are set to use 64bit ints. Is this something you are relying on? I can have a think about how this issue can be fixed, and I welcome suggestions from anyone else!

from server.

ogelami avatar ogelami commented on May 22, 2024

Thanks for the quick response! Shouldn't 32-bit be enough for the broker counters?

Could using int where possible and wrapping the sync/atomic with 32/64-bit check for calling say AddUint64 or AddUint32 be a partial solution?

package atomthic

import (
	"sync/atomic"
)

const uintSize = 32 << (^uint(0) >> 32 & 1)

func AddInt(addr *int, delta int) int {
	var c int

	if uintSize == 64 {
		var a int64 = int64(*addr)
		var b = atomic.AddInt64(&a, int64(delta))

		c = int(b)
	} else {
		var a int32 = int32(*addr)
		var b = atomic.AddInt32(&a, int32(delta))

		c = int(b)
	}

	return c
}

func AddUint(addr *uint, delta uint) uint {
	var c uint

	if uintSize == 64 {
		var a uint64 = uint64(*addr)
		var b = atomic.AddUint64(&a, uint64(delta))

		c = uint(b)
	} else {
		var a uint32 = uint32(*addr)
		var b = atomic.AddUint32(&a, uint32(delta))

		c = uint(b)
	}

	return c
}

func LoadInt(addr *int) int {
	var c int

	if uintSize == 64 {
		var a int64 = int64(*addr)
		c = int(atomic.LoadInt64(&a))
	} else {
		var a int32 = int32(*addr)
		c = int(atomic.LoadInt32(&a))
	}

	return c
}

func LoadUint(addr *uint) uint {
	var c uint

	if uintSize == 64 {
		var a uint64 = uint64(*addr)
		c = uint(atomic.LoadUint64(&a))
	} else {
		var a uint32 = uint32(*addr)
		c = uint(atomic.LoadUint32(&a))
	}

	return c
}

func StoreInt(addr *int, val int) {
	if uintSize == 64 {
		var a int64 = int64(*addr)
		var b = int64(val)

		atomic.StoreInt64(&a, b)
	} else {
		var a int32 = int32(*addr)
		var b = int32(val)

		atomic.StoreInt32(&a, b)
	}
}

func StoreUint(addr *uint, val uint) {
	if uintSize == 64 {
		var a uint64 = uint64(*addr)
		var b = uint64(val)

		atomic.StoreUint64(&a, b)
	} else {
		var a uint32 = uint32(*addr)
		var b = uint32(val)

		atomic.StoreUint32(&a, b)
	}
}

from server.

rkennedy avatar rkennedy commented on May 22, 2024

I'm seeing something similar on what I'm pretty sure is the same hardware. I'm my case, the panic is from an unaligned operation:

/tmp/mqtt $ go run examples/tcp/main.go                                                        
Mochi MQTT Server initializing... TCP                                                          
  Started!                                                                                     
panic: unaligned 64-bit atomic operation                                                       
                                                                                               
goroutine 37 [running]:                                                                        
runtime/internal/atomic.panicUnaligned()                                                       
        /usr/local/go/src/runtime/internal/atomic/unaligned.go:8 +0x24                         
runtime/internal/atomic.Load64(0x8be03c)                                                       
        /usr/local/go/src/runtime/internal/atomic/atomic_arm.s:286 +0x14                       
github.com/mochi-co/mqtt/server/listeners.(*TCP).Serve(0x8be000, 0x8a6028)                     
        /tmp/mqtt/server/listeners/tcp.go:89 +0x28                                             
github.com/mochi-co/mqtt/server/listeners.(*Listeners).Serve.func1(0x8bc000, {0x295934, 0x8be00
0}, 0x8a6028)                                                                                  
        /tmp/mqtt/server/listeners/listeners.go:94 +0x70                                       
created by github.com/mochi-co/mqtt/server/listeners.(*Listeners).Serve                        
        /tmp/mqtt/server/listeners/listeners.go:91 +0x9c                                       
exit status 2

I'm using v1.0.4 with Go 1.17.6.

I'm pretty sure none of the functions in ogelami's reply are atomic anymore, and some of them end up storing into the wrong place. I think you can use unsafe.Sizeof instead of bit-twiddling, but I wonder whether just using uintptr might be a simpler fix. There are functions in atomic for that type already. I tried making that change and it seemed to work. I can submit a PR if you like.

from server.

mochi-co avatar mochi-co commented on May 22, 2024

I wonder whether just using uintptr might be a simpler fix. There are functions in atomic for that type already. I tried making that change and it seemed to work. I can submit a PR if you like.

@rkennedy I have been thinking along similar lines and would love to see what you've come up with.

I think there might be some trivial improvements to be had by re-aligning the struct declarations across the board, so I am continuing to look into that.

from server.

mochi-co avatar mochi-co commented on May 22, 2024

After extensive testing, I have released @rkennedy 's fix for this as v1.1.0! Thank you @ogelami and @rkennedy for your hard work and making the project better!

from server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.