Avoid manual memory management.

Change-Id: Id29d9ec366e338d519aad5353a15a44ecf5998e4
This commit is contained in:
Piotr Fusik
2019-02-11 16:49:23 +01:00
committed by sys_ocldev
parent 5e8fb19e5d
commit 6882cf09c1
10 changed files with 41 additions and 87 deletions

View File

@@ -7,6 +7,7 @@
#pragma once #pragma once
#include "runtime/command_stream/command_stream_receiver.h" #include "runtime/command_stream/command_stream_receiver.h"
#include <memory>
namespace OCLRT { namespace OCLRT {
@@ -18,7 +19,6 @@ class CommandStreamReceiverWithAUBDump : public BaseCSR {
public: public:
using BaseCSR::createMemoryManager; using BaseCSR::createMemoryManager;
CommandStreamReceiverWithAUBDump(const HardwareInfo &hwInfoIn, const std::string &baseName, ExecutionEnvironment &executionEnvironment); CommandStreamReceiverWithAUBDump(const HardwareInfo &hwInfoIn, const std::string &baseName, ExecutionEnvironment &executionEnvironment);
~CommandStreamReceiverWithAUBDump() override;
CommandStreamReceiverWithAUBDump(const CommandStreamReceiverWithAUBDump &) = delete; CommandStreamReceiverWithAUBDump(const CommandStreamReceiverWithAUBDump &) = delete;
CommandStreamReceiverWithAUBDump &operator=(const CommandStreamReceiverWithAUBDump &) = delete; CommandStreamReceiverWithAUBDump &operator=(const CommandStreamReceiverWithAUBDump &) = delete;
@@ -29,7 +29,7 @@ class CommandStreamReceiverWithAUBDump : public BaseCSR {
void activateAubSubCapture(const MultiDispatchInfo &dispatchInfo) override; void activateAubSubCapture(const MultiDispatchInfo &dispatchInfo) override;
void setupContext(OsContext &osContext) override; void setupContext(OsContext &osContext) override;
CommandStreamReceiver *aubCSR = nullptr; std::unique_ptr<CommandStreamReceiver> aubCSR;
}; };
} // namespace OCLRT } // namespace OCLRT

View File

@@ -19,14 +19,7 @@ CommandStreamReceiverWithAUBDump<BaseCSR>::CommandStreamReceiverWithAUBDump(cons
bool createAubCsr = !executionEnvironment.aubCenter || executionEnvironment.aubCenter->getAubManager() == nullptr; bool createAubCsr = !executionEnvironment.aubCenter || executionEnvironment.aubCenter->getAubManager() == nullptr;
if (createAubCsr) { if (createAubCsr) {
aubCSR = AUBCommandStreamReceiver::create(hwInfoIn, baseName, false, executionEnvironment); aubCSR.reset(AUBCommandStreamReceiver::create(hwInfoIn, baseName, false, executionEnvironment));
}
}
template <typename BaseCSR>
CommandStreamReceiverWithAUBDump<BaseCSR>::~CommandStreamReceiverWithAUBDump() {
if (aubCSR) {
delete aubCSR;
} }
} }

View File

@@ -13,9 +13,10 @@
#include "runtime/gmm_helper/gmm_helper.h" #include "runtime/gmm_helper/gmm_helper.h"
#include "drm/i915_drm.h" #include "drm/i915_drm.h"
#include "runtime/os_interface/debug_settings_manager.h" #include "runtime/os_interface/debug_settings_manager.h"
#include <stdio.h>
#include <limits.h>
#include <array> #include <array>
#include <cstdio>
#include <cstring>
#include <memory>
namespace OCLRT { namespace OCLRT {
@@ -49,28 +50,17 @@ void Drm::closeDevice(int32_t deviceOrdinal) {
bool Drm::isi915Version(int fd) { bool Drm::isi915Version(int fd) {
drm_version_t version = {}; drm_version_t version = {};
int ret; char name[5] = {};
version.name = name;
version.name = reinterpret_cast<char *>(calloc(1, 5));
version.name_len = 5; version.name_len = 5;
ret = ::ioctl(fd, DRM_IOCTL_VERSION, &version); int ret = ::ioctl(fd, DRM_IOCTL_VERSION, &version);
if (ret) { if (ret) {
free(version.name);
return false; return false;
} }
version.name[4] = '\0'; name[4] = '\0';
return strcmp(name, "i915") == 0;
if (!strcmp(version.name, "i915")) {
free(version.name);
return true;
}
free(version.name);
return false;
} }
int Drm::getDeviceFd(const int devType) { int Drm::getDeviceFd(const int devType) {
@@ -130,18 +120,17 @@ Drm *Drm::create(int32_t deviceOrdinal) {
return nullptr; return nullptr;
} }
Drm *drmObject = nullptr; std::unique_ptr<Drm> drmObject;
if (DebugManager.flags.EnableNullHardware.get() == true) { if (DebugManager.flags.EnableNullHardware.get() == true) {
drmObject = new DrmNullDevice(fd); drmObject.reset(new DrmNullDevice(fd));
} else { } else {
drmObject = new Drm(fd); drmObject.reset(new Drm(fd));
} }
// Get HW version (I915_drm.h) // Get HW version (I915_drm.h)
int ret = drmObject->getDeviceID(drmObject->deviceId); int ret = drmObject->getDeviceID(drmObject->deviceId);
if (ret != 0) { if (ret != 0) {
printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "FATAL: Cannot query device ID parameter!\n"); printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "FATAL: Cannot query device ID parameter!\n");
delete drmObject;
return nullptr; return nullptr;
} }
@@ -149,7 +138,6 @@ Drm *Drm::create(int32_t deviceOrdinal) {
ret = drmObject->getDeviceRevID(drmObject->revisionId); ret = drmObject->getDeviceRevID(drmObject->revisionId);
if (ret != 0) { if (ret != 0) {
printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "FATAL: Cannot query device Rev ID parameter!\n"); printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "FATAL: Cannot query device Rev ID parameter!\n");
delete drmObject;
return nullptr; return nullptr;
} }
@@ -170,7 +158,6 @@ Drm *Drm::create(int32_t deviceOrdinal) {
} else { } else {
printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr,
"FATAL: Unknown device: deviceId: %04x, revisionId: %04x\n", drmObject->deviceId, drmObject->revisionId); "FATAL: Unknown device: deviceId: %04x, revisionId: %04x\n", drmObject->deviceId, drmObject->revisionId);
delete drmObject;
return nullptr; return nullptr;
} }
@@ -179,14 +166,12 @@ Drm *Drm::create(int32_t deviceOrdinal) {
ret = drmObject->getExecSoftPin(hasExecSoftPin); ret = drmObject->getExecSoftPin(hasExecSoftPin);
if (ret != 0) { if (ret != 0) {
printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "FATAL: Cannot query Soft Pin parameter!\n"); printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "FATAL: Cannot query Soft Pin parameter!\n");
delete drmObject;
return nullptr; return nullptr;
} }
if (!hasExecSoftPin) { if (!hasExecSoftPin) {
printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s",
"FATAL: Device doesn't support Soft-Pin but this is required.\n"); "FATAL: Device doesn't support Soft-Pin but this is required.\n");
delete drmObject;
return nullptr; return nullptr;
} }
@@ -201,7 +186,7 @@ Drm *Drm::create(int32_t deviceOrdinal) {
printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "WARNING: Failed to request OCL Turbo Boost\n"); printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s", "WARNING: Failed to request OCL Turbo Boost\n");
} }
drms[deviceOrdinal % drms.size()] = drmObject; drms[deviceOrdinal % drms.size()] = drmObject.release();
return drms[deviceOrdinal % drms.size()]; return drms[deviceOrdinal % drms.size()];
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2017-2018 Intel Corporation * Copyright (C) 2017-2019 Intel Corporation
* *
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
* *
@@ -39,6 +39,8 @@ class Drm {
public: public:
static Drm *get(int32_t deviceOrdinal); static Drm *get(int32_t deviceOrdinal);
virtual ~Drm();
virtual int ioctl(unsigned long request, void *arg); virtual int ioctl(unsigned long request, void *arg);
int getDeviceID(int &devId); int getDeviceID(int &devId);
@@ -74,7 +76,6 @@ class Drm {
int revisionId; int revisionId;
GTTYPE eGtType; GTTYPE eGtType;
Drm(int fd) : fd(fd), deviceId(0), revisionId(0), eGtType(GTTYPE_UNDEFINED) {} Drm(int fd) : fd(fd), deviceId(0), revisionId(0), eGtType(GTTYPE_UNDEFINED) {}
virtual ~Drm();
static bool isi915Version(int fd); static bool isi915Version(int fd);
static int getDeviceFd(const int devType); static int getDeviceFd(const int devType);

View File

@@ -131,10 +131,10 @@ struct AUBHelloWorldIntegrateTest : public HelloWorldFixture<AUBHelloWorldFixtur
void writeMemory(GraphicsAllocation *allocation) { void writeMemory(GraphicsAllocation *allocation) {
AUBCommandStreamReceiverHw<FamilyType> *aubCsr = nullptr; AUBCommandStreamReceiverHw<FamilyType> *aubCsr = nullptr;
if (testMode == TestMode::AubTests) { if (testMode == TestMode::AubTests) {
aubCsr = reinterpret_cast<AUBCommandStreamReceiverHw<FamilyType> *>(pCommandStreamReceiver); aubCsr = static_cast<AUBCommandStreamReceiverHw<FamilyType> *>(pCommandStreamReceiver);
} else if (testMode == TestMode::AubTestsWithTbx) { } else if (testMode == TestMode::AubTestsWithTbx) {
auto tbxWithAubCsr = reinterpret_cast<CommandStreamReceiverWithAUBDump<TbxCommandStreamReceiverHw<FamilyType>> *>(pCommandStreamReceiver); auto tbxWithAubCsr = static_cast<CommandStreamReceiverWithAUBDump<TbxCommandStreamReceiverHw<FamilyType>> *>(pCommandStreamReceiver);
aubCsr = reinterpret_cast<AUBCommandStreamReceiverHw<FamilyType> *>(tbxWithAubCsr->aubCSR); aubCsr = static_cast<AUBCommandStreamReceiverHw<FamilyType> *>(tbxWithAubCsr->aubCSR.get());
tbxWithAubCsr->writeMemory(*allocation); tbxWithAubCsr->writeMemory(*allocation);
} }

View File

@@ -28,6 +28,15 @@ class AUBCommandStreamFixture : public CommandStreamFixture {
virtual void SetUp(CommandQueue *pCommandQueue); virtual void SetUp(CommandQueue *pCommandQueue);
virtual void TearDown(); virtual void TearDown();
template <typename FamilyType>
AUBCommandStreamReceiverHw<FamilyType> *getAubCsr() const {
CommandStreamReceiver *csr = pCommandStreamReceiver;
if (testMode == TestMode::AubTestsWithTbx) {
csr = static_cast<CommandStreamReceiverWithAUBDump<TbxCommandStreamReceiverHw<FamilyType>> *>(csr)->aubCSR.get();
}
return static_cast<AUBCommandStreamReceiverHw<FamilyType> *>(csr);
}
template <typename FamilyType> template <typename FamilyType>
void expectMMIO(uint32_t mmioRegister, uint32_t expectedValue) { void expectMMIO(uint32_t mmioRegister, uint32_t expectedValue) {
using AubMemDump::CmdServicesMemTraceRegisterCompare; using AubMemDump::CmdServicesMemTraceRegisterCompare;
@@ -44,45 +53,18 @@ class AUBCommandStreamFixture : public CommandStreamFixture {
header.readMaskHigh = 0xffffffff; header.readMaskHigh = 0xffffffff;
header.dwordCount = (sizeof(header) / sizeof(uint32_t)) - 1; header.dwordCount = (sizeof(header) / sizeof(uint32_t)) - 1;
CommandStreamReceiver *csr = pCommandStreamReceiver;
if (testMode == TestMode::AubTestsWithTbx) {
csr = reinterpret_cast<CommandStreamReceiverWithAUBDump<TbxCommandStreamReceiverHw<FamilyType>> *>(pCommandStreamReceiver)->aubCSR;
}
// Write our pseudo-op to the AUB file // Write our pseudo-op to the AUB file
auto aubCsr = reinterpret_cast<AUBCommandStreamReceiverHw<FamilyType> *>(csr); getAubCsr<FamilyType>()->getAubStream()->fileHandle.write(reinterpret_cast<char *>(&header), sizeof(header));
aubCsr->getAubStream()->fileHandle.write(reinterpret_cast<char *>(&header), sizeof(header));
} }
template <typename FamilyType> template <typename FamilyType>
void expectMemory(void *gfxAddress, const void *srcAddress, size_t length) { void expectMemory(void *gfxAddress, const void *srcAddress, size_t length) {
CommandStreamReceiver *csr = pCommandStreamReceiver; getAubCsr<FamilyType>()->expectMemoryEqual(gfxAddress, srcAddress, length);
if (testMode == TestMode::AubTestsWithTbx) {
csr = reinterpret_cast<CommandStreamReceiverWithAUBDump<TbxCommandStreamReceiverHw<FamilyType>> *>(pCommandStreamReceiver)->aubCSR;
}
auto aubCsr = reinterpret_cast<AUBCommandStreamReceiverHw<FamilyType> *>(csr);
aubCsr->expectMemoryEqual(gfxAddress, srcAddress, length);
} }
template <typename FamilyType> template <typename FamilyType>
void expectMemoryNotEqual(void *gfxAddress, const void *srcAddress, size_t length) { void expectMemoryNotEqual(void *gfxAddress, const void *srcAddress, size_t length) {
CommandStreamReceiver *csr = pCommandStreamReceiver; getAubCsr<FamilyType>()->expectMemoryNotEqual(gfxAddress, srcAddress, length);
if (testMode == TestMode::AubTestsWithTbx) {
csr = reinterpret_cast<CommandStreamReceiverWithAUBDump<TbxCommandStreamReceiverHw<FamilyType>> *>(pCommandStreamReceiver)->aubCSR;
}
auto aubCsr = reinterpret_cast<AUBCommandStreamReceiverHw<FamilyType> *>(csr);
aubCsr->expectMemoryNotEqual(gfxAddress, srcAddress, length);
}
template <typename FamilyType>
AUBCommandStreamReceiverHw<FamilyType> *getAubCsr() {
CommandStreamReceiver *csr = pCommandStreamReceiver;
if (testMode == TestMode::AubTestsWithTbx) {
csr = reinterpret_cast<CommandStreamReceiverWithAUBDump<TbxCommandStreamReceiverHw<FamilyType>> *>(pCommandStreamReceiver)->aubCSR;
}
return reinterpret_cast<AUBCommandStreamReceiverHw<FamilyType> *>(csr);
} }
template <typename FamilyType> template <typename FamilyType>

View File

@@ -59,10 +59,10 @@ class AUBFixture : public CommandQueueHwFixture {
AUBCommandStreamReceiverHw<FamilyType> *getAubCsr() { AUBCommandStreamReceiverHw<FamilyType> *getAubCsr() {
AUBCommandStreamReceiverHw<FamilyType> *aubCsr = nullptr; AUBCommandStreamReceiverHw<FamilyType> *aubCsr = nullptr;
if (testMode == TestMode::AubTests) { if (testMode == TestMode::AubTests) {
aubCsr = reinterpret_cast<AUBCommandStreamReceiverHw<FamilyType> *>(csr); aubCsr = static_cast<AUBCommandStreamReceiverHw<FamilyType> *>(csr);
} else if (testMode == TestMode::AubTestsWithTbx) { } else if (testMode == TestMode::AubTestsWithTbx) {
aubCsr = reinterpret_cast<AUBCommandStreamReceiverHw<FamilyType> *>( aubCsr = static_cast<AUBCommandStreamReceiverHw<FamilyType> *>(
reinterpret_cast<CommandStreamReceiverWithAUBDump<TbxCommandStreamReceiverHw<FamilyType>> *>(csr)->aubCSR); static_cast<CommandStreamReceiverWithAUBDump<TbxCommandStreamReceiverHw<FamilyType>> *>(csr)->aubCSR.get());
} }
return aubCsr; return aubCsr;
} }

View File

@@ -91,17 +91,10 @@ struct MyMockCsr : UltCommandStreamReceiver<DEFAULT_TEST_FAMILY_NAME> {
template <typename BaseCSR> template <typename BaseCSR>
struct MyMockCsrWithAubDump : CommandStreamReceiverWithAUBDump<BaseCSR> { struct MyMockCsrWithAubDump : CommandStreamReceiverWithAUBDump<BaseCSR> {
MyMockCsrWithAubDump<BaseCSR>(const HardwareInfo &hwInfoIn, bool createAubCSR, ExecutionEnvironment &executionEnvironment) : CommandStreamReceiverWithAUBDump<BaseCSR>(hwInfoIn, "aubfile", executionEnvironment) { MyMockCsrWithAubDump<BaseCSR>(const HardwareInfo &hwInfoIn, bool createAubCSR, ExecutionEnvironment &executionEnvironment) : CommandStreamReceiverWithAUBDump<BaseCSR>(hwInfoIn, "aubfile", executionEnvironment) {
if (this->aubCSR != nullptr) { this->aubCSR.reset(createAubCSR ? new MyMockCsr(hwInfoIn, executionEnvironment) : nullptr);
delete this->aubCSR;
this->aubCSR = nullptr;
}
if (createAubCSR) {
// overwrite with mock
this->aubCSR = new MyMockCsr(hwInfoIn, executionEnvironment);
}
} }
MyMockCsr &getAubMockCsr() { MyMockCsr &getAubMockCsr() const {
return static_cast<MyMockCsr &>(*this->aubCSR); return static_cast<MyMockCsr &>(*this->aubCSR);
} }
}; };

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2018 Intel Corporation * Copyright (C) 2018-2019 Intel Corporation
* *
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
* *
@@ -51,7 +51,7 @@ HWTEST_F(DeviceCommandStreamLeaksTest, givenDefaultDrmCsrWithAubDumWhenItIsCreat
*executionEnvironment)); *executionEnvironment));
auto drmCsrWithAubDump = (CommandStreamReceiverWithAUBDump<DrmCommandStreamReceiver<FamilyType>> *)ptr.get(); auto drmCsrWithAubDump = (CommandStreamReceiverWithAUBDump<DrmCommandStreamReceiver<FamilyType>> *)ptr.get();
EXPECT_EQ(drmCsrWithAubDump->peekGemCloseWorkerOperationMode(), gemCloseWorkerMode::gemCloseWorkerActive); EXPECT_EQ(drmCsrWithAubDump->peekGemCloseWorkerOperationMode(), gemCloseWorkerMode::gemCloseWorkerActive);
auto aubCSR = static_cast<CommandStreamReceiverWithAUBDump<DrmCommandStreamReceiver<FamilyType>> *>(ptr.get())->aubCSR; auto aubCSR = static_cast<CommandStreamReceiverWithAUBDump<DrmCommandStreamReceiver<FamilyType>> *>(ptr.get())->aubCSR.get();
EXPECT_NE(nullptr, aubCSR); EXPECT_NE(nullptr, aubCSR);
} }

View File

@@ -169,7 +169,7 @@ TEST_F(DeviceCommandStreamTest, CreateWddmCSRWithAubDump) {
EXPECT_NE(nullptr, csr); EXPECT_NE(nullptr, csr);
auto wddmFromCsr = csr->peekWddm(); auto wddmFromCsr = csr->peekWddm();
EXPECT_NE(nullptr, wddmFromCsr); EXPECT_NE(nullptr, wddmFromCsr);
auto aubCSR = static_cast<CommandStreamReceiverWithAUBDump<WddmCommandStreamReceiver<DEFAULT_TEST_FAMILY_NAME>> *>(csr.get())->aubCSR; auto aubCSR = static_cast<CommandStreamReceiverWithAUBDump<WddmCommandStreamReceiver<DEFAULT_TEST_FAMILY_NAME>> *>(csr.get())->aubCSR.get();
EXPECT_NE(nullptr, aubCSR); EXPECT_NE(nullptr, aubCSR);
} }