| 1 |
/* |
| 2 |
* Copyright 2024 Google LLC |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "GfxStreamVulkanConnection.h" |
| 7 |
|
| 8 |
#include <string> |
| 9 |
|
| 10 |
#include "util/detect_os.h" |
| 11 |
#include "util/u_process.h" |
| 12 |
#include "VirtGpu.h" |
| 13 |
|
| 14 |
#if DETECT_OS_LINUX |
| 15 |
#include <sys/prctl.h> |
| 16 |
#if !defined(HAVE_GETTID) |
| 17 |
#include <sys/syscall.h> |
| 18 |
#endif // !defined(HAVE_GETTID) |
| 19 |
#include <unistd.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
namespace { |
| 23 |
|
| 24 |
// TODO: move to a more common src/util location. |
| 25 |
static uint64_t getProcessId() { |
| 26 |
#if DETECT_OS_LINUX |
| 27 |
return getpid(); |
| 28 |
#endif |
| 29 |
return -1; |
| 30 |
} |
| 31 |
|
| 32 |
// TODO: move to a more common src/util location. |
| 33 |
static std::string getThreadName() { |
| 34 |
std::string threadName; |
| 35 |
#if DETECT_OS_LINUX |
| 36 |
char buf[16] = {}; |
| 37 |
if (prctl(PR_GET_NAME, buf) == 0) { |
| 38 |
threadName = buf; |
| 39 |
} |
| 40 |
#endif |
| 41 |
return threadName; |
| 42 |
} |
| 43 |
|
| 44 |
// TODO: move to a more common src/util location. |
| 45 |
static uint64_t getThreadId() { |
| 46 |
#if DETECT_OS_LINUX |
| 47 |
#if defined(HAVE_GETTID) |
| 48 |
return gettid(); |
| 49 |
#else |
| 50 |
return (pid_t)syscall(SYS_gettid); |
| 51 |
#endif // defined(HAVE_GETTID) |
| 52 |
#endif |
| 53 |
return -1; |
| 54 |
} |
| 55 |
|
| 56 |
} // namespace |
| 57 |
|
| 58 |
GfxStreamVulkanConnection::GfxStreamVulkanConnection(gfxstream::guest::IOStream* stream) { |
| 59 |
mVkEnc = std::make_unique<gfxstream::vk::VkEncoder>(stream); |
| 60 |
|
| 61 |
auto* device = VirtGpuDevice::getInstance(kCapsetGfxStreamVulkan); |
| 62 |
if (device != nullptr) { |
| 63 |
const auto caps = device->getCaps(); |
| 64 |
if (caps.vulkanCapset.hasSetMetadataCommand) { |
| 65 |
const std::string processName = util_get_process_name(); |
| 66 |
const struct VkDebugMetadataGuestProcessNameGOOGLE debugMetadataGuestProcessName = { |
| 67 |
.sType = VK_STRUCTURE_TYPE_DEBUG_METADATA_GUEST_PROCESS_NAME_GOOGLE, |
| 68 |
.pNext = nullptr, |
| 69 |
.pName = processName.c_str(), |
| 70 |
}; |
| 71 |
const uint64_t processId = getProcessId(); |
| 72 |
const struct VkDebugMetadataGuestThreadIdGOOGLE debugMetadataGuestProcessId = { |
| 73 |
.sType = VK_STRUCTURE_TYPE_DEBUG_METADATA_GUEST_PROCESS_ID_GOOGLE, |
| 74 |
.pNext = &debugMetadataGuestProcessName, |
| 75 |
.id = processId, |
| 76 |
}; |
| 77 |
const std::string threadName = getThreadName(); |
| 78 |
const struct VkDebugMetadataGuestThreadNameGOOGLE debugMetadataGuestThreadName = { |
| 79 |
.sType = VK_STRUCTURE_TYPE_DEBUG_METADATA_GUEST_THREAD_NAME_GOOGLE, |
| 80 |
.pNext = &debugMetadataGuestProcessId, |
| 81 |
.pName = threadName.c_str(), |
| 82 |
}; |
| 83 |
const uint64_t threadId = getThreadId(); |
| 84 |
const struct VkDebugMetadataGuestThreadIdGOOGLE debugMetadataGuestThreadId = { |
| 85 |
.sType = VK_STRUCTURE_TYPE_DEBUG_METADATA_GUEST_THREAD_ID_GOOGLE, |
| 86 |
.pNext = &debugMetadataGuestThreadName, |
| 87 |
.id = threadId, |
| 88 |
}; |
| 89 |
const struct VkDebugMetadataGOOGLE debugMetadata = { |
| 90 |
.sType = VK_STRUCTURE_TYPE_DEBUG_METADATA_GOOGLE, |
| 91 |
.pNext = &debugMetadataGuestThreadId, |
| 92 |
}; |
| 93 |
mVkEnc->vkSetDebugMetadataAsyncGOOGLE(&debugMetadata, false /* no lock */); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
GfxStreamVulkanConnection::~GfxStreamVulkanConnection() {} |
| 99 |
|
| 100 |
void* GfxStreamVulkanConnection::getEncoder() { return mVkEnc.get(); } |