aboutsummaryrefslogtreecommitdiff
path: root/src/libANGLE/ResourceMap_unittest.cpp
blob: 26411fd2603e7901d89d9f6fa606a0448e93c36f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// Copyright 2018 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ResourceMap_unittest:
//   Unit tests for the ResourceMap template class.
//

#include <gtest/gtest.h>
#include <map>

#include "libANGLE/ResourceMap.h"

using namespace gl;

namespace gl
{
template <>
inline GLuint GetIDValue(int id)
{
    return id;
}
template <>
inline GLuint GetIDValue(unsigned int id)
{
    return id;
}
}  // namespace gl

namespace
{
// The resourceMap class uses a lock for "unsigned int" types to support this unit test.
using LocklessType = int;
using LockedType   = unsigned int;

template <typename T>
void AssignAndErase()
{
    constexpr size_t kSize = 300;
    ResourceMap<size_t, T> resourceMap;
    std::vector<size_t> objects(kSize, 1);
    for (size_t index = 0; index < kSize; ++index)
    {
        resourceMap.assign(index + 1, &objects[index]);
    }

    for (size_t index = 0; index < kSize; ++index)
    {
        size_t *found = nullptr;
        ASSERT_TRUE(resourceMap.erase(index + 1, &found));
        ASSERT_EQ(&objects[index], found);
    }

    ASSERT_TRUE(UnsafeResourceMapIter(resourceMap).empty());
}

// Tests assigning slots in the map and then deleting elements.
TEST(ResourceMapTest, AssignAndEraseLockless)
{
    AssignAndErase<LocklessType>();
}
// Tests assigning slots in the map and then deleting elements.
TEST(ResourceMapTest, AssignAndEraseLocked)
{
    AssignAndErase<LockedType>();
}

template <typename T>
void AssignAndClear()
{
    constexpr size_t kSize = 280;
    ResourceMap<size_t, T> resourceMap;
    std::vector<size_t> objects(kSize, 1);
    for (size_t index = 0; index < kSize; ++index)
    {
        resourceMap.assign(index + 1, &objects[index]);
    }

    resourceMap.clear();
    ASSERT_TRUE(UnsafeResourceMapIter(resourceMap).empty());
}

// Tests assigning slots in the map and then using clear() to free it.
TEST(ResourceMapTest, AssignAndClearLockless)
{
    AssignAndClear<LocklessType>();
}
// Tests assigning slots in the map and then using clear() to free it.
TEST(ResourceMapTest, AssignAndClearLocked)
{
    AssignAndClear<LockedType>();
}

template <typename T>
void BigGrowth()
{
    constexpr size_t kSize = 8;

    ResourceMap<size_t, T> resourceMap;
    std::vector<size_t> objects;

    for (size_t index = 0; index < kSize; ++index)
    {
        objects.push_back(index);
    }

    // Assign a large value.
    constexpr size_t kLargeIndex = 128;
    objects.push_back(kLargeIndex);

    for (size_t &object : objects)
    {
        resourceMap.assign(object, &object);
    }

    for (size_t object : objects)
    {
        size_t *found = nullptr;
        ASSERT_TRUE(resourceMap.erase(object, &found));
        ASSERT_EQ(object, *found);
    }

    ASSERT_TRUE(UnsafeResourceMapIter(resourceMap).empty());
}

// Tests growing a map more than double the size.
TEST(ResourceMapTest, BigGrowthLockless)
{
    BigGrowth<LocklessType>();
}
// Tests growing a map more than double the size.
TEST(ResourceMapTest, BigGrowthLocked)
{
    BigGrowth<LockedType>();
}

template <typename T>
void QueryUnassigned()
{
    constexpr size_t kSize        = 8;
    constexpr T kZero             = 0;
    constexpr T kIdInFlatRange    = 10;
    constexpr T kIdOutOfFlatRange = 500;

    ResourceMap<size_t, T> resourceMap;
    std::vector<size_t> objects;

    for (size_t index = 0; index < kSize; ++index)
    {
        objects.push_back(index);
    }

    ASSERT_FALSE(resourceMap.contains(kZero));
    ASSERT_EQ(nullptr, resourceMap.query(kZero));
    ASSERT_FALSE(resourceMap.contains(kIdOutOfFlatRange));
    ASSERT_EQ(nullptr, resourceMap.query(kIdOutOfFlatRange));

    for (size_t &object : objects)
    {
        resourceMap.assign(object, &object);
    }

    ASSERT_FALSE(UnsafeResourceMapIter(resourceMap).empty());

    for (size_t &object : objects)
    {
        ASSERT_TRUE(resourceMap.contains(object));
        ASSERT_EQ(&object, resourceMap.query(object));
    }

    ASSERT_FALSE(resourceMap.contains(kIdInFlatRange));
    ASSERT_EQ(nullptr, resourceMap.query(kIdInFlatRange));
    ASSERT_FALSE(resourceMap.contains(kIdOutOfFlatRange));
    ASSERT_EQ(nullptr, resourceMap.query(kIdOutOfFlatRange));

    for (size_t object : objects)
    {
        size_t *found = nullptr;
        ASSERT_TRUE(resourceMap.erase(object, &found));
        ASSERT_EQ(object, *found);
    }

    ASSERT_TRUE(UnsafeResourceMapIter(resourceMap).empty());

    ASSERT_FALSE(resourceMap.contains(kZero));
    ASSERT_EQ(nullptr, resourceMap.query(kZero));
    ASSERT_FALSE(resourceMap.contains(kIdOutOfFlatRange));
    ASSERT_EQ(nullptr, resourceMap.query(kIdOutOfFlatRange));
}

// Tests querying unassigned or erased values.
TEST(ResourceMapTest, QueryUnassignedLockless)
{
    QueryUnassigned<LocklessType>();
}
// Tests querying unassigned or erased values.
TEST(ResourceMapTest, QueryUnassignedLocked)
{
    QueryUnassigned<LockedType>();
}

void ConcurrentAccess(size_t iterations, size_t idCycleSize)
{
    if (std::is_same_v<ResourceMapMutex, angle::NoOpMutex>)
    {
        GTEST_SKIP() << "Test skipped: Locking is disabled in build.";
    }

    constexpr size_t kThreadCount = 13;

    ResourceMap<size_t, LockedType> resourceMap;

    std::array<std::thread, kThreadCount> threads;
    std::array<std::map<LockedType, size_t>, kThreadCount> insertedIds;

    for (size_t i = 0; i < kThreadCount; ++i)
    {
        threads[i] = std::thread([&, i]() {
            // Each thread manipulates a different set of ids.  The resource map guarantees that the
            // data structure itself is thread-safe, not accesses to the same id.
            for (size_t j = 0; j < iterations; ++j)
            {
                const LockedType id = (j % (idCycleSize / kThreadCount)) * kThreadCount + i;

                ASSERT_LE(id, 0xFFFFu);
                ASSERT_LE(j, 0xFFFFu);
                const size_t value = id | j << 16;

                size_t *valuePtr = reinterpret_cast<size_t *>(value);

                const size_t *queryResult = resourceMap.query(id);
                const bool containsResult = resourceMap.contains(id);

                const bool expectContains = insertedIds[i].count(id) > 0;
                if (expectContains)
                {
                    EXPECT_TRUE(containsResult);
                    const LockedType queryResultInt =
                        static_cast<LockedType>(reinterpret_cast<size_t>(queryResult) & 0xFFFF);
                    const size_t queryResultIteration = reinterpret_cast<size_t>(queryResult) >> 16;
                    EXPECT_EQ(queryResultInt, id);
                    EXPECT_LT(queryResultIteration, j);

                    size_t *erasedValue = nullptr;
                    const bool erased   = resourceMap.erase(id, &erasedValue);

                    EXPECT_TRUE(erased);
                    EXPECT_EQ(erasedValue, queryResult);

                    insertedIds[i].erase(id);
                }
                else
                {
                    EXPECT_FALSE(containsResult);
                    EXPECT_EQ(queryResult, nullptr);

                    resourceMap.assign(id, valuePtr);
                    EXPECT_TRUE(resourceMap.contains(id));

                    ASSERT_TRUE(insertedIds[i].count(id) == 0);
                    insertedIds[i][id] = value;
                }
            }
        });
    }

    for (size_t i = 0; i < kThreadCount; ++i)
    {
        threads[i].join();
    }

    // Verify that every value that is expected to be there is actually there
    std::map<size_t, size_t> allIds;
    size_t allIdsPrevSize = 0;

    for (size_t i = 0; i < kThreadCount; ++i)
    {
        // Merge all the sets together.  The sets are disjoint, which is verified by the ASSERT_EQ.
        allIds.insert(insertedIds[i].begin(), insertedIds[i].end());
        ASSERT_EQ(allIds.size(), allIdsPrevSize + insertedIds[i].size());
        allIdsPrevSize = allIds.size();

        // Make sure every id that is expected to be there is actually there.
        for (auto &idValue : insertedIds[i])
        {
            EXPECT_TRUE(resourceMap.contains(idValue.first));
            EXPECT_EQ(resourceMap.query(idValue.first), reinterpret_cast<size_t *>(idValue.second));
        }
    }

    // Verify that every value that is NOT expected to be there isn't actually there
    for (auto &idValue : UnsafeResourceMapIter(resourceMap))
    {
        EXPECT_TRUE(allIds.count(idValue.first) == 1);
        EXPECT_EQ(idValue.second, reinterpret_cast<size_t *>(allIds[idValue.first]));
    }

    resourceMap.clear();
}

// Tests that concurrent access to thread-safe resource maps works for small ids that are mostly in
// the flat map range.
TEST(ResourceMapTest, ConcurrentAccessSmallIds)
{
    ConcurrentAccess(50'000, 128);
}
// Tests that concurrent access to thread-safe resource maps works for a wider range of ids.
TEST(ResourceMapTest, ConcurrentAccessLargeIds)
{
    ConcurrentAccess(10'000, 20'000);
}
}  // anonymous namespace