• 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 <atomic>
20 #include <memory>
21 #include <utility>
22 
23 #include "macros.h"
24 
25 #include "ecmascript/pgo_profiler/pgo_info.h"
26 #include "ecmascript/pgo_profiler/pgo_profiler_info.h"
27 
28 namespace panda::ecmascript::pgo {
29 class PGOProfilerDecoder;
30 class PGOProfilerEncoder {
31 public:
32     enum ApGenMode { OVERWRITE, MERGE };
33 
PGOProfilerEncoder(const std::string & path,ApGenMode mode)34     PGOProfilerEncoder(const std::string& path, ApGenMode mode): path_(path), mode_(mode) {}
35     ~PGOProfilerEncoder() = default;
36 
37     NO_COPY_SEMANTIC(PGOProfilerEncoder);
38     NO_MOVE_SEMANTIC(PGOProfilerEncoder);
39 
40     void TerminateSaveTask();
41     static void PostSaveTask(const std::string& path, ApGenMode mode, const std::shared_ptr<PGOInfo> pgoInfo);
42     void SetApGenMode(ApGenMode mode);
43     ApGenMode GetApGenMode() const;
44     bool PUBLIC_API Save(const std::shared_ptr<PGOInfo> pgoInfo);
45 
46 private:
47     void StartSaveTask(const std::shared_ptr<PGOInfo> info, const SaveTask* task);
48     bool InternalSave(const std::shared_ptr<PGOInfo> info, const SaveTask* task = nullptr);
49     bool SaveAndRename(const std::shared_ptr<PGOInfo> info, const SaveTask* task = nullptr);
50     void AddChecksum(std::fstream& fileStream);
51 
52     std::string path_;
53     ApGenMode mode_ {OVERWRITE};
54     Mutex mutex_;
55     friend SaveTask;
56 };
57 
58 class SaveTask : public Task {
59 public:
SaveTask(const std::shared_ptr<PGOProfilerEncoder> encoder,const std::shared_ptr<PGOInfo> pgoInfo,int32_t id)60     explicit SaveTask(const std::shared_ptr<PGOProfilerEncoder> encoder,
61                       const std::shared_ptr<PGOInfo> pgoInfo,
62                       int32_t id)
63         : Task(id), encoder_(encoder), pgoInfo_(pgoInfo) {};
64     virtual ~SaveTask() override = default;
65 
Run(uint32_t threadIndex)66     bool Run([[maybe_unused]] uint32_t threadIndex) override
67     {
68         ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SaveTask::Run");
69         encoder_->StartSaveTask(pgoInfo_, this);
70         return true;
71     }
72 
GetTaskType()73     TaskType GetTaskType() const override
74     {
75         return TaskType::PGO_SAVE_TASK;
76     }
77 
78     NO_COPY_SEMANTIC(SaveTask);
79     NO_MOVE_SEMANTIC(SaveTask);
80 private:
81     std::shared_ptr<PGOProfilerEncoder> encoder_;
82     std::shared_ptr<PGOInfo> pgoInfo_;
83 };
84 } // namespace panda::ecmascript::pgo
85 #endif  // ECMASCRIPT_PGO_PROFILER_ENCODER_H
86