From 628e170473fdf4cf6f07b3f0686b59724046d9ff Mon Sep 17 00:00:00 2001 From: "qcqcqc@wsl" <1220204124@zust.edu.cn> Date: Thu, 24 Apr 2025 18:18:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E5=8F=82=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E5=B0=BD=E5=8F=AF=E8=83=BD=E5=90=83=E6=BB=A1=E5=85=B1?= =?UTF-8?q?=E4=BA=AB=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-loader/internal/models/config.go | 30 +++++++++++++++++++++++++ shared/pkg/constants/constants.go | 12 +++++----- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/config-loader/internal/models/config.go b/config-loader/internal/models/config.go index 4a61cfa..2db177d 100644 --- a/config-loader/internal/models/config.go +++ b/config-loader/internal/models/config.go @@ -71,3 +71,33 @@ func (r *Rule) ToJSONRule() JSONRule { Args: args, } } + +// func init() { +// // 计算一个Rule结构体的大小 +// ruleSize := int(unsafe.Sizeof(Rule{})) + +// // 计算ConfigData结构体的总大小 +// configSize := int(unsafe.Sizeof(ConfigData{})) + +// // 转换为MB的函数 +// toMB := func(bytes int) float64 { +// return float64(bytes) / 1024 / 1024 +// } + +// // 验证共享内存大小是否足够 +// if configSize > constants.ShmSize { +// panic(fmt.Sprintf("Shared memory size is too small to hold ConfigData structure. "+ +// "Required size: %d bytes (%.2f MB), "+ +// "Current size: %d bytes (%.2f MB)", +// configSize, toMB(configSize), +// constants.ShmSize, toMB(constants.ShmSize))) +// } + +// // 打印详细信息便于调试 +// log.Printf("Memory allocation details:") +// log.Printf("- Single Rule size: %d bytes (%.2f MB)", ruleSize, toMB(ruleSize)) +// log.Printf("- Total ConfigData size: %d bytes (%.2f MB)", configSize, toMB(configSize)) +// log.Printf("- Shared memory size: %d bytes (%.2f MB)", constants.ShmSize, toMB(constants.ShmSize)) +// log.Printf("- Available space: %d bytes (%.2f MB)", +// constants.ShmSize-configSize, toMB(constants.ShmSize-configSize)) +// } diff --git a/shared/pkg/constants/constants.go b/shared/pkg/constants/constants.go index 4913a55..cddaa05 100644 --- a/shared/pkg/constants/constants.go +++ b/shared/pkg/constants/constants.go @@ -1,12 +1,12 @@ package constants const ( - MaxRules = 256 // 最大规则数量 - MaxArgs = 64 // 每条规则的最大参数数量 - MaxRuleCmdLength = 256 // 规则命令的最大长度 - MaxRuleTypeLength = 32 // 规则类型的最大长度 - MaxRuleMsgLength = 256 // 规则消息的最大长度 - MaxArgLength = 128 // 单个参数的最大长度 + MaxRules = 512 + 64 + 32 // 最大规则数量(尽可能把空间占用完) + MaxArgs = 128 // 每条规则的最大参数数量 + MaxRuleCmdLength = 256 // 规则命令的最大长度(可执行文件路径受到ext4限制,为255) + MaxRuleTypeLength = 32 // 规则类型的最大长度(warn,error,info) + MaxRuleMsgLength = 4096 // 规则消息的最大长度(可以很复杂的内容,长一点) + MaxArgLength = 256 + 128 // 单个参数的最大长度(最大可能就是255的文件路径加上前缀) ShmKey = 0x78945 // 共享内存的键值 ShmSize = 32 * 1024 * 1024 // 共享内存的大小(字节)