cmd/geth: atomic types used (#27756)

pull/27767/head
ucwong 1 year ago committed by GitHub
parent 88f3d61468
commit 7a1fba1a02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      cmd/geth/attach_test.go
  2. 14
      cmd/geth/chaincmd.go
  3. 4
      cmd/geth/les_test.go

@ -61,7 +61,7 @@ func TestRemoteDbWithHeaders(t *testing.T) {
} }
func testReceiveHeaders(t *testing.T, ln net.Listener, gethArgs ...string) { func testReceiveHeaders(t *testing.T, ln net.Listener, gethArgs ...string) {
var ok uint32 var ok atomic.Uint32
server := &http.Server{ server := &http.Server{
Addr: "localhost:0", Addr: "localhost:0",
Handler: &testHandler{func(w http.ResponseWriter, r *http.Request) { Handler: &testHandler{func(w http.ResponseWriter, r *http.Request) {
@ -72,12 +72,12 @@ func testReceiveHeaders(t *testing.T, ln net.Listener, gethArgs ...string) {
if have, want := r.Header.Get("second"), "two"; have != want { if have, want := r.Header.Get("second"), "two"; have != want {
t.Fatalf("missing header, have %v want %v", have, want) t.Fatalf("missing header, have %v want %v", have, want)
} }
atomic.StoreUint32(&ok, 1) ok.Store(1)
}}} }}}
go server.Serve(ln) go server.Serve(ln)
defer server.Close() defer server.Close()
runGeth(t, gethArgs...).WaitExit() runGeth(t, gethArgs...).WaitExit()
if atomic.LoadUint32(&ok) != 1 { if ok.Load() != 1 {
t.Fatal("Test fail, expected invocation to succeed") t.Fatal("Test fail, expected invocation to succeed")
} }
} }

@ -261,16 +261,16 @@ func importChain(ctx *cli.Context) error {
defer db.Close() defer db.Close()
// Start periodically gathering memory profiles // Start periodically gathering memory profiles
var peakMemAlloc, peakMemSys uint64 var peakMemAlloc, peakMemSys atomic.Uint64
go func() { go func() {
stats := new(runtime.MemStats) stats := new(runtime.MemStats)
for { for {
runtime.ReadMemStats(stats) runtime.ReadMemStats(stats)
if atomic.LoadUint64(&peakMemAlloc) < stats.Alloc { if peakMemAlloc.Load() < stats.Alloc {
atomic.StoreUint64(&peakMemAlloc, stats.Alloc) peakMemAlloc.Store(stats.Alloc)
} }
if atomic.LoadUint64(&peakMemSys) < stats.Sys { if peakMemSys.Load() < stats.Sys {
atomic.StoreUint64(&peakMemSys, stats.Sys) peakMemSys.Store(stats.Sys)
} }
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
} }
@ -303,8 +303,8 @@ func importChain(ctx *cli.Context) error {
mem := new(runtime.MemStats) mem := new(runtime.MemStats)
runtime.ReadMemStats(mem) runtime.ReadMemStats(mem)
fmt.Printf("Object memory: %.3f MB current, %.3f MB peak\n", float64(mem.Alloc)/1024/1024, float64(atomic.LoadUint64(&peakMemAlloc))/1024/1024) fmt.Printf("Object memory: %.3f MB current, %.3f MB peak\n", float64(mem.Alloc)/1024/1024, float64(peakMemAlloc.Load())/1024/1024)
fmt.Printf("System memory: %.3f MB current, %.3f MB peak\n", float64(mem.Sys)/1024/1024, float64(atomic.LoadUint64(&peakMemSys))/1024/1024) fmt.Printf("System memory: %.3f MB current, %.3f MB peak\n", float64(mem.Sys)/1024/1024, float64(peakMemSys.Load())/1024/1024)
fmt.Printf("Allocations: %.3f million\n", float64(mem.Mallocs)/1000000) fmt.Printf("Allocations: %.3f million\n", float64(mem.Mallocs)/1000000)
fmt.Printf("GC pause: %v\n\n", time.Duration(mem.PauseTotalNs)) fmt.Printf("GC pause: %v\n\n", time.Duration(mem.PauseTotalNs))

@ -107,10 +107,10 @@ func ipcEndpoint(ipcPath, datadir string) string {
// but windows require pipes to sit in "\\.\pipe\". Therefore, to run several // but windows require pipes to sit in "\\.\pipe\". Therefore, to run several
// nodes simultaneously, we need to distinguish between them, which we do by // nodes simultaneously, we need to distinguish between them, which we do by
// the pipe filename instead of folder. // the pipe filename instead of folder.
var nextIPC = uint32(0) var nextIPC atomic.Uint32
func startGethWithIpc(t *testing.T, name string, args ...string) *gethrpc { func startGethWithIpc(t *testing.T, name string, args ...string) *gethrpc {
ipcName := fmt.Sprintf("geth-%d.ipc", atomic.AddUint32(&nextIPC, 1)) ipcName := fmt.Sprintf("geth-%d.ipc", nextIPC.Add(1))
args = append([]string{"--networkid=42", "--port=0", "--authrpc.port", "0", "--ipcpath", ipcName}, args...) args = append([]string{"--networkid=42", "--port=0", "--authrpc.port", "0", "--ipcpath", ipcName}, args...)
t.Logf("Starting %v with rpc: %v", name, args) t.Logf("Starting %v with rpc: %v", name, args)

Loading…
Cancel
Save