xref: /aosp_15_r20/art/runtime/jit/profile_saver_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "common_runtime_test.h"
20*795d594fSAndroid Build Coastguard Worker #include "compiler_callbacks.h"
21*795d594fSAndroid Build Coastguard Worker #include "jit/jit.h"
22*795d594fSAndroid Build Coastguard Worker #include "profile_saver.h"
23*795d594fSAndroid Build Coastguard Worker #include "profile/profile_compilation_info.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker using Hotness = ProfileCompilationInfo::MethodHotness;
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker class ProfileSaverTest : public CommonRuntimeTest {
30*795d594fSAndroid Build Coastguard Worker  public:
SetUpRuntimeOptions(RuntimeOptions * options)31*795d594fSAndroid Build Coastguard Worker   void SetUpRuntimeOptions(RuntimeOptions *options) override {
32*795d594fSAndroid Build Coastguard Worker     // Reset the callbacks so that the runtime doesn't think it's for AOT.
33*795d594fSAndroid Build Coastguard Worker     callbacks_ = nullptr;
34*795d594fSAndroid Build Coastguard Worker     CommonRuntimeTest::SetUpRuntimeOptions(options);
35*795d594fSAndroid Build Coastguard Worker     // Enable profile saving and the jit.
36*795d594fSAndroid Build Coastguard Worker     options->push_back(std::make_pair("-Xjitsaveprofilinginfo", nullptr));
37*795d594fSAndroid Build Coastguard Worker     options->push_back(std::make_pair("-Xusejit:true", nullptr));
38*795d594fSAndroid Build Coastguard Worker   }
39*795d594fSAndroid Build Coastguard Worker 
PostRuntimeCreate()40*795d594fSAndroid Build Coastguard Worker   void PostRuntimeCreate() override {
41*795d594fSAndroid Build Coastguard Worker     // Create a profile saver.
42*795d594fSAndroid Build Coastguard Worker     Runtime* runtime = Runtime::Current();
43*795d594fSAndroid Build Coastguard Worker     profile_saver_ = new ProfileSaver(
44*795d594fSAndroid Build Coastguard Worker         runtime->GetJITOptions()->GetProfileSaverOptions(),
45*795d594fSAndroid Build Coastguard Worker         runtime->GetJitCodeCache());
46*795d594fSAndroid Build Coastguard Worker   }
47*795d594fSAndroid Build Coastguard Worker 
~ProfileSaverTest()48*795d594fSAndroid Build Coastguard Worker   ~ProfileSaverTest() {
49*795d594fSAndroid Build Coastguard Worker     if (profile_saver_ != nullptr) {
50*795d594fSAndroid Build Coastguard Worker       delete profile_saver_;
51*795d594fSAndroid Build Coastguard Worker     }
52*795d594fSAndroid Build Coastguard Worker   }
53*795d594fSAndroid Build Coastguard Worker 
GetProfileSampleAnnotation()54*795d594fSAndroid Build Coastguard Worker   ProfileCompilationInfo::ProfileSampleAnnotation GetProfileSampleAnnotation() {
55*795d594fSAndroid Build Coastguard Worker     return profile_saver_->GetProfileSampleAnnotation();
56*795d594fSAndroid Build Coastguard Worker   }
57*795d594fSAndroid Build Coastguard Worker 
AnnotateSampleFlags(uint32_t flags)58*795d594fSAndroid Build Coastguard Worker   Hotness::Flag AnnotateSampleFlags(uint32_t flags) {
59*795d594fSAndroid Build Coastguard Worker     return profile_saver_->AnnotateSampleFlags(flags);
60*795d594fSAndroid Build Coastguard Worker   }
61*795d594fSAndroid Build Coastguard Worker 
62*795d594fSAndroid Build Coastguard Worker  protected:
63*795d594fSAndroid Build Coastguard Worker   ProfileSaver* profile_saver_ = nullptr;
64*795d594fSAndroid Build Coastguard Worker };
65*795d594fSAndroid Build Coastguard Worker 
66*795d594fSAndroid Build Coastguard Worker // Test profile saving operations for boot image.
67*795d594fSAndroid Build Coastguard Worker class ProfileSaverForBootTest : public ProfileSaverTest {
68*795d594fSAndroid Build Coastguard Worker  public:
SetUpRuntimeOptions(RuntimeOptions * options)69*795d594fSAndroid Build Coastguard Worker   void SetUpRuntimeOptions(RuntimeOptions *options) override {
70*795d594fSAndroid Build Coastguard Worker     ProfileSaverTest::SetUpRuntimeOptions(options);
71*795d594fSAndroid Build Coastguard Worker     options->push_back(std::make_pair("-Xps-profile-boot-class-path", nullptr));
72*795d594fSAndroid Build Coastguard Worker   }
73*795d594fSAndroid Build Coastguard Worker };
74*795d594fSAndroid Build Coastguard Worker 
TEST_F(ProfileSaverTest,GetProfileSampleAnnotation)75*795d594fSAndroid Build Coastguard Worker TEST_F(ProfileSaverTest, GetProfileSampleAnnotation) {
76*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(ProfileCompilationInfo::ProfileSampleAnnotation::kNone,
77*795d594fSAndroid Build Coastguard Worker             GetProfileSampleAnnotation());
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker 
TEST_F(ProfileSaverForBootTest,GetProfileSampleAnnotationUnkown)80*795d594fSAndroid Build Coastguard Worker TEST_F(ProfileSaverForBootTest, GetProfileSampleAnnotationUnkown) {
81*795d594fSAndroid Build Coastguard Worker   ProfileCompilationInfo::ProfileSampleAnnotation expected("unknown");
82*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(expected, GetProfileSampleAnnotation());
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker 
TEST_F(ProfileSaverForBootTest,GetProfileSampleAnnotation)85*795d594fSAndroid Build Coastguard Worker TEST_F(ProfileSaverForBootTest, GetProfileSampleAnnotation) {
86*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->SetProcessPackageName("test.package");
87*795d594fSAndroid Build Coastguard Worker   ProfileCompilationInfo::ProfileSampleAnnotation expected("test.package");
88*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(expected, GetProfileSampleAnnotation());
89*795d594fSAndroid Build Coastguard Worker }
90*795d594fSAndroid Build Coastguard Worker 
TEST_F(ProfileSaverForBootTest,AnnotateSampleFlags)91*795d594fSAndroid Build Coastguard Worker TEST_F(ProfileSaverForBootTest, AnnotateSampleFlags) {
92*795d594fSAndroid Build Coastguard Worker   Hotness::Flag expected_flag = Is64BitInstructionSet(Runtime::Current()->GetInstructionSet())
93*795d594fSAndroid Build Coastguard Worker         ? Hotness::kFlag64bit
94*795d594fSAndroid Build Coastguard Worker         : Hotness::kFlag32bit;
95*795d594fSAndroid Build Coastguard Worker   Hotness::Flag actual = AnnotateSampleFlags(Hotness::kFlagHot);
96*795d594fSAndroid Build Coastguard Worker 
97*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(static_cast<Hotness::Flag>(expected_flag | Hotness::kFlagHot), actual);
98*795d594fSAndroid Build Coastguard Worker }
99*795d594fSAndroid Build Coastguard Worker 
TEST_F(ProfileSaverTest,AnnotateSampleFlags)100*795d594fSAndroid Build Coastguard Worker TEST_F(ProfileSaverTest, AnnotateSampleFlags) {
101*795d594fSAndroid Build Coastguard Worker   Hotness::Flag actual = AnnotateSampleFlags(Hotness::kFlagHot);
102*795d594fSAndroid Build Coastguard Worker 
103*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(Hotness::kFlagHot, actual);
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker 
106*795d594fSAndroid Build Coastguard Worker }  // namespace art
107