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 #include "thread_sampler_utils.h"
16
17 #include <ctime>
18 #include <cstdio>
19
20 namespace OHOS {
21 namespace HiviewDFX {
22 constexpr uint64_t SEC_TO_NANOSEC = 1000000000;
23 constexpr uint64_t MICROSEC_TO_NANOSEC = 1000;
24 constexpr int FORMAT_TIME_LEN = 20;
25 constexpr int MICROSEC_LEN = 6;
26
GetCurrentTimeNanoseconds()27 uint64_t GetCurrentTimeNanoseconds()
28 {
29 struct timespec t;
30 t.tv_sec = 0;
31 t.tv_nsec = 0;
32 clock_gettime(CLOCK_REALTIME, &t);
33 return static_cast<uint64_t>(t.tv_sec) * SEC_TO_NANOSEC + static_cast<uint64_t>(t.tv_nsec);
34 }
35
TimeFormat(uint64_t time)36 std::string TimeFormat(uint64_t time)
37 {
38 uint64_t nsec = time % SEC_TO_NANOSEC;
39 time_t sec = static_cast<time_t>(time / SEC_TO_NANOSEC);
40 char timeChars[FORMAT_TIME_LEN];
41 struct tm* localTime = localtime(&sec);
42 if (localTime == nullptr) {
43 return "";
44 }
45 size_t sz = strftime(timeChars, FORMAT_TIME_LEN, "%Y-%m-%d-%H-%M-%S", localTime);
46 if (sz == 0) {
47 return "";
48 }
49 std::string s = timeChars;
50 uint64_t usec = nsec / MICROSEC_TO_NANOSEC;
51 std::string usecStr = std::to_string(usec);
52 while (usecStr.size() < MICROSEC_LEN) {
53 usecStr = "0" + usecStr;
54 }
55 s = s + "." + usecStr;
56 return s;
57 }
58
PutStackId(std::vector<StackIdAndCount> & stackIdCount,uint64_t stackId)59 void PutStackId(std::vector<StackIdAndCount>& stackIdCount, uint64_t stackId)
60 {
61 auto it = std::find_if(stackIdCount.begin(), stackIdCount.end(), [&stackId](const auto& stackIdCnt) {
62 return stackIdCnt.stackId == stackId;
63 });
64 if (it == stackIdCount.end()) {
65 StackIdAndCount sac = {
66 .stackId = stackId,
67 .count = 1,
68 };
69 stackIdCount.emplace_back(sac);
70 } else {
71 it->count++;
72 }
73 }
74
DoUnwind(const std::shared_ptr<Unwinder> & unwinder,UnwindInfo & unwindInfo)75 void DoUnwind(const std::shared_ptr<Unwinder>& unwinder, UnwindInfo& unwindInfo)
76 {
77 #if defined(__aarch64__)
78 static std::shared_ptr<DfxRegs> regs = std::make_shared<DfxRegsArm64>();
79 regs->SetSp(unwindInfo.context->sp);
80 regs->SetPc(unwindInfo.context->pc);
81 regs->SetFp(unwindInfo.context->fp);
82 regs->SetReg(REG_LR, &(unwindInfo.context->lr));
83 unwinder->SetRegs(regs);
84 unwinder->EnableFillFrames(false);
85 unwinder->Unwind(&unwindInfo);
86 #endif // #if defined(__aarch64__)
87 }
88 } // end of namespace HiviewDFX
89 } // end of namespace OHOS