[Mesa-dev] [PATCH 05/16] i965: Enable BatchbufferLogger in i965 driver

kevin.rogovin at intel.com kevin.rogovin at intel.com
Tue Dec 12 07:44:06 UTC 2017


From: Kevin Rogovin <kevin.rogovin at intel.com>

The interface for BatchbufferLogger is that it is active
only if it is LD_PRELOAD'ed. Thus, the i965 driver is to
use dlsym to see if it is there, and if so fetch the object
at intel_screen creation.

Signed-off-by: Kevin Rogovin <kevin.rogovin at intel.com>
---
 src/mesa/drivers/dri/i965/brw_bufmgr.c        | 22 ++++++++++++-
 src/mesa/drivers/dri/i965/brw_bufmgr.h        |  8 ++++-
 src/mesa/drivers/dri/i965/brw_context.c       | 37 +++++++++++++++++++++
 src/mesa/drivers/dri/i965/brw_context.h       | 12 +++++++
 src/mesa/drivers/dri/i965/intel_batchbuffer.c | 30 +++++++++++++++--
 src/mesa/drivers/dri/i965/intel_screen.c      | 46 +++++++++++++++++++++++++--
 src/mesa/drivers/dri/i965/intel_screen.h      |  3 ++
 7 files changed, 150 insertions(+), 8 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_bufmgr.c b/src/mesa/drivers/dri/i965/brw_bufmgr.c
index 52b5bf97a1..c5b1bfb83d 100644
--- a/src/mesa/drivers/dri/i965/brw_bufmgr.c
+++ b/src/mesa/drivers/dri/i965/brw_bufmgr.c
@@ -65,6 +65,8 @@
 #include "string.h"
 
 #include "i915_drm.h"
+#include "intel_screen.h"
+#include "tools/i965_batchbuffer_logger.h"
 
 #ifdef HAVE_VALGRIND
 #include <valgrind.h>
@@ -104,6 +106,7 @@ struct bo_cache_bucket {
 };
 
 struct brw_bufmgr {
+   struct intel_screen *screen;
    int fd;
 
    mtx_t lock;
@@ -631,9 +634,14 @@ bo_unreference_final(struct brw_bo *bo, time_t time)
 {
    struct brw_bufmgr *bufmgr = bo->bufmgr;
    struct bo_cache_bucket *bucket;
+   struct i965_batchbuffer_logger *bb_logger =
+      bufmgr->screen->batchbuffer_logger;
 
    DBG("bo_unreference final: %d (%s)\n", bo->gem_handle, bo->name);
 
+   if(bb_logger != NULL) {
+      bb_logger->clear_batchbuffer_log(bb_logger, bufmgr->fd, bo->gem_handle);
+   }
    bucket = bucket_for_size(bufmgr, bo->size);
    /* Put the buffer into our internal cache for reuse if we can. */
    if (bufmgr->bo_reuse && bo->reusable && bucket != NULL &&
@@ -1065,6 +1073,12 @@ brw_bufmgr_destroy(struct brw_bufmgr *bufmgr)
    free(bufmgr);
 }
 
+int
+brw_bufmgr_fd(const struct brw_bufmgr *bufmgr)
+{
+   return bufmgr->fd;
+}
+
 static int
 bo_set_tiling_internal(struct brw_bo *bo, uint32_t tiling_mode,
                        uint32_t stride)
@@ -1369,9 +1383,14 @@ gem_param(int fd, int name)
  * \param fd File descriptor of the opened DRM device.
  */
 struct brw_bufmgr *
-brw_bufmgr_init(struct gen_device_info *devinfo, int fd)
+brw_bufmgr_init(struct intel_screen *screen)
 {
    struct brw_bufmgr *bufmgr;
+   struct gen_device_info *devinfo;
+   int fd;
+
+   devinfo = &screen->devinfo;
+   fd = screen->driScrnPriv->fd;
 
    bufmgr = calloc(1, sizeof(*bufmgr));
    if (bufmgr == NULL)
@@ -1387,6 +1406,7 @@ brw_bufmgr_init(struct gen_device_info *devinfo, int fd)
     * fd so that its namespace does not clash with another.
     */
    bufmgr->fd = fd;
+   bufmgr->screen = screen;
 
    if (mtx_init(&bufmgr->lock, mtx_plain) != 0) {
       free(bufmgr);
diff --git a/src/mesa/drivers/dri/i965/brw_bufmgr.h b/src/mesa/drivers/dri/i965/brw_bufmgr.h
index 0ae541cda0..b5d724be7b 100644
--- a/src/mesa/drivers/dri/i965/brw_bufmgr.h
+++ b/src/mesa/drivers/dri/i965/brw_bufmgr.h
@@ -46,6 +46,7 @@ extern "C" {
 
 struct gen_device_info;
 struct brw_context;
+struct intel_screen;
 
 struct brw_bo {
    /**
@@ -274,6 +275,11 @@ void brw_bo_wait_rendering(struct brw_bo *bo);
  */
 void brw_bufmgr_destroy(struct brw_bufmgr *bufmgr);
 
+/**
+ * Returns the file descriptor of the buffer manager
+ */
+int brw_bufmgr_fd(const struct brw_bufmgr *bufmgr);
+
 /**
  * Get the current tiling (and resulting swizzling) mode for the bo.
  *
@@ -313,7 +319,7 @@ int brw_bo_busy(struct brw_bo *bo);
 int brw_bo_madvise(struct brw_bo *bo, int madv);
 
 /* drm_bacon_bufmgr_gem.c */
-struct brw_bufmgr *brw_bufmgr_init(struct gen_device_info *devinfo, int fd);
+struct brw_bufmgr *brw_bufmgr_init(struct intel_screen *screen);
 struct brw_bo *brw_bo_gem_create_from_name(struct brw_bufmgr *bufmgr,
                                            const char *name,
                                            unsigned int handle);
diff --git a/src/mesa/drivers/dri/i965/brw_context.c b/src/mesa/drivers/dri/i965/brw_context.c
index 126c187f62..b5e041bb7b 100644
--- a/src/mesa/drivers/dri/i965/brw_context.c
+++ b/src/mesa/drivers/dri/i965/brw_context.c
@@ -75,6 +75,8 @@
 #include "util/debug.h"
 #include "isl/isl.h"
 
+#include "tools/i965_batchbuffer_logger.h"
+
 /***************************************
  * Mesa's Driver Functions
  ***************************************/
@@ -1075,6 +1077,7 @@ brwCreateContext(gl_api api,
 
    vbo_use_buffer_objects(ctx);
    vbo_always_unmap_buffers(ctx);
+   brw->have_active_batchbuffer = true;
 
    brw_disk_cache_init(brw);
 
@@ -1089,6 +1092,7 @@ intelDestroyContext(__DRIcontext * driContextPriv)
    struct gl_context *ctx = &brw->ctx;
    const struct gen_device_info *devinfo = &brw->screen->devinfo;
 
+   brw->have_active_batchbuffer = false;
    _mesa_meta_free(&brw->ctx);
 
    if (INTEL_DEBUG & DEBUG_SHADER_TIME) {
@@ -1739,3 +1743,36 @@ intel_update_image_buffers(struct brw_context *brw, __DRIdrawable *drawable)
                                 __DRI_IMAGE_BUFFER_BACK);
    }
 }
+
+void
+brw_batchbuffer_log(struct brw_context *brw, const char *fmt, ...)
+{
+   struct i965_batchbuffer_logger *logger = brw->screen->batchbuffer_logger;
+   if (logger != NULL) {
+      struct i965_logged_batchbuffer dst;
+      va_list args;
+      char buffer[4096];
+
+      brw_get_active_batchbuffer(brw, &dst);
+      va_start(args, fmt);
+      vsnprintf(buffer, sizeof(buffer), fmt, args);
+      buffer[sizeof(buffer) - 1] = 0;
+      va_end(args);
+      logger->add_message(logger, &dst, buffer);
+   }
+}
+
+void
+brw_get_active_batchbuffer(struct brw_context *brw,
+                           struct i965_logged_batchbuffer *dst)
+{
+  if (brw != NULL && brw->have_active_batchbuffer) {
+      dst->driver_data = &brw->batch;
+      dst->gem_bo = brw->batch.batch.bo->gem_handle;
+      dst->fd = brw_bufmgr_fd(brw->bufmgr);
+   } else {
+      dst->driver_data = NULL;
+      dst->gem_bo = 0;
+      dst->fd = -1;
+   }
+}
diff --git a/src/mesa/drivers/dri/i965/brw_context.h b/src/mesa/drivers/dri/i965/brw_context.h
index 0f0aad8534..d5da4493cc 100644
--- a/src/mesa/drivers/dri/i965/brw_context.h
+++ b/src/mesa/drivers/dri/i965/brw_context.h
@@ -143,6 +143,7 @@ struct brw_wm_prog_key;
 struct brw_wm_prog_data;
 struct brw_cs_prog_key;
 struct brw_cs_prog_data;
+struct i965_logged_batchbuffer;
 
 enum brw_pipeline {
    BRW_RENDER_PIPELINE,
@@ -1263,6 +1264,12 @@ struct brw_context
     */
    bool draw_aux_buffer_disabled[MAX_DRAW_BUFFERS];
 
+   /* Set to true if the brw_context is in a state (i.e. not in the middle
+    * of construction or deconstruction) that it has an active batchbuffer
+    * and it can report "where" it is in that batchbuffer.
+    */
+   bool have_active_batchbuffer;
+
    __DRIcontext *driContext;
    struct intel_screen *screen;
 };
@@ -1695,6 +1702,11 @@ void brw_query_internal_format(struct gl_context *ctx, GLenum target,
                                GLenum internalFormat, GLenum pname,
                                GLint *params);
 
+/* i965_batchbuffer_logger */
+void brw_batchbuffer_log(struct brw_context *brw, const char *fmt, ...);
+void brw_get_active_batchbuffer(struct brw_context *brw,
+                                struct i965_logged_batchbuffer *dst);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/mesa/drivers/dri/i965/intel_batchbuffer.c b/src/mesa/drivers/dri/i965/intel_batchbuffer.c
index 91a6506a89..efa483b2a7 100644
--- a/src/mesa/drivers/dri/i965/intel_batchbuffer.c
+++ b/src/mesa/drivers/dri/i965/intel_batchbuffer.c
@@ -35,6 +35,8 @@
 
 #include "util/hash_table.h"
 
+#include "tools/i965_batchbuffer_logger.h"
+
 #include <xf86drm.h>
 #include <i915_drm.h>
 
@@ -286,10 +288,12 @@ grow_buffer(struct brw_context *brw,
             uint32_t **map_ptr,
             uint32_t **cpu_map_ptr,
             unsigned existing_bytes,
-            unsigned new_size)
+            unsigned new_size,
+            bool is_commandbuffer)
 {
    struct intel_batchbuffer *batch = &brw->batch;
    struct brw_bufmgr *bufmgr = brw->bufmgr;
+   struct i965_batchbuffer_logger *logger = brw->screen->batchbuffer_logger;
 
    uint32_t *old_map = *map_ptr;
    struct brw_bo *old_bo = *bo_ptr;
@@ -308,6 +312,24 @@ grow_buffer(struct brw_context *brw,
       memcpy(new_map, old_map, existing_bytes);
    }
 
+   /* if BatchbufferLogger is active we need to tell it about the migration
+    * to the new batchbuffer so that API calls are correctly associated to
+    * the new batchbuffer.
+    */
+   if (is_commandbuffer && logger) {
+      struct i965_logged_batchbuffer from, to;
+      int fd = brw_bufmgr_fd(bufmgr);
+
+      from.gem_bo = old_bo->gem_handle;
+      from.fd = fd;
+
+      to.gem_bo = new_bo->gem_handle;
+      to.fd = fd;
+      to.driver_data = &brw->batch;
+
+      logger->migrate_batchbuffer(logger, &from, &to);
+   }
+
    /* Try to put the new BO at the same GTT offset as the old BO (which
     * we're throwing away, so it doesn't need to be there).
     *
@@ -373,7 +395,8 @@ intel_batchbuffer_require_space(struct brw_context *brw, GLuint sz,
          MIN2(batch->batch.bo->size + batch->batch.bo->size / 2,
               MAX_BATCH_SIZE);
       grow_buffer(brw, &batch->batch.bo, &batch->batch.map,
-                  &batch->batch.cpu_map, batch_used, new_size);
+                  &batch->batch.cpu_map, batch_used, new_size,
+                  true);
       batch->map_next = (void *) batch->batch.map + batch_used;
       assert(batch_used + sz < batch->batch.bo->size);
    }
@@ -1074,7 +1097,8 @@ brw_state_batch(struct brw_context *brw,
          MIN2(batch->state.bo->size + batch->state.bo->size / 2,
               MAX_STATE_SIZE);
       grow_buffer(brw, &batch->state.bo, &batch->state.map,
-                  &batch->state.cpu_map, batch->state_used, new_size);
+                  &batch->state.cpu_map, batch->state_used,
+                  new_size, false);
       assert(offset + size < batch->state.bo->size);
    }
 
diff --git a/src/mesa/drivers/dri/i965/intel_screen.c b/src/mesa/drivers/dri/i965/intel_screen.c
index 0c17b4050c..69dee18c7d 100644
--- a/src/mesa/drivers/dri/i965/intel_screen.c
+++ b/src/mesa/drivers/dri/i965/intel_screen.c
@@ -23,6 +23,7 @@
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include <dlfcn.h>
 #include <drm_fourcc.h>
 #include <errno.h>
 #include <time.h>
@@ -39,10 +40,14 @@
 #include "brw_defines.h"
 #include "brw_state.h"
 #include "compiler/nir/nir.h"
+#include "intel_batchbuffer.h"
+#include "brw_context.h"
 
 #include "utils.h"
 #include "util/xmlpool.h"
 
+#include "tools/i965_batchbuffer_logger.h"
+
 static const __DRIconfigOptionsExtension brw_config_options = {
    .base = { __DRI_CONFIG_OPTIONS, 1 },
    .xml =
@@ -1554,7 +1559,12 @@ static void
 intelDestroyScreen(__DRIscreen * sPriv)
 {
    struct intel_screen *screen = sPriv->driverPrivate;
+   struct i965_batchbuffer_logger *batchbuffer_logger;
 
+   batchbuffer_logger = screen->batchbuffer_logger;
+   if(batchbuffer_logger != NULL) {
+      batchbuffer_logger->release_driver(batchbuffer_logger);
+   }
    brw_bufmgr_destroy(screen->bufmgr);
    driDestroyOptionInfo(&screen->optionCache);
 
@@ -1744,12 +1754,10 @@ err_out:
 static bool
 intel_init_bufmgr(struct intel_screen *screen)
 {
-   __DRIscreen *dri_screen = screen->driScrnPriv;
-
    if (getenv("INTEL_NO_HW") != NULL)
       screen->no_hw = true;
 
-   screen->bufmgr = brw_bufmgr_init(&screen->devinfo, dri_screen->fd);
+   screen->bufmgr = brw_bufmgr_init(screen);
    if (screen->bufmgr == NULL) {
       fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
 	      __func__, __LINE__);
@@ -2334,6 +2342,26 @@ get_pci_device_id(struct intel_screen *screen)
    return intel_get_integer(screen, I915_PARAM_CHIPSET_ID);
 }
 
+static
+uint32_t
+intel_batchbuffer_state(const struct i965_logged_batchbuffer *st)
+{
+   struct intel_batchbuffer *batch
+      = (struct intel_batchbuffer*) st->driver_data;
+
+   assert(batch->batch.bo->gem_handle == st->gem_bo);
+   return USED_BATCH(*batch);
+}
+
+static
+void
+intel_active_batchbuffer(struct i965_logged_batchbuffer *dst)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   struct brw_context *brw = brw_context(ctx);
+   brw_get_active_batchbuffer(brw, dst);
+}
+
 /**
  * This is the driver specific part of the createNewScreen entry point.
  * Called when using DRI2.
@@ -2344,6 +2372,7 @@ static const
 __DRIconfig **intelInitScreen2(__DRIscreen *dri_screen)
 {
    struct intel_screen *screen;
+   i965_batchbuffer_logger_acquire_fcn i965_batchbuffer_logger_acquire;
 
    if (dri_screen->image.loader) {
    } else if (dri_screen->dri2.loader->base.version <= 2 ||
@@ -2635,6 +2664,17 @@ __DRIconfig **intelInitScreen2(__DRIscreen *dri_screen)
       }
    }
 
+   i965_batchbuffer_logger_acquire = (i965_batchbuffer_logger_acquire_fcn)
+      dlsym(NULL, "i965_batchbuffer_logger_acquire");
+   if(i965_batchbuffer_logger_acquire) {
+      screen->batchbuffer_logger =
+         i965_batchbuffer_logger_acquire(screen->deviceID,
+                                         intel_batchbuffer_state,
+                                         intel_active_batchbuffer);
+   } else {
+      screen->batchbuffer_logger = NULL;
+   }
+
    return (const __DRIconfig**) intel_screen_make_configs(dri_screen);
 }
 
diff --git a/src/mesa/drivers/dri/i965/intel_screen.h b/src/mesa/drivers/dri/i965/intel_screen.h
index 7948617b7f..8394344db3 100644
--- a/src/mesa/drivers/dri/i965/intel_screen.h
+++ b/src/mesa/drivers/dri/i965/intel_screen.h
@@ -44,6 +44,7 @@
 extern "C" {
 #endif
 
+struct i965_batchbuffer_logger;
 struct intel_screen
 {
    int deviceID;
@@ -117,6 +118,8 @@ struct intel_screen
    bool mesa_format_supports_texture[MESA_FORMAT_COUNT];
    bool mesa_format_supports_render[MESA_FORMAT_COUNT];
    enum isl_format mesa_to_isl_render_format[MESA_FORMAT_COUNT];
+
+   struct i965_batchbuffer_logger *batchbuffer_logger;
 };
 
 extern void intelDestroyContext(__DRIcontext * driContextPriv);
-- 
2.15.0



More information about the mesa-dev mailing list