• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 panda::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_ = panda::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()
49     {
50         thread_->ManagedCodeEnd();
51         Runtime::Destroy();
52     }
53 
54 protected:
55     panda::MTManagedThread *thread_;
56 };
57 
GetMethod(Class * klass,size_t num)58 Method *GetMethod(Class *klass, size_t num)
59 {
60     PandaStringStream ss;
61     ss << "f" << num;
62     Method *method = klass->GetDirectMethod(utf::CStringAsMutf8(ss.str().c_str()));
63     return method;
64 }
65 
GetClass()66 Class *GetClass()
67 {
68     pandasm::Parser p;
69 
70     PandaStringStream ss;
71 
72     for (size_t i = 0; i < CompilerThreadPoolTest::METHOD_COUNT; i++) {
73         ss << ".function void f" << i << "() {" << std::endl;
74         ss << "    return.void" << std::endl;
75         ss << "}" << std::endl;
76     }
77 
78     auto source = ss.str();
79     auto res = p.Parse(source.c_str());
80     auto pf = pandasm::AsmEmitter::Emit(res.Value());
81 
82     ClassLinker *class_linker = Runtime::GetCurrent()->GetClassLinker();
83     class_linker->AddPandaFile(std::move(pf));
84 
85     PandaString descriptor;
86 
87     return class_linker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY)
88         ->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("_GLOBAL"), &descriptor));
89 }
90 
CompileMethods(int initial_number_of_threads,size_t scaled_number_of_threads)91 void CompileMethods(int initial_number_of_threads, size_t scaled_number_of_threads)
92 {
93     auto *klass = GetClass();
94     ASSERT_NE(klass, nullptr);
95 
96     auto *compiler = static_cast<Compiler *>(PandaVM::GetCurrent()->GetCompiler());
97 
98     compiler->ScaleThreadPool(initial_number_of_threads);
99 
100     for (size_t i = 0; i < CompilerThreadPoolTest::METHOD_COUNT; i++) {
101         Method *method = GetMethod(klass, i);
102         ASSERT_NE(method, nullptr);
103         compiler->CompileMethod(method, i, false);
104     }
105 
106     compiler->ScaleThreadPool(scaled_number_of_threads);
107 
108     for (;;) {
109         bool is_completed = true;
110         for (size_t i = 0; i < CompilerThreadPoolTest::METHOD_COUNT; i++) {
111             Method *method = GetMethod(klass, i);
112             if (method->GetCompilationStatus() == Method::NOT_COMPILED) {
113                 // In case queue was full.
114                 compiler->CompileMethod(method, i, false);
115             }
116             if (method->GetCompilationStatus() != Method::COMPILED) {
117                 is_completed = false;
118             }
119         }
120         if (is_completed) {
121             break;
122         }
123     }
124 }
125 
TEST_F(CompilerThreadPoolTest,SeveralThreads)126 TEST_F(CompilerThreadPoolTest, SeveralThreads)
127 {
128     constexpr size_t NUMBER_OF_THREADS = 8;
129     CompileMethods(NUMBER_OF_THREADS, NUMBER_OF_THREADS);
130 }
131 
TEST_F(CompilerThreadPoolTest,ReduceThreads)132 TEST_F(CompilerThreadPoolTest, ReduceThreads)
133 {
134     constexpr size_t NUMBER_OF_THREADS = 8;
135     constexpr size_t NUMBER_OF_THREADS_SCALED = 4;
136     CompileMethods(NUMBER_OF_THREADS, NUMBER_OF_THREADS_SCALED);
137 }
138 
TEST_F(CompilerThreadPoolTest,IncreaseThreads)139 TEST_F(CompilerThreadPoolTest, IncreaseThreads)
140 {
141     constexpr size_t NUMBER_OF_THREADS = 4;
142     constexpr size_t NUMBER_OF_THREADS_SCALED = 8;
143     CompileMethods(NUMBER_OF_THREADS, NUMBER_OF_THREADS_SCALED);
144 }
145 
146 }  // namespace panda::test
147