Browse Source

Fixed #TASK_QT-9810 入口调用频率过高,语言判断增加缓存

Carl 1 year ago
parent
commit
32a375e69b
4 changed files with 70 additions and 21 deletions
  1. 21 7
      containers/container.go
  2. 33 0
      containers/container_apm.go
  3. 2 0
      containers/process.go
  4. 14 14
      containers/util.go

+ 21 - 7
containers/container.go

@@ -1090,14 +1090,28 @@ func (c *Container) revalidateListens(now time.Time, actualListens map[netaddr.I
 	}
 }
 
-func (c *Container) attachUprobes(tracer *ebpftracer.Tracer, pid uint32){
-	codeType := GetExeType(pid)
-	switch codeType{
-	case "java":
-		c.attachJVMUprobes(tracer,pid)
-	case "go":
-		c.attachTlsUprobes(tracer,pid)
+func (c *Container) attachUprobes(tracer *ebpftracer.Tracer, pid uint32) {
+	codeType := c.GetCodeTypeFromCache(pid)
+	if codeType.IsUnknownCode() {
+		return
+	}
+	switch codeType {
+	case CodeTypeJava:
+		c.attachJVMUprobes(tracer, pid)
+	case CodeTypeGo:
+		c.attachTlsUprobes(tracer, pid)
+	}
+}
+
+func (c *Container) GetCodeTypeFromCache(pid uint32) CodeType {
+	p := c.processes[pid]
+	if p == nil {
+		return CodeTypeUnknown
+	}
+	if p.codeType.IsWaitCheck() {
+		p.codeType = GetExeType(pid)
 	}
+	return p.codeType
 }
 
 func (c *Container) attachTlsUprobes(tracer *ebpftracer.Tracer, pid uint32) {

+ 33 - 0
containers/container_apm.go

@@ -17,6 +17,39 @@ import (
 	"inet.af/netaddr"
 )
 
+type CodeType int16
+
+const (
+	CodeTypeUnknown   CodeType = -1
+	CodeTypeWaitCheck CodeType = 0
+	CodeTypeGo        CodeType = 1006
+	CodeTypeJava      CodeType = 1002
+)
+
+func (p CodeType) String() string {
+	switch p {
+	case CodeTypeGo:
+		return "GO"
+	case CodeTypeJava:
+		return "JAVA"
+	}
+	return "UNKNOWN:Language"
+}
+
+func (p CodeType) IsWaitCheck() bool {
+	if p == CodeTypeWaitCheck {
+		return true
+	}
+	return false
+}
+
+func (p CodeType) IsUnknownCode() bool {
+	if p == CodeTypeUnknown {
+		return true
+	}
+	return false
+}
+
 func (c *Container) getTrace(traceId uint64) (*tracing.Trace, bool) {
 	trace, ok := c.traceMap[traceId]
 	return trace, ok

+ 2 - 0
containers/process.go

@@ -26,6 +26,8 @@ type Process struct {
 	goTlsUprobesChecked   bool
 	openSslUprobesChecked bool
 	jvmUprobesChecked     bool
+
+	codeType CodeType
 }
 
 func NewProcess(pid uint32, stats *taskstats.Stats) *Process {

+ 14 - 14
containers/util.go

@@ -1,19 +1,19 @@
 package containers
 
 import (
+	"debug/elf"
 	"fmt"
 	"io/ioutil"
 	"log"
-	"regexp"
-	"debug/elf"
 	"os"
-    "os/exec"
-    "runtime"
+	"os/exec"
+	"regexp"
+	"runtime"
 )
 
 var libjvmRegex = regexp.MustCompile(`.*/libjvm\.so`)
 
-func GetExeType(pid uint32) string{
+func GetExeType(pid uint32) CodeType {
 	mapsFilePath := fmt.Sprintf("/proc/%d/maps", pid)
 
 	data, err := ioutil.ReadFile(mapsFilePath)
@@ -25,15 +25,15 @@ func GetExeType(pid uint32) string{
 
 	if libjvmRegex.MatchString(content) {
 		fmt.Println("is java process")
-		return string("java")
-	}else if isGoProcess(pid){
-		return string("go")
+		return CodeTypeJava
+	} else if isGoProcess(pid) {
+		return CodeTypeGo
 		fmt.Println("is go process")
 	}
-	return ""
+	return CodeTypeUnknown
 }
 
-func isGoProcess(pid uint32) bool{
+func isGoProcess(pid uint32) bool {
 	path, err := getProcessPath(pid)
 	if err != nil {
 		fmt.Printf("无法获取进程路径:%s\n", err)
@@ -47,10 +47,10 @@ func isGoProcess(pid uint32) bool{
 	defer ef.Close()
 
 	gopclntabSection := ef.Section(".gopclntab")
-    if gopclntabSection != nil {
-            fmt.Println("is a go process")
-            return true
-    } 
+	if gopclntabSection != nil {
+		fmt.Println("is a go process")
+		return true
+	}
 	return false
 }