aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVladimir Lavor <vlavor@cisco.com>2020-09-04 14:23:10 +0200
committerOndrej Fabry <ofabry@cisco.com>2020-09-07 13:54:08 +0000
commite517439567ad843033257664fdfe90a9173b0aa6 (patch)
tree325bf97c0512b6ce729ba74a287af98a39c06a85
parente3cea0cd4447be264374b56788da0e93b2e67a7e (diff)
Remove unnecessary allocation
Change-Id: If77b7b37ae736fdf21f4c337350905480e6fb011 Signed-off-by: Vladimir Lavor <vlavor@cisco.com>
-rw-r--r--adapter/statsclient/stat_segment_api.go4
-rw-r--r--adapter/statsclient/statsclient.go20
-rw-r--r--adapter/statsclient/statseg_v1.go4
-rw-r--r--adapter/statsclient/statseg_v2.go75
4 files changed, 51 insertions, 52 deletions
diff --git a/adapter/statsclient/stat_segment_api.go b/adapter/statsclient/stat_segment_api.go
index f2e4219..0ca0c8b 100644
--- a/adapter/statsclient/stat_segment_api.go
+++ b/adapter/statsclient/stat_segment_api.go
@@ -55,8 +55,8 @@ type statDirectoryName []byte
// statSegment represents common API for every stats API version
type statSegment interface {
// GetDirectoryVector returns pointer to memory where the beginning
- // of the data directory is located
- GetDirectoryVector() (unsafe.Pointer, error)
+ // of the data directory is located.
+ GetDirectoryVector() unsafe.Pointer
// GetStatDirOnIndex accepts directory vector and particular index.
// Returns pointer to the beginning of the segment. Also the directory
diff --git a/adapter/statsclient/statsclient.go b/adapter/statsclient/statsclient.go
index 9c1c334..a39cbd5 100644
--- a/adapter/statsclient/statsclient.go
+++ b/adapter/statsclient/statsclient.go
@@ -138,8 +138,8 @@ func (sc *StatsClient) ListStats(patterns ...string) ([]string, error) {
return nil, err
}
- dirVector, err := sc.GetDirectoryVector()
- if err != nil {
+ dirVector := sc.GetDirectoryVector()
+ if dirVector == nil {
return nil, fmt.Errorf("failed to list stats: %v", err)
}
vecLen := *(*uint32)(vectorLen(dirVector))
@@ -171,8 +171,8 @@ func (sc *StatsClient) DumpStats(patterns ...string) (entries []adapter.StatEntr
return nil, err
}
- dirVector, err := sc.GetDirectoryVector()
- if err != nil {
+ dirVector := sc.GetDirectoryVector()
+ if dirVector == nil {
return nil, err
}
dirLen := *(*uint32)(vectorLen(dirVector))
@@ -217,8 +217,8 @@ func (sc *StatsClient) PrepareDir(patterns ...string) (*adapter.StatDir, error)
}
dir.Indexes = indexes
- dirVector, err := sc.GetDirectoryVector()
- if err != nil {
+ dirVector := sc.GetDirectoryVector()
+ if dirVector == nil {
return nil, err
}
dirLen := *(*uint32)(vectorLen(dirVector))
@@ -263,8 +263,8 @@ func (sc *StatsClient) UpdateDir(dir *adapter.StatDir) (err error) {
return adapter.ErrStatsAccessFailed
}
- dirVector, err := sc.GetDirectoryVector()
- if err != nil {
+ dirVector := sc.GetDirectoryVector()
+ if dirVector == nil {
return err
}
for i, index := range dir.Indexes {
@@ -412,8 +412,8 @@ func (sc *StatsClient) listIndexesFunc(f func(name []byte) bool) (indexes []uint
indexes = make([]uint32, 0, 3200)
}
- dirVector, err := sc.GetDirectoryVector()
- if err != nil {
+ dirVector := sc.GetDirectoryVector()
+ if dirVector == nil {
return nil, err
}
vecLen := *(*uint32)(vectorLen(dirVector))
diff --git a/adapter/statsclient/statseg_v1.go b/adapter/statsclient/statseg_v1.go
index a02c7ac..3d45201 100644
--- a/adapter/statsclient/statseg_v1.go
+++ b/adapter/statsclient/statseg_v1.go
@@ -66,9 +66,9 @@ func (ss *statSegmentV1) loadSharedHeader(b []byte) (header sharedHeaderV1) {
}
}
-func (ss *statSegmentV1) GetDirectoryVector() (unsafe.Pointer, error) {
+func (ss *statSegmentV1) GetDirectoryVector() unsafe.Pointer {
dirOffset, _, _ := ss.getOffsets()
- return unsafe.Pointer(&ss.sharedHeader[dirOffset]), nil
+ return unsafe.Pointer(&ss.sharedHeader[dirOffset])
}
func (ss *statSegmentV1) GetErrorVector() (unsafe.Pointer, error) {
diff --git a/adapter/statsclient/statseg_v2.go b/adapter/statsclient/statseg_v2.go
index 7091ff9..d52077f 100644
--- a/adapter/statsclient/statseg_v2.go
+++ b/adapter/statsclient/statseg_v2.go
@@ -15,7 +15,6 @@
package statsclient
import (
- "fmt"
"sync/atomic"
"unsafe"
@@ -65,7 +64,7 @@ func (ss *statSegmentV2) loadSharedHeader(b []byte) (header sharedHeaderV2) {
}
}
-func (ss *statSegmentV2) GetDirectoryVector() (unsafe.Pointer, error) {
+func (ss *statSegmentV2) GetDirectoryVector() unsafe.Pointer {
header := ss.loadSharedHeader(ss.sharedHeader)
return ss.adjust(unsafe.Pointer(&header.dirVector))
}
@@ -100,8 +99,8 @@ func (ss *statSegmentV2) CopyEntryData(statSegDir unsafe.Pointer) adapter.Stat {
return adapter.ScalarStat(dirEntry.unionData)
case statDirErrorIndex:
- dirVector, err := ss.getErrorVector()
- if err != nil {
+ dirVector := ss.getErrorVector()
+ if dirVector == nil {
debugf("error vector pointer is out of range for %s", dirEntry.name)
return nil
}
@@ -109,8 +108,8 @@ func (ss *statSegmentV2) CopyEntryData(statSegDir unsafe.Pointer) adapter.Stat {
var errData adapter.Counter
for i := uint32(0); i < vecLen; i++ {
cb := statSegPointer(dirVector, uintptr(i+1)*unsafe.Sizeof(uint64(0)))
- cbVal, err := ss.adjust(vectorLen(cb))
- if err != nil {
+ cbVal := ss.adjust(vectorLen(cb))
+ if cbVal == nil {
debugf("error counter pointer out of range")
continue
}
@@ -121,8 +120,8 @@ func (ss *statSegmentV2) CopyEntryData(statSegDir unsafe.Pointer) adapter.Stat {
return adapter.ErrorStat(errData)
case statDirCounterVectorSimple:
- dirVector, err := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
- if err != nil {
+ dirVector := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
+ if dirVector == nil {
debugf("data vector pointer is out of range for %s", dirEntry.name)
return nil
}
@@ -130,8 +129,8 @@ func (ss *statSegmentV2) CopyEntryData(statSegDir unsafe.Pointer) adapter.Stat {
data := make([][]adapter.Counter, vecLen)
for i := uint32(0); i < vecLen; i++ {
counterVectorOffset := statSegPointer(dirVector, uintptr(i+1)*unsafe.Sizeof(uint64(0)))
- counterVector, err := ss.adjust(vectorLen(counterVectorOffset))
- if err != nil {
+ counterVector := ss.adjust(vectorLen(counterVectorOffset))
+ if counterVector == nil {
debugf("counter (vector simple) pointer out of range")
continue
}
@@ -146,8 +145,8 @@ func (ss *statSegmentV2) CopyEntryData(statSegDir unsafe.Pointer) adapter.Stat {
return adapter.SimpleCounterStat(data)
case statDirCounterVectorCombined:
- dirVector, err := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
- if err != nil {
+ dirVector := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
+ if dirVector == nil {
debugf("data vector pointer is out of range for %s", dirEntry.name)
return nil
}
@@ -155,8 +154,8 @@ func (ss *statSegmentV2) CopyEntryData(statSegDir unsafe.Pointer) adapter.Stat {
data := make([][]adapter.CombinedCounter, vecLen)
for i := uint32(0); i < vecLen; i++ {
counterVectorOffset := statSegPointer(dirVector, uintptr(i+1)*unsafe.Sizeof(uint64(0)))
- counterVector, err := ss.adjust(vectorLen(counterVectorOffset))
- if err != nil {
+ counterVector := ss.adjust(vectorLen(counterVectorOffset))
+ if counterVector == nil {
debugf("counter (vector combined) pointer out of range")
continue
}
@@ -171,8 +170,8 @@ func (ss *statSegmentV2) CopyEntryData(statSegDir unsafe.Pointer) adapter.Stat {
return adapter.CombinedCounterStat(data)
case statDirNameVector:
- dirVector, err := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
- if err != nil {
+ dirVector := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
+ if dirVector == nil {
debugf("data vector pointer is out of range for %s", dirEntry.name)
return nil
}
@@ -184,8 +183,8 @@ func (ss *statSegmentV2) CopyEntryData(statSegDir unsafe.Pointer) adapter.Stat {
debugf("name vector out of range for %s (%v)", dirEntry.name, i)
continue
}
- nameVector, err := ss.adjust(vectorLen(nameVectorOffset))
- if err != nil {
+ nameVector := ss.adjust(vectorLen(nameVectorOffset))
+ if nameVector == nil {
debugf("name data pointer out of range")
continue
}
@@ -219,8 +218,8 @@ func (ss *statSegmentV2) UpdateEntryData(statSegDir unsafe.Pointer, stat *adapte
*stat = adapter.ScalarStat(dirEntry.unionData)
case adapter.ErrorStat:
- dirVector, err := ss.getErrorVector()
- if err != nil {
+ dirVector := ss.getErrorVector()
+ if dirVector == nil {
debugf("error vector pointer is out of range for %s", dirEntry.name)
return nil
}
@@ -228,8 +227,8 @@ func (ss *statSegmentV2) UpdateEntryData(statSegDir unsafe.Pointer, stat *adapte
var errData adapter.Counter
for i := uint32(0); i < vecLen; i++ {
cb := statSegPointer(dirVector, uintptr(i+1)*unsafe.Sizeof(uint64(0)))
- cbVal, err := ss.adjust(vectorLen(cb))
- if err != nil {
+ cbVal := ss.adjust(vectorLen(cb))
+ if cbVal == nil {
debugf("error counter pointer out of range")
continue
}
@@ -240,8 +239,8 @@ func (ss *statSegmentV2) UpdateEntryData(statSegDir unsafe.Pointer, stat *adapte
*stat = adapter.ErrorStat(errData)
case adapter.SimpleCounterStat:
- dirVector, err := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
- if err != nil {
+ dirVector := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
+ if dirVector == nil {
debugf("data vector pointer is out of range for %s", dirEntry.name)
return nil
}
@@ -252,8 +251,8 @@ func (ss *statSegmentV2) UpdateEntryData(statSegDir unsafe.Pointer, stat *adapte
}
for i := uint32(0); i < vecLen; i++ {
counterVectorOffset := statSegPointer(dirVector, uintptr(i+1)*unsafe.Sizeof(uint64(0)))
- counterVector, err := ss.adjust(vectorLen(counterVectorOffset))
- if err != nil {
+ counterVector := ss.adjust(vectorLen(counterVectorOffset))
+ if counterVector == nil {
debugf("counter (vector simple) pointer out of range")
continue
}
@@ -267,8 +266,8 @@ func (ss *statSegmentV2) UpdateEntryData(statSegDir unsafe.Pointer, stat *adapte
}
case adapter.CombinedCounterStat:
- dirVector, err := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
- if err != nil {
+ dirVector := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
+ if dirVector == nil {
debugf("data vector pointer is out of range for %s", dirEntry.name)
return nil
}
@@ -276,8 +275,8 @@ func (ss *statSegmentV2) UpdateEntryData(statSegDir unsafe.Pointer, stat *adapte
data := (*stat).(adapter.CombinedCounterStat)
for i := uint32(0); i < vecLen; i++ {
counterVectorOffset := statSegPointer(dirVector, uintptr(i+1)*unsafe.Sizeof(uint64(0)))
- counterVector, err := ss.adjust(vectorLen(counterVectorOffset))
- if err != nil {
+ counterVector := ss.adjust(vectorLen(counterVectorOffset))
+ if counterVector == nil {
debugf("counter (vector combined) pointer out of range")
continue
}
@@ -291,8 +290,8 @@ func (ss *statSegmentV2) UpdateEntryData(statSegDir unsafe.Pointer, stat *adapte
}
case adapter.NameStat:
- dirVector, err := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
- if err != nil {
+ dirVector := ss.adjust(unsafe.Pointer(&dirEntry.unionData))
+ if dirVector == nil {
debugf("data vector pointer is out of range for %s", dirEntry.name)
return nil
}
@@ -304,8 +303,8 @@ func (ss *statSegmentV2) UpdateEntryData(statSegDir unsafe.Pointer, stat *adapte
debugf("name vector out of range for %s (%v)", dirEntry.name, i)
continue
}
- nameVector, err := ss.adjust(vectorLen(nameVectorOffset))
- if err != nil {
+ nameVector := ss.adjust(vectorLen(nameVectorOffset))
+ if nameVector == nil {
debugf("name data pointer out of range")
continue
}
@@ -334,18 +333,18 @@ func (ss *statSegmentV2) UpdateEntryData(statSegDir unsafe.Pointer, stat *adapte
// Adjust data pointer using shared header and base and return
// the pointer to a data segment
-func (ss *statSegmentV2) adjust(data unsafe.Pointer) (unsafe.Pointer, error) {
+func (ss *statSegmentV2) adjust(data unsafe.Pointer) unsafe.Pointer {
header := ss.loadSharedHeader(ss.sharedHeader)
adjusted := unsafe.Pointer(uintptr(unsafe.Pointer(&ss.sharedHeader[0])) +
uintptr(*(*uint64)(data)) - uintptr(*(*uint64)(unsafe.Pointer(&header.base))))
if uintptr(unsafe.Pointer(&ss.sharedHeader[len(ss.sharedHeader)-1])) <= uintptr(adjusted) ||
uintptr(unsafe.Pointer(&ss.sharedHeader[0])) >= uintptr(adjusted) {
- return nil, fmt.Errorf("adjusted data out of range for %v", data)
+ return nil
}
- return adjusted, nil
+ return adjusted
}
-func (ss *statSegmentV2) getErrorVector() (unsafe.Pointer, error) {
+func (ss *statSegmentV2) getErrorVector() unsafe.Pointer {
header := ss.loadSharedHeader(ss.sharedHeader)
return ss.adjust(unsafe.Pointer(&header.errorVector))
}