1 /*
2 * Copyright (C) 2013 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 <sys/mman.h>
18 #include <memory>
19 #include <sys/sysinfo.h>
20
21 #include <gtest/gtest.h>
22
23 #include <ion/ion.h>
24 #include "ion_test_fixture.h"
25
26 class Allocate : public IonTest {};
27
TEST_F(Allocate,Allocate)28 TEST_F(Allocate, Allocate) {
29 static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
30 for (const auto& heap : ion_heaps) {
31 for (size_t size : allocationSizes) {
32 SCOPED_TRACE(::testing::Message()
33 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
34 SCOPED_TRACE(::testing::Message() << "size " << size);
35 int fd;
36 ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &fd));
37 ASSERT_TRUE(fd != 0);
38 ASSERT_EQ(close(fd), 0); // free the buffer
39 }
40 }
41 }
42
TEST_F(Allocate,AllocateCached)43 TEST_F(Allocate, AllocateCached) {
44 static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
45 for (const auto& heap : ion_heaps) {
46 for (size_t size : allocationSizes) {
47 SCOPED_TRACE(::testing::Message()
48 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
49 SCOPED_TRACE(::testing::Message() << "size " << size);
50 int fd;
51 ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), ION_FLAG_CACHED, &fd));
52 ASSERT_TRUE(fd != 0);
53 ASSERT_EQ(close(fd), 0); // free the buffer
54 }
55 }
56 }
57
TEST_F(Allocate,AllocateCachedNeedsSync)58 TEST_F(Allocate, AllocateCachedNeedsSync) {
59 static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
60 for (const auto& heap : ion_heaps) {
61 for (size_t size : allocationSizes) {
62 SCOPED_TRACE(::testing::Message()
63 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
64 SCOPED_TRACE(::testing::Message() << "size " << size);
65 int fd;
66 ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id),
67 ION_FLAG_CACHED_NEEDS_SYNC, &fd));
68 ASSERT_TRUE(fd != 0);
69 ASSERT_EQ(close(fd), 0); // free the buffer
70 }
71 }
72 }
73
TEST_F(Allocate,RepeatedAllocate)74 TEST_F(Allocate, RepeatedAllocate) {
75 static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
76 for (const auto& heap : ion_heaps) {
77 for (size_t size : allocationSizes) {
78 SCOPED_TRACE(::testing::Message()
79 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
80 SCOPED_TRACE(::testing::Message() << "size " << size);
81 int fd;
82
83 for (unsigned int i = 0; i < 1024; i++) {
84 SCOPED_TRACE(::testing::Message() << "iteration " << i);
85 ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &fd));
86 ASSERT_TRUE(fd != 0);
87 ASSERT_EQ(close(fd), 0); // free the buffer
88 }
89 }
90 }
91 }
92
TEST_F(Allocate,Large)93 TEST_F(Allocate, Large) {
94 struct sysinfo si;
95 ASSERT_TRUE(sysinfo(&si) != -1);
96
97 auto totalRamSize = si.totalram * si.mem_unit;
98
99 for (const auto& heap : ion_heaps) {
100 SCOPED_TRACE(::testing::Message()
101 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
102 int fd;
103 ASSERT_EQ(-ENOMEM,
104 ion_alloc_fd(ionfd, totalRamSize, 0, (1 << heap.heap_id), 0, &fd));
105 }
106 }
107
108 // Make sure all heaps always return zeroed pages
TEST_F(Allocate,Zeroed)109 TEST_F(Allocate, Zeroed) {
110 auto zeroes_ptr = std::make_unique<char[]>(4096);
111
112 for (const auto& heap : ion_heaps) {
113 SCOPED_TRACE(::testing::Message()
114 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
115 int fds[16];
116 for (unsigned int i = 0; i < 16; i++) {
117 int map_fd = -1;
118
119 ASSERT_EQ(0, ion_alloc_fd(ionfd, 4096, 0, (1 << heap.heap_id), 0, &map_fd));
120 ASSERT_GE(map_fd, 0);
121
122 void* ptr = NULL;
123 ptr = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED, map_fd, 0);
124 ASSERT_TRUE(ptr != NULL);
125
126 memset(ptr, 0xaa, 4096);
127
128 ASSERT_EQ(0, munmap(ptr, 4096));
129 fds[i] = map_fd;
130 }
131
132 for (unsigned int i = 0; i < 16; i++) {
133 ASSERT_EQ(0, close(fds[i]));
134 }
135
136 int new_ionfd = ion_open();
137 int map_fd = -1;
138
139 ASSERT_EQ(0, ion_alloc_fd(new_ionfd, 4096, 0, (1 << heap.heap_id), 0, &map_fd));
140 ASSERT_GE(map_fd, 0);
141
142 void* ptr = NULL;
143 ptr = mmap(NULL, 4096, PROT_READ, MAP_SHARED, map_fd, 0);
144 ASSERT_TRUE(ptr != NULL);
145
146 ASSERT_EQ(0, memcmp(ptr, zeroes_ptr.get(), 4096));
147
148 ASSERT_EQ(0, munmap(ptr, 4096));
149 ASSERT_EQ(0, close(map_fd));
150 ASSERT_EQ(0, ion_close(new_ionfd));
151 }
152 }
153