forked from LunarG/VulkanTools
-
Notifications
You must be signed in to change notification settings - Fork 1
Add CPUTiming layer #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
emrekultursay
merged 14 commits into
android-graphics:main
from
emrekultursay:cputiming_layer
Mar 2, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
18286fa
Restore python scripts
emrekultursay 7fd928a
Modify the api_dump layer to dump only API names
emrekultursay 58c1e86
Add a simple cputiming layer that does nothing
emrekultursay 39fb690
Add a simple timer class
emrekultursay 36ce547
Also print latency in api_dump layer
emrekultursay 1312526
Update CPUTiming layer to use the generated style similar to api_dump
emrekultursay 968d85d
Use android_log_print on Android
emrekultursay e36beca
Emit Perfetto events on start/end of APIs
emrekultursay f98830e
Fully isolate cputiming, undo changes to api_dump
emrekultursay dccf2c9
Cleanup
emrekultursay 3bd5589
Add cputiming test
emrekultursay 0cf5708
A bit more cleanup
emrekultursay 29e3419
Group emitted perfetto events into categories
emrekultursay a39d128
Do not build on WIN32 (windows and mingw)
emrekultursay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* Copyright (c) 2015-2023 The Khronos Group Inc. | ||
| * Copyright (c) 2015-2023 Valve Corporation | ||
| * Copyright (c) 2015-2023 LunarG, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "cputiming.h" | ||
|
|
||
| CpuTiming& CpuTiming::Get() { | ||
| static CpuTiming instance; | ||
| return instance; | ||
| } | ||
|
|
||
| void CpuTiming::SetVkInstance(VkPhysicalDevice phys_dev, VkInstance instance) { | ||
| std::lock_guard<std::mutex> lock(map_mutex_); | ||
| vk_instance_map_[phys_dev] = instance; | ||
| } | ||
|
|
||
| VkInstance CpuTiming::GetVkInstance(VkPhysicalDevice phys_dev) { | ||
| std::lock_guard<std::mutex> lock(map_mutex_); | ||
| auto it = vk_instance_map_.find(phys_dev); | ||
| if (it != vk_instance_map_.end()) return it->second; | ||
| return VK_NULL_HANDLE; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* Copyright (c) 2015-2023 The Khronos Group Inc. | ||
| * Copyright (c) 2015-2023 Valve Corporation | ||
| * Copyright (c) 2015-2023 LunarG, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <vulkan/vulkan.h> | ||
| #include <mutex> | ||
| #include <unordered_map> | ||
|
|
||
| class CpuTiming { | ||
| public: | ||
| static CpuTiming& Get(); | ||
|
|
||
| void SetVkInstance(VkPhysicalDevice phys_dev, VkInstance instance); | ||
| VkInstance GetVkInstance(VkPhysicalDevice phys_dev); | ||
|
|
||
| private: | ||
| std::mutex map_mutex_; | ||
| std::unordered_map<VkPhysicalDevice, VkInstance> vk_instance_map_; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* Copyright (c) 2015-2023 The Khronos Group Inc. | ||
| * Copyright (c) 2015-2023 Valve Corporation | ||
| * Copyright (c) 2015-2023 LunarG, Inc. | ||
| * Copyright (C) 2015-2016 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| // Implementation file for specifically implemented functions | ||
|
|
||
| #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | ||
|
|
||
| #include "generated/cputiming_dispatch.h" | ||
| #include "vk_layer_table.h" | ||
|
|
||
| extern "C" { | ||
|
|
||
| EXPORT_FUNCTION VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* pName) { | ||
| Timer timer(CPUTimingCategory::VkInstance, "vkGetInstanceProcAddr"); | ||
| PFN_vkVoidFunction instance_func = cputiming_known_instance_functions(instance, pName); | ||
| if (instance_func) return instance_func; | ||
| PFN_vkVoidFunction device_func = cputiming_known_device_functions(NULL, pName); | ||
| // Make sure that device functions queried through GIPA works | ||
| if (device_func) return device_func; | ||
|
|
||
| // Haven't created an instance yet, exit now since there is no instance_dispatch_table | ||
| if (instance_dispatch_table(instance)->GetInstanceProcAddr == NULL) return nullptr; | ||
| return instance_dispatch_table(instance)->GetInstanceProcAddr(instance, pName); | ||
| } | ||
|
|
||
| EXPORT_FUNCTION VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice device, const char* pName) { | ||
| Timer timer(CPUTimingCategory::VkDevice, "vkGetDeviceProcAddr"); | ||
| PFN_vkVoidFunction device_func = cputiming_known_device_functions(device, pName); | ||
| if (device_func) return device_func; | ||
|
|
||
| // Haven't created a device yet, exit now since there is no device_dispatch_table | ||
| if (device_dispatch_table(device)->GetDeviceProcAddr == NULL) return nullptr; | ||
| return device_dispatch_table(device)->GetDeviceProcAddr(device, pName); | ||
| } | ||
| } | ||
emrekultursay marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* Copyright (c) 2015-2023 The Khronos Group Inc. | ||
| * Copyright (c) 2015-2023 Valve Corporation | ||
| * Copyright (c) 2015-2023 LunarG, Inc. | ||
| * Copyright (C) 2015-2016 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| // Implementation file for specifically implemented functions | ||
|
|
||
| #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | ||
|
|
||
| #include "generated/cputiming_implementation.h" | ||
| #include "timer.h" | ||
| #include "cputiming.h" | ||
| #include "perfetto/perfetto_helpers.h" | ||
| #include "vk_layer_table.h" | ||
|
|
||
| #if defined(__GNUC__) && __GNUC__ >= 4 | ||
| #define EXPORT_FUNCTION __attribute__((visibility("default"))) | ||
| #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590) | ||
| #define EXPORT_FUNCTION __attribute__((visibility("default"))) | ||
| #else | ||
| #define EXPORT_FUNCTION | ||
| #endif | ||
|
|
||
| #include <mutex> | ||
|
|
||
| static std::once_flag g_perfetto_init_flag; | ||
|
|
||
| extern "C" { | ||
|
|
||
| VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, | ||
| VkInstance* pInstance) { | ||
| std::call_once(g_perfetto_init_flag, []() { InitializePerfetto(); }); | ||
| Timer timer(CPUTimingCategory::Other, "vkCreateInstance"); | ||
|
|
||
|
|
||
|
|
||
| // Get the function pointer | ||
| VkLayerInstanceCreateInfo* chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); | ||
| assert(chain_info->u.pLayerInfo != 0); | ||
| PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; | ||
| assert(fpGetInstanceProcAddr != 0); | ||
| PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance)fpGetInstanceProcAddr(NULL, "vkCreateInstance"); | ||
| if (fpCreateInstance == NULL) { | ||
| return VK_ERROR_INITIALIZATION_FAILED; | ||
| } | ||
|
|
||
| // Call the function and create the dispatch table | ||
| chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext; | ||
| VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance); | ||
| if (result == VK_SUCCESS) { | ||
| initInstanceTable(*pInstance, fpGetInstanceProcAddr); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
|
|
||
| VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, | ||
| const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) { | ||
| Timer timer(CPUTimingCategory::VkPhysicalDevice, "vkCreateDevice"); | ||
|
|
||
| // Get the function pointer | ||
| VkLayerDeviceCreateInfo* chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); | ||
| assert(chain_info->u.pLayerInfo != 0); | ||
| PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; | ||
| PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr; | ||
| VkInstance vk_instance = CpuTiming::Get().GetVkInstance(physicalDevice); | ||
| PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice)fpGetInstanceProcAddr(vk_instance, "vkCreateDevice"); | ||
| if (fpCreateDevice == NULL) { | ||
| return VK_ERROR_INITIALIZATION_FAILED; | ||
| } | ||
|
|
||
| // Call the function and create the dispatch table | ||
| chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext; | ||
| VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice); | ||
| if (result == VK_SUCCESS) { | ||
| initDeviceTable(*pDevice, fpGetDeviceProcAddr); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| extern "C" { | ||
|
|
||
| EXPORT_FUNCTION VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char* pLayerName, | ||
| uint32_t* pPropertyCount, | ||
| VkExtensionProperties* pProperties) { | ||
| Timer timer(CPUTimingCategory::Other, "vkEnumerateInstanceExtensionProperties"); | ||
| return util_GetExtensionProperties(0, NULL, pPropertyCount, pProperties); | ||
| } | ||
|
|
||
| EXPORT_FUNCTION VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t* pPropertyCount, | ||
| VkLayerProperties* pProperties) { | ||
| Timer timer(CPUTimingCategory::Other, "vkEnumerateInstanceLayerProperties"); | ||
| static const VkLayerProperties layerProperties[] = {{ | ||
| "CPUTiming", | ||
| VK_MAKE_VERSION(1, 4, VK_HEADER_VERSION), // specVersion | ||
| VK_MAKE_VERSION(0, 2, 0), // implementationVersion | ||
| "layer: CPUTiming", | ||
| }}; | ||
|
|
||
| return util_GetLayerProperties(ARRAY_SIZE(layerProperties), layerProperties, pPropertyCount, pProperties); | ||
| } | ||
|
|
||
| EXPORT_FUNCTION VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, | ||
| uint32_t* pPropertyCount, | ||
| VkLayerProperties* pProperties) { | ||
| Timer timer(CPUTimingCategory::VkPhysicalDevice, "vkEnumerateDeviceLayerProperties"); | ||
| static const VkLayerProperties layerProperties[] = {{ | ||
| "CPUTiming", | ||
| VK_MAKE_VERSION(1, 4, VK_HEADER_VERSION), | ||
| VK_MAKE_VERSION(0, 2, 0), | ||
| "layer: CPUTiming", | ||
| }}; | ||
|
|
||
| return util_GetLayerProperties(ARRAY_SIZE(layerProperties), layerProperties, pPropertyCount, pProperties); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.