• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RELIABILITY_SAMPLE_STACK_PRINTER_H
17 #define RELIABILITY_SAMPLE_STACK_PRINTER_H
18 
19 #include <map>
20 #include <vector>
21 
22 #include "unwinder.h"
23 #include "dfx_frame.h"
24 #include "thread_sampler.h"
25 #include "unique_stack_table.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 struct SampleStackItem {
30     uintptr_t pc;
31     int32_t count;
32     uint64_t level;
33     std::shared_ptr<DfxFrame> current;
34     std::shared_ptr<SampleStackItem> child;
35     std::shared_ptr<SampleStackItem> siblings;
36 
SampleStackItemSampleStackItem37     SampleStackItem() : pc(0),
38         count(0),
39         level(0),
40         current(nullptr),
41         child(nullptr),
42         siblings(nullptr)
43         {};
44 };
45 class SampleStackPrinter {
46 public:
SampleStackPrinter(std::shared_ptr<Unwinder> unwinder,std::shared_ptr<DfxMaps> maps)47     SampleStackPrinter(std::shared_ptr<Unwinder> unwinder, std::shared_ptr<DfxMaps> maps) : unwinder_(unwinder),
48         maps_(maps)
49     {
50         root_ = nullptr;
51     };
52     SampleStackPrinter(const SampleStackPrinter& other) = delete;
53     SampleStackPrinter& operator=(const SampleStackPrinter& other) = delete;
~SampleStackPrinter()54     ~SampleStackPrinter()
55     {};
56 
57     void Insert(std::vector<uintptr_t>& pcs, int32_t count);
58     std::string GetFullStack(const std::vector<TimeAndFrames>& timeAndFrameList);
59     std::string GetTreeStack(std::vector<StackIdAndCount>& stackIdCount,
60         std::unique_ptr<UniqueStackTable>& uniqueStackTable, std::string& heaviestStack);
61     std::string Print();
62 
63 private:
64     std::shared_ptr<SampleStackItem> Insert(std::shared_ptr<SampleStackItem> curNode,
65         uintptr_t pc, int32_t count, uint64_t level, std::shared_ptr<SampleStackItem> acientNode);
66     std::shared_ptr<SampleStackItem> AdjustSiblings(std::shared_ptr<SampleStackItem> acient,
67         std::shared_ptr<SampleStackItem> cur, std::shared_ptr<SampleStackItem> node);
68     std::shared_ptr<SampleStackItem> root_;
69     std::shared_ptr<Unwinder> unwinder_;
70     std::shared_ptr<DfxMaps> maps_;
71 };
72 } // end of namespace HiviewDFX
73 } // end of namespace OHOS
74 #endif
75