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

kevin.rogovin at intel.com kevin.rogovin at intel.com
Mon Nov 13 13:17:53 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       | 34 ++++++++++++++++++++
 src/mesa/drivers/dri/i965/brw_context.h       | 12 +++++++
 src/mesa/drivers/dri/i965/intel_batchbuffer.c | 29 +++++++++++++++--
 src/mesa/drivers/dri/i965/intel_screen.c      | 46 +++++++++++++++++++++++++--
 src/mesa/drivers/dri/i965/intel_screen.h      |  3 ++
 7 files changed, 146 insertions(+), 8 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_bufmgr.c b/src/mesa/drivers/dri/i965/brw_bufmgr.c
index 17036b53bc..6a70fc50d8 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;
@@ -621,9 +624,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->aborted_batchbuffer(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 &&
@@ -1064,6 +1072,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)
@@ -1367,9 +1381,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)
@@ -1385,6 +1404,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 ee91324043..f4bffef4e4 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 19d5a2e350..456316deb7 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
  ***************************************/
@@ -1062,6 +1064,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);
 
@@ -1076,6 +1079,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) {
@@ -1726,3 +1730,33 @@ 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;
+
+      brw_get_active_batchbuffer(brw, &dst);
+      va_start(args, fmt);
+      logger->add_message(logger, &dst, fmt, args);
+      va_end(args);
+   }
+}
+
+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.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 8aa0c5ff64..15c8d43799 100644
--- a/src/mesa/drivers/dri/i965/brw_context.h
+++ b/src/mesa/drivers/dri/i965/brw_context.h
@@ -142,6 +142,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,
@@ -1226,6 +1227,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;
 };
@@ -1673,6 +1680,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 1a366c78b0..8cae20e0b1 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>
 
@@ -294,10 +296,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;
@@ -315,6 +319,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).
     *
@@ -377,7 +399,7 @@ intel_batchbuffer_require_space(struct brw_context *brw, GLuint sz,
          const unsigned new_size =
             MIN2(batch->bo->size + batch->bo->size / 2, MAX_BATCH_SIZE);
          grow_buffer(brw, &batch->bo, &batch->map, &batch->batch_cpu_map,
-                     batch_used, new_size);
+                     batch_used, new_size, true);
          batch->map_next = (void *) batch->map + batch_used;
          assert(batch_used + sz < batch->bo->size);
       }
@@ -1056,7 +1078,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 3d238769ad..5e5107e75e 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 =
@@ -1492,7 +1497,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);
 
@@ -1661,12 +1671,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__);
@@ -2251,6 +2259,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->bo->gem_handle == st->gem_bo);
+   return batch->map_next - batch->map;
+}
+
+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.
@@ -2261,6 +2289,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 ||
@@ -2549,6 +2578,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.14.2



More information about the mesa-dev mailing list