Do not copy arguments passed to FileLogger

Historically, FileLogger was intended to work with scalar types.
Therefore, its member functions utilized parameter packs, which
copied the arguments. However, some time ago std::strings had
started to be passed to this function. The recursion was performing
multiple copies of the same std::string, which could cause
unneeded memory allocations.

This change:
- replaces copying with const references
- applies std::move() operator if possible
- replaces std::unique_lock with std::lock_guard

Signed-off-by: Patryk Wrobel <patryk.wrobel@intel.com>
This commit is contained in:
Patryk Wrobel
2022-08-16 13:24:22 +00:00
committed by Compute-Runtime-Automation
parent edeaa968f5
commit cb23a38aad
3 changed files with 16 additions and 11 deletions

View File

@@ -22,7 +22,7 @@ FileLogger<globalDebugFunctionalityLevel> &fileLoggerInstance() {
template <DebugFunctionalityLevel DebugLevel>
FileLogger<DebugLevel>::FileLogger(std::string filename, const DebugVariables &flags) {
logFileName = filename;
logFileName = std::move(filename);
std::remove(logFileName.c_str());
dumpKernels = flags.DumpKernels.get();
@@ -37,7 +37,7 @@ FileLogger<DebugLevel>::~FileLogger() = default;
template <DebugFunctionalityLevel DebugLevel>
void FileLogger<DebugLevel>::writeToFile(std::string filename, const char *str, size_t length, std::ios_base::openmode mode) {
std::unique_lock<std::mutex> theLock(mutex);
std::lock_guard theLock(mutex);
std::ofstream outFile(filename, mode);
if (outFile.is_open()) {
outFile.write(str, length);

View File

@@ -15,6 +15,7 @@
#include <sstream>
#include <string>
#include <thread>
#include <utility>
namespace NEO {
class Kernel;
@@ -28,7 +29,7 @@ template <DebugFunctionalityLevel DebugLevel>
class FileLogger {
public:
FileLogger(std::string filename, const DebugVariables &flags);
~FileLogger();
MOCKABLE_VIRTUAL ~FileLogger();
FileLogger(const FileLogger &) = delete;
FileLogger &operator=(const FileLogger &) = delete;
@@ -93,7 +94,7 @@ class FileLogger {
// Expects pairs of args (even number of args)
template <typename... Types>
void logInputs(Types &&...params) {
void logInputs(const Types &...params) {
if (enabled()) {
if (logApiCalls) {
std::thread::id thisThread = std::this_thread::get_id();
@@ -101,7 +102,9 @@ class FileLogger {
ss << "------------------------------\n";
printInputs(ss, "ThreadID", thisThread, params...);
ss << "------------------------------" << std::endl;
writeToFile(logFileName, ss.str().c_str(), ss.str().length(), std::ios::app);
const auto str = ss.str();
writeToFile(logFileName, str.c_str(), str.length(), std::ios::app);
}
}
}
@@ -116,13 +119,15 @@ class FileLogger {
}
template <typename... Types>
void log(bool enableLog, Types... params) {
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...);
writeToFile(logFileName, ss.str().c_str(), ss.str().length(), std::ios::app);
const auto str = ss.str();
writeToFile(logFileName, str.c_str(), str.length(), std::ios::app);
}
}
}
@@ -132,7 +137,7 @@ class FileLogger {
}
void setLogFileName(std::string filename) {
logFileName = filename;
logFileName = std::move(filename);
}
bool peekLogApiCalls() { return logApiCalls; }
@@ -151,7 +156,7 @@ class FileLogger {
// Prints inputs in format: InputName: InputValue \newline
template <typename T1, typename... Types>
void printInputs(std::stringstream &ss, T1 first, Types... params) {
void printInputs(std::stringstream &ss, const T1 &first, const Types &...params) {
if (enabled()) {
const size_t argsLeft = sizeof...(params);
@@ -169,7 +174,7 @@ class FileLogger {
void print(std::stringstream &ss) {}
template <typename T1, typename... Types>
void print(std::stringstream &ss, T1 first, Types... params) {
void print(std::stringstream &ss, const T1 &first, const Types &...params) {
if (enabled()) {
const size_t argsLeft = sizeof...(params);

View File

@@ -351,7 +351,7 @@ struct DummyEvaluator {
wasCalled = true;
}
operator const char *() {
operator const char *() const {
return "";
}
};