• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef ECMASCRIPT_PGO_PROFILER_ENCODER_H
17 #define ECMASCRIPT_PGO_PROFILER_ENCODER_H
18 
19 #include "ecmascript/pgo_profiler/pgo_profiler_info.h"
20 #include "macros.h"
21 
22 namespace panda::ecmascript {
23 class PGOProfilerDecoder;
24 class PGOProfilerEncoder {
25 public:
26     enum ApGenMode { OVERWRITE, MERGE };
27 
PGOProfilerEncoder(const std::string & outDir,uint32_t hotnessThreshold,ApGenMode mode)28     PGOProfilerEncoder(const std::string &outDir, uint32_t hotnessThreshold, ApGenMode mode)
29         : outDir_(outDir), hotnessThreshold_(hotnessThreshold), mode_(mode) {}
30 
31     NO_COPY_SEMANTIC(PGOProfilerEncoder);
32     NO_MOVE_SEMANTIC(PGOProfilerEncoder);
33 
34     static void AddChecksum(std::fstream& fileStream);
35 
36     bool PUBLIC_API InitializeData();
37 
38     void PUBLIC_API Destroy();
39 
IsInitialized()40     bool IsInitialized() const
41     {
42         return isInitialized_;
43     }
44 
45     void SamplePandaFileInfo(uint32_t checksum);
46     void Merge(const PGORecordDetailInfos &recordInfos);
47     void Merge(const PGOPandaFileInfos &pandaFileInfos);
48     bool VerifyPandaFileMatched(const PGOPandaFileInfos &pandaFileInfos, const std::string &base,
49                                 const std::string &incoming) const;
50     void TerminateSaveTask();
51     void PostSaveTask();
SetApGenMode(ApGenMode mode)52     void SetApGenMode(ApGenMode mode)
53     {
54         mode_ = mode;
55     }
56 
57     bool PUBLIC_API Save();
58 
59     bool PUBLIC_API LoadAPTextFile(const std::string &inPath);
60 
61 private:
62     void StartSaveTask(const SaveTask *task);
63     bool InternalSave(const SaveTask *task = nullptr);
64     bool SaveAndRename(const SaveTask *task = nullptr);
65     void MergeWithExistProfile(PGOProfilerEncoder &encoder, PGOProfilerDecoder &decoder,
66                                const SaveTask *task = nullptr);
67 
68     bool isInitialized_ {false};
69     std::string outDir_;
70     uint32_t hotnessThreshold_ {2};
71     std::string realOutPath_;
72     PGOProfilerHeader *header_ {nullptr};
73     std::unique_ptr<PGOPandaFileInfos> pandaFileInfos_;
74     std::shared_ptr<PGORecordDetailInfos> globalRecordInfos_;
75     os::memory::Mutex mutex_;
76     ApGenMode mode_ {OVERWRITE};
77     friend SaveTask;
78 };
79 
80 class SaveTask : public Task {
81 public:
SaveTask(PGOProfilerEncoder * encoder,int32_t id)82     explicit SaveTask(PGOProfilerEncoder *encoder, int32_t id) : Task(id), encoder_(encoder) {};
83     virtual ~SaveTask() override = default;
84 
Run(uint32_t threadIndex)85     bool Run([[maybe_unused]] uint32_t threadIndex) override
86     {
87         encoder_->StartSaveTask(this);
88         return true;
89     }
90 
GetTaskType()91     TaskType GetTaskType() const override
92     {
93         return TaskType::PGO_SAVE_TASK;
94     }
95 
96     NO_COPY_SEMANTIC(SaveTask);
97     NO_MOVE_SEMANTIC(SaveTask);
98 private:
99     PGOProfilerEncoder *encoder_;
100 };
101 } // namespace panda::ecmascript
102 #endif  // ECMASCRIPT_PGO_PROFILER_ENCODER_H
103