• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "lperf_record.h"
17 
18 #include "dfx_lperf.h"
19 
20 namespace OHOS {
21 namespace HiviewDFX {
22 namespace {
23 #undef LOG_DOMAIN
24 #undef LOG_TAG
25 #define LOG_DOMAIN 0xD002D11
26 #define LOG_TAG "DfxLperfRecord"
27 
28 const char *const LPERF_UNIQUE_TABLE = "lperf_unique_table";
29 constexpr uint32_t UNIQUE_STABLE_SIZE = 1024 * 1024;
30 }
31 
StartProcessSampling(int pid,const std::vector<int> & tids,int freq,int duration)32 int LperfRecord::StartProcessSampling(int pid, const std::vector<int>& tids, int freq, int duration)
33 {
34     CHECK_TRUE_AND_RET(!CheckOutOfRange<int>(tids.size(), MIN_SAMPLE_TIDS, MAX_SAMPLE_TIDS), -1,
35                        "invalid tids size: %d", tids.size());
36     CHECK_TRUE_AND_RET(!CheckOutOfRange<int>(freq, MIN_SAMPLE_FREQUENCY, MAX_SAMPLE_FREQUENCY), -1,
37                        "invalid frequency value: %d", freq);
38     CHECK_TRUE_AND_RET(!CheckOutOfRange<int>(duration, MIN_STOP_SECONDS, MAX_STOP_SECONDS), -1,
39                        "invalid duration value: %d", duration);
40     for (int tid : tids) {
41         CHECK_TRUE_AND_RET(tid > 0, -1, "invalid tid: %d", tid);
42     }
43 
44     pid_ = pid;
45     tids_ = tids;
46     frequency_ = static_cast<unsigned int>(freq);
47     timeStopSec_ = static_cast<unsigned int>(duration);
48 
49     int ret = OnSubCommand();
50     lperfEvents_.Clear();
51     return ret;
52 }
53 
CollectSampleStack(std::string & datas)54 int LperfRecord::CollectSampleStack(std::string& datas)
55 {
56     if (stackPrinter_ == nullptr) {
57         return -1;
58     }
59     auto frames = stackPrinter_->GetThreadSampledFrames();
60     if (frames.empty()) {
61         return -1;
62     }
63     std::ostringstream oss;
64     StackPrinter::SerializeSampledFrameMap(frames, oss);
65     datas = oss.str();
66     return datas.empty() ? -1 : 0;
67 }
68 
FinishProcessSampling()69 void LperfRecord::FinishProcessSampling()
70 {
71     stackPrinter_ = nullptr;
72 }
73 
OnSubCommand()74 int LperfRecord::OnSubCommand()
75 {
76     PrepareLperfEvent();
77     CHECK_TRUE_AND_RET(lperfEvents_.PrepareRecord() == 0, -1, "OnSubCommand prepareRecord failed");
78     CHECK_TRUE_AND_RET(lperfEvents_.StartRecord() == 0, -1, "OnSubCommand startRecord failed");
79     return 0;
80 }
81 
PrepareLperfEvent()82 void LperfRecord::PrepareLperfEvent()
83 {
84     lperfEvents_.SetTid(tids_);
85     lperfEvents_.SetTimeOut(timeStopSec_);
86     lperfEvents_.SetSampleFrequency(frequency_);
87     auto processRecord = [this](LperfRecordSample& record) -> void {
88         this->SymbolicRecord(record);
89     };
90     lperfEvents_.SetRecordCallBack(processRecord);
91 }
92 
SymbolicRecord(LperfRecordSample & record)93 void LperfRecord::SymbolicRecord(LperfRecordSample& record)
94 {
95     CHECK_TRUE_AND_RET(record.data_.tid > 0, NO_RETVAL, "Symbolic invalid Record, tid: %d", record.data_.tid);
96     const uint64_t maxNr = 51;
97     CHECK_TRUE_AND_RET(record.data_.nr > 0 && record.data_.nr < maxNr,
98                        NO_RETVAL, "Symbolic invalid Record, nr: %llu", record.data_.nr);
99     std::vector<uintptr_t> pcs;
100     for (uint64_t i = 0; i < record.data_.nr; i++) {
101         if (record.data_.ips[i] != PERF_CONTEXT_USER) {
102             pcs.emplace_back(static_cast<uintptr_t>(record.data_.ips[i]));
103         }
104     }
105 
106     if (stackPrinter_ == nullptr) {
107         stackPrinter_ = std::make_unique<StackPrinter>();
108         stackPrinter_->InitUniqueTable(static_cast<pid_t>(record.data_.pid), UNIQUE_STABLE_SIZE, LPERF_UNIQUE_TABLE);
109     }
110     stackPrinter_->PutPcsInTable(pcs, static_cast<int>(record.data_.tid), record.data_.time);
111 }
112 } // namespace HiviewDFX
113 } // namespace OHOS