File: pocl_debug.c

package info (click to toggle)
pocl 6.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,320 kB
  • sloc: lisp: 149,513; ansic: 103,778; cpp: 54,947; python: 1,513; sh: 949; ruby: 255; pascal: 226; tcl: 180; makefile: 175; java: 72; xml: 49
file content (216 lines) | stat: -rw-r--r-- 8,411 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* OpenCL runtime library: PoCL debug functions

   Copyright (c) 2015-2023 PoCL developers

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to
   deal in the Software without restriction, including without limitation the
   rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
   sell copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
   IN THE SOFTWARE.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pocl_debug.h"
#include "pocl_threads.h"
#include "pocl_timing.h"

#ifdef POCL_DEBUG_MESSAGES

uint64_t pocl_debug_messages_filter; /* Bitfield */
int pocl_stderr_is_a_tty;

static pocl_lock_t console_mutex;

void pocl_debug_output_lock(void) { POCL_LOCK(console_mutex); }

void pocl_debug_output_unlock(void) { POCL_UNLOCK(console_mutex); }

void pocl_debug_messages_setup(const char *debug) {
  POCL_INIT_LOCK(console_mutex);
  pocl_debug_messages_filter = 0;
  if (strlen(debug) == 1) {
    if (debug[0] == '1')
      pocl_debug_messages_filter = POCL_DEBUG_FLAG_GENERAL |
                                   POCL_DEBUG_FLAG_WARNING |
                                   POCL_DEBUG_FLAG_ERROR;
    return;
  }
  /* else parse */
  char *tokenize = strdup(debug);
  for (size_t i = 0; i < strlen (tokenize); i++)
  {
    tokenize[i] = tolower(tokenize[i]);
  }
  char *ptr = NULL;
  ptr = strtok(tokenize, ",");

  while (ptr != NULL) {
    if (strncmp(ptr, "general", 7) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_GENERAL;
    else if (strncmp(ptr, "level0", 6) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_LEVEL0;
    else if (strncmp(ptr, "vulkan", 6) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_VULKAN;
    else if (strncmp(ptr, "remote", 6) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_REMOTE;
    else if (strncmp(ptr, "event", 5) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_EVENTS;
    else if (strncmp(ptr, "cache", 5) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_CACHE;
    else if (strncmp(ptr, "proxy", 5) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_PROXY;
    else if (strncmp(ptr, "llvm", 4) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_LLVM;
    else if (strncmp(ptr, "refc", 4) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_REFCOUNTS;
    else if (strncmp(ptr, "lock", 4) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_LOCKING;
    else if (strncmp(ptr, "cuda", 4) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_CUDA;
    else if (strncmp(ptr, "almaif", 6) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_ALMAIF;
    else if (strncmp(ptr, "mmap", 4) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_ALMAIF_MMAP;
    else if (strncmp(ptr, "warn", 4) == 0)
      pocl_debug_messages_filter |=
          (POCL_DEBUG_FLAG_WARNING | POCL_DEBUG_FLAG_ERROR);
    else if (strncmp(ptr, "hsa", 3) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_HSA;
    else if (strncmp(ptr, "tce", 3) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_TCE;
    else if (strncmp(ptr, "mem", 3) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_MEMORY;
    else if (strncmp(ptr, "tim", 3) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_TIMING;
    else if (strncmp(ptr, "all", 3) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_ALL;
    else if (strncmp(ptr, "err", 3) == 0)
      pocl_debug_messages_filter |= POCL_DEBUG_FLAG_ERROR;
    else
      POCL_MSG_WARN("Unknown token in POCL_DEBUG env var: %s", ptr);

    ptr = strtok(NULL, ",");
  }

  free(tokenize);
}

void pocl_debug_print_header(const char *func, unsigned line,
                             const char *filter, int filter_type) {

  int year, mon, day, hour, min, sec, nanosec;
  pocl_gettimereal(&year, &mon, &day, &hour, &min, &sec, &nanosec);

  const char *filter_type_str;
  const char *formatstring;

  if (filter_type == POCL_FILTER_TYPE_ERR)
    filter_type_str =
        (pocl_stderr_is_a_tty ? POCL_COLOR_RED : " *** ERROR *** ");
  else if (filter_type == POCL_FILTER_TYPE_WARN)
    filter_type_str =
        (pocl_stderr_is_a_tty ? POCL_COLOR_YELLOW : " *** WARNING *** ");
  else if (filter_type == POCL_FILTER_TYPE_INFO)
    filter_type_str =
        (pocl_stderr_is_a_tty ? POCL_COLOR_GREEN : " *** INFO *** ");
  else
    filter_type_str =
        (pocl_stderr_is_a_tty ? POCL_COLOR_GREEN : " *** UNKNOWN *** ");

#ifndef POCL_DEBUG_LOG_PREFIX
#define POCL_DEBUG_LOG_PREFIX "PoCL"
#endif

  if (pocl_stderr_is_a_tty)
    formatstring = POCL_COLOR_BLUE
        "[%04i-%02i-%02i %02i:%02i:%02i.%09li] " POCL_COLOR_RESET
        "" POCL_DEBUG_LOG_PREFIX ": in fn %s " POCL_COLOR_RESET
        "at line %u:\n%s | %9s | ";
  else
    /* Print the log entries to a single line to enable merging of
       PoCL-R and PoCL-D logs with 'sort client.log server.log'. */
    formatstring
        = "[%04i-%02i-%02i %02i:%02i:%02i.%09i] " POCL_DEBUG_LOG_PREFIX
          ": in fn %s at line %u: %s | %9s | ";

  log_printf(formatstring, year, mon, day, hour, min, sec, nanosec, func, line,
             filter_type_str, filter);
}

void pocl_debug_measure_start(uint64_t *start) {
  *start = pocl_gettimemono_ns();
}

#define PRINT_DURATION(func, line, ...)                                        \
  do {                                                                         \
    pocl_debug_output_lock();                                                  \
    pocl_debug_print_header(func, line, "TIMING", POCL_FILTER_TYPE_INFO);      \
    log_printf(__VA_ARGS__);                                                   \
    pocl_debug_output_unlock();                                                \
  } while (0)

void pocl_debug_print_duration(const char *func, unsigned line, const char *msg,
                               uint64_t nanosecs) {
  if (!(pocl_debug_messages_filter & POCL_DEBUG_FLAG_TIMING))
    return;
  const char *formatstring;
  if (pocl_stderr_is_a_tty)
    formatstring = "      >>>  " POCL_COLOR_MAGENTA "     %3" PRIu64
                   ".%03" PRIu64 " " POCL_COLOR_RESET " %s    %s\n";
  else
    formatstring = "      >>>       %3" PRIu64 ".%03" PRIu64 "  %s    %s\n";

  uint64_t nsec = nanosecs % 1000000000;
  uint64_t sec = nanosecs / 1000000000;
  uint64_t a, b;

  if ((sec == 0) && (nsec < 1000)) {
    b = nsec % 1000;
    if (pocl_stderr_is_a_tty)
      formatstring = "      >>>      " POCL_COLOR_MAGENTA "     %3" PRIu64
                     " " POCL_COLOR_RESET " ns    %s\n";
    else
      formatstring = "      >>>           %3" PRIu64 "  ns    %s\n";
    PRINT_DURATION(func, line, formatstring, b, msg);
  } else if ((sec == 0) && (nsec < 1000000)) {
    a = nsec / 1000;
    b = nsec % 1000;
    PRINT_DURATION(func, line, formatstring, a, b, "us", msg);
  } else if (sec == 0) {
    a = nsec / 1000000;
    b = (nsec % 1000000) / 1000;
    PRINT_DURATION(func, line, formatstring, a, b, "ms", msg);
  } else {
    if (pocl_stderr_is_a_tty)
      formatstring = "      >>>  " POCL_COLOR_MAGENTA "     %3" PRIu64
                     ".%09" PRIu64 " " POCL_COLOR_RESET " %s    %s\n";
    else
      formatstring = "      >>>       %3" PRIu64 ".%09" PRIu64 "  %s    %s\n";

    PRINT_DURATION(func, line, formatstring, sec, nsec, "s", msg);
  }
}

void pocl_debug_measure_finish(uint64_t *start, uint64_t *finish,
                               const char *msg, const char *func,
                               unsigned line) {
  *finish = pocl_gettimemono_ns();
  pocl_debug_print_duration(func, line, msg, (*finish - *start));
}

#endif