MCPcopy Create free account
hub / github.com/SeeFlowerX/stackplz / perfEventReader

Method perfEventReader

user/module/imodule.go:223–278  ·  view source on GitHub ↗
(errChan chan error, em *ebpf.Map)

Source from the content-addressed store, hash-verified

221}
222
223func (this *Module) perfEventReader(errChan chan error, em *ebpf.Map) {
224 // 这里对原ebpf包代码做了修改 以此控制是否让内核发生栈空间数据和寄存器数据
225 // 用于进行堆栈回溯 以后可以细分栈数据与寄存器数据
226 // 每个 模块都是 Clone 得到的 map 虽然名字相同 但是 fd不同 所以可以正常区分
227
228 eopt := this.getExtraOptions(em)
229
230 var rd *perf.Reader
231 var err error
232
233 rd, err = perf.NewReaderWithOptions(em, this.getPerCPUBuffer(), perf.ReaderOptions{}, eopt)
234 if err != nil {
235 errChan <- fmt.Errorf("creating %s reader dns: %s", em.String(), err)
236 return
237 }
238 // 可能存在多种类型的reader 添加到reader列表 异常时便于一起安全关闭
239 this.reader = append(this.reader, rd)
240 go func() {
241 for {
242 // 先判断ctx正不正常
243 select {
244 case _ = <-this.ctx.Done():
245 this.logger.Printf("%s\tperfEventReader received close signal from context.Done().", this.child.Name())
246 return
247 default:
248 }
249
250 record, err := rd.ReadWithExtraOptions(&eopt)
251
252 if err != nil {
253 if errors.Is(err, perf.ErrClosed) {
254 return
255 }
256 errChan <- fmt.Errorf("%s\treading from perf event reader: %s", this.child.Name(), err)
257 return
258 }
259
260 if record.LostSamples != 0 {
261 this.TotalLost += record.LostSamples
262 this.logger.Printf("%s\tperf event ring buffer full, dropped %d samples, record_type:%d", this.child.Name(), record.LostSamples, record.RecordType)
263 continue
264 }
265
266 // 只做简单的准备 数据解析不要在这个部分做
267 var e event.IEventStruct
268 e, err = this.child.PrePare(em, record)
269 if err != nil {
270 this.logger.Printf("%s\tthis.child.decode error:%v", this.child.Name(), err)
271 continue
272 }
273 // 准备完成将数据交给 processor 处理
274 // 从而加快读取环形缓冲区的数据 减缓数据丢失的概率
275 this.processor.Write(e)
276 }
277 }()
278}
279
280func (this *Module) PrePare(em *ebpf.Map, rec perf.Record) (event event.IEventStruct, err error) {

Callers 1

readEventsMethod · 0.95

Calls 6

getExtraOptionsMethod · 0.95
getPerCPUBufferMethod · 0.95
StringMethod · 0.65
NameMethod · 0.65
PrePareMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected