File: cache_info.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (114 lines) | stat: -rw-r--r-- 3,997 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
/*
 * Copyright (C) 2022-2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/os_interface/linux/cache_info.h"

#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/helpers/common_types.h"
#include "shared/source/helpers/debug_helpers.h"

namespace NEO {

CacheInfo::~CacheInfo() {
    // skip the defaultRegion
    constexpr auto regionStart{toUnderlying(CacheRegion::region1)};
    constexpr auto regionEnd{toUnderlying(CacheRegion::count)};

    for (auto regionIndex{regionStart}; regionIndex < regionEnd; ++regionIndex) {
        const auto cacheLevel{getLevelForRegion(regionIndex)};

        if (reservedCacheRegionsSize[regionIndex]) {
            cacheReserve.freeCache(cacheLevel, toCacheRegion(regionIndex));
            reservedCacheRegionsSize[regionIndex] = 0u;
        }
    }
}

CacheRegion CacheInfo::reserveRegion(CacheLevel cacheLevel, size_t cacheReservationSize) {
    auto &limits{getLimitsForCacheLevel(cacheLevel)};

    if (limits.maxSize == 0U || limits.maxNumWays == 0U || limits.maxNumRegions == 0U) {
        return CacheRegion::none;
    }

    auto numWays{static_cast<uint16_t>((limits.maxNumWays * cacheReservationSize) / limits.maxSize)};
    if (debugManager.flags.ClosNumCacheWays.get() != -1) {
        numWays = debugManager.flags.ClosNumCacheWays.get();
        cacheReservationSize = (numWays * limits.maxSize) / limits.maxNumWays;
    }

    auto regionIndex = cacheReserve.reserveCache(cacheLevel, numWays);
    if (regionIndex == CacheRegion::none) {
        return CacheRegion::none;
    }
    DEBUG_BREAK_IF(regionIndex == CacheRegion::defaultRegion);
    DEBUG_BREAK_IF(regionIndex >= CacheRegion::count);
    reservedCacheRegionsSize[toUnderlying(regionIndex)] = cacheReservationSize;

    return regionIndex;
}

CacheRegion CacheInfo::freeRegion(CacheLevel cacheLevel, CacheRegion regionIndex) {
    if (regionIndex < CacheRegion::count && reservedCacheRegionsSize[toUnderlying(regionIndex)] > 0u) {
        reservedCacheRegionsSize[toUnderlying(regionIndex)] = 0u;
        return cacheReserve.freeCache(cacheLevel, regionIndex);
    }
    return CacheRegion::none;
}

bool CacheInfo::isRegionReserved(CacheRegion regionIndex, [[maybe_unused]] size_t expectedRegionSize) const {
    DEBUG_BREAK_IF(regionIndex == CacheRegion::defaultRegion);
    DEBUG_BREAK_IF(regionIndex >= CacheRegion::count);

    const auto cacheLevel{getLevelForRegion(regionIndex)};
    auto &limits{getLimitsForCacheLevel(cacheLevel)};

    if (limits.maxSize == 0U || limits.maxNumWays == 0U || limits.maxNumRegions == 0U) {
        return false;
    }

    if (regionIndex < CacheRegion::count && reservedCacheRegionsSize[toUnderlying(regionIndex)]) {
        if (debugManager.flags.ClosNumCacheWays.get() != -1) {
            auto numWays = debugManager.flags.ClosNumCacheWays.get();
            expectedRegionSize = (numWays * limits.maxSize) / limits.maxNumWays;
        }
        DEBUG_BREAK_IF(expectedRegionSize != reservedCacheRegionsSize[toUnderlying(regionIndex)]);
        return true;
    }
    return false;
}

bool CacheInfo::getRegion(size_t regionSize, CacheRegion regionIndex) {
    DEBUG_BREAK_IF(regionIndex >= CacheRegion::count);

    if (regionIndex == CacheRegion::defaultRegion) {
        return true;
    }

    if (!isRegionReserved(regionIndex, regionSize)) {
        const auto cacheLevel{getLevelForRegion(regionIndex)};
        auto regionIdx = reserveRegion(cacheLevel, regionSize);
        if (regionIdx == CacheRegion::none) {
            return false;
        }
        DEBUG_BREAK_IF(regionIdx != regionIndex);
    }
    return true;
}

const CacheReservationParameters &CacheInfo::getLimitsForCacheLevel(CacheLevel cacheLevel) const {
    switch (cacheLevel) {
    case CacheLevel::level2:
        return l2ReservationLimits;
    case CacheLevel::level3:
        return l3ReservationLimits;
    default:
        UNRECOVERABLE_IF(true);
    }
}

} // namespace NEO