Skip to content

Commit

Permalink
Merge branch 'master' into gnmi_native_write
Browse files Browse the repository at this point in the history
  • Loading branch information
ganglyu committed Nov 21, 2022
2 parents 2cd6dfa + ae72767 commit f019892
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 294 deletions.
22 changes: 19 additions & 3 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ stages:
sudo dpkg -i ../target/debs/bullseye/libnl-nf-3-200_*.deb
sudo dpkg -i ../target/debs/bullseye/libhiredis0.14_*.deb
sudo dpkg -i ../target/debs/bullseye/libhiredis-dev_*.deb
sudo dpkg -i ../target/debs/bullseye/libswsscommon_1.0.0_amd64.deb
sudo dpkg -i ../target/debs/bullseye/libswsscommon-dev_1.0.0_amd64.deb
sudo dpkg -i ../target/debs/bullseye/python3-swsscommon_1.0.0_amd64.deb
displayName: "Install libswsscommon dependencies"
- script: |
Expand All @@ -116,6 +113,25 @@ stages:
sudo apt-get install -y dotnet-sdk-5.0
displayName: "Install .NET CORE"
- task: DownloadPipelineArtifact@2
inputs:
source: specific
project: build
pipeline: Azure.sonic-swss-common
artifact: sonic-swss-common.bullseye.amd64
runVersion: 'latestFromBranch'
runBranch: 'refs/heads/master'
displayName: "Download sonic-swss-common"

- script: |
set -ex
# LIBSWSSCOMMON
sudo dpkg -i libswsscommon_1.0.0_amd64.deb
sudo dpkg -i libswsscommon-dev_1.0.0_amd64.deb
sudo dpkg -i python3-swsscommon_1.0.0_amd64.deb
workingDirectory: $(Pipeline.Workspace)/
displayName: 'Install libswsscommon package'
- script: |
set -ex
ls -l
Expand Down
76 changes: 53 additions & 23 deletions common_utils/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,58 @@ const requestContextKey contextkey = 0
// Request Id generator
var requestCounter uint64

var CountersName = [...]string {
"GNMI get",
"GNMI get fail",
"GNMI set",
"GNMI set fail",
"GNOI reboot",
"DBUS",
"DBUS fail",
"DBUS apply patch db",
"DBUS apply patch yang",
"DBUS create checkpoint",
"DBUS delete checkpoint",
"DBUS config save",
"DBUS config reload",
type CounterType int
const (
GNMI_GET CounterType = iota
GNMI_GET_FAIL
GNMI_SET
GNMI_SET_FAIL
GNOI_REBOOT
DBUS
DBUS_FAIL
DBUS_APPLY_PATCH_DB
DBUS_APPLY_PATCH_YANG
DBUS_CREATE_CHECKPOINT
DBUS_DELETE_CHECKPOINT
DBUS_CONFIG_SAVE
DBUS_CONFIG_RELOAD
COUNTER_SIZE
)

func (c CounterType) String() string {
switch c {
case GNMI_GET:
return "GNMI get"
case GNMI_GET_FAIL:
return "GNMI get fail"
case GNMI_SET:
return "GNMI set"
case GNMI_SET_FAIL:
return "GNMI set fail"
case GNOI_REBOOT:
return "GNOI reboot"
case DBUS:
return "DBUS"
case DBUS_FAIL:
return "DBUS fail"
case DBUS_APPLY_PATCH_DB:
return "DBUS apply patch db"
case DBUS_APPLY_PATCH_YANG:
return "DBUS apply patch yang"
case DBUS_CREATE_CHECKPOINT:
return "DBUS create checkpoint"
case DBUS_DELETE_CHECKPOINT:
return "DBUS delete checkpoint"
case DBUS_CONFIG_SAVE:
return "DBUS config save"
case DBUS_CONFIG_RELOAD:
return "DBUS config reload"
default:
return ""
}
}

var globalCounters [len(CountersName)]uint64
var globalCounters [COUNTER_SIZE]uint64


// GetContext function returns the RequestContext object for a
Expand Down Expand Up @@ -80,19 +115,14 @@ func GetUsername(ctx context.Context, username *string) {
}

func InitCounters() {
for i := 0; i < len(CountersName); i++ {
for i := 0; i < int(COUNTER_SIZE); i++ {
globalCounters[i] = 0
}
SetMemCounters(&globalCounters)
}

func IncCounter(name string) {
for i := 0; i < len(CountersName); i++ {
if CountersName[i] == name {
atomic.AddUint64(&globalCounters[i], 1)
break
}
}
func IncCounter(cnt CounterType) {
atomic.AddUint64(&globalCounters[cnt], 1)
SetMemCounters(&globalCounters)
}

128 changes: 64 additions & 64 deletions common_utils/shareMem.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
package common_utils

import (
"fmt"
"syscall"
"unsafe"
)

// Use share memory to dump GNMI internal counters,
// GNMI server and gnmi_dump should use memKey to access the share memory,
// memSize is 1024 bytes, so we can support 128 counters
// memMode is 0x380, this value is O_RDWR|IPC_CREAT,
// O_RDWR means: Owner can write and read the file, everyone else can't.
// IPC_CREAT means: Create a shared memory segment if a shared memory identifier does not exist for memKey.
var (
memKey = 7749
memSize = 1024
memMode = 0x380
)

func SetMemCounters(counters *[len(CountersName)]uint64) error {
shmid, _, err := syscall.Syscall(syscall.SYS_SHMGET, uintptr(memKey), uintptr(memSize), uintptr(memMode))
if int(shmid) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}

shmaddr, _, err := syscall.Syscall(syscall.SYS_SHMAT, shmid, 0, 0)
if int(shmaddr) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}
defer syscall.Syscall(syscall.SYS_SHMDT, shmaddr, 0, 0)

const size = unsafe.Sizeof(uint64(0))
addr := uintptr(unsafe.Pointer(shmaddr))

for i := 0; i < len(counters); i++ {
*(*uint64)(unsafe.Pointer(addr)) = counters[i]
addr += size
}
return nil
}

func GetMemCounters(counters *[len(CountersName)]uint64) error {
shmid, _, err := syscall.Syscall(syscall.SYS_SHMGET, uintptr(memKey), uintptr(memSize), uintptr(memMode))
if int(shmid) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}

shmaddr, _, err := syscall.Syscall(syscall.SYS_SHMAT, shmid, 0, 0)
if int(shmaddr) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}
defer syscall.Syscall(syscall.SYS_SHMDT, shmaddr, 0, 0)

const size = unsafe.Sizeof(uint64(0))
addr := uintptr(unsafe.Pointer(shmaddr))

for i := 0; i < len(counters); i++ {
counters[i] = *(*uint64)(unsafe.Pointer(addr))
addr += size
}
return nil
}
package common_utils

import (
"fmt"
"syscall"
"unsafe"
)

// Use share memory to dump GNMI internal counters,
// GNMI server and gnmi_dump should use memKey to access the share memory,
// memSize is 1024 bytes, so we can support 128 counters
// memMode is 0x380, this value is O_RDWR|IPC_CREAT,
// O_RDWR means: Owner can write and read the file, everyone else can't.
// IPC_CREAT means: Create a shared memory segment if a shared memory identifier does not exist for memKey.
var (
memKey = 7749
memSize = 1024
memMode = 0x380
)

func SetMemCounters(counters *[int(COUNTER_SIZE)]uint64) error {
shmid, _, err := syscall.Syscall(syscall.SYS_SHMGET, uintptr(memKey), uintptr(memSize), uintptr(memMode))
if int(shmid) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}

shmaddr, _, err := syscall.Syscall(syscall.SYS_SHMAT, shmid, 0, 0)
if int(shmaddr) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}
defer syscall.Syscall(syscall.SYS_SHMDT, shmaddr, 0, 0)

const size = unsafe.Sizeof(uint64(0))
addr := uintptr(unsafe.Pointer(shmaddr))

for i := 0; i < len(counters); i++ {
*(*uint64)(unsafe.Pointer(addr)) = counters[i]
addr += size
}
return nil
}

func GetMemCounters(counters *[int(COUNTER_SIZE)]uint64) error {
shmid, _, err := syscall.Syscall(syscall.SYS_SHMGET, uintptr(memKey), uintptr(memSize), uintptr(memMode))
if int(shmid) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}

shmaddr, _, err := syscall.Syscall(syscall.SYS_SHMAT, shmid, 0, 0)
if int(shmaddr) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}
defer syscall.Syscall(syscall.SYS_SHMDT, shmaddr, 0, 0)

const size = unsafe.Sizeof(uint64(0))
addr := uintptr(unsafe.Pointer(shmaddr))

for i := 0; i < len(counters); i++ {
counters[i] = *(*uint64)(unsafe.Pointer(addr))
addr += size
}
return nil
}

59 changes: 30 additions & 29 deletions gnmi_dump/gnmi_dump.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package main

import (
"flag"
"fmt"
"github.com/sonic-net/sonic-gnmi/common_utils"
)

const help = `
gnmi_dump is used to dump internal counters for debugging purpose,
including GNMI request counter, GNOI request counter and DBUS request counter.
`

func main() {
flag.Usage = func() {
fmt.Print(help)
}
flag.Parse()
var counters [len(common_utils.CountersName)]uint64
err := common_utils.GetMemCounters(&counters)
if err != nil {
fmt.Printf("Error: Fail to read counters, %v", err)
return
}
fmt.Printf("Dump GNMI counters\n")
for i := 0; i < len(common_utils.CountersName); i++ {
fmt.Printf("%v---%v\n", common_utils.CountersName[i], counters[i])
}
}
package main

import (
"flag"
"fmt"
"github.com/sonic-net/sonic-gnmi/common_utils"
)

const help = `
gnmi_dump is used to dump internal counters for debugging purpose,
including GNMI request counter, GNOI request counter and DBUS request counter.
`

func main() {
flag.Usage = func() {
fmt.Print(help)
}
flag.Parse()
var counters [common_utils.COUNTER_SIZE]uint64
err := common_utils.GetMemCounters(&counters)
if err != nil {
fmt.Printf("Error: Fail to read counters, %v", err)
return
}
fmt.Printf("Dump GNMI counters\n")
for i := 0; i < int(common_utils.COUNTER_SIZE); i++ {
cnt := common_utils.CounterType(i)
fmt.Printf("%v---%v\n", cnt.String(), counters[i])
}
}
Loading

0 comments on commit f019892

Please sign in to comment.