-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the delay queue for exited thread
Signed-off-by: yaofighting <siyao@zju.edu.cn>
- Loading branch information
1 parent
9d1ad53
commit 999f552
Showing
3 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
collector/pkg/component/analyzer/cpuanalyzer/delete_tid.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cpuanalyzer | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type tidDeleteQueue struct { | ||
queueMutex sync.Mutex | ||
queue []deleteTid | ||
} | ||
|
||
var tidExpiredQueue *tidDeleteQueue | ||
|
||
type deleteTid struct { | ||
pid uint32 | ||
tid uint32 | ||
exitTime time.Time | ||
} | ||
|
||
func (dq *tidDeleteQueue) GetFront() *deleteTid { | ||
if len(dq.queue) > 0 { | ||
return &dq.queue[0] | ||
} | ||
return nil | ||
} | ||
|
||
func (dq *tidDeleteQueue) Push(elem deleteTid) { | ||
dq.queue = append(dq.queue, elem) | ||
} | ||
|
||
func (dq *tidDeleteQueue) Pop() { | ||
if len(dq.queue) > 0 { | ||
dq.queue = dq.queue[1:len(dq.queue)] | ||
} | ||
} | ||
|
||
func InitTidDeleteQueue() { | ||
tidExpiredQueue = &tidDeleteQueue{queue: make([]deleteTid, 0)} | ||
} | ||
|
||
//Add procexit tid | ||
func (ca *CpuAnalyzer) AddTidToDeleteCache(curTime time.Time, pid uint32, tid uint32) { | ||
defer tidExpiredQueue.queueMutex.Unlock() | ||
cacheElem := deleteTid{pid: pid, tid: tid, exitTime: curTime} | ||
tidExpiredQueue.queueMutex.Lock() | ||
tidExpiredQueue.Push(cacheElem) | ||
} | ||
|
||
func (ca *CpuAnalyzer) TidDelete(interval time.Duration, expiredDuration time.Duration) { | ||
for { | ||
select { | ||
case <-time.After(interval): | ||
now := time.Now() | ||
tidExpiredQueue.queueMutex.Lock() | ||
for { | ||
elem := tidExpiredQueue.GetFront() | ||
if elem == nil { | ||
break | ||
} | ||
if elem.exitTime.Add(expiredDuration).Before(now) { | ||
tidEventsMap := ca.cpuPidEvents[elem.pid] | ||
if tidEventsMap == nil { | ||
continue | ||
} | ||
ca.telemetry.Logger.Debugf("Delete expired thread... pid=%d, tid=%d", elem.pid, elem.tid) | ||
delete(tidEventsMap, elem.tid) | ||
tidExpiredQueue.Pop() | ||
} else { | ||
break | ||
} | ||
} | ||
tidExpiredQueue.queueMutex.Unlock() | ||
} | ||
} | ||
} |