• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 OHOS_ARKCOMPILER_AOTCOMPILER_IMPL_H
17 #define OHOS_ARKCOMPILER_AOTCOMPILER_IMPL_H
18 
19 #include <atomic>
20 #include <mutex>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 #include "aot_args_handler.h"
25 #include "ecmascript/compiler/aot_file/aot_version.h"
26 
27 namespace OHOS::ArkCompiler {
28 class AotCompilerImpl {
29 public:
30     static constexpr int32_t AOT_COMPILE_STOP_LEVEL = 2;
31     static AotCompilerImpl &GetInstance();
32     /**
33      * @brief ecmascript aot_compiler
34      * @param argsMap input command strings
35      * @param sigData  contain signature string data for EnforceCodeSignForFile()
36      * @return err code
37      */
38     int32_t EcmascriptAotCompiler(const std::unordered_map<std::string, std::string> &argsMap,
39                                   std::vector<int16_t> &sigData);
40     int32_t StopAotCompiler();
41     int32_t GetAOTVersion(std::string& sigData);
42     int32_t NeedReCompile(const std::string& args, bool& sigData);
43     void HandlePowerDisconnected();
44     void HandleScreenOn();
45     void HandleThermalLevelChanged(const int32_t level);
46     int32_t SendSysEvent(const std::unordered_map<std::string, std::string> &argsMap) const;
47     std::string ParseArkCacheFromArgs(const std::unordered_map<std::string, std::string> &argsMap) const;
48 
49 protected:
50     void DropCapabilities() const;
51     void ExecuteInChildProcess() const;
52     int32_t PrintAOTCompilerResult(const int compilerStatus) const;
53     void ExecuteInParentProcess(pid_t childPid, int32_t &ret);
54     int32_t AOTLocalCodeSign(std::vector<int16_t> &sigData) const;
55     int32_t RemoveAotFiles() const;
56     void InitState(const pid_t childPid);
57     void ResetState();
58     void PauseAotCompiler();
59     void AllowAotCompiler();
60     bool IsSystemComponent(const std::unordered_map<std::string, std::string> &argsMap);
61     bool VerifyCompilerModeAndPkgInfo(const std::unordered_map<std::string, std::string> &argsMap);
62 
63     AotCompilerImpl() = default;
64     ~AotCompilerImpl() = default;
65     AotCompilerImpl(const AotCompilerImpl&) = delete;
66     AotCompilerImpl(AotCompilerImpl&&) = delete;
67     AotCompilerImpl& operator=(const AotCompilerImpl&) = delete;
68     AotCompilerImpl& operator=(AotCompilerImpl&&) = delete;
69 protected:
70     std::atomic<bool> allowAotCompiler_ {true};
71     mutable std::mutex stateMutex_;
72     std::unique_ptr<AOTArgsHandler> argsHandler_;
73     struct AOTState {
74         bool running {false};
75         pid_t childPid {-1};
76     } state_;
77     int32_t thermalLevel_ {0};
78 };
79 } // namespace OHOS::ArkCompiler
80 #endif  // OHOS_ARKCOMPILER_AOTCOMPILER_IMPL_H