修改handler写coredump大文件分块写入降低内存使用
This commit is contained in:
17
core-handler/core-handler.go
Normal file
17
core-handler/core-handler.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
//#cgo CXXFLAGS: -std=c++11
|
||||||
|
// #cgo CFLAGS: -I /root/breakpad/src/tools/linux/core_handler/coredump_handler_wrapper
|
||||||
|
// #cgo LDFLAGS: /root/breakpad/src/tools/linux/core_handler/coredump_handler_wrapper/coredump_handler_wrapper.so
|
||||||
|
//#include "/root/breakpad/src/tools/linux/core_handler/coredump_handler_wrapper/coredump_handler_wrapper.h"
|
||||||
|
import "C"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func HandleCrash(pid int, procfsDir string, mdFilename string) bool {
|
||||||
|
fmt.Println("start")
|
||||||
|
return bool(C.HandleCrash(C.pid_t(pid), C.CString(procfsDir), C.CString(mdFilename)))
|
||||||
|
}
|
||||||
|
func main() {
|
||||||
|
// 调用 coredump_handler_wrapper 动态链接库中的 HandleCrash 函数
|
||||||
|
HandleCrash(123, "/proc/123", "/var/tmp")
|
||||||
|
}
|
||||||
@@ -162,22 +162,50 @@ func writeCoreDumpToFile(config types.Coredump_config) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
buf := make([]byte, 1024*1024)
|
||||||
|
data := make([]byte, 1024*1024)
|
||||||
// create a pipe to write file
|
// create a pipe to write file
|
||||||
reader, writer := io.Pipe()
|
reader, writer := io.Pipe()
|
||||||
go func() {
|
go func() {
|
||||||
defer writer.Close()
|
defer writer.Close()
|
||||||
io.Copy(writer, os.Stdin)
|
// io.Copy(writer, os.Stdin)
|
||||||
|
for {
|
||||||
|
// 从标准输入中读取数据块
|
||||||
|
n, err := os.Stdin.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
journal.Print(journal.PriErr, err.Error())
|
||||||
|
writer.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 将读取的数据块写入到管道中
|
||||||
|
_, err = writer.Write(buf[:n])
|
||||||
|
if err != nil {
|
||||||
|
journal.Print(journal.PriErr, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
io.Copy(file, reader)
|
for {
|
||||||
|
// 从管道中读取数据,直到管道被关闭
|
||||||
|
_, err = io.CopyBuffer(file, reader, data)
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
_, err = file.Write(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func writeMiniDumpToFile(config types.Coredump_config) error {
|
func writeMiniDumpToFile(config types.Coredump_config) error {
|
||||||
filename := fmt.Sprintf("%s/%s_%s_%d.minidump", config.Storage, config.Initial_ns_pid, config.Process_ns_pid, config.Timestamp)
|
filename := fmt.Sprintf("%s/%s_%s_%d.minidump", config.Storage, config.Initial_ns_pid, config.Process_ns_pid, config.Timestamp)
|
||||||
cmd := exec.Command("/opt/tsg/coredump/bin/core_handler", config.Initial_ns_pid, filename)
|
cmd := exec.Command("/opt/tsg/coredump/bin/core_handler", config.Initial_ns_pid, filename)
|
||||||
cmd.Stdin = os.Stdin
|
cmd.Stdin = os.Stdin
|
||||||
// 获取 Python 脚本的输出
|
|
||||||
output, err := cmd.Output()
|
output, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -203,12 +231,31 @@ func compress(config types.Coredump_config) error {
|
|||||||
Name: filename,
|
Name: filename,
|
||||||
Method: zip.Deflate,
|
Method: zip.Deflate,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the header to the zip file.
|
// Write the header to the zip file.
|
||||||
writer, err := zipwriter.CreateHeader(header)
|
writer, err := zipwriter.CreateHeader(header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
io.Copy(writer, os.Stdin)
|
|
||||||
|
// Set the block size to 1 megabyte.
|
||||||
|
const blockSize = 1024 * 1024
|
||||||
|
|
||||||
|
// Read and write the input in blocks.
|
||||||
|
buf := make([]byte, blockSize)
|
||||||
|
for {
|
||||||
|
n, err := os.Stdin.Read(buf)
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
_, err = writer.Write(buf[:n])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user