| Teng Qin | ef9d83f | 2019-02-28 15:29:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Facebook, Inc. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License") |
| 4 | */ |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <string> |
| Teng Qin | 873e939 | 2019-03-07 21:56:10 -0800 | [diff] [blame^] | 9 | #include <unordered_map> |
| Teng Qin | ef9d83f | 2019-02-28 15:29:55 -0800 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
| 12 | #include <linux/perf_event.h> |
| 13 | #include <sys/types.h> |
| 14 | |
| 15 | #include "BPF.h" |
| Teng Qin | 873e939 | 2019-03-07 21:56:10 -0800 | [diff] [blame^] | 16 | #include "PyPerfSampleProcessor.h" |
| Teng Qin | ef9d83f | 2019-02-28 15:29:55 -0800 | [diff] [blame] | 17 | #include "PyPerfType.h" |
| 18 | |
| 19 | namespace ebpf { |
| 20 | namespace pyperf { |
| 21 | |
| 22 | class PyPerfUtil { |
| 23 | public: |
| 24 | enum class PyPerfResult : int { |
| 25 | SUCCESS = 0, |
| 26 | INIT_FAIL, |
| 27 | PERF_BUF_OPEN_FAIL, |
| 28 | NO_INIT, |
| 29 | EVENT_ATTACH_FAIL, |
| 30 | EVENT_DETACH_FAIL |
| 31 | }; |
| 32 | |
| Teng Qin | ef9d83f | 2019-02-28 15:29:55 -0800 | [diff] [blame] | 33 | // init must be invoked exactly once before invoking profile |
| 34 | PyPerfResult init(); |
| 35 | |
| Teng Qin | 873e939 | 2019-03-07 21:56:10 -0800 | [diff] [blame^] | 36 | PyPerfResult profile(int64_t sampleRate, int64_t durationMs, |
| 37 | PyPerfSampleProcessor* processor); |
| 38 | |
| 39 | std::unordered_map<int32_t, std::string> getSymbolMapping(); |
| 40 | |
| 41 | uint32_t getTotalSamples() const { return totalSamples_; } |
| 42 | |
| 43 | uint32_t getLostSamples() const { return lostSamples_; } |
| Teng Qin | ef9d83f | 2019-02-28 15:29:55 -0800 | [diff] [blame] | 44 | |
| 45 | private: |
| Teng Qin | 873e939 | 2019-03-07 21:56:10 -0800 | [diff] [blame^] | 46 | uint32_t totalSamples_ = 0, lostSamples_ = 0; |
| Teng Qin | ef9d83f | 2019-02-28 15:29:55 -0800 | [diff] [blame] | 47 | |
| 48 | ebpf::BPF bpf_{0, nullptr, false, "", true}; |
| Teng Qin | 873e939 | 2019-03-07 21:56:10 -0800 | [diff] [blame^] | 49 | std::vector<PyPerfSample> samples_; |
| Teng Qin | ef9d83f | 2019-02-28 15:29:55 -0800 | [diff] [blame] | 50 | bool initCompleted_{false}; |
| 51 | |
| 52 | void handleSample(const void* data, int dataSize); |
| 53 | void handleLostSamples(int lostCnt); |
| 54 | friend void handleLostSamplesCallback(void*, uint64_t); |
| 55 | friend void handleSampleCallback(void*, void*, int); |
| 56 | |
| 57 | std::string getSymbolName(Symbol& sym) const; |
| 58 | |
| 59 | bool tryTargetPid(int pid, PidData& data); |
| 60 | }; |
| 61 | } // namespace pyperf |
| 62 | } // namespace ebpf |