• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2024. 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 
16 #ifndef FFRT_PROFILER_HANDLE_H
17 #define FFRT_PROFILER_HANDLE_H
18 
19 #include <chrono>
20 #include <variant>
21 
22 #include "logging.h"
23 
24 #include "buffer_writer.h"
25 #include "ffrt_profiler_result.pb.h"
26 #include "ffrt_profiler_result.pbencoder.h"
27 #include "ffrt_profiler_common.h"
28 
29 using WriterStructPtr = std::unique_ptr<WriterStruct>::pointer;
30 namespace OHOS::Developtools::Profiler {
31 class FfrtProfilerManager;
32 
33 class FfrtProfilerHandle {
34 public:
35     explicit FfrtProfilerHandle(uint32_t bufferSize, bool isProtobufSerialize = true);
36     ~FfrtProfilerHandle();
37 
38     void SetWriter(const std::shared_ptr<Writer>& writer);
39     void SetWriter(const WriterStructPtr& writer);
40     void SerializeData(const int8_t data[], uint32_t size);
SetTargetProcessInfo(uint32_t pid,const std::string & name)41     void SetTargetProcessInfo(uint32_t pid, const std::string& name)
42     {
43         pid_ = pid;
44         processName_ = name;
45     }
46 
47 private:
48     template <typename T>
49     void FlushCheck(T& protoData);
50     void FlushData(::FfrtProfilerResult& data);
51     void FlushData(ProtoEncoder::FfrtProfilerResult& data);
52     template <typename T>
53     void SerializeDataImpl(T& protoData, const int8_t data[], uint32_t size);
54     template <typename T>
55     void SerializeBaseData(T& baseDataProto, const int8_t* data);
56     template <typename T>
57     void SerializeTraceData(T& protoData, const int8_t data[], uint32_t size);
58     template <typename T>
59     void ReportCommonData(T& protoData, FfrtResultBase* base);
60     template <typename T>
61     void SerializeRawData(T& protoData, const int8_t data[], uint32_t size);
62 
63 private:
64     std::unique_ptr<uint8_t[]> buffer_{nullptr};
65     std::shared_ptr<Writer> writer_{nullptr};
66     WriterStructPtr writerStruct_{nullptr};
67     bool isProtobufSerialize_{true};
68     std::variant<::FfrtProfilerResult, ProtoEncoder::FfrtProfilerResult> protoData_;
69     uint32_t flushCount_{0};
70     uint32_t bufferSize_{0};
71     uint32_t pid_{0};
72     std::string processName_;
73     bool reportProcessName_{true};
74 };
75 } // OHOS::Developtools::Profiler
76 
77 #endif // FFRT_PROFILER_HANDLE_H