File: PyPerfUtil.h

package info (click to toggle)
bpfcc 0.18.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,368 kB
  • sloc: ansic: 132,727; python: 36,226; cpp: 26,973; sh: 710; yacc: 525; makefile: 141; lex: 94
file content (62 lines) | stat: -rw-r--r-- 1,449 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * Copyright (c) Facebook, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License")
 */

#pragma once

#include <string>
#include <unordered_map>
#include <vector>

#include <linux/perf_event.h>
#include <sys/types.h>

#include "BPF.h"
#include "PyPerfSampleProcessor.h"
#include "PyPerfType.h"

namespace ebpf {
namespace pyperf {

class PyPerfUtil {
 public:
  enum class PyPerfResult : int {
    SUCCESS = 0,
    INIT_FAIL,
    PERF_BUF_OPEN_FAIL,
    NO_INIT,
    EVENT_ATTACH_FAIL,
    EVENT_DETACH_FAIL
  };

  // init must be invoked exactly once before invoking profile
  PyPerfResult init();

  PyPerfResult profile(int64_t sampleRate, int64_t durationMs,
                       PyPerfSampleProcessor* processor);

  std::unordered_map<int32_t, std::string> getSymbolMapping();

  uint32_t getTotalSamples() const { return totalSamples_; }

  uint32_t getLostSamples() const { return lostSamples_; }

 private:
  uint32_t totalSamples_ = 0, lostSamples_ = 0;

  ebpf::BPF bpf_{0, nullptr, false, "", true};
  std::vector<PyPerfSample> samples_;
  bool initCompleted_{false};

  void handleSample(const void* data, int dataSize);
  void handleLostSamples(int lostCnt);
  friend void handleLostSamplesCallback(void*, uint64_t);
  friend void handleSampleCallback(void*, void*, int);

  std::string getSymbolName(Symbol& sym) const;

  bool tryTargetPid(int pid, PidData& data);
};
}  // namespace pyperf
}  // namespace ebpf