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 {
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 const std::vector<std::string> code_paths;
44 const std::string fake_file = "fake_file";
45 profile_saver_ = new ProfileSaver(
46 runtime->GetJITOptions()->GetProfileSaverOptions(),
47 fake_file,
48 runtime->GetJitCodeCache(),
49 code_paths);
50 }
51
~ProfileSaverTest()52 ~ProfileSaverTest() {
53 if (profile_saver_ != nullptr) {
54 delete profile_saver_;
55 }
56 }
57
GetProfileSampleAnnotation()58 ProfileCompilationInfo::ProfileSampleAnnotation GetProfileSampleAnnotation() {
59 return profile_saver_->GetProfileSampleAnnotation();
60 }
61
AnnotateSampleFlags(uint32_t flags)62 Hotness::Flag AnnotateSampleFlags(uint32_t flags) {
63 return profile_saver_->AnnotateSampleFlags(flags);
64 }
65
66 protected:
67 ProfileSaver* profile_saver_ = nullptr;
68 };
69
70 // Test profile saving operations for boot image.
71 class ProfileSaverForBootTest : public ProfileSaverTest {
72 public:
SetUpRuntimeOptions(RuntimeOptions * options)73 void SetUpRuntimeOptions(RuntimeOptions *options) override {
74 ProfileSaverTest::SetUpRuntimeOptions(options);
75 options->push_back(std::make_pair("-Xps-profile-boot-class-path", nullptr));
76 }
77 };
78
TEST_F(ProfileSaverTest,GetProfileSampleAnnotation)79 TEST_F(ProfileSaverTest, GetProfileSampleAnnotation) {
80 ASSERT_EQ(ProfileCompilationInfo::ProfileSampleAnnotation::kNone,
81 GetProfileSampleAnnotation());
82 }
83
TEST_F(ProfileSaverForBootTest,GetProfileSampleAnnotationUnkown)84 TEST_F(ProfileSaverForBootTest, GetProfileSampleAnnotationUnkown) {
85 ProfileCompilationInfo::ProfileSampleAnnotation expected("unknown");
86 ASSERT_EQ(expected, GetProfileSampleAnnotation());
87 }
88
TEST_F(ProfileSaverForBootTest,GetProfileSampleAnnotation)89 TEST_F(ProfileSaverForBootTest, GetProfileSampleAnnotation) {
90 Runtime::Current()->SetProcessPackageName("test.package");
91 ProfileCompilationInfo::ProfileSampleAnnotation expected("test.package");
92 ASSERT_EQ(expected, GetProfileSampleAnnotation());
93 }
94
TEST_F(ProfileSaverForBootTest,AnnotateSampleFlags)95 TEST_F(ProfileSaverForBootTest, AnnotateSampleFlags) {
96 Hotness::Flag expected_flag = Is64BitInstructionSet(Runtime::Current()->GetInstructionSet())
97 ? Hotness::kFlag64bit
98 : Hotness::kFlag32bit;
99 Hotness::Flag actual = AnnotateSampleFlags(Hotness::kFlagHot);
100
101 ASSERT_EQ(static_cast<Hotness::Flag>(expected_flag | Hotness::kFlagHot), actual);
102 }
103
TEST_F(ProfileSaverTest,AnnotateSampleFlags)104 TEST_F(ProfileSaverTest, AnnotateSampleFlags) {
105 Hotness::Flag actual = AnnotateSampleFlags(Hotness::kFlagHot);
106
107 ASSERT_EQ(Hotness::kFlagHot, actual);
108 }
109
110 } // namespace art
111