lima_util.c revision 9f464c52
1/*
2 * Copyright (C) 2018-2019 Lima Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
24#include <stdio.h>
25#include <stdarg.h>
26#include <time.h>
27
28#include <pipe/p_defines.h>
29
30#include "lima_util.h"
31
32FILE *lima_dump_command_stream = NULL;
33
34bool lima_get_absolute_timeout(uint64_t *timeout)
35{
36   struct timespec current;
37   uint64_t current_ns;
38
39   if (*timeout == PIPE_TIMEOUT_INFINITE)
40      return true;
41
42   if (clock_gettime(CLOCK_MONOTONIC, &current))
43      return false;
44
45   current_ns = ((uint64_t)current.tv_sec) * 1000000000ull;
46   current_ns += current.tv_nsec;
47   *timeout += current_ns;
48
49   return true;
50}
51
52void lima_dump_blob(FILE *fp, void *data, int size, bool is_float)
53{
54   for (int i = 0; i * 4 < size; i++) {
55      if (i % 4 == 0) {
56         if (i) fprintf(fp, "\n");
57         fprintf(fp, "%04x:", i * 4);
58      }
59
60      if (is_float)
61         fprintf(fp, " %f", ((float *)data)[i]);
62      else
63         fprintf(fp, " 0x%08x", ((uint32_t *)data)[i]);
64   }
65   fprintf(fp, "\n");
66}
67
68void
69lima_dump_command_stream_print(void *data, int size, bool is_float,
70                               const char *fmt, ...)
71{
72   if (lima_dump_command_stream) {
73      va_list ap;
74      va_start(ap, fmt);
75      vfprintf(lima_dump_command_stream, fmt, ap);
76      va_end(ap);
77
78      lima_dump_blob(lima_dump_command_stream, data, size, is_float);
79   }
80}
81