Skip to content
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

Speed Up info_gpu #155

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

Carterpersall
Copy link
Contributor

@Carterpersall Carterpersall commented Sep 13, 2022

  • Optimized GPU fetching
    • Instead of using Get-CimInstance to get GPU information, which can be very slow, gets the registry entries at 'HKLM:\SOFTWARE\Microsoft\DirectX', which contains a list of video adapters
    • 1690 ms -> 40.16 ms
      • 42x faster (nice)

TODO:

  • Fix edge case for users with multiple of the same GPU
  • Create case for when LastSeen key doesn't exist
    • Note to Self: HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Hardware Profiles\0001\System\CurrentControlSet\Enum\PCI contains currently connected GPU's hardware IDs
      • Could use as alternative to LastSeen key
      • HKEY_CURRENT_CONFIG\System\CurrentControlSet\Enum\PCI has same keys
    • Another Note to Self: Using HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\ could be easier and faster than current method
    • Yet another Note to Self: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinSAT has the primary GPU
    • Yeet another one: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Video\

- Instead of using Get-CimInstance to get GPU information, which can be very slow, gets the registry entry at HKLM:\SOFTWARE\Microsoft\DirectX\ which contains a list of installed video adapters
  - 1690 ms -> 40.16 ms
    - 42x faster (nice)
This was referenced Sep 13, 2022
- Whenever someone doesn't have the LastSeen key, falls back on a method which is incompatible with users with multiple of the same GPU
- If for some reason the entire registry entry doesn't exist, fall back to the previous slow implementation.
@Carterpersall
Copy link
Contributor Author

@jcwillox Created a fallback implementation if the LastSeen key doesn't exist, can you try it?

The only issue with the fallback is that theoretically, it won't work properly if someone has two of the same GPU installed. But I think this is a very small minority of users that would be both lacking the LastSeen key and have two of the same GPU for some reason.

I also added back the old implementation in case the entire DirectX key doesn't exist, which I think would also be very unlikely. But since the implementation was already created, I saw no harm in adding it.

@jcwillox
Copy link
Member

I tried the new function and it is now returning the GPU which is not connected to my system again.

The only issue with the fallback is that theoretically, it won't work properly if someone has two of the same GPU installed. But I think this is a very small minority of users that would be both lacking the LastSeen key and have two of the same GPU for some reason.

Besides returning GPUs that aren't connected anymore, this would still be a deal breaker, we should never sacrifice reliability for performance, otherwise, it kinda defeats one of the main purposes of winfetch. Instead, we need a better solution.

My recommendation is to drop the alternative method and if the LastSeen key is not present then just revert to the original functionality.


Out of interest what version of windows are you running?

@Carterpersall
Copy link
Contributor Author

Carterpersall commented Sep 15, 2022

Out of interest what version of windows are you running?

Windows 11

Also, I kinda forgot about the bug with returning no longer connected gpus, I'll fix it probably tomorrow sometime. I found a new thing I could try implementing as a backup. I found where the entries for device manager are stored in the registry, and I could go through the PCI category and look for devices that match keywords like NVIDIA GeForce and AMD Radeon.

@jcwillox
Copy link
Member

Thought that might be the case, I'm still running Windows 10 so that may be the reason for the missing LastSeen key.

I found where the entries for device manager are stored in the registry, and I could go through the PCI category and look for devices that match keywords like NVIDIA GeForce and AMD Radeon.

That could be a much better option, it may even work for older versions that don't have DirectX.

@Carterpersall
Copy link
Contributor Author

Thought that might be the case, I'm still running Windows 10 so that may be the reason for the missing LastSeen key.

Can confirm that my Windows 10 21H1 VM lacks the key so maybe it is Windows 11 exclusive

- Gets the subkeys of HKLM:\SYSTEM\CurrentControlSet\Enum\PCI\ and pulls the value of DeviceDesc of those subkey's subkeys, then pulls the device name from the value, then it takes those values and compares those values to a list of GPU titles.
@Carterpersall
Copy link
Contributor Author

The most recent commit implements the new method which is run if LastSeen is not detected on the user's system.

The list I created is probably missing some more obscure GPUs but they can be easily added if found.

@Carterpersall
Copy link
Contributor Author

@jcwillox can you confirm if the new backup implementation works? The script should automatically fall back to the backup for you so you shouldn't have to worry about changing anything to enable it.

@jcwillox
Copy link
Member

Yep, the new method works and returns just the connected GPU. I did notice that both the device manager and cim methods take around the same time on my machine, both 11ms.

image

image

@Carterpersall
Copy link
Contributor Author

Carterpersall commented Sep 19, 2022

Yep, the new method works and returns just the connected GPU. I did notice that both the device manager and cim methods take around the same time on my machine, both 11ms.

For me, most CIM accesses take at least 1.2 seconds, including this one. But the device manager method takes around 50 ms.
I'm not sure why, but when PowerShell is bottlenecked badly, most WMI and CIM calls take ages to complete

@Carterpersall
Copy link
Contributor Author

Carterpersall commented Sep 19, 2022

Also, to get the correct speed for a scriptblock, it's best to run each measure-command in a new instance by running pwsh -nop {Measure-Command {Stuff}}

When testing on my PC with a Ryzen 5 3600, without bottlenecking PowerShell, and using the CIM method, I got 46 ms when opening a new instance every time and around 11 ms when running it on the same instance multiple times

@Carterpersall Carterpersall marked this pull request as ready for review April 17, 2023 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants