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

Windows / speeup: dynamically load libraries on startup and never again #1422

Merged
merged 48 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
a8191b4
port NtQuerySystemInformation
giampaolo Feb 20, 2019
86c9558
port NtQuerySystemInformation
giampaolo Feb 20, 2019
4277cf2
port NtQuerySystemInformation
giampaolo Feb 20, 2019
5ebac31
port NtQueryInformationProcess
giampaolo Feb 20, 2019
cf5fa73
port NtSetInformationProcess
giampaolo Feb 20, 2019
b297938
port WinStationQueryInformationW
giampaolo Feb 20, 2019
850e6a8
port RtlIpv4AddressToStringA and rtlIpv6AddressToStringA
giampaolo Feb 20, 2019
fd48e95
port GetExtendedTcpTable
giampaolo Feb 20, 2019
8c54a5f
port GetExtendedUdpTable
giampaolo Feb 20, 2019
a7ff4a3
port GetActiveProcessorCount
giampaolo Feb 20, 2019
2ca2119
port GetTickCount64
giampaolo Feb 20, 2019
ef0195d
Merge branch 'master' into winloadlib
giampaolo Feb 20, 2019
8d28579
rename function and make it private
giampaolo Feb 20, 2019
aeccf0c
port NtQueryObject
giampaolo Feb 20, 2019
afc95f9
fix compilation warning
giampaolo Feb 20, 2019
d141020
port NtWow64QueryInformationProcess64
giampaolo Feb 20, 2019
4a386e4
port NtWow64ReadVirtualMemory64
giampaolo Feb 20, 2019
265ee24
move definitions in ntextapi.h
giampaolo Feb 20, 2019
710008d
remove duplicated definition
giampaolo Feb 20, 2019
7a32431
add comments re. platform availability
giampaolo Feb 20, 2019
f609d75
refactor definitions
giampaolo Feb 20, 2019
e26ec99
types refactoring
giampaolo Feb 20, 2019
b8133ff
refactor process_handles
giampaolo Feb 20, 2019
ec17917
remove unused def; move another one in ntextapi.h
giampaolo Feb 20, 2019
d2cdf61
remove unnecessary definitions
giampaolo Feb 20, 2019
071786d
move definitions
giampaolo Feb 20, 2019
86a18e4
move struct
giampaolo Feb 20, 2019
b1046ad
remove unused struct def
giampaolo Feb 20, 2019
4ab6854
move stuff around
giampaolo Feb 20, 2019
bb045af
remove mingw32 support
giampaolo Feb 20, 2019
661f5bb
remove unused def
giampaolo Feb 20, 2019
9a03d6d
remove unused struct
giampaolo Feb 20, 2019
aba261d
remove win xp support
giampaolo Feb 20, 2019
8f2f5b1
move struct around
giampaolo Feb 20, 2019
d785adf
port GetLogicalProcessorInformationEx
giampaolo Feb 20, 2019
8f70a95
proper definition of WinStationQueryInformationW
giampaolo Feb 20, 2019
5e4df5f
proper typedef redefinition
giampaolo Feb 20, 2019
c2b26cb
move load-lib utility functions in global.c
giampaolo Feb 20, 2019
388a537
fix compiler warning
giampaolo Feb 20, 2019
b259735
remove unused enum
giampaolo Feb 20, 2019
8b41229
move stuff around
giampaolo Feb 20, 2019
b46ec41
use Py_BEGIN/ENDALLOW_THREADS; use PyErr_Clear()
giampaolo Feb 20, 2019
fd690d4
add benchmark script
giampaolo Feb 20, 2019
71bb78d
too complicated to load NtWow64ReadVirtualMemory64 dynamically; resto…
giampaolo Feb 20, 2019
e1e8320
update benchmark script
giampaolo Feb 20, 2019
1a28679
fix test
giampaolo Feb 20, 2019
3dbfee8
remove unused definition
giampaolo Feb 21, 2019
03fca72
add PyErr_Clear()
giampaolo Feb 21, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 55 additions & 234 deletions psutil/_psutil_windows.c

Large diffs are not rendered by default.

129 changes: 129 additions & 0 deletions psutil/arch/windows/global.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/

#include <windows.h>
#include <Python.h>
#include "ntextapi.h"
#include "global.h"


// A wrapper around GetModuleHandle and GetProcAddress.
PVOID
psutil_GetProcAddress(LPCSTR libname, LPCSTR procname) {
HMODULE mod;
FARPROC addr;

if ((mod = GetModuleHandleA(libname)) == NULL) {
PyErr_SetFromWindowsErrWithFilename(0, libname);
return NULL;
}
if ((addr = GetProcAddress(mod, procname)) == NULL) {
PyErr_SetFromWindowsErrWithFilename(0, procname);
return NULL;
}
return addr;
}


// A wrapper around LoadLibrary and GetProcAddress.
PVOID
psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname) {
HMODULE mod;
FARPROC addr;

Py_BEGIN_ALLOW_THREADS
mod = LoadLibraryA(libname);
Py_END_ALLOW_THREADS
if (mod == NULL) {
PyErr_SetFromWindowsErrWithFilename(0, libname);
return NULL;
}
if ((addr = GetProcAddress(mod, procname)) == NULL) {
PyErr_SetFromWindowsErrWithFilename(0, procname);
FreeLibrary(mod);
return NULL;
}
// Causes crash.
// FreeLibrary(mod);
return addr;
}


/*
* This is executed on import and loads Windows APIs so that they
* are available globally.
*/
psutil_loadlibs() {
/*
* Mandatory.
*/

psutil_NtQuerySystemInformation = psutil_GetProcAddressFromLib(
"ntdll.dll", "NtQuerySystemInformation");
if (psutil_NtQuerySystemInformation == NULL)
return 1;

psutil_NtQueryInformationProcess = psutil_GetProcAddress(
"ntdll.dll", "NtQueryInformationProcess");
if (! psutil_NtQueryInformationProcess)
return 1;

psutil_NtSetInformationProcess = psutil_GetProcAddress(
"ntdll.dll", "NtSetInformationProcess");
if (! psutil_NtSetInformationProcess)
return 1;

psutil_WinStationQueryInformationW = psutil_GetProcAddressFromLib(
"winsta.dll", "WinStationQueryInformationW");
if (! psutil_WinStationQueryInformationW)
return 1;

psutil_NtQueryObject = psutil_GetProcAddressFromLib(
"ntdll.dll", "NtQueryObject");
if (! psutil_NtQueryObject)
return 1;

psutil_rtlIpv4AddressToStringA = psutil_GetProcAddressFromLib(
"ntdll.dll", "RtlIpv4AddressToStringA");
if (! psutil_rtlIpv4AddressToStringA)
return 1;

psutil_rtlIpv6AddressToStringA = psutil_GetProcAddressFromLib(
"ntdll.dll", "RtlIpv6AddressToStringA");
if (! psutil_rtlIpv6AddressToStringA)
return 1;

// minimum requirement: Win XP SP3
psutil_GetExtendedTcpTable = psutil_GetProcAddressFromLib(
"iphlpapi.dll", "GetExtendedTcpTable");
if (! psutil_GetExtendedTcpTable)
return 1;

// minimum requirement: Win XP SP3
psutil_GetExtendedUdpTable = psutil_GetProcAddressFromLib(
"iphlpapi.dll", "GetExtendedUdpTable");
if (! psutil_GetExtendedUdpTable)
return 1;

/*
* Optional.
*/

// minimum requirement: Win Vista
psutil_GetTickCount64 = psutil_GetProcAddress(
"kernel32", "GetTickCount64");

// minimum requirement: Win 7
psutil_GetActiveProcessorCount = psutil_GetProcAddress(
"kernel32", "GetActiveProcessorCount");

// minumum requirement: Win 7
psutil_GetLogicalProcessorInformationEx = psutil_GetProcAddressFromLib(
"kernel32", "GetLogicalProcessorInformationEx");

PyErr_Clear();
return 0;
}
11 changes: 11 additions & 0 deletions psutil/arch/windows/global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/

#include <windows.h>

int psutil_loadlibs();
PVOID psutil_GetProcAddress(LPCSTR libname, LPCSTR procname);
PVOID psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname);
Loading