ishm

package
v0.0.0-...-8301186 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2021 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IpcNone                        = 0
	IpcCreate    SharedMemoryFlags = C.IPC_CREAT
	IpcExclusive                   = C.IPC_EXCL
	HugePages                      = C.SHM_HUGETLB
	NoReserve                      = C.SHM_NORESERVE
)

Variables

View Source
var Counter int64 = 0
View Source
var DEBUG bool

DEBUG decide print some debug info or not

View Source
var MCL uint = 102400
View Source
var MTL uint = 64

Functions

func Bytes2String

func Bytes2String(b []byte) string

func CmdAndChangeDirToShow

func CmdAndChangeDirToShow(commandName string, params []string, key int64) (error, bool)

func Decode

func Decode(data []byte, v interface{}, codec ...string) error

Decode ...

func DestroySegment

func DestroySegment(id int64) error

DestroySegment destroy a shared memory segment by its ID

func Encode

func Encode(v interface{}, codec ...string) ([]byte, error)

Encode ...

func HasKeyofSHM

func HasKeyofSHM(key int64) bool

func Readtlv

func Readtlv(k int64)

func RegisterConverter

func RegisterConverter(codec string, c Converter) error

RegisterConverter ...

func SHMInfoStructToBytes

func SHMInfoStructToBytes(s *SHMInfo) []byte

func SizeStruct

func SizeStruct(data interface{}) int

func StartSubscribe

func StartSubscribe(key int64, callBack TLVCallBack) bool

func StringToByteArr

func StringToByteArr(s string, arr []byte)

func TagTLVStructToBytes

func TagTLVStructToBytes(s *TagTLV) []byte

func UpdateCtx

func UpdateCtx(shmparam CreateSHMParam, updatectx UpdateContent) (index int, err error)

Types

type ConsumerHandler

type ConsumerHandler interface {
	OnMessage(topic string, eventType string, content []byte)
}

type ContentData

type ContentData struct {
	Tag   int64
	Topic string
	Value string
}

type Converter

type Converter interface {
	Marshal(interface{}) ([]byte, error)
	Unmarshal([]byte, interface{}) error
}

Converter is the interface to convert obj to []byte

type CreateSHMParam

type CreateSHMParam struct {
	Key    int64
	Size   int64
	Create bool
}

type DefaultConverter

type DefaultConverter struct{}

DefaultConverter is the default Converter using reflect

func (DefaultConverter) Marshal

func (df DefaultConverter) Marshal(v interface{}) ([]byte, error)

Marshal ...

func (DefaultConverter) Unmarshal

func (df DefaultConverter) Unmarshal(data []byte, v interface{}) error

Unmarshal ...

type HeadData

type HeadData struct {
	ReadOffSet  uint64
	WriteOffSet uint64
}

func GetHeadData

func GetHeadData(segment *Segment) (*HeadData, error)

type MemBlock

type MemBlock struct {
	// contains filtered or unexported fields
}

MemBlock ...

func NewBlock

func NewBlock(start, end int64, once ...bool) *MemBlock

NewBlock ...

func (*MemBlock) After

func (mb *MemBlock) After(otherMb *MemBlock) bool

After ...

func (*MemBlock) Equal

func (mb *MemBlock) Equal(otherMb *MemBlock) bool

Equal ...

func (*MemBlock) Size

func (mb *MemBlock) Size() int64

Size ...

type MemChain

type MemChain struct {
	// contains filtered or unexported fields
}

MemChain ...

func NewMemChain

func NewMemChain() *MemChain

NewMemChain ...

func (*MemChain) Delete

func (mC *MemChain) Delete(mB *MemBlock) error

Delete ...

func (*MemChain) Insert

func (mC *MemChain) Insert(mB *MemBlock) error

Insert ...

func (*MemChain) MergeBlocks

func (mC *MemChain) MergeBlocks()

MergeBlocks ...

func (*MemChain) MergeOnTime

func (mC *MemChain) MergeOnTime(gap int)

MergeOnTime ...

func (*MemChain) PrintChain

func (mC *MemChain) PrintChain()

PrintChain ...

func (*MemChain) SearchBlock

func (mC *MemChain) SearchBlock(needSize int64) (*MemBlock, error)

SearchBlock ...

type SHMInfo

type SHMInfo struct {
	MaxTopicLen   uint64
	MaxContentLen uint64
	MaxSHMSize    uint64
	Count         uint64
	Key           [200]int32
}

todo this will read from key of 999999 SHM typedef struct SHMInfo{ unsigned long long max_topic_len; unsigned long long max_content_len; unsigned long long max_shm_size; unsigned long long count; key_t key[200]; }SHMI;

func BytesToSHMInfoStruct

func BytesToSHMInfoStruct(b []byte) *SHMInfo

func GetShareMemoryInfo

func GetShareMemoryInfo(defaultKey int64, create bool) (*SHMInfo, error)

todo run it use root

type Segment

type Segment struct {
	Key  int64
	Id   int64
	Size int64
	// contains filtered or unexported fields
}

Segment is a native representation of a SysV shared memory segment

func Create

func Create(size int64) (*Segment, error)

Create a new shared memory segment with the given size (in bytes). The system will automatically round the size up to the nearest memory page boundary (typically 4KB).

func CreateWithKey

func CreateWithKey(key, size int64) (*Segment, error)

func Open

func Open(id int64) (*Segment, error)

Open an existing shared memory segment located at the given ID. This ID is returned in the struct that is populated by Create(), or by the shmget() system call.

func OpenSegment

func OpenSegment(size int64, flags SharedMemoryFlags, perms os.FileMode) (*Segment, error)

OpenSegment creates a shared memory segment of a given size, and also allows for the specification of creation flags supported by the shmget() call, as well as specifying permissions.

func OpenSegmentWithKey

func OpenSegmentWithKey(key, size int64, flags SharedMemoryFlags, perms os.FileMode) (*Segment, error)

func (*Segment) Attach

func (s *Segment) Attach() (unsafe.Pointer, error)

Attaches the segment to the current processes resident memory. The pointer that is returned is the actual memory address of the shared memory segment for use with third party libraries that can directly read from memory.

func (*Segment) Destroy

func (s *Segment) Destroy() error

Destroys the current shared memory segment.

func (*Segment) Detach

func (s *Segment) Detach() error

Detaches the segment from the current processes memory space.

func (*Segment) GetBytes

func (s *Segment) GetBytes(key interface{}) ([]byte, error)

func (*Segment) IsAttached

func (s *Segment) IsAttached() bool

func (*Segment) Position

func (s *Segment) Position() int64

Returns the current position of the Read/Write pointer.

func (*Segment) Read

func (s *Segment) Read(p []byte) (n int, err error)

func (*Segment) ReadChunk

func (s *Segment) ReadChunk(length int64, start int64) ([]byte, error)

ReadChunk reads some or all of the shared memory segment and return a byte slice.

func (*Segment) ReadChunkWithoutAttach

func (s *Segment) ReadChunkWithoutAttach(length int64, start int64) ([]byte, error)

func (*Segment) ReadObj

func (s *Segment) ReadObj(key *interface{}) (n int, err error)

func (*Segment) Reset

func (s *Segment) Reset()

Resets the internal offset counter for this segment, allowing subsequent calls to Read() or Write() to start from the beginning.

func (*Segment) Seek

func (s *Segment) Seek(offset int64, whence int) (int64, error)

Implements the io.Seeker interface for shared memory. Subsequent calls to Read() or Write() will start from this position.

func (*Segment) Write

func (s *Segment) Write(p []byte) (n int, err error)

Implements the io.Writer interface for shared memory

func (*Segment) WriteObj

func (s *Segment) WriteObj(key interface{}) (n int, err error)

type SharedMemoryFlags

type SharedMemoryFlags int

type ShmCmdEnum

type ShmCmdEnum int32
const (
	ShmCmdOK       ShmCmdEnum = 0
	ShmCmdUnAttach ShmCmdEnum = 1
	ShmCmdStop     ShmCmdEnum = 2
)

type ShmCommand

type ShmCommand struct {
	// contains filtered or unexported fields
}

type ShmConsumerStatus

type ShmConsumerStatus int32
const (
	ShmConsumerOk         ShmConsumerStatus = 0
	ShmConsumerReadErr    ShmConsumerStatus = 1
	ShmConsumerLenErr     ShmConsumerStatus = 2
	ShmConsumerInitErr    ShmConsumerStatus = 3
	ShmConsumerNoData     ShmConsumerStatus = 4
	ShmConsumerUnAttached ShmConsumerStatus = 5
)

type ShmDataBlockConsumer

type ShmDataBlockConsumer struct {
	PreTag        uint64
	CurTag        uint64
	TopicLen      uint64
	MaxContentLen uint64
	MaxShmSize    uint64
	CurOffset     uint64
	PreOffset     uint64
	SegLen        uint64
	ShmKey        int64
	IsRunning     bool
	// contains filtered or unexported fields
}

func (*ShmDataBlockConsumer) AutoNext

func (consumer *ShmDataBlockConsumer) AutoNext() (*TagTLV, ShmConsumerStatus)

func (*ShmDataBlockConsumer) Close

func (consumer *ShmDataBlockConsumer) Close()

func (*ShmDataBlockConsumer) Detach

func (consumer *ShmDataBlockConsumer) Detach() error

func (*ShmDataBlockConsumer) GetCmdChannel

func (consumer *ShmDataBlockConsumer) GetCmdChannel() chan ShmCommand

func (*ShmDataBlockConsumer) Init

func (consumer *ShmDataBlockConsumer) Init(key int64, maxSHMSize uint64, maxContentLen uint64, handler ConsumerHandler) (bool, error)

func (*ShmDataBlockConsumer) Next

func (consumer *ShmDataBlockConsumer) Next() (*TagTLV, ShmConsumerStatus)

func (*ShmDataBlockConsumer) Reset

func (consumer *ShmDataBlockConsumer) Reset()

func (*ShmDataBlockConsumer) Start

func (consumer *ShmDataBlockConsumer) Start() bool

type ShmManager

type ShmManager struct {
	// contains filtered or unexported fields
}

ShmManager is a tool to manager shm

func NewShmManager

func NewShmManager(MemSize int64) *ShmManager

NewShmManager ...

func (*ShmManager) DeleteBlock

func (sm *ShmManager) DeleteBlock(blockName string) error

DeleteBlock ...

func (*ShmManager) Init

func (sm *ShmManager) Init() (err error)

Init ...

func (*ShmManager) InitWithKey

func (sm *ShmManager) InitWithKey(key, usedBlockSize int64) (err error)

func (*ShmManager) ReadBlock

func (sm *ShmManager) ReadBlock(blockName string) ([]byte, error)

ReadBlock ...

func (*ShmManager) Show

func (sm *ShmManager) Show()

Show ...

func (*ShmManager) WriteBlock

func (sm *ShmManager) WriteBlock(blockName string, data []byte) (int, error)

WriteBlock ...

type ShmSubscriber

type ShmSubscriber struct {
	// contains filtered or unexported fields
}

func (*ShmSubscriber) Close

func (subscriber *ShmSubscriber) Close()

func (*ShmSubscriber) Init

func (subscriber *ShmSubscriber) Init(confShmId int64, handler ConsumerHandler)

func (*ShmSubscriber) Start

func (subscriber *ShmSubscriber) Start()

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack ...

func NewStack

func NewStack(maxSize int) *Stack

NewStack ...

func (*Stack) Clear

func (s *Stack) Clear()

Clear ...

func (Stack) IsEmpty

func (s Stack) IsEmpty() bool

IsEmpty ...

func (Stack) Len

func (s Stack) Len() int

Len ...

func (*Stack) Pop

func (s *Stack) Pop() interface{}

Pop ...

func (*Stack) Push

func (s *Stack) Push(v interface{}) error

Push ...

type TLVCallBack

type TLVCallBack func(*TagTLV)

type TagTL

type TagTL struct {
	Tag uint64
	Len uint64
}

type TagTLV

type TagTLV struct {
	Tag          int64
	Len          uint64
	TopicLen     uint16
	EventTypeLen uint16
	Topic        [30]byte
	EventType    [30]byte
	Value        [40960]byte
}

func BytesToTagTLVStruct

func BytesToTagTLVStruct(b []byte) *TagTLV

func ReadTLVData

func ReadTLVData(segment *Segment, offset int64) (*TagTLV, int64, error)

type UpdateContent

type UpdateContent struct {
	EventType string
	Topic     string
	Content   string
}

func GetCtx

func GetCtx(shmparam CreateSHMParam) (*UpdateContent, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL