Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Commit

Permalink
Update queryResource tool to support released NV extension
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
U-NVIDIA.COM\klefebvre committed Sep 11, 2017
1 parent c73f84f commit 8bd0f86
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 272 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ About

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

Requirements
------------

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

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

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

* pid: the process ID of the target OpenGL application of the query
* query\_type: this may be 'summary' or 'detailed'. The default is 'summary'.
+ summary: reports a summary, per device, of allocated video and system
memory, the total amount of memory in use by the the driver, and the
total amount of allocated but unused memory.
+ detailed: includes the summary information, and additionally reports
separate allocation amounts for various object types. The current set
of reported object types includes:
The tool reports a summary, per device, of allocated video memory, the total
amount of memory in use by the the driver, and the total amount of allocated
but unused memory.

In addition a more detailed per object memory usage is reported. The current
set of reported object types includes:
- SYSTEM RESERVED - driver allocated memory
- TEXTURE - memory in use by 1D/2D/3D textures
- RENDERBUFFER - render buffer memory
Expand Down
86 changes: 28 additions & 58 deletions include/nvidia-query-resource-opengl-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,72 +25,42 @@

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

#define NVQR_MIN_DATA_FORMAT_VERSION 1
#define NVQR_MAX_DATA_FORMAT_VERSION 1
#define NVQR_VERSIONED_IDENT(IDENT, VERSION) IDENT ## _v ## VERSION
#define NVQR_VERSION_TYPEDEF(TYPE, VERSION) \
typedef NVQR_VERSIONED_IDENT(TYPE, VERSION) TYPE
#define NVQR_DATA_FORMAT_VERSION 2
#define NVQR_MAX_DATA_BUFFER_LEN 1024

typedef int NVQRQueryData_t;

typedef struct NVQRQueryDetailedInfoRec_v1 {
NVQRQueryData_t memType;
NVQRQueryData_t objectType;
NVQRQueryData_t memUsedkiB;
NVQRQueryData_t numAllocs;
} NVQRQueryDetailedInfo_v1;
NVQR_VERSION_TYPEDEF(NVQRQueryDetailedInfo, NVQR_MAX_DATA_FORMAT_VERSION);
typedef struct NVQRQueryDataHeaderRec {
NVQRQueryData_t headerBlkSize;
NVQRQueryData_t version;
NVQRQueryData_t numDevices;
} NVQRQueryDataHeader;

typedef struct NVQRQuerySummaryInfoRec_v1 {
typedef struct NVQRQueryDeviceInfoRec {
NVQRQueryData_t deviceBlkSize;
NVQRQueryData_t summaryBlkSize;
NVQRQueryData_t totalAllocs;
NVQRQueryData_t vidMemUsedkiB;
NVQRQueryData_t sysMemUsedkiB;
NVQRQueryData_t vidMemFreekiB;
NVQRQueryData_t sysMemFreekiB;
} NVQRQuerySummaryInfo_v1;
NVQR_VERSION_TYPEDEF(NVQRQuerySummaryInfo, NVQR_MAX_DATA_FORMAT_VERSION);

typedef struct NVQRQueryDeviceInfoRec_v1 {
NVQRQuerySummaryInfo_v1 summary;
NVQRQueryData_t numDetailedBlocks;
NVQRQueryDetailedInfo_v1 detailed[];
} NVQRQueryDeviceInfo_v1;
NVQR_VERSION_TYPEDEF(NVQRQueryDeviceInfo, NVQR_MAX_DATA_FORMAT_VERSION);

typedef struct NVQRQueryDataHeaderRec_v1 {
NVQRQueryData_t version;
NVQRQueryData_t numDevices;
} NVQRQueryDataHeader_v1;
NVQR_VERSION_TYPEDEF(NVQRQueryDataHeader, NVQR_MAX_DATA_FORMAT_VERSION);

#define NVQR_MAX_DEVICES 16
#define NVQR_MAX_DETAILED_INFOS 8
#define NVQR_MAX_DATA_BUFFER_LEN ( \
( \
sizeof(NVQRQueryDataHeader) + ( \
NVQR_MAX_DEVICES * ( \
sizeof(NVQRQueryDeviceInfo) + \
NVQR_MAX_DETAILED_INFOS * sizeof(NVQRQueryDetailedInfo) \
) \
) \
) / sizeof(NVQRQueryData_t) \
)

// Helper functions for extracting data from raw int array
NVQRQueryData_t numDetailBlocks;
} NVQRQueryDeviceInfo;

int nvqr_get_data_format_version (NVQRQueryData_t *data);
int nvqr_get_num_devices (NVQRQueryData_t *data);

// Returns a pointer to a DeviceInfo structure. The versioned functions
// (e.g. nvqr_get_device_v1()) must only be used with the correct data format
// version, and will return a pointer within the already allocated data
// array passed into the function. This pointer must not be freed.
// The unversioned nvqr_get_device() function will return a pointer to a *newly
// allocated* structure of the highest type version supported by this header,
// and copy data from the source array into this structure. The caller is
//responsible for freeing the memory allocated by nvqr_get_device().
typedef struct NVQRQueryDetailInfoRec {
NVQRQueryData_t detailBlkSize;
NVQRQueryData_t memType;
NVQRQueryData_t objectType;
NVQRQueryData_t numAllocs;
NVQRQueryData_t memUsedkiB;
} NVQRQueryDetailInfo;

NVQRQueryDeviceInfo_v1 *nvqr_get_device_v1(NVQRQueryData_t *data, int device);
NVQRQueryDeviceInfo *nvqr_get_device(NVQRQueryData_t *data, int device);
typedef struct NVQRTagBlockRec {
NVQRQueryData_t tagBlkSize;
NVQRQueryData_t tagId;
NVQRQueryData_t deviceId;
NVQRQueryData_t numAllocs;
NVQRQueryData_t vidmemUsedkiB;
NVQRQueryData_t tagLength;
char tag[4];
} NVQRTagBlock;

#endif
34 changes: 19 additions & 15 deletions include/nvidia-query-resource-opengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ typedef struct {

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

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

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

/* XXX GL_NVX_query_resource defines - these should be removed once the
* extension has been finalized and promoted to GL_NV_query_resource, and
* these values become part of real OpenGL header files. */
#ifndef GL_QUERY_RESOURCE_TYPE_SUMMARY_NVX

#define GL_QUERY_RESOURCE_TYPE_SUMMARY_NVX 0x9540
#define GL_QUERY_RESOURCE_TYPE_DETAILED_NVX 0x9541
#define GL_QUERY_RESOURCE_MEMTYPE_VIDMEM_NVX 0x9542
#define GL_QUERY_RESOURCE_MEMTYPE_SYSMEM_NVX 0x9543
#define GL_QUERY_RESOURCE_SYS_RESERVED_NVX 0x9544
#define GL_QUERY_RESOURCE_TEXTURE_NVX 0x9545
#define GL_QUERY_RESOURCE_RENDERBUFFER_NVX 0x9546
#define GL_QUERY_RESOURCE_BUFFEROBJECT_NVX 0x9547
//------------------------------------------------------------------------------
// Decode and print out the dta buffer returned from glQueryResourceNV()
//

void nvqr_print_memory_info(GLenum queryType, NVQRQueryData_t *buffer);

/* GL_NV_query_resource defines - these should be removed once the
* extension has been finalized and these values become part of real
* OpenGL header files. */
#ifndef GL_QUERY_RESOURCE_TYPE_VIDMEM_ALLOC_NV

#define GL_QUERY_RESOURCE_TYPE_VIDMEM_ALLOC_NV 0x9540
#define GL_QUERY_RESOURCE_MEMTYPE_VIDMEM_NV 0x9542
#define GL_QUERY_RESOURCE_SYS_RESERVED_NV 0x9544
#define GL_QUERY_RESOURCE_TEXTURE_NV 0x9545
#define GL_QUERY_RESOURCE_RENDERBUFFER_NV 0x9546
#define GL_QUERY_RESOURCE_BUFFEROBJECT_NV 0x9547

#endif

Expand Down
29 changes: 13 additions & 16 deletions preload/nvidia-query-resource-opengl-preload.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ __attribute__((destructor)) void queryResourcePreloadExit(void);

#define NVQR_QUEUE_MAX 8

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

#ifdef GL_GLEXT_PROTOTYPES
GLAPI GLint GLAPIENTRY glQueryResourceNVX (GLenum queryType, GLuint pname, GLuint bufSize, GLint *buffer);
GLAPI GLint GLAPIENTRY glQueryResourceNV (GLenum queryType, GLuint pname, GLuint bufSize, GLint *buffer);
#endif /* GL_GLEXT_PROTOTYPES */
typedef GLint (GLAPIENTRYP PFNGLQUERYRESOURCENVXPROC) (GLenum queryType, GLuint pname, GLuint bufSize, GLint *buffer);
typedef GLint (GLAPIENTRYP PFNGLQUERYRESOURCENVPROC) (GLenum queryType, GLuint pname, GLuint bufSize, GLint *buffer);

#endif

#define NVQR_EXTENSION ((const GLubyte *)"glQueryResourceNVX")
#define NVQR_EXTENSION ((const GLubyte *)"glQueryResourceNV")

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

static PFNGLQUERYRESOURCENVXPROC glQueryResourceNVX = NULL;
static PFNGLQUERYRESOURCENVPROC glQueryResourceNV = NULL;

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

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

pthread_mutex_init(&connect_lock, NULL);

glQueryResourceNVX =
(PFNGLQUERYRESOURCENVXPROC) glXGetProcAddressARB(NVQR_EXTENSION);
glQueryResourceNV =
(PFNGLQUERYRESOURCENVPROC) glXGetProcAddressARB(NVQR_EXTENSION);

if (glQueryResourceNVX == NULL) {
if (glQueryResourceNV == NULL) {
// XXX should check extension string once extension is exported there
error_msg("failed to load %s", NVQR_EXTENSION);
return;
Expand Down
Loading

0 comments on commit 8bd0f86

Please sign in to comment.