• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 #include <gtest/gtest.h>
17 
18 #include <vector>
19 
20 #include "assembly-parser.h"
21 #include "runtime/include/method.h"
22 #include "runtime/include/runtime.h"
23 
24 #include "runtime/compiler.h"
25 
26 namespace ark::test {
27 
28 class CompilerThreadPoolTest : public testing::Test {
29 public:
30     static const size_t METHOD_COUNT = 32;
CompilerThreadPoolTest()31     CompilerThreadPoolTest()
32     {
33         RuntimeOptions options;
34         options.SetShouldLoadBootPandaFiles(false);
35         options.SetShouldInitializeIntrinsics(false);
36         Runtime::Create(options);
37         thread_ = ark::MTManagedThread::GetCurrent();
38         thread_->ManagedCodeBegin();
39     }
40 
SetUp()41     void SetUp() override
42     {
43 #ifndef PANDA_COMPILER_ENABLE
44         GTEST_SKIP();
45 #endif
46     }
47 
~CompilerThreadPoolTest()48     ~CompilerThreadPoolTest() override
49     {
50         thread_->ManagedCodeEnd();
51         Runtime::Destroy();
52     }
53 
54     NO_COPY_SEMANTIC(CompilerThreadPoolTest);
55     NO_MOVE_SEMANTIC(CompilerThreadPoolTest);
56 
57 private:
58     ark::MTManagedThread *thread_;
59 };
60 
GetMethod(Class * klass,size_t num)61 Method *GetMethod(Class *klass, size_t num)
62 {
63     PandaStringStream ss;
64     ss << "f" << num;
65     Method *method = klass->GetDirectMethod(utf::CStringAsMutf8(ss.str().c_str()));
66     return method;
67 }
68 
GetClass()69 Class *GetClass()
70 {
71     pandasm::Parser p;
72 
73     PandaStringStream ss;
74 
75     for (size_t i = 0; i < CompilerThreadPoolTest::METHOD_COUNT; i++) {
76         ss << ".function void f" << i << "() {" << std::endl;
77         ss << "    return.void" << std::endl;
78         ss << "}" << std::endl;
79     }
80 
81     auto source = ss.str();
82     // NOLINTNEXTLINE(readability-redundant-string-cstr)
83     auto res = p.Parse(source.c_str());
84     auto pf = pandasm::AsmEmitter::Emit(res.Value());
85 
86     ClassLinker *classLinker = Runtime::GetCurrent()->GetClassLinker();
87     classLinker->AddPandaFile(std::move(pf));
88 
89     PandaString descriptor;
90 
91     return classLinker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY)
92         ->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("_GLOBAL"), &descriptor));
93 }
94 
CompileMethods(int initialNumberOfThreads,size_t scaledNumberOfThreads)95 void CompileMethods(int initialNumberOfThreads, size_t scaledNumberOfThreads)
96 {
97     auto *klass = GetClass();
98     ASSERT_NE(klass, nullptr);
99 
100     auto *compiler = static_cast<Compiler *>(PandaVM::GetCurrent()->GetCompiler());
101 
102     compiler->ScaleThreadPool(initialNumberOfThreads);
103 
104     for (size_t i = 0; i < CompilerThreadPoolTest::METHOD_COUNT; i++) {
105         Method *method = GetMethod(klass, i);
106         ASSERT_NE(method, nullptr);
107         compiler->CompileMethod(method, i, false, TaggedValue::Hole());
108     }
109 
110     compiler->ScaleThreadPool(scaledNumberOfThreads);
111 
112     for (;;) {
113         bool isCompleted = true;
114         for (size_t i = 0; i < CompilerThreadPoolTest::METHOD_COUNT; i++) {
115             Method *method = GetMethod(klass, i);
116             if (method->GetCompilationStatus() == Method::NOT_COMPILED) {
117                 // In case queue was full.
118                 compiler->CompileMethod(method, i, false, TaggedValue::Hole());
119             }
120             if (method->GetCompilationStatus() != Method::COMPILED) {
121                 isCompleted = false;
122             }
123         }
124         if (isCompleted) {
125             break;
126         }
127     }
128 }
129 
TEST_F(CompilerThreadPoolTest,SeveralThreads)130 TEST_F(CompilerThreadPoolTest, SeveralThreads)
131 {
132     constexpr size_t NUMBER_OF_THREADS = 8;
133     CompileMethods(NUMBER_OF_THREADS, NUMBER_OF_THREADS);
134 }
135 
TEST_F(CompilerThreadPoolTest,ReduceThreads)136 TEST_F(CompilerThreadPoolTest, ReduceThreads)
137 {
138     constexpr size_t NUMBER_OF_THREADS = 8;
139     constexpr size_t NUMBER_OF_THREADS_SCALED = 4;
140     CompileMethods(NUMBER_OF_THREADS, NUMBER_OF_THREADS_SCALED);
141 }
142 
TEST_F(CompilerThreadPoolTest,IncreaseThreads)143 TEST_F(CompilerThreadPoolTest, IncreaseThreads)
144 {
145     constexpr size_t NUMBER_OF_THREADS = 4;
146     constexpr size_t NUMBER_OF_THREADS_SCALED = 8;
147     CompileMethods(NUMBER_OF_THREADS, NUMBER_OF_THREADS_SCALED);
148 }
149 
150 }  // namespace ark::test
151