File: logger.h

package info (click to toggle)
intel-compute-runtime 25.35.35096.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,324 kB
  • sloc: cpp: 926,243; lisp: 3,433; sh: 715; makefile: 162; python: 21
file content (224 lines) | stat: -rw-r--r-- 7,487 bytes parent folder | download
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
217
218
219
220
221
222
223
224
/*
 * Copyright (C) 2019-2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#pragma once
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/helpers/non_copyable_or_moveable.h"

#include <mutex>
#include <sstream>
#include <thread>

#define CREATE_DEBUG_STRING(buffer, format, ...)                            \
    std::unique_ptr<char[]> buffer(new char[NEO::maxErrorDescriptionSize]); \
    snprintf(buffer.get(), NEO::maxErrorDescriptionSize, format, __VA_ARGS__)

namespace NEO {
class Kernel;
struct MultiDispatchInfo;
class GraphicsAllocation;
class MemoryManager;

static const int32_t maxErrorDescriptionSize = 1024;
const char *getAllocationTypeString(GraphicsAllocation const *graphicsAllocation);
const char *getMemoryPoolString(GraphicsAllocation const *graphicsAllocation);

template <DebugFunctionalityLevel debugLevel>
class FileLogger : NEO::NonCopyableAndNonMovableClass {
  public:
    FileLogger(std::string filename, const DebugVariables &flags);
    MOCKABLE_VIRTUAL ~FileLogger();

    static constexpr bool enabled() {
        return debugLevel != DebugFunctionalityLevel::none;
    }

    void dumpKernel(const std::string &name, const std::string &src);
    void logApiCall(const char *function, bool enter, int32_t errorCode);
    size_t getInput(const size_t *input, int32_t index);

    MOCKABLE_VIRTUAL void writeToFile(std::string filename, const char *str, size_t length, std::ios_base::openmode mode);

    void dumpBinaryProgram(int32_t numDevices, const size_t *lengths, const unsigned char **binaries);

    const std::string getSizes(const uintptr_t *input, uint32_t workDim, bool local) {
        if (false == enabled()) {
            return "";
        }

        std::stringstream os;
        std::string workSize;
        if (local) {
            workSize = "localWorkSize";
        } else {
            workSize = "globalWorkSize";
        }

        for (uint32_t i = 0; i < workDim; i++) {
            if (input != nullptr) {
                os << workSize << "[" << i << "]: \t" << input[i] << "\n";
            }
        }
        return os.str();
    }

    const std::string infoPointerToString(const void *paramValue, size_t paramSize) {
        if (false == enabled()) {
            return "";
        }

        std::stringstream os;
        if (paramValue) {
            switch (paramSize) {
            case sizeof(uint32_t):
                os << *(uint32_t *)paramValue;
                break;
            case sizeof(uint64_t):
                os << *(uint64_t *)paramValue;
                break;
            case sizeof(uint8_t):
                os << (uint32_t)(*(uint8_t *)paramValue);
                break;
            default:
                break;
            }
        }
        return os.str();
    }

    // Expects pairs of args (even number of args)
    template <typename... Types>
    void logInputs(const Types &...params) {
        if (enabled()) {
            if (logApiCalls) {
                std::thread::id thisThread = std::this_thread::get_id();
                std::stringstream ss;
                ss << "------------------------------\n";
                printInputs(ss, "ThreadID", thisThread, params...);
                ss << "------------------------------" << std::endl;

                const auto str = ss.str();
                writeToFile(logFileName, str.c_str(), str.length(), std::ios::app);
            }
        }
    }

    template <typename FT>
    void logLazyEvaluateArgs(bool predicate, FT &&callable) {
        if (enabled()) {
            if (predicate) {
                callable();
            }
        }
    }

    template <typename... Types>
    void log(bool enableLog, const Types &...params) {
        if (enabled()) {
            if (enableLog) {
                std::thread::id thisThread = std::this_thread::get_id();
                std::stringstream ss;
                print(ss, "ThreadID", thisThread, params...);

                const auto str = ss.str();
                writeToFile(logFileName, str.c_str(), str.length(), std::ios::app);
            }
        }
    }

    void logDebugString(bool enableLog, std::string_view debugString);

    const char *getLogFileName() { return logFileName.c_str(); }
    std::string getLogFileNameString() { return logFileName; }

    void setLogFileName(std::string filename) { logFileName = std::move(filename); }

    bool peekLogApiCalls() { return logApiCalls; }
    bool shouldLogAllocationType() { return logAllocationType; }
    bool shouldLogAllocationToStdout() { return logAllocationStdout; }
    bool shouldLogAllocationMemoryPool() { return logAllocationMemoryPool; }

  protected:
    std::mutex mutex;
    std::string logFileName;
    bool dumpKernels = false;
    bool logApiCalls = false;
    bool logAllocationMemoryPool = false;
    bool logAllocationType = false;
    bool logAllocationStdout = false;

    // Required for variadic template with 0 args passed
    void printInputs(std::stringstream &ss) {}

    // Prints inputs in format: InputName: InputValue \newline
    template <typename T1, typename... Types>
    void printInputs(std::stringstream &ss, const T1 &first, const Types &...params) {
        if (enabled()) {
            const size_t argsLeft = sizeof...(params);

            ss << "\t" << first;
            if (argsLeft % 2) {
                ss << ": ";
            } else {
                ss << std::endl;
            }
            printInputs(ss, params...);
        }
    }

    // Required for variadic template with 0 args passed
    void print(std::stringstream &ss) {}

    template <typename T1, typename... Types>
    void print(std::stringstream &ss, const T1 &first, const Types &...params) {
        if (enabled()) {
            const size_t argsLeft = sizeof...(params);

            ss << first << " ";
            if (argsLeft == 0) {
                ss << std::endl;
            }
            print(ss, params...);
        }
    }
};

static_assert(NEO::NonCopyableAndNonMovable<FileLogger<DebugFunctionalityLevel::none>>);
static_assert(NEO::NonCopyableAndNonMovable<FileLogger<DebugFunctionalityLevel::full>>);
static_assert(NEO::NonCopyableAndNonMovable<FileLogger<DebugFunctionalityLevel::regKeys>>);

extern FileLogger<globalDebugFunctionalityLevel> &fileLoggerInstance();
extern FileLogger<globalDebugFunctionalityLevel> &usmReusePerfLoggerInstance();

template <bool enabled>
class LoggerApiEnterWrapper {
  public:
    LoggerApiEnterWrapper(const char *funcName, const int *errorCode)
        : funcName(funcName), errorCode(errorCode) {
        if (enabled) {
            fileLoggerInstance().logApiCall(funcName, true, 0);
        }
    }
    ~LoggerApiEnterWrapper() {
        if (enabled) {
            fileLoggerInstance().logApiCall(funcName, false, (errorCode != nullptr) ? *errorCode : 0);
        }
    }
    const char *funcName;
    const int *errorCode;
};

}; // namespace NEO

#define DBG_LOG_LAZY_EVALUATE_ARGS(LOGGER, PREDICATE, LOG_FUNCTION, ...) \
    LOGGER.logLazyEvaluateArgs(PREDICATE, [&] { LOGGER.LOG_FUNCTION(__VA_ARGS__); })

#define DBG_LOG(PREDICATE, ...) \
    DBG_LOG_LAZY_EVALUATE_ARGS(NEO::fileLoggerInstance(), NEO::debugManager.flags.PREDICATE.get(), log, NEO::debugManager.flags.PREDICATE.get(), __VA_ARGS__)

#define DBG_LOG_INPUTS(...) \
    DBG_LOG_LAZY_EVALUATE_ARGS(NEO::fileLoggerInstance(), NEO::fileLoggerInstance().peekLogApiCalls(), logInputs, __VA_ARGS__)