123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #ifndef SIMPLE_PERF_CALLCHAIN_JOINER_H_
- #define SIMPLE_PERF_CALLCHAIN_JOINER_H_
- #include <inttypes.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <unordered_set>
- #include <vector>
- namespace simpleperf {
- namespace call_chain_joiner_impl {
- struct CacheNode {
- uint64_t ip;
- uint64_t sp;
- uint32_t tid;
-
- uint32_t is_leaf : 1;
-
- uint32_t parent_index : 31;
-
-
- union {
- uint32_t children_count;
- struct {
- uint32_t leaf_link_prev;
- uint32_t leaf_link_next;
- };
- };
- };
- static_assert(sizeof(CacheNode) == 32, "");
- struct LRUCacheStat {
- size_t cache_size = 0u;
- size_t matched_node_count_to_extend_callchain = 0u;
- size_t max_node_count = 0u;
- size_t used_node_count = 0u;
- size_t recycled_node_count = 0u;
- };
- class LRUCache {
- public:
-
-
-
- LRUCache(size_t cache_size = 8 * 1024 * 1024, size_t matched_node_count_to_extend_callchain = 1);
- ~LRUCache();
-
- void AddCallChain(pid_t tid, std::vector<uint64_t>& ips, std::vector<uint64_t>& sps);
- const LRUCacheStat& Stat() {
- return cache_stat_;
- }
- CacheNode* FindNode(uint32_t tid, uint64_t ip, uint64_t sp) {
- CacheNode key;
- key.tid = tid;
- key.ip = ip;
- key.sp = sp;
- auto it = node_set_.find(&key);
- return it != node_set_.end() ? *it : nullptr;
- }
- private:
- static bool CacheNodeEqual(const CacheNode* n1, const CacheNode* n2);
- static size_t CacheNodeHash(const CacheNode* n);
- typedef std::unordered_set<CacheNode*, decltype(&CacheNodeHash), decltype(&CacheNodeEqual)>
- set_type;
- CacheNode* GetParent(CacheNode* node) {
- return node->parent_index == 0u ? nullptr : nodes_ + node->parent_index;
- }
- int GetNodeIndex(CacheNode* node) {
- return node - nodes_;
- }
- void RemoveNodeFromLRUList(CacheNode* node) {
- CacheNode* prev = &nodes_[node->leaf_link_prev];
- CacheNode* next = &nodes_[node->leaf_link_next];
- prev->leaf_link_next = node->leaf_link_next;
- next->leaf_link_prev = node->leaf_link_prev;
- }
- void AppendNodeToLRUList(CacheNode* node) {
- CacheNode* next = &nodes_[0];
- CacheNode* prev = &nodes_[next->leaf_link_prev];
- node->leaf_link_next = 0;
- node->leaf_link_prev = next->leaf_link_prev;
- next->leaf_link_prev = prev->leaf_link_next = GetNodeIndex(node);
- }
- void DecreaseChildCountOfNode(CacheNode* node) {
- if (--node->children_count == 0u) {
- node->is_leaf = true;
- AppendNodeToLRUList(node);
- }
- }
- CacheNode* GetNode(uint32_t tid, uint64_t ip, uint64_t sp);
- CacheNode* AllocNode();
- void LinkParent(CacheNode* child, CacheNode* new_parent);
- void UnlinkParent(CacheNode* child);
- CacheNode* nodes_;
- set_type node_set_;
- LRUCacheStat cache_stat_;
- };
- }
- class CallChainJoiner {
- public:
-
- CallChainJoiner(size_t cache_size, size_t matched_node_count_to_extend_callchain,
- bool keep_original_callchains);
- ~CallChainJoiner();
- enum ChainType {
- ORIGINAL_OFFLINE,
- ORIGINAL_REMOTE,
- JOINED_OFFLINE,
- JOINED_REMOTE,
- };
- bool AddCallChain(pid_t pid, pid_t tid, ChainType type, const std::vector<uint64_t>& ips,
- const std::vector<uint64_t>& sps);
- bool JoinCallChains();
- bool GetNextCallChain(pid_t& pid, pid_t& tid, ChainType& type, std::vector<uint64_t>& ips,
- std::vector<uint64_t>& sps);
- struct Stat {
- size_t chain_count = 0u;
- size_t before_join_node_count = 0u;
- size_t after_join_node_count = 0u;
- size_t after_join_max_chain_length = 0u;
- };
- void DumpStat();
- const Stat& GetStat() {
- return stat_;
- }
- const call_chain_joiner_impl::LRUCacheStat& GetCacheStat() {
- return cache_stat_;
- }
- private:
- bool keep_original_callchains_;
- FILE* original_chains_fp_;
- FILE* joined_chains_fp_;
- size_t next_chain_index_;
- call_chain_joiner_impl::LRUCacheStat cache_stat_;
- Stat stat_;
- };
- }
- #endif
|