<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, Jul 18, 2018 at 7:33 AM Lionel Landwerlin <<a href="mailto:lionel.g.landwerlin@intel.com">lionel.g.landwerlin@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Awesome tool! And not even too much code!<br>
<br>
Reviewed-by: Lionel Landwerlin <<a href="mailto:lionel.g.landwerlin@intel.com" target="_blank">lionel.g.landwerlin@intel.com</a>><br></blockquote><div><br></div><div>Thanks!<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In the interest of consistency and because this is an installable <br>
binary, I would rename it.<br>
Now for the hard part : intel_error2aub ?<br></blockquote><div><br></div><div>Done.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-<br>
Lionel<br>
<br>
On 18/07/18 00:05, Jason Ekstrand wrote:<br>
> ---<br>
>   src/intel/tools/error2aub.c | 332 ++++++++++++++++++++++++++++++++++++<br>
>   src/intel/tools/meson.build |  11 ++<br>
>   2 files changed, 343 insertions(+)<br>
>   create mode 100644 src/intel/tools/error2aub.c<br>
><br>
> diff --git a/src/intel/tools/error2aub.c b/src/intel/tools/error2aub.c<br>
> new file mode 100644<br>
> index 00000000000..ece41d93e6c<br>
> --- /dev/null<br>
> +++ b/src/intel/tools/error2aub.c<br>
> @@ -0,0 +1,332 @@<br>
> +/*<br>
> + * Copyright © 2007-2017 Intel Corporation<br>
> + *<br>
> + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> + * copy of this software and associated documentation files (the "Software"),<br>
> + * to deal in the Software without restriction, including without limitation<br>
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> + * and/or sell copies of the Software, and to permit persons to whom the<br>
> + * Software is furnished to do so, subject to the following conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice (including the next<br>
> + * paragraph) shall be included in all copies or substantial portions of the<br>
> + * Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> + * IN THE SOFTWARE.<br>
> + *<br>
> + */<br>
> +<br>
> +#include <assert.h><br>
> +#include <getopt.h><br>
> +#include <inttypes.h><br>
> +#include <signal.h><br>
> +#include <stdio.h><br>
> +#include <stdlib.h><br>
> +#include <string.h><br>
> +#include <zlib.h><br>
> +<br>
> +#include "aub_write.h"<br>
> +#include "i915_drm.h"<br>
> +#include "intel_aub.h"<br>
> +<br>
> +static void __attribute__ ((format(__printf__, 2, 3)))<br>
> +fail_if(int cond, const char *format, ...)<br>
> +{<br>
> +   va_list args;<br>
> +<br>
> +   if (!cond)<br>
> +      return;<br>
> +<br>
> +   va_start(args, format);<br>
> +   vfprintf(stderr, format, args);<br>
> +   va_end(args);<br>
> +<br>
> +   raise(SIGTRAP);<br>
> +}<br>
> +<br>
> +#define fail(...) fail_if(true, __VA_ARGS__)<br>
> +<br>
> +static int zlib_inflate(uint32_t **ptr, int len)<br>
> +{<br>
> +   struct z_stream_s zstream;<br>
> +   void *out;<br>
> +   const uint32_t out_size = 128*4096;  /* approximate obj size */<br>
> +<br>
> +   memset(&zstream, 0, sizeof(zstream));<br>
> +<br>
> +   zstream.next_in = (unsigned char *)*ptr;<br>
> +   zstream.avail_in = 4*len;<br>
> +<br>
> +   if (inflateInit(&zstream) != Z_OK)<br>
> +      return 0;<br>
> +<br>
> +   out = malloc(out_size);<br>
> +   zstream.next_out = out;<br>
> +   zstream.avail_out = out_size;<br>
> +<br>
> +   do {<br>
> +      switch (inflate(&zstream, Z_SYNC_FLUSH)) {<br>
> +      case Z_STREAM_END:<br>
> +         goto end;<br>
> +      case Z_OK:<br>
> +         break;<br>
> +      default:<br>
> +         inflateEnd(&zstream);<br>
> +         return 0;<br>
> +      }<br>
> +<br>
> +      if (zstream.avail_out)<br>
> +         break;<br>
> +<br>
> +      out = realloc(out, 2*zstream.total_out);<br>
> +      if (out == NULL) {<br>
> +         inflateEnd(&zstream);<br>
> +         return 0;<br>
> +      }<br>
> +<br>
> +      zstream.next_out = (unsigned char *)out + zstream.total_out;<br>
> +      zstream.avail_out = zstream.total_out;<br>
> +   } while (1);<br>
> + end:<br>
> +   inflateEnd(&zstream);<br>
> +   free(*ptr);<br>
> +   *ptr = out;<br>
> +   return zstream.total_out / 4;<br>
> +}<br>
> +<br>
> +static int ascii85_decode(const char *in, uint32_t **out, bool inflate)<br>
> +{<br>
> +   int len = 0, size = 1024;<br>
> +<br>
> +   *out = realloc(*out, sizeof(uint32_t)*size);<br>
> +   if (*out == NULL)<br>
> +      return 0;<br>
> +<br>
> +   while (*in >= '!' && *in <= 'z') {<br>
> +      uint32_t v = 0;<br>
> +<br>
> +      if (len == size) {<br>
> +         size *= 2;<br>
> +         *out = realloc(*out, sizeof(uint32_t)*size);<br>
> +         if (*out == NULL)<br>
> +            return 0;<br>
> +      }<br>
> +<br>
> +      if (*in == 'z') {<br>
> +         in++;<br>
> +      } else {<br>
> +         v += in[0] - 33; v *= 85;<br>
> +         v += in[1] - 33; v *= 85;<br>
> +         v += in[2] - 33; v *= 85;<br>
> +         v += in[3] - 33; v *= 85;<br>
> +         v += in[4] - 33;<br>
> +         in += 5;<br>
> +      }<br>
> +      (*out)[len++] = v;<br>
> +   }<br>
> +<br>
> +   if (!inflate)<br>
> +      return len;<br>
> +<br>
> +   return zlib_inflate(out, len);<br>
> +}<br>
> +<br>
> +static void<br>
> +print_help(const char *progname, FILE *file)<br>
> +{<br>
> +   fprintf(file,<br>
> +           "Usage: %s [OPTION]... [FILE]\n"<br>
> +           "Convert an Intel GPU i915 error state to an aub file.\n"<br>
> +           "  -h, --help          display this help and exit\n"<br>
> +           "  -o, --output=FILE   the output aub file (default FILE.aub)\n",<br>
> +           progname);<br>
> +}<br>
> +<br>
> +int<br>
> +main(int argc, char *argv[])<br>
> +{<br>
> +   int i, c;<br>
> +   bool help = false;<br>
> +   char *out_filename = NULL, *in_filename = NULL;<br>
> +   const struct option aubinator_opts[] = {<br>
> +      { "help",       no_argument,       NULL,     'h' },<br>
> +      { "output",     required_argument, NULL,     'o' },<br>
> +      { NULL,         0,                 NULL,     0 }<br>
> +   };<br>
> +<br>
> +   i = 0;<br>
> +   while ((c = getopt_long(argc, argv, "ho:", aubinator_opts, &i)) != -1) {<br>
> +      switch (c) {<br>
> +      case 'h':<br>
> +         help = true;<br>
> +         break;<br>
> +      case 'o':<br>
> +         out_filename = strdup(optarg);<br>
> +         break;<br>
> +      default:<br>
> +         break;<br>
> +      }<br>
> +   }<br>
> +<br>
> +   if (optind < argc)<br>
> +      in_filename = argv[optind++];<br>
> +<br>
> +   if (help || argc == 1 || !in_filename) {<br>
> +      print_help(argv[0], stderr);<br>
> +      return in_filename ? EXIT_SUCCESS : EXIT_FAILURE;<br>
> +   }<br>
> +<br>
> +   if (out_filename == NULL) {<br>
> +      int out_filename_size = strlen(in_filename) + 5;<br>
> +      out_filename = malloc(out_filename_size);<br>
> +      snprintf(out_filename, out_filename_size, "%s.aub", in_filename);<br>
> +   }<br>
> +<br>
> +   FILE *err_file = fopen(in_filename, "r");<br>
> +   fail_if(!err_file, "Failed to open error file \"%s\": %m\n", in_filename);<br>
> +<br>
> +   FILE *aub_file = fopen(out_filename, "w");<br>
> +   fail_if(!aub_file, "Failed to open aub file \"%s\": %m\n", in_filename);<br>
> +<br>
> +   struct aub_file aub = {};<br>
> +<br>
> +   uint32_t active_ring = 0;<br>
> +   int num_ring_bos = 0;<br>
> +<br>
> +   uint64_t batch_addr = 0;<br>
> +<br>
> +   enum bo_type {<br>
> +      BO_TYPE_UNKNOWN = 0,<br>
> +      BO_TYPE_BATCH,<br>
> +      BO_TYPE_USER,<br>
> +   } bo_type;<br>
> +   uint64_t bo_addr;<br>
> +<br>
> +   char *line = NULL;<br>
> +   size_t line_size;<br>
> +   while (getline(&line, &line_size, err_file) > 0) {<br>
> +      const char *pci_id_start = strstr(line, "PCI ID");<br>
> +      if (pci_id_start) {<br>
> +         int pci_id;<br>
> +         int matched = sscanf(line, "PCI ID: 0x%04x\n", &pci_id);<br>
> +         fail_if(!matched, "Invalid error state file!\n");<br>
> +<br>
> +         aub_file_init(&aub, aub_file, pci_id);<br>
> +         fail_if(!aub_use_execlists(&aub),<br>
> +                 "%s currently only works on gen8+\n", argv[0]);<br>
> +<br>
> +         aub_write_header(&aub, "error state");<br>
> +         continue;<br>
> +      }<br>
> +<br>
> +      const char *active_start = "Active (";<br>
> +      if (strncmp(line, active_start, strlen(active_start)) == 0) {<br>
> +         fail_if(active_ring != 0, "TODO: Handle multiple active rings\n");<br>
> +<br>
> +         char *ring = line + strlen(active_start);<br>
> +<br>
> +         const struct {<br>
> +            const char *match;<br>
> +            uint32_t ring;<br>
> +         } rings[] = {<br>
> +            { "rcs", I915_EXEC_RENDER },<br>
> +            { "vcs", I915_EXEC_VEBOX },<br>
> +            { "bcs", I915_EXEC_BLT },<br>
> +            { NULL, BO_TYPE_UNKNOWN },<br>
> +         }, *r;<br>
> +<br>
> +         for (r = rings; r->match; r++) {<br>
> +            if (strncasecmp(ring, r->match, strlen(r->match)) == 0) {<br>
> +               active_ring = r->ring;<br>
> +               break;<br>
> +            }<br>
> +         }<br>
> +<br>
> +         char *count = strchr(ring, '[');<br>
> +         fail_if(!count || sscanf(count, "[%d]:", &num_ring_bos) < 1,<br>
> +                 "Failed to parse BO table header\n");<br>
> +         continue;<br>
> +      }<br>
> +<br>
> +      if (num_ring_bos > 0) {<br>
> +         unsigned hi, lo, size;<br>
> +         if (sscanf(line, " %x_%x %d", &hi, &lo, &size) == 3) {<br>
> +            assert(aub_use_execlists(&aub));<br>
> +            aub_map_ppgtt(&aub, ((uint64_t)hi) << 32 | lo, size);<br>
> +            num_ring_bos--;<br>
> +         } else {<br>
> +            fail("Not enough BO entries in the active table\n");<br>
> +         }<br>
> +         continue;<br>
> +      }<br>
> +<br>
> +      if (line[0] == ':' || line[0] == '~') {<br>
> +         if (bo_type == BO_TYPE_UNKNOWN)<br>
> +            continue;<br>
> +<br>
> +         uint32_t *data = NULL;<br>
> +         int count = ascii85_decode(line+1, &data, line[0] == ':');<br>
> +         fail_if(count == 0, "ASCII85 decode failed.\n");<br>
> +         uint64_t bo_size = count * 4;<br>
> +<br>
> +         if (bo_type == BO_TYPE_BATCH) {<br>
> +            aub_write_trace_block(&aub, AUB_TRACE_TYPE_BATCH,<br>
> +                                  data, bo_size, bo_addr);<br>
> +            batch_addr = bo_addr;<br>
> +         } else {<br>
> +            assert(bo_type == BO_TYPE_USER);<br>
> +            aub_write_trace_block(&aub, AUB_TRACE_TYPE_NOTYPE,<br>
> +                                  data, bo_size, bo_addr);<br>
> +         }<br>
> +<br>
> +         continue;<br>
> +      }<br>
> +<br>
> +      char *dashes = strstr(line, "---");<br>
> +      if (dashes) {<br>
> +         dashes += 4;<br>
> +<br>
> +         const struct {<br>
> +            const char *match;<br>
> +            enum bo_type type;<br>
> +         } bo_types[] = {<br>
> +            { "gtt_offset", BO_TYPE_BATCH },<br>
> +            { "user", BO_TYPE_USER },<br>
> +            { NULL, BO_TYPE_UNKNOWN },<br>
> +         }, *b;<br>
> +<br>
> +         bo_type = BO_TYPE_UNKNOWN;<br>
> +         for (b = bo_types; b->match; b++) {<br>
> +            if (strncasecmp(dashes, b->match, strlen(b->match)) == 0) {<br>
> +               bo_type = b->type;<br>
> +               break;<br>
> +            }<br>
> +         }<br>
> +<br>
> +         if (bo_type != BO_TYPE_UNKNOWN) {<br>
> +            uint32_t hi, lo;<br>
> +            dashes = strchr(dashes, '=');<br>
> +            if (dashes && sscanf(dashes, "= 0x%08x %08x\n", &hi, &lo) == 2) {<br>
> +               bo_addr = ((uint64_t) hi) << 32 | lo;<br>
> +            } else {<br>
> +               fail("User BO does not have an address\n");<br>
> +            }<br>
> +         }<br>
> +         continue;<br>
> +      }<br>
> +   }<br>
> +<br>
> +   fail_if(!batch_addr, "Failed to find batch buffer.\n");<br>
> +<br>
> +   aub_write_exec(&aub, batch_addr, aub_gtt_size(&aub), I915_EXEC_RENDER);<br>
> +<br>
> +   return EXIT_SUCCESS;<br>
> +}<br>
> +<br>
> +/* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/<br>
> diff --git a/src/intel/tools/meson.build b/src/intel/tools/meson.build<br>
> index b4c23e01db2..1a6f8b2eebf 100644<br>
> --- a/src/intel/tools/meson.build<br>
> +++ b/src/intel/tools/meson.build<br>
> @@ -40,6 +40,17 @@ aubinator_error_decode = executable(<br>
>     install : with_tools.contains('intel'),<br>
>   )<br>
>   <br>
> +error2aub = executable(<br>
> +  'error2aub',<br>
> +  files('aub_write.h', 'aub_write.c', 'error2aub.c'),<br>
> +  dependencies : [dep_zlib, dep_dl, dep_thread, dep_m],<br>
> +  include_directories : [inc_common, inc_intel, inc_drm_uapi],<br>
> +  link_with : [libintel_dev],<br>
> +  c_args : [c_vis_args, no_override_init_args],<br>
> +  build_by_default : with_tools.contains('intel'),<br>
> +  install : with_tools.contains('intel'),<br>
> +)<br>
> +<br>
>   if with_tools.contains('intel')<br>
>     sanitize_data = configuration_data()<br>
>     sanitize_data.set(<br>
<br>
<br>
</blockquote></div></div>