File: timestamp_pool_allocator.cpp

package info (click to toggle)
intel-compute-runtime 25.35.35096.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,324 kB
  • sloc: cpp: 926,243; lisp: 3,433; sh: 715; makefile: 162; python: 21
file content (115 lines) | stat: -rw-r--r-- 4,022 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
/*
 * Copyright (C) 2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/utilities/timestamp_pool_allocator.h"

#include "shared/source/device/device.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/memory_manager/allocation_properties.h"
#include "shared/source/memory_manager/memory_manager.h"
#include "shared/source/os_interface/device_factory.h"
#include "shared/source/utilities/buffer_pool_allocator.inl"

namespace NEO {
TimestampPool::TimestampPool(Device *device, size_t poolSize)
    : BaseType(device->getMemoryManager(), nullptr), device(device) {
    DEBUG_BREAK_IF(device->getProductHelper().is2MBLocalMemAlignmentEnabled() &&
                   !isAligned(poolSize, MemoryConstants::pageSize2M));

    AllocationProperties properties{device->getRootDeviceIndex(),
                                    poolSize,
                                    AllocationType::gpuTimestampDeviceBuffer,
                                    device->getDeviceBitfield()};
    auto graphicsAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties);

    this->mainStorage.reset(graphicsAllocation);
    this->chunkAllocator.reset(new HeapAllocator(params.startingOffset, poolSize, MemoryConstants::pageSize, 0u));
    stackVec.push_back(graphicsAllocation);
    this->mtx = std::make_unique<std::mutex>();
}

TimestampPool::TimestampPool(TimestampPool &&pool) noexcept : BaseType(std::move(pool)) {
    mtx.reset(pool.mtx.release());
    this->stackVec = std::move(pool.stackVec);
    this->device = pool.device;
}

TimestampPool::~TimestampPool() {
    if (mainStorage) {
        device->getMemoryManager()->freeGraphicsMemory(mainStorage.release());
    }
}

SharedTimestampAllocation *TimestampPool::allocate(size_t size) {
    auto offset = static_cast<size_t>(this->chunkAllocator->allocate(size));
    if (offset == 0) {
        return nullptr;
    }
    return new SharedTimestampAllocation{this->mainStorage.get(), offset - params.startingOffset, size, mtx.get()};
}

const StackVec<GraphicsAllocation *, 1> &TimestampPool::getAllocationsVector() {
    return stackVec;
}

TimestampPoolAllocator::TimestampPoolAllocator(Device *device) : device(device) {}

bool TimestampPoolAllocator::isEnabled() const {
    if (NEO::debugManager.flags.EnableTimestampPoolAllocator.get() != -1) {
        return NEO::debugManager.flags.EnableTimestampPoolAllocator.get();
    }

    return NEO::DeviceFactory::isHwModeSelected() && device->getProductHelper().is2MBLocalMemAlignmentEnabled();
}

SharedTimestampAllocation *TimestampPoolAllocator::requestGraphicsAllocationForTimestamp(size_t size) {
    if (size > maxAllocationSize) {
        return nullptr;
    }

    std::lock_guard<std::mutex> lock(allocatorMtx);

    if (bufferPools.empty()) {
        addNewBufferPool(TimestampPool(device, alignToPoolSize(defaultPoolSize)));
    }

    auto allocFromPool = allocateFromPools(size);
    if (allocFromPool != nullptr) {
        return allocFromPool;
    }

    this->drain();

    allocFromPool = allocateFromPools(size);
    if (allocFromPool != nullptr) {
        return allocFromPool;
    }

    addNewBufferPool(TimestampPool(device, alignToPoolSize(defaultPoolSize)));
    return allocateFromPools(size);
}

void TimestampPoolAllocator::freeSharedTimestampAllocation(SharedTimestampAllocation *sharedTimestampAllocation) {
    std::unique_lock lock(allocatorMtx);
    tryFreeFromPoolBuffer(sharedTimestampAllocation->getGraphicsAllocation(), sharedTimestampAllocation->getOffset(), sharedTimestampAllocation->getSize());
    delete sharedTimestampAllocation;
}

SharedTimestampAllocation *TimestampPoolAllocator::allocateFromPools(size_t size) {
    for (auto &pool : bufferPools) {
        if (auto allocation = pool.allocate(size)) {
            return allocation;
        }
    }
    return nullptr;
}

size_t TimestampPoolAllocator::alignToPoolSize(size_t size) const {
    return alignUp(size, poolAlignment);
}

} // namespace NEO