blob: c76dc1110a3cd6dc90e94e808e75ab02e5cee25c [file] [log] [blame]
Mathieu Chartierbce416f2015-03-23 12:37:35 -07001/*
2 * Copyright (C) 2015 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 "common_runtime_test.h"
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Mathieu Chartierbce416f2015-03-23 12:37:35 -070020#include "class_linker.h"
21#include "jit_code_cache.h"
Mathieu Chartierbce416f2015-03-23 12:37:35 -070022#include "scoped_thread_state_change.h"
23#include "thread-inl.h"
Mathieu Chartierbce416f2015-03-23 12:37:35 -070024
25namespace art {
26namespace jit {
27
28class JitCodeCacheTest : public CommonRuntimeTest {
29 public:
30};
31
32TEST_F(JitCodeCacheTest, TestCoverage) {
33 std::string error_msg;
34 constexpr size_t kSize = 1 * MB;
35 std::unique_ptr<JitCodeCache> code_cache(
36 JitCodeCache::Create(kSize, &error_msg));
37 ASSERT_TRUE(code_cache.get() != nullptr) << error_msg;
38 ASSERT_TRUE(code_cache->CodeCachePtr() != nullptr);
39 ASSERT_EQ(code_cache->CodeCacheSize(), 0u);
40 ASSERT_GT(code_cache->CodeCacheRemain(), 0u);
41 ASSERT_TRUE(code_cache->DataCachePtr() != nullptr);
42 ASSERT_EQ(code_cache->DataCacheSize(), 0u);
43 ASSERT_GT(code_cache->DataCacheRemain(), 0u);
44 ASSERT_EQ(code_cache->CodeCacheRemain() + code_cache->DataCacheRemain(), kSize);
45 ASSERT_EQ(code_cache->NumMethods(), 0u);
46 ScopedObjectAccess soa(Thread::Current());
47 StackHandleScope<1> hs(soa.Self());
48 uint8_t* const reserved_code = code_cache->ReserveCode(soa.Self(), 4 * KB);
49 ASSERT_TRUE(reserved_code != nullptr);
50 ASSERT_TRUE(code_cache->ContainsCodePtr(reserved_code));
51 ASSERT_EQ(code_cache->NumMethods(), 1u);
Mathieu Chartier951ec2c2015-09-22 08:50:05 -070052 Runtime* const runtime = Runtime::Current();
53 ClassLinker* const class_linker = runtime->GetClassLinker();
54 ArtMethod* method = &class_linker->AllocArtMethodArray(soa.Self(),
55 runtime->GetLinearAlloc(),
56 1)->At(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -070057 ASSERT_FALSE(code_cache->ContainsMethod(method));
58 method->SetEntryPointFromQuickCompiledCode(reserved_code);
59 ASSERT_TRUE(code_cache->ContainsMethod(method));
60 ASSERT_EQ(code_cache->GetCodeFor(method), reserved_code);
Mathieu Chartierbce416f2015-03-23 12:37:35 -070061 // Save the code and then change it.
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 code_cache->SaveCompiledCode(method, reserved_code);
63 method->SetEntryPointFromQuickCompiledCode(nullptr);
64 ASSERT_EQ(code_cache->GetCodeFor(method), reserved_code);
Mathieu Chartierbce416f2015-03-23 12:37:35 -070065 const uint8_t data_arr[] = {1, 2, 3, 4, 5};
66 uint8_t* data_ptr = code_cache->AddDataArray(soa.Self(), data_arr, data_arr + sizeof(data_arr));
67 ASSERT_TRUE(data_ptr != nullptr);
68 ASSERT_EQ(memcmp(data_ptr, data_arr, sizeof(data_arr)), 0);
69}
70
71TEST_F(JitCodeCacheTest, TestOverflow) {
72 std::string error_msg;
73 constexpr size_t kSize = 1 * MB;
74 std::unique_ptr<JitCodeCache> code_cache(
75 JitCodeCache::Create(kSize, &error_msg));
76 ASSERT_TRUE(code_cache.get() != nullptr) << error_msg;
77 ASSERT_TRUE(code_cache->CodeCachePtr() != nullptr);
78 size_t code_bytes = 0;
79 size_t data_bytes = 0;
80 constexpr size_t kCodeArrSize = 4 * KB;
81 constexpr size_t kDataArrSize = 4 * KB;
Mathieu Chartiere401d142015-04-22 13:56:20 -070082 uint8_t data_arr[kDataArrSize];
83 std::fill_n(data_arr, arraysize(data_arr), 53);
Mathieu Chartierbce416f2015-03-23 12:37:35 -070084 // Add code and data until we are full.
85 uint8_t* code_ptr = nullptr;
86 uint8_t* data_ptr = nullptr;
87 do {
88 code_ptr = code_cache->ReserveCode(Thread::Current(), kCodeArrSize);
89 data_ptr = code_cache->AddDataArray(Thread::Current(), data_arr, data_arr + kDataArrSize);
90 if (code_ptr != nullptr) {
91 code_bytes += kCodeArrSize;
92 }
93 if (data_ptr != nullptr) {
94 data_bytes += kDataArrSize;
95 }
96 } while (code_ptr != nullptr || data_ptr != nullptr);
97 // Make sure we added a reasonable amount
98 CHECK_GT(code_bytes, 0u);
99 CHECK_LE(code_bytes, kSize);
100 CHECK_GT(data_bytes, 0u);
101 CHECK_LE(data_bytes, kSize);
102 CHECK_GE(code_bytes + data_bytes, kSize * 4 / 5);
103}
104
105} // namespace jit
106} // namespace art