• 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_SAVER_H
17 #define ECMASCRIPT_PGO_PROFILER_SAVER_H
18 
19 #include "ecmascript/pgo_profiler/pgo_profiler_info.h"
20 #include "macros.h"
21 
22 namespace panda::ecmascript {
23 class PGOProfilerSaver {
24 public:
PGOProfilerSaver(const std::string & outDir,uint32_t hotnessThreshold)25     PGOProfilerSaver(const std::string &outDir, uint32_t hotnessThreshold)
26         : outDir_(outDir), hotnessThreshold_(hotnessThreshold) {}
27 
28     NO_COPY_SEMANTIC(PGOProfilerSaver);
29     NO_MOVE_SEMANTIC(PGOProfilerSaver);
30 
31     bool PUBLIC_API InitializeData();
32 
33     void PUBLIC_API Destroy();
34 
IsInitialized()35     bool IsInitialized() const
36     {
37         return isInitialized_;
38     }
39 
40     void SamplePandaFileInfo(uint32_t checksum);
41     void Merge(const PGORecordDetailInfos &recordInfos);
42     void TerminateSaveTask();
43     void PostSaveTask();
44 
45     void PUBLIC_API Save();
46 
47     bool PUBLIC_API LoadAPTextFile(const std::string &inPath);
48 
49 private:
50     void StartSaveTask(const SaveTask *task);
51     void SaveProfiler(const SaveTask *task = nullptr);
52 
53     bool isInitialized_ {false};
54     std::string outDir_;
55     uint32_t hotnessThreshold_ {2};
56     std::string realOutPath_;
57     PGOProfilerHeader *header_ {nullptr};
58     std::unique_ptr<PGOPandaFileInfos> pandaFileInfos_;
59     std::unique_ptr<PGORecordDetailInfos> globalRecordInfos_;
60     os::memory::Mutex mutex_;
61     friend SaveTask;
62 };
63 
64 class SaveTask : public Task {
65 public:
SaveTask(PGOProfilerSaver * saver,int32_t id)66     explicit SaveTask(PGOProfilerSaver *saver, int32_t id) : Task(id), saver_(saver) {};
67     virtual ~SaveTask() override = default;
68 
Run(uint32_t threadIndex)69     bool Run([[maybe_unused]] uint32_t threadIndex) override
70     {
71         saver_->StartSaveTask(this);
72         return true;
73     }
74 
GetTaskType()75     TaskType GetTaskType() const override
76     {
77         return TaskType::PGO_SAVE_TASK;
78     }
79 
80     NO_COPY_SEMANTIC(SaveTask);
81     NO_MOVE_SEMANTIC(SaveTask);
82 private:
83     PGOProfilerSaver *saver_;
84 };
85 } // namespace panda::ecmascript
86 #endif  // ECMASCRIPT_PGO_PROFILER_SAVER_H
87