• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 #ifndef STACK_DATA_REPEATER_H
16 #define STACK_DATA_REPEATER_H
17 
18 #include <condition_variable>
19 #include <deque>
20 #include <memory>
21 #include <mutex>
22 #include "logging.h"
23 #include "nocopyable.h"
24 #include "native_hook_result.pb.h"
25 #include "hook_common.h"
26 #include "utilities.h"
27 #include "hook_record.h"
28 
29 namespace OHOS::Developtools::NativeDaemon {
30 
31 using BatchNativeHookDataPtr = STD_PTR(shared, BatchNativeHookData);
32 constexpr const int32_t CACHE_ARRAY_SIZE = 10;
33 
34 class StackDataRepeater {
35 public:
36     explicit StackDataRepeater(size_t maxSize);
37     ~StackDataRepeater();
38     bool PutRawStack(const std::shared_ptr<HookRecord>& rawData, bool isRecordAccurately);
39     bool PutRawStackArray(std::array<std::shared_ptr<HookRecord>, CACHE_ARRAY_SIZE>& rawDataArray, uint32_t batchCount);
40     std::shared_ptr<HookRecord> TakeRawData(uint32_t during, clockid_t clockId, uint32_t batchCount,
41         std::shared_ptr<HookRecord> batchRawStack[], uint32_t statInterval, bool& isTimeOut);
42     void Close();
43     void Reset();
44     size_t Size();
ClearCache()45     void ClearCache()
46     {
47         std::unique_lock<std::mutex> lock(cacheMutex_);
48         rawDataCacheQueue_.clear();
49     }
50 
51 private:
52     std::mutex mutex_;
53     std::mutex cacheMutex_;
54     std::deque<std::shared_ptr<HookRecord>> rawDataCacheQueue_;
55     std::condition_variable slotCondVar_;
56     std::condition_variable itemCondVar_;
57     std::deque<std::shared_ptr<HookRecord>> rawDataQueue_;
58     std::unordered_map<uint64_t, std::shared_ptr<HookRecord>> mallocMap_ = {};
59     size_t maxSize_;
60     uint64_t reducedStackCount_;
61     std::atomic_bool closed_;
62 
63     DISALLOW_COPY_AND_MOVE(StackDataRepeater);
64 };
65 
66 using StackDataRepeaterPtr = STD_PTR(shared, StackDataRepeater);
67 }
68 #endif // STACK_DATA_REPEATER_H