File: drm_cache_info_tests.cpp

package info (click to toggle)
intel-compute-runtime-legacy 24.35.30872.40-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 73,292 kB
  • sloc: cpp: 826,355; lisp: 3,686; sh: 677; makefile: 148; python: 21
file content (166 lines) | stat: -rw-r--r-- 8,139 bytes parent folder | download
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
/*
 * Copyright (C) 2022-2024 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/helpers/constants.h"
#include "shared/source/os_interface/linux/cache_info.h"
#include "shared/source/os_interface/product_helper.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/helpers/default_hw_info.h"
#include "shared/test/common/libult/linux/drm_query_mock.h"
#include "shared/test/common/mocks/mock_execution_environment.h"
#include "shared/test/common/os_interface/linux/drm_mock_cache_info.h"

#include "gtest/gtest.h"

using namespace NEO;

TEST(DrmCacheInfoTest, givenCacheRegionsExistsWhenCallingSetUpCacheInfoThenCacheInfoIsCreatedAndReturnsMaxReservationCacheLimits) {
    auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
    DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);

    auto &productHelper = executionEnvironment->rootDeviceEnvironments[0]->getHelper<ProductHelper>();

    drm.setupCacheInfo(*defaultHwInfo.get());

    auto cacheInfo = drm.getCacheInfo();
    EXPECT_NE(nullptr, cacheInfo);

    if (productHelper.getNumCacheRegions() == 0) {
        EXPECT_EQ(0u, cacheInfo->getMaxReservationCacheSize());
        EXPECT_EQ(0u, cacheInfo->getMaxReservationNumCacheRegions());
        EXPECT_EQ(0u, cacheInfo->getMaxReservationNumWays());
    } else {
        const GT_SYSTEM_INFO *gtSysInfo = &defaultHwInfo->gtSystemInfo;
        constexpr uint16_t maxNumWays = 32;
        constexpr uint16_t globalReservationLimit = 16;
        constexpr uint16_t clientReservationLimit = 8;
        constexpr uint16_t maxReservationNumWays = std::min(globalReservationLimit, clientReservationLimit);
        const size_t totalCacheSize = gtSysInfo->L3CacheSizeInKb * MemoryConstants::kiloByte;
        const size_t maxReservationCacheSize = (totalCacheSize * maxReservationNumWays) / maxNumWays;
        const size_t maxReservationNumCacheRegions = productHelper.getNumCacheRegions() - 1;

        EXPECT_EQ(maxReservationCacheSize, cacheInfo->getMaxReservationCacheSize());
        EXPECT_EQ(maxReservationNumCacheRegions, cacheInfo->getMaxReservationNumCacheRegions());
        EXPECT_EQ(maxReservationNumWays, cacheInfo->getMaxReservationNumWays());
    }
}

TEST(DrmCacheInfoTest, givenDebugFlagSetWhenCallingSetUpCacheInfoThenCacheInfoIsCreatedWithoutValues) {
    DebugManagerStateRestore restorer;
    debugManager.flags.ClosEnabled.set(0);
    auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
    DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);

    drm.setupCacheInfo(*defaultHwInfo.get());
    EXPECT_NE(nullptr, drm.getCacheInfo());
    auto cacheInfo = drm.getCacheInfo();

    EXPECT_EQ(0u, cacheInfo->getMaxReservationCacheSize());
    EXPECT_EQ(0u, cacheInfo->getMaxReservationNumCacheRegions());
    EXPECT_EQ(0u, cacheInfo->getMaxReservationNumWays());
}

TEST(DrmCacheInfoTest, givenCacheInfoCreatedWhenGetCacheRegionSucceedsToReserveCacheRegionThenReturnTrue) {
    auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();

    DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
    CacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32);
    size_t cacheReservationSize = cacheInfo.getMaxReservationCacheSize();

    EXPECT_TRUE(cacheInfo.getCacheRegion(cacheReservationSize, CacheRegion::region1));

    EXPECT_EQ(CacheRegion::region1, cacheInfo.freeCacheRegion(CacheRegion::region1));
}

TEST(DrmCacheInfoTest, givenCacheInfoCreatedWhenGetCacheRegionFailsToReserveCacheRegionThenReturnFalse) {
    auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();

    DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
    CacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32);
    size_t cacheReservationSize = cacheInfo.getMaxReservationCacheSize();

    drm.context.closIndex = 0xFFFF;
    EXPECT_FALSE(cacheInfo.getCacheRegion(cacheReservationSize, CacheRegion::region1));

    EXPECT_EQ(CacheRegion::none, cacheInfo.freeCacheRegion(CacheRegion::region1));
}

TEST(DrmCacheInfoTest, givenCacheInfoWithReservedCacheRegionWhenGetCacheRegionIsCalledForReservedCacheRegionThenReturnTrue) {
    auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();

    DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
    CacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32);
    size_t cacheReservationSize = cacheInfo.getMaxReservationCacheSize();

    EXPECT_EQ(CacheRegion::region1, cacheInfo.reserveCacheRegion(cacheReservationSize));

    EXPECT_TRUE(cacheInfo.getCacheRegion(cacheReservationSize, CacheRegion::region1));

    EXPECT_EQ(CacheRegion::region1, cacheInfo.freeCacheRegion(CacheRegion::region1));
}

TEST(DrmCacheInfoTest, givenCacheInfoCreatedWhenGetCacheRegionIsCalledForReservableRegionsWithRegionSizesInverselyProportionalToNumCacheRegionsThenReturnTrue) {
    auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();

    DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
    CacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32);
    size_t regionSize = cacheInfo.getMaxReservationCacheSize() / cacheInfo.getMaxReservationNumCacheRegions();

    EXPECT_TRUE(cacheInfo.getCacheRegion(regionSize, CacheRegion::region1));
    EXPECT_TRUE(cacheInfo.getCacheRegion(regionSize, CacheRegion::region2));

    EXPECT_EQ(CacheRegion::region2, cacheInfo.freeCacheRegion(CacheRegion::region2));
    EXPECT_EQ(CacheRegion::region1, cacheInfo.freeCacheRegion(CacheRegion::region1));
}

TEST(DrmCacheInfoTest, givenCacheInfoWhenSpecificNumCacheWaysIsRequestedThenReserveAppropriateCacheSize) {
    constexpr uint16_t maxNumCacheWays = 32;

    DebugManagerStateRestore restorer;
    debugManager.flags.ClosNumCacheWays.set(maxNumCacheWays / 2);

    auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();

    DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
    MockCacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, maxNumCacheWays);
    size_t maxReservationCacheSize = cacheInfo.getMaxReservationCacheSize();

    EXPECT_EQ(CacheRegion::region1, cacheInfo.reserveCacheRegion(maxReservationCacheSize));
    EXPECT_TRUE(cacheInfo.isRegionReserved(CacheRegion::region1, maxReservationCacheSize));

    auto cacheRegion = cacheInfo.cacheRegionsReserved.begin();
    EXPECT_EQ(CacheRegion::region1, cacheRegion->first);
    EXPECT_EQ(maxReservationCacheSize / 2, cacheRegion->second);

    EXPECT_EQ(CacheRegion::region1, cacheInfo.freeCacheRegion(CacheRegion::region1));
}

TEST(DrmCacheInfoTest, givenCacheInfoWhenNumCacheWaysIsExceededThenDontReserveCacheRegion) {
    constexpr uint16_t maxNumCacheWays = 32;

    auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();

    DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
    MockCacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, maxNumCacheWays);
    size_t maxReservationCacheSize = cacheInfo.getMaxReservationCacheSize();

    EXPECT_EQ(CacheRegion::region1, cacheInfo.reserveCacheRegion(maxReservationCacheSize));
    EXPECT_TRUE(cacheInfo.isRegionReserved(CacheRegion::region1, maxReservationCacheSize));

    EXPECT_EQ(CacheRegion::none, cacheInfo.reserveCacheRegion(maxReservationCacheSize));
    EXPECT_FALSE(cacheInfo.isRegionReserved(CacheRegion::region2, maxReservationCacheSize));
}

TEST(DrmCacheInfoTest, givenCacheInfoCreatedWhenFreeCacheRegionIsCalledForNonReservedRegionThenItFails) {
    auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();

    DrmQueryMock drm(*executionEnvironment->rootDeviceEnvironments[0]);
    MockCacheInfo cacheInfo(*drm.getIoctlHelper(), 32 * MemoryConstants::kiloByte, 2, 32);

    cacheInfo.cacheRegionsReserved.insert({CacheRegion::region1, MemoryConstants::kiloByte});
    EXPECT_EQ(CacheRegion::none, cacheInfo.freeCacheRegion(CacheRegion::region1));
}