Skip to content

Commit 8bd0f86

Browse files
author
U-NVIDIA.COM\klefebvre
committed
Update queryResource tool to support released NV extension
This tool provides access to the GL_NV_query_resource functionality provided by the NVidia OpenGL driver (both Windows and Linux). While the tool is fully functional it is mainly meant to be sample code that can be used as a basis for a users own implementation. The main changes in this version are: - numerous changes to the extension/routine names due to the promotion of the queryResource extension from NVX to NV (and any relevant comment changes). The NVX extension has been obsoleted and is no longer supported. - rework of the returned data buffer decoding and printing. The change is mainly to emphasize the sample code aspect of the code rather than provide forward compatibility to the code for future buffer format changes (this is left to the user to implement if it is desired). There is some future compatibility support built into the extension based on the embedded record sizes, the tool will decode the portion of the records it knows about and then use the size to step over portions it doesn't know about.
1 parent c73f84f commit 8bd0f86

6 files changed

+178
-272
lines changed

README.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ About
66

77
nvidia-query-resource-opengl queries the NVIDIA OpenGL driver to determine the
88
OpenGL resource usage of an application. OpenGL applications may query their
9-
own resource usage using the GL\_NVX\_query\_resource extension, but the
9+
own resource usage using the GL\_NV\_query\_resource extension, but the
1010
nvidia-query-resource-opengl tool allows users to perform resource queries
1111
externally, against unmodified OpenGL applications.
1212

1313
Requirements
1414
------------
1515

1616
* A Windows, Linux, Solaris, or FreeBSD system with an NVIDIA GPU, running a
17-
version of the NVIDIA OpenGL driver supporting the GL\_NVX\_query\_resource
18-
extension. Support for this extension was introduced with the 355.xx driver
19-
release. **Note that this extension is still under development and subject to
20-
change, so applications developed against it, including this query resource
21-
tool, may need to be updated for compatibility with future driver versions.**
17+
version of the NVIDIA OpenGL driver supporting the GL\_NV\_query\_resource
18+
extension. Support for this extension was introduced with the 387.xx driver
19+
release. **Note: this tool operates on the released NV extension and
20+
replaces an earlier tool that operated on the experimental NVX version of
21+
the extension.***
2222
* CMake 2.6 or later, and a suitable build system (e.g. Windows SDK and/or
2323
Microsoft Visual Studio on Windows; make and cc/gcc on Unix-like systems)
2424
that is supported by the CMake generators on the target platform. (Not needed
@@ -63,16 +63,16 @@ Usage
6363

6464
You can query an application's OpenGL resource usage by executing the command:
6565

66-
nvidia-query-resource-opengl -p <pid> [-qt <query_type>]
66+
nvidia-query-resource-opengl -p <pid>
6767

6868
* pid: the process ID of the target OpenGL application of the query
69-
* query\_type: this may be 'summary' or 'detailed'. The default is 'summary'.
70-
+ summary: reports a summary, per device, of allocated video and system
71-
memory, the total amount of memory in use by the the driver, and the
72-
total amount of allocated but unused memory.
73-
+ detailed: includes the summary information, and additionally reports
74-
separate allocation amounts for various object types. The current set
75-
of reported object types includes:
69+
70+
The tool reports a summary, per device, of allocated video memory, the total
71+
amount of memory in use by the the driver, and the total amount of allocated
72+
but unused memory.
73+
74+
In addition a more detailed per object memory usage is reported. The current
75+
set of reported object types includes:
7676
- SYSTEM RESERVED - driver allocated memory
7777
- TEXTURE - memory in use by 1D/2D/3D textures
7878
- RENDERBUFFER - render buffer memory

include/nvidia-query-resource-opengl-data.h

+28-58
Original file line numberDiff line numberDiff line change
@@ -25,72 +25,42 @@
2525

2626
// Data types for interpreting data returned by the queryResource extension
2727

28-
#define NVQR_MIN_DATA_FORMAT_VERSION 1
29-
#define NVQR_MAX_DATA_FORMAT_VERSION 1
30-
#define NVQR_VERSIONED_IDENT(IDENT, VERSION) IDENT ## _v ## VERSION
31-
#define NVQR_VERSION_TYPEDEF(TYPE, VERSION) \
32-
typedef NVQR_VERSIONED_IDENT(TYPE, VERSION) TYPE
28+
#define NVQR_DATA_FORMAT_VERSION 2
29+
#define NVQR_MAX_DATA_BUFFER_LEN 1024
3330

3431
typedef int NVQRQueryData_t;
3532

36-
typedef struct NVQRQueryDetailedInfoRec_v1 {
37-
NVQRQueryData_t memType;
38-
NVQRQueryData_t objectType;
39-
NVQRQueryData_t memUsedkiB;
40-
NVQRQueryData_t numAllocs;
41-
} NVQRQueryDetailedInfo_v1;
42-
NVQR_VERSION_TYPEDEF(NVQRQueryDetailedInfo, NVQR_MAX_DATA_FORMAT_VERSION);
33+
typedef struct NVQRQueryDataHeaderRec {
34+
NVQRQueryData_t headerBlkSize;
35+
NVQRQueryData_t version;
36+
NVQRQueryData_t numDevices;
37+
} NVQRQueryDataHeader;
4338

44-
typedef struct NVQRQuerySummaryInfoRec_v1 {
39+
typedef struct NVQRQueryDeviceInfoRec {
40+
NVQRQueryData_t deviceBlkSize;
41+
NVQRQueryData_t summaryBlkSize;
4542
NVQRQueryData_t totalAllocs;
4643
NVQRQueryData_t vidMemUsedkiB;
47-
NVQRQueryData_t sysMemUsedkiB;
4844
NVQRQueryData_t vidMemFreekiB;
49-
NVQRQueryData_t sysMemFreekiB;
50-
} NVQRQuerySummaryInfo_v1;
51-
NVQR_VERSION_TYPEDEF(NVQRQuerySummaryInfo, NVQR_MAX_DATA_FORMAT_VERSION);
52-
53-
typedef struct NVQRQueryDeviceInfoRec_v1 {
54-
NVQRQuerySummaryInfo_v1 summary;
55-
NVQRQueryData_t numDetailedBlocks;
56-
NVQRQueryDetailedInfo_v1 detailed[];
57-
} NVQRQueryDeviceInfo_v1;
58-
NVQR_VERSION_TYPEDEF(NVQRQueryDeviceInfo, NVQR_MAX_DATA_FORMAT_VERSION);
59-
60-
typedef struct NVQRQueryDataHeaderRec_v1 {
61-
NVQRQueryData_t version;
62-
NVQRQueryData_t numDevices;
63-
} NVQRQueryDataHeader_v1;
64-
NVQR_VERSION_TYPEDEF(NVQRQueryDataHeader, NVQR_MAX_DATA_FORMAT_VERSION);
65-
66-
#define NVQR_MAX_DEVICES 16
67-
#define NVQR_MAX_DETAILED_INFOS 8
68-
#define NVQR_MAX_DATA_BUFFER_LEN ( \
69-
( \
70-
sizeof(NVQRQueryDataHeader) + ( \
71-
NVQR_MAX_DEVICES * ( \
72-
sizeof(NVQRQueryDeviceInfo) + \
73-
NVQR_MAX_DETAILED_INFOS * sizeof(NVQRQueryDetailedInfo) \
74-
) \
75-
) \
76-
) / sizeof(NVQRQueryData_t) \
77-
)
78-
79-
// Helper functions for extracting data from raw int array
45+
NVQRQueryData_t numDetailBlocks;
46+
} NVQRQueryDeviceInfo;
8047

81-
int nvqr_get_data_format_version (NVQRQueryData_t *data);
82-
int nvqr_get_num_devices (NVQRQueryData_t *data);
83-
84-
// Returns a pointer to a DeviceInfo structure. The versioned functions
85-
// (e.g. nvqr_get_device_v1()) must only be used with the correct data format
86-
// version, and will return a pointer within the already allocated data
87-
// array passed into the function. This pointer must not be freed.
88-
// The unversioned nvqr_get_device() function will return a pointer to a *newly
89-
// allocated* structure of the highest type version supported by this header,
90-
// and copy data from the source array into this structure. The caller is
91-
//responsible for freeing the memory allocated by nvqr_get_device().
48+
typedef struct NVQRQueryDetailInfoRec {
49+
NVQRQueryData_t detailBlkSize;
50+
NVQRQueryData_t memType;
51+
NVQRQueryData_t objectType;
52+
NVQRQueryData_t numAllocs;
53+
NVQRQueryData_t memUsedkiB;
54+
} NVQRQueryDetailInfo;
9255

93-
NVQRQueryDeviceInfo_v1 *nvqr_get_device_v1(NVQRQueryData_t *data, int device);
94-
NVQRQueryDeviceInfo *nvqr_get_device(NVQRQueryData_t *data, int device);
56+
typedef struct NVQRTagBlockRec {
57+
NVQRQueryData_t tagBlkSize;
58+
NVQRQueryData_t tagId;
59+
NVQRQueryData_t deviceId;
60+
NVQRQueryData_t numAllocs;
61+
NVQRQueryData_t vidmemUsedkiB;
62+
NVQRQueryData_t tagLength;
63+
char tag[4];
64+
} NVQRTagBlock;
9565

9666
#endif

include/nvidia-query-resource-opengl.h

+19-15
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ typedef struct {
5353

5454
//------------------------------------------------------------------------------
5555
// Open a connection to an OpenGL process. This process must be using an NVIDIA
56-
// driver that supports the GL_query_resource_NVX extension. On Unix, this
56+
// driver that supports the GL_query_resource_NV extension. On Unix, this
5757
// process must additionally have libnvidia-query-resource-opengl-preload.so
5858
// preloaded into it with LD_PRELOAD. The connection may be reused for any
5959
// number of query operations as long as it is left open; however, Windows only
@@ -67,25 +67,29 @@ nvqrReturn_t nvqr_connect(NVQRConnection *connection, pid_t pid);
6767
nvqrReturn_t nvqr_disconnect(NVQRConnection *connection);
6868

6969
//------------------------------------------------------------------------------
70-
// Perform a glQueryResourceNVX() query in the remote OpenGL process. The
70+
// Perform a glQueryResourceNV() query in the remote OpenGL process. The
7171
// process must be in the connected state when performing the query.
7272

7373
nvqrReturn_t nvqr_request_meminfo(NVQRConnection c, GLenum queryType,
7474
NVQRQueryDataBuffer *buf);
7575

76-
/* XXX GL_NVX_query_resource defines - these should be removed once the
77-
* extension has been finalized and promoted to GL_NV_query_resource, and
78-
* these values become part of real OpenGL header files. */
79-
#ifndef GL_QUERY_RESOURCE_TYPE_SUMMARY_NVX
80-
81-
#define GL_QUERY_RESOURCE_TYPE_SUMMARY_NVX 0x9540
82-
#define GL_QUERY_RESOURCE_TYPE_DETAILED_NVX 0x9541
83-
#define GL_QUERY_RESOURCE_MEMTYPE_VIDMEM_NVX 0x9542
84-
#define GL_QUERY_RESOURCE_MEMTYPE_SYSMEM_NVX 0x9543
85-
#define GL_QUERY_RESOURCE_SYS_RESERVED_NVX 0x9544
86-
#define GL_QUERY_RESOURCE_TEXTURE_NVX 0x9545
87-
#define GL_QUERY_RESOURCE_RENDERBUFFER_NVX 0x9546
88-
#define GL_QUERY_RESOURCE_BUFFEROBJECT_NVX 0x9547
76+
//------------------------------------------------------------------------------
77+
// Decode and print out the dta buffer returned from glQueryResourceNV()
78+
//
79+
80+
void nvqr_print_memory_info(GLenum queryType, NVQRQueryData_t *buffer);
81+
82+
/* GL_NV_query_resource defines - these should be removed once the
83+
* extension has been finalized and these values become part of real
84+
* OpenGL header files. */
85+
#ifndef GL_QUERY_RESOURCE_TYPE_VIDMEM_ALLOC_NV
86+
87+
#define GL_QUERY_RESOURCE_TYPE_VIDMEM_ALLOC_NV 0x9540
88+
#define GL_QUERY_RESOURCE_MEMTYPE_VIDMEM_NV 0x9542
89+
#define GL_QUERY_RESOURCE_SYS_RESERVED_NV 0x9544
90+
#define GL_QUERY_RESOURCE_TEXTURE_NV 0x9545
91+
#define GL_QUERY_RESOURCE_RENDERBUFFER_NV 0x9546
92+
#define GL_QUERY_RESOURCE_BUFFEROBJECT_NV 0x9547
8993

9094
#endif
9195

preload/nvidia-query-resource-opengl-preload.c

+13-16
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ __attribute__((destructor)) void queryResourcePreloadExit(void);
4545

4646
#define NVQR_QUEUE_MAX 8
4747

48-
/* XXX GL_NVX_query_resource defines - these should be removed once the
49-
* extension has been finalized and promoted to GL_NV_query_resource, and
50-
* these values become part of real OpenGL header files. */
51-
#ifndef GL_NVX_query_resource
52-
#define GL_NVX_query_resource 1
48+
/* XXX GL_NV_query_resource defines - these should be removed once the
49+
* extension has been finalized and these values become part of real
50+
* OpenGL header files. */
51+
#ifndef GL_NV_query_resource
52+
#define GL_NV_query_resource 1
5353

5454
#ifdef GL_GLEXT_PROTOTYPES
55-
GLAPI GLint GLAPIENTRY glQueryResourceNVX (GLenum queryType, GLuint pname, GLuint bufSize, GLint *buffer);
55+
GLAPI GLint GLAPIENTRY glQueryResourceNV (GLenum queryType, GLuint pname, GLuint bufSize, GLint *buffer);
5656
#endif /* GL_GLEXT_PROTOTYPES */
57-
typedef GLint (GLAPIENTRYP PFNGLQUERYRESOURCENVXPROC) (GLenum queryType, GLuint pname, GLuint bufSize, GLint *buffer);
57+
typedef GLint (GLAPIENTRYP PFNGLQUERYRESOURCENVPROC) (GLenum queryType, GLuint pname, GLuint bufSize, GLint *buffer);
5858

5959
#endif
6060

61-
#define NVQR_EXTENSION ((const GLubyte *)"glQueryResourceNVX")
61+
#define NVQR_EXTENSION ((const GLubyte *)"glQueryResourceNV")
6262

6363
#define SOCKET_NAME_MAX_LENGTH sizeof(((struct sockaddr_un *)0)->sun_path)
6464
static char socket_name[SOCKET_NAME_MAX_LENGTH];
@@ -71,7 +71,7 @@ static pthread_mutex_t connect_lock;
7171
// current to the thread that is making the query
7272
static pthread_mutex_t query_lock;
7373

74-
static PFNGLQUERYRESOURCENVXPROC glQueryResourceNVX = NULL;
74+
static PFNGLQUERYRESOURCENVPROC glQueryResourceNV = NULL;
7575

7676
static Display *dpy = NULL;
7777
static GLXContext ctx = NULL;
@@ -208,10 +208,7 @@ static int do_query(GLenum queryType, size_t len, int *data)
208208
pthread_mutex_lock(&query_lock);
209209

210210
if (glXMakeCurrent(dpy, None, ctx)) {
211-
// glQueryResourceNVX() is part of an experimental OpenGL extension
212-
// whose details are subject to change. Be prepared to update any
213-
// code that uses this API for compatibility with future GL drivers.
214-
ret = glQueryResourceNVX(queryType, 0, len, data);
211+
ret = glQueryResourceNV(queryType, -1, len, data);
215212
if (!glXMakeCurrent(dpy, None, NULL)) {
216213
ret = 0;
217214
}
@@ -341,10 +338,10 @@ __attribute__((constructor)) void queryResourcePreloadInit(void)
341338

342339
pthread_mutex_init(&connect_lock, NULL);
343340

344-
glQueryResourceNVX =
345-
(PFNGLQUERYRESOURCENVXPROC) glXGetProcAddressARB(NVQR_EXTENSION);
341+
glQueryResourceNV =
342+
(PFNGLQUERYRESOURCENVPROC) glXGetProcAddressARB(NVQR_EXTENSION);
346343

347-
if (glQueryResourceNVX == NULL) {
344+
if (glQueryResourceNV == NULL) {
348345
// XXX should check extension string once extension is exported there
349346
error_msg("failed to load %s", NVQR_EXTENSION);
350347
return;

0 commit comments

Comments
 (0)