intel/dump_gpu: Disambiguate between BOs from different GEM handle spaces.
[mesa.git] / src / intel / tools / intel_dump_gpu.c
index 6d2c4b7f983514fe66c358c50241d99bce6331b1..19e054c894c4a48b0a4a693cafd8f98e78b67497 100644 (file)
@@ -53,11 +53,12 @@ static int (*libc_close)(int fd) = close_init_helper;
 static int (*libc_ioctl)(int fd, unsigned long request, ...) = ioctl_init_helper;
 
 static int drm_fd = -1;
-static char *filename = NULL;
-static FILE *files[2] = { NULL, NULL };
+static char *output_filename = NULL;
+static FILE *output_file = NULL;
 static int verbose = 0;
 static bool device_override;
 
+#define MAX_FD_COUNT 64
 #define MAX_BO_COUNT 64 * 1024
 
 struct bo {
@@ -86,6 +87,7 @@ fail_if(int cond, const char *format, ...)
       return;
 
    va_start(args, format);
+   fprintf(stderr, "intel_dump_gpu: ");
    vfprintf(stderr, format, args);
    va_end(args);
 
@@ -93,12 +95,13 @@ fail_if(int cond, const char *format, ...)
 }
 
 static struct bo *
-get_bo(uint32_t handle)
+get_bo(unsigned fd, uint32_t handle)
 {
    struct bo *bo;
 
    fail_if(handle >= MAX_BO_COUNT, "bo handle too large\n");
-   bo = &bos[handle];
+   fail_if(fd >= MAX_FD_COUNT, "bo fd too large\n");
+   bo = &bos[handle + fd * MAX_BO_COUNT];
 
    return bo;
 }
@@ -110,11 +113,11 @@ align_u32(uint32_t v, uint32_t a)
 }
 
 static struct gen_device_info devinfo = {0};
-static uint32_t device;
-static struct aub_file aubs[2];
+static uint32_t device = 0;
+static struct aub_file aub_file;
 
 static void *
-relocate_bo(struct bo *bo, const struct drm_i915_gem_execbuffer2 *execbuffer2,
+relocate_bo(int fd, struct bo *bo, const struct drm_i915_gem_execbuffer2 *execbuffer2,
             const struct drm_i915_gem_exec_object2 *obj)
 {
    const struct drm_i915_gem_exec_object2 *exec_objects =
@@ -125,10 +128,10 @@ relocate_bo(struct bo *bo, const struct drm_i915_gem_execbuffer2 *execbuffer2,
    int handle;
 
    relocated = malloc(bo->size);
-   fail_if(relocated == NULL, "intel_aubdump: out of memory\n");
+   fail_if(relocated == NULL, "out of memory\n");
    memcpy(relocated, GET_PTR(bo->map), bo->size);
    for (size_t i = 0; i < obj->relocation_count; i++) {
-      fail_if(relocs[i].offset >= bo->size, "intel_aubdump: reloc outside bo\n");
+      fail_if(relocs[i].offset >= bo->size, "reloc outside bo\n");
 
       if (execbuffer2->flags & I915_EXEC_HANDLE_LUT)
          handle = exec_objects[relocs[i].target_handle].handle;
@@ -136,7 +139,7 @@ relocate_bo(struct bo *bo, const struct drm_i915_gem_execbuffer2 *execbuffer2,
          handle = relocs[i].target_handle;
 
       aub_write_reloc(&devinfo, ((char *)relocated) + relocs[i].offset,
-                      get_bo(handle)->offset + relocs[i].delta);
+                      get_bo(fd, handle)->offset + relocs[i].delta);
    }
 
    return relocated;
@@ -205,35 +208,27 @@ dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
       fail_if(!gen_get_device_info(device, &devinfo),
               "failed to identify chipset=0x%x\n", device);
 
-      for (int i = 0; i < ARRAY_SIZE(files); i++) {
-         if (files[i] != NULL) {
-            aub_file_init(&aubs[i], files[i], device);
-            if (verbose == 2)
-               aubs[i].verbose_log_file = stdout;
-            aub_write_header(&aubs[i], program_invocation_short_name);
-         }
-      }
+      aub_file_init(&aub_file, output_file, device);
+      if (verbose == 2)
+         aub_file.verbose_log_file = stdout;
+      aub_write_header(&aub_file, program_invocation_short_name);
 
       if (verbose)
-         printf("[intel_aubdump: running, "
-                "output file %s, chipset id 0x%04x, gen %d]\n",
-                filename, device, devinfo.gen);
+         printf("[running, output file %s, chipset id 0x%04x, gen %d]\n",
+                output_filename, device, devinfo.gen);
    }
 
-   /* Any aub */
-   struct aub_file *any_aub = files[0] ? &aubs[0] : &aubs[1];;
-
-   if (aub_use_execlists(any_aub))
+   if (aub_use_execlists(&aub_file))
       offset = 0x1000;
    else
-      offset = aub_gtt_size(any_aub);
+      offset = aub_gtt_size(&aub_file);
 
    if (verbose)
       printf("Dumping execbuffer2:\n");
 
    for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
       obj = &exec_objects[i];
-      bo = get_bo(obj->handle);
+      bo = get_bo(fd, obj->handle);
 
       /* If bo->size == 0, this means they passed us an invalid
        * buffer.  The kernel will reject it and so should we.
@@ -261,53 +256,39 @@ dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
 
       if (bo->map == NULL && bo->size > 0)
          bo->map = gem_mmap(fd, obj->handle, 0, bo->size);
-      fail_if(bo->map == MAP_FAILED, "intel_aubdump: bo mmap failed\n");
-
-      for (int i = 0; i < ARRAY_SIZE(files); i++) {
-         if (files[i] == NULL)
-            continue;
+      fail_if(bo->map == MAP_FAILED, "bo mmap failed\n");
 
-         if (aub_use_execlists(&aubs[i]))
-            aub_map_ppgtt(&aubs[i], bo->offset, bo->size);
-      }
+      if (aub_use_execlists(&aub_file))
+         aub_map_ppgtt(&aub_file, bo->offset, bo->size);
    }
 
    batch_index = (execbuffer2->flags & I915_EXEC_BATCH_FIRST) ? 0 :
       execbuffer2->buffer_count - 1;
-   batch_bo = get_bo(exec_objects[batch_index].handle);
+   batch_bo = get_bo(fd, exec_objects[batch_index].handle);
    for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
       obj = &exec_objects[i];
-      bo = get_bo(obj->handle);
+      bo = get_bo(fd, obj->handle);
 
       if (obj->relocation_count > 0)
-         data = relocate_bo(bo, execbuffer2, obj);
+         data = relocate_bo(fd, bo, execbuffer2, obj);
       else
          data = bo->map;
 
-      for (int i = 0; i < ARRAY_SIZE(files); i++) {
-         if (files[i] == NULL)
-            continue;
-
-         if (bo == batch_bo) {
-            aub_write_trace_block(&aubs[i], AUB_TRACE_TYPE_BATCH,
-                                  GET_PTR(data), bo->size, bo->offset);
-         } else {
-            aub_write_trace_block(&aubs[i], AUB_TRACE_TYPE_NOTYPE,
-                                  GET_PTR(data), bo->size, bo->offset);
-         }
+      if (bo == batch_bo) {
+         aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_BATCH,
+                               GET_PTR(data), bo->size, bo->offset);
+      } else {
+         aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_NOTYPE,
+                               GET_PTR(data), bo->size, bo->offset);
       }
+
       if (data != bo->map)
          free(data);
    }
 
-   for (int i = 0; i < ARRAY_SIZE(files); i++) {
-      if (files[i] != NULL)
-         continue;
-
-      aub_write_exec(&aubs[i],
-                     batch_bo->offset + execbuffer2->batch_start_offset,
-                     offset, ring_flag);
-   }
+   aub_write_exec(&aub_file,
+                  batch_bo->offset + execbuffer2->batch_start_offset,
+                  offset, ring_flag);
 
    if (device_override &&
        (execbuffer2->flags & I915_EXEC_FENCE_ARRAY) != 0) {
@@ -327,21 +308,22 @@ dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
 }
 
 static void
-add_new_bo(int handle, uint64_t size, void *map)
+add_new_bo(unsigned fd, int handle, uint64_t size, void *map)
 {
-   struct bo *bo = &bos[handle];
+   struct bo *bo = &bos[handle + fd * MAX_BO_COUNT];
 
-   fail_if(handle >= MAX_BO_COUNT, "intel_aubdump: bo handle out of range\n");
-   fail_if(size == 0, "intel_aubdump: bo size is invalid\n");
+   fail_if(handle >= MAX_BO_COUNT, "bo handle out of range\n");
+   fail_if(fd >= MAX_FD_COUNT, "bo fd out of range\n");
+   fail_if(size == 0, "bo size is invalid\n");
 
    bo->size = size;
    bo->map = map;
 }
 
 static void
-remove_bo(int handle)
+remove_bo(int fd, int handle)
 {
-   struct bo *bo = get_bo(handle);
+   struct bo *bo = get_bo(fd, handle);
 
    if (bo->map && !IS_USERPTR(bo->map))
       munmap(bo->map, bo->size);
@@ -358,40 +340,6 @@ close(int fd)
    return libc_close(fd);
 }
 
-static FILE *
-launch_command(char *command)
-{
-   int i = 0, fds[2];
-   char **args = calloc(strlen(command), sizeof(char *));
-   char *iter = command;
-
-   args[i++] = iter = command;
-
-   while ((iter = strstr(iter, ",")) != NULL) {
-      *iter = '\0';
-      iter += 1;
-      args[i++] = iter;
-   }
-
-   if (pipe(fds) == -1)
-      return NULL;
-
-   switch (fork()) {
-   case 0:
-      dup2(fds[0], 0);
-      fail_if(execvp(args[0], args) == -1,
-              "intel_aubdump: failed to launch child command\n");
-      return NULL;
-
-   default:
-      free(args);
-      return fdopen(fds[1], "w");
-
-   case -1:
-      return NULL;
-   }
-}
-
 static void
 maybe_init(void)
 {
@@ -404,7 +352,7 @@ maybe_init(void)
 
    initialized = true;
 
-   config = fdopen(3, "r");
+   config = fopen(getenv("INTEL_DUMP_GPU_CONFIG"), "r");
    while (fscanf(config, "%m[^=]=%m[^\n]\n", &key, &value) != EOF) {
       if (!strcmp(key, "verbose")) {
          if (!strcmp(value, "1")) {
@@ -413,23 +361,24 @@ maybe_init(void)
             verbose = 2;
          }
       } else if (!strcmp(key, "device")) {
+         fail_if(device != 0, "Device/Platform override specified multiple times.");
          fail_if(sscanf(value, "%i", &device) != 1,
-                 "intel_aubdump: failed to parse device id '%s'",
+                 "failed to parse device id '%s'",
                  value);
          device_override = true;
+      } else if (!strcmp(key, "platform")) {
+         fail_if(device != 0, "Device/Platform override specified multiple times.");
+         device = gen_device_name_to_pci_device_id(value);
+         fail_if(device == -1, "Unknown platform '%s'", value);
+         device_override = true;
       } else if (!strcmp(key, "file")) {
-         filename = strdup(value);
-         files[0] = fopen(filename, "w+");
-         fail_if(files[0] == NULL,
-                 "intel_aubdump: failed to open file '%s'\n",
-                 filename);
-      } else if (!strcmp(key,  "command")) {
-         files[1] = launch_command(value);
-         fail_if(files[1] == NULL,
-                 "intel_aubdump: failed to launch command '%s'\n",
-                 value);
+         output_filename = strdup(value);
+         output_file = fopen(output_filename, "w+");
+         fail_if(output_file == NULL,
+                 "failed to open file '%s'\n",
+                 output_filename);
       } else {
-         fprintf(stderr, "intel_aubdump: unknown option '%s'\n", key);
+         fprintf(stderr, "unknown option '%s'\n", key);
       }
 
       free(key);
@@ -437,8 +386,8 @@ maybe_init(void)
    }
    fclose(config);
 
-   bos = calloc(MAX_BO_COUNT, sizeof(bos[0]));
-   fail_if(bos == NULL, "intel_aubdump: out of memory\n");
+   bos = calloc(MAX_FD_COUNT * MAX_BO_COUNT, sizeof(bos[0]));
+   fail_if(bos == NULL, "out of memory\n");
 }
 
 __attribute__ ((visibility ("default"))) int
@@ -458,7 +407,7 @@ ioctl(int fd, unsigned long request, ...)
        (buf.st_mode & S_IFMT) == S_IFCHR && major(buf.st_rdev) == DRM_MAJOR) {
       drm_fd = fd;
       if (verbose)
-         printf("[intel_aubdump: intercept drm ioctl on fd %d]\n", fd);
+         printf("[intercept drm ioctl on fd %d]\n", fd);
    }
 
    if (fd == drm_fd) {
@@ -479,7 +428,7 @@ ioctl(int fd, unsigned long request, ...)
           * (they typically do), we'll piggy-back on
           * their ioctl and store the id for later
           * use. */
-         if (getparam->param == I915_PARAM_CHIPSET_ID)
+         if (ret == 0 && getparam->param == I915_PARAM_CHIPSET_ID)
             device = *getparam->value;
 
          return ret;
@@ -488,7 +437,7 @@ ioctl(int fd, unsigned long request, ...)
       case DRM_IOCTL_I915_GEM_EXECBUFFER: {
          static bool once;
          if (!once) {
-            fprintf(stderr, "intel_aubdump: "
+            fprintf(stderr,
                     "application uses DRM_IOCTL_I915_GEM_EXECBUFFER, not handled\n");
             once = true;
          }
@@ -509,7 +458,7 @@ ioctl(int fd, unsigned long request, ...)
 
          ret = libc_ioctl(fd, request, argp);
          if (ret == 0)
-            add_new_bo(create->handle, create->size, NULL);
+            add_new_bo(fd, create->handle, create->size, NULL);
 
          return ret;
       }
@@ -519,15 +468,16 @@ ioctl(int fd, unsigned long request, ...)
 
          ret = libc_ioctl(fd, request, argp);
          if (ret == 0)
-            add_new_bo(userptr->handle, userptr->user_size,
+            add_new_bo(fd, userptr->handle, userptr->user_size,
                        (void *) (uintptr_t) (userptr->user_ptr | USERPTR_FLAG));
+
          return ret;
       }
 
       case DRM_IOCTL_GEM_CLOSE: {
          struct drm_gem_close *close = argp;
 
-         remove_bo(close->handle);
+         remove_bo(fd, close->handle);
 
          return libc_ioctl(fd, request, argp);
       }
@@ -537,7 +487,7 @@ ioctl(int fd, unsigned long request, ...)
 
          ret = libc_ioctl(fd, request, argp);
          if (ret == 0)
-            add_new_bo(open->handle, open->size, NULL);
+            add_new_bo(fd, open->handle, open->size, NULL);
 
          return ret;
       }
@@ -550,8 +500,9 @@ ioctl(int fd, unsigned long request, ...)
             off_t size;
 
             size = lseek(prime->fd, 0, SEEK_END);
-            fail_if(size == -1, "intel_aubdump: failed to get prime bo size\n");
-            add_new_bo(prime->handle, size, NULL);
+            fail_if(size == -1, "failed to get prime bo size\n");
+            add_new_bo(fd, prime->handle, size, NULL);
+
          }
 
          return ret;
@@ -571,7 +522,7 @@ init(void)
    libc_close = dlsym(RTLD_NEXT, "close");
    libc_ioctl = dlsym(RTLD_NEXT, "ioctl");
    fail_if(libc_close == NULL || libc_ioctl == NULL,
-           "intel_aubdump: failed to get libc ioctl or close\n");
+           "failed to get libc ioctl or close\n");
 }
 
 static int
@@ -598,12 +549,7 @@ ioctl_init_helper(int fd, unsigned long request, ...)
 static void __attribute__ ((destructor))
 fini(void)
 {
-   free(filename);
-   for (int i = 0; i < ARRAY_SIZE(files); i++) {
-      if (aubs[i].file)
-         aub_file_finish(&aubs[i]);
-      else if (files[i])
-         fclose(files[i]);
-   }
+   free(output_filename);
+   aub_file_finish(&aub_file);
    free(bos);
 }