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