From fb60fb3996fe3a6ed79cdab91e694e338ed4af52 Mon Sep 17 00:00:00 2001
From: capossele <angelocapossele@gmail.com>
Date: Tue, 16 Jun 2020 18:04:56 +0100
Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20WIP?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 plugins/metrics/plugin.go  |  8 ++------
 plugins/metrics/process.go | 16 +++++-----------
 2 files changed, 7 insertions(+), 17 deletions(-)

diff --git a/plugins/metrics/plugin.go b/plugins/metrics/plugin.go
index 974ed49d..bd99aac4 100644
--- a/plugins/metrics/plugin.go
+++ b/plugins/metrics/plugin.go
@@ -43,14 +43,10 @@ func configure(_ *node.Plugin) {
 		analysisOutboundBytes.Add(amountBytes)
 	}))
 	metrics.Events().CPUUsage.Attach(events.NewClosure(func(cpuPercent float64) {
-		cpuLock.Lock()
-		defer cpuLock.Unlock()
-		_cpuUsage = cpuPercent
+		cpuUsage.Store(cpuPercent)
 	}))
 	metrics.Events().MemUsage.Attach(events.NewClosure(func(memAllocBytes uint64) {
-		memUsageLock.Lock()
-		defer memUsageLock.Unlock()
-		_memUsageBytes = memAllocBytes
+		memUsageBytes.Store(memAllocBytes)
 	}))
 	metrics.Events().Synced.Attach(events.NewClosure(func(synced bool) {
 		syncLock.Lock()
diff --git a/plugins/metrics/process.go b/plugins/metrics/process.go
index 137844b4..10dc4423 100644
--- a/plugins/metrics/process.go
+++ b/plugins/metrics/process.go
@@ -5,22 +5,18 @@ import (
 	"time"
 
 	"github.com/iotaledger/goshimmer/packages/metrics"
-	"github.com/iotaledger/hive.go/syncutils"
 	"github.com/shirou/gopsutil/cpu"
+	"go.uber.org/atomic"
 )
 
 var (
-	_cpuUsage      float64
-	cpuLock        syncutils.RWMutex
-	_memUsageBytes uint64
-	memUsageLock   syncutils.RWMutex
+	cpuUsage      atomic.Float64
+	memUsageBytes atomic.Uint64
 )
 
 // CpuUsage returns the current cpu usage
 func CpuUsage() float64 {
-	cpuLock.RLock()
-	defer cpuLock.RUnlock()
-	return _cpuUsage
+	return cpuUsage.Load()
 }
 
 func measureCPUUsage() {
@@ -42,7 +38,5 @@ func measureMemUsage() {
 
 // MemUsage returns the current memory allocated as bytes
 func MemUsage() uint64 {
-	memUsageLock.RLock()
-	defer memUsageLock.RUnlock()
-	return _memUsageBytes
+	return memUsageBytes.Load()
 }
-- 
GitLab