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 "page_history_recorder.h"
17 #include "hiview_logger.h"
18
19 namespace OHOS {
20 namespace HiviewDFX {
21 DEFINE_LOG_LABEL(0xD002D11, "Faultlogger");
GetPageTrace(const std::string & boundleName,int32_t pid) const22 std::string PageHistoryRecorder::GetPageTrace(const std::string& boundleName, int32_t pid) const
23 {
24 std::lock_guard<std::mutex> lock(mutex_);
25 if (auto it = lruCache_.find(boundleName); it != lruCache_.end()) {
26 return it->second->second.ToString(pid);
27 }
28 HIVIEW_LOGE("save process num is %{public}zu", lruCache_.size());
29 return "";
30 }
31
PutPageTrace(const std::string & boundleName,PageTraceNode node)32 void PageHistoryRecorder::PutPageTrace(const std::string& boundleName, PageTraceNode node)
33 {
34 std::lock_guard<std::mutex> lock(mutex_);
35 if (auto it = lruCache_.find(boundleName); it != lruCache_.end()) {
36 pagesList_.splice(pagesList_.begin(), pagesList_, it->second);
37 it->second->second.AddPageTrace(std::move(node));
38 return;
39 }
40
41 PagesTrace trace;
42 trace.AddPageTrace(std::move(node));
43 pagesList_.emplace_front(boundleName, trace);
44 lruCache_[boundleName] = pagesList_.begin();
45
46 while (lruCache_.size() > MAX_RECORDED_PROCESS_NUM) {
47 lruCache_.erase(pagesList_.back().first);
48 pagesList_.pop_back();
49 }
50 }
51 } // HiviewDFX
52 } // OHOS
53