<commit_before>/*
	This file is part of the MinSG library extension Profiling.
	Copyright (C) 2013 <NAME> <<EMAIL>>
	Copyright (C) 2020 <NAME> <<EMAIL>>
	
	This library is subject to the terms of the Mozilla Public License, v. 2.0.
	You should have received a copy of the MPL along with this library; see the 
	file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef UTIL_PROFILING_PROFILER_H_
#define UTIL_PROFILING_PROFILER_H_

#include "Action.h"
#include <cstdint>
#include <iosfwd>
#include <memory>
#include <string>
#include <unordered_map>

#ifdef PROFILING_ENABLED

#define INIT_PROFILING(Logger, out) \
	static Util::Profiling::Profiler _profiler; \
	static Util::Profiling::Logger _logger(out); \
	static std::unordered_map<std::string, Util::Profiling::Action> _actions; \
	static bool _loggerRegistered = []() -> bool { _profiler.registerLogger(&_logger); return true; }()

#define INIT_PROFILING_PLAIN_TEXT(out) INIT_PROFILING(LoggerPlainText, out)
#define INIT_PROFILING_TIME(out) INIT_PROFILING(LoggerTime, out)

#define SCOPED_PROFILING(descr) Util::Profiling::ScopedAction _action(&_profiler, #descr)
#define SCOPED_PROFILING_COND(descr, condition) Util::Profiling::ScopedAction _action((condition) ? &_profiler : nullptr, #descr)

#define BEGIN_PROFILING(descr) _actions[#descr] = _profiler.beginTimeMemoryAction(#descr)
#define END_PROFILING(descr) _profiler.endTimeMemoryAction(_actions[#descr])

#define BEGIN_PROFILING_COND(descr, condition) if(condition) _actions[#descr] = _profiler.beginTimeMemoryAction(#descr)
#define END_PROFILING_COND(descr, condition) if(condition) _profiler.endTimeMemoryAction(_actions[#descr])

#else

#define INIT_PROFILING(Logger, out)
#define INIT_PROFILING_PLAIN_TEXT(out)
#define INIT_PROFILING_TIME(out) 
#define SCOPED_PROFILING(descr)
#define BEGIN_PROFILING(descr)
#define END_PROFILING(descr)

#endif // PROFILING_ENABLED

namespace Util {
namespace Profiling {
typedef Util::GenericAttributeMap Action;
class Logger;

/**
 * @brief Profiling code (measure running time, memory consumption and log it)
 * 
 * The profiler manages actions. An action is an extendable object that can be
 * annotated by arbitrary attributes. The profile contains convenience
 * functions to create actions and to annotate some standard attributes.
 * Different loggers can be attached to the profiler that are used to write
 * the actions to streams (e.g. a file, stdout). For example, the profiler can
 * be used to measure the duration and memory consumption of a code section:
 * @code
 * Util::Profiling::Profiler profiler;
 * Util::Profiling::LoggerPlainText logger(std::cout);<commit_msg>Fixed missing defines<commit_after>/*
	This file is part of the MinSG library extension Profiling.
	Copyright (C) 2013 <NAME> <<EMAIL>>
	Copyright (C) 2020 <NAME> <<EMAIL>>
	
	This library is subject to the terms of the Mozilla Public License, v. 2.0.
	You should have received a copy of the MPL along with this library; see the 
	file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef UTIL_PROFILING_PROFILER_H_
#define UTIL_PROFILING_PROFILER_H_

#include "Action.h"
#include <cstdint>
#include <iosfwd>
#include <memory>
#include <string>
#include <unordered_map>

#ifdef PROFILING_ENABLED

#define INIT_PROFILING(Logger, out) \
	static Util::Profiling::Profiler _profiler; \
	static Util::Profiling::Logger _logger(out); \
	static std::unordered_map<std::string, Util::Profiling::Action> _actions; \
	static bool _loggerRegistered = []() -> bool { _profiler.registerLogger(&_logger); return true; }()

#define INIT_PROFILING_JSON(out) INIT_PROFILING(LoggerJSON, out)
#define INIT_PROFILING_TSV(out) INIT_PROFILING(LoggerTSV, out)
#define INIT_PROFILING_XML(out) INIT_PROFILING(LoggerXML, out)
#define INIT_PROFILING_PLAIN_TEXT(out) INIT_PROFILING(LoggerPlainText, out)
#define INIT_PROFILING_TIME(out) INIT_PROFILING(LoggerTime, out)

#define SCOPED_PROFILING(descr) Util::Profiling::ScopedAction _action(&_profiler, #descr)
#define SCOPED_PROFILING_COND(descr, condition) Util::Profiling::ScopedAction _action((condition) ? &_profiler : nullptr, #descr)

#define BEGIN_PROFILING(descr) _actions[#descr] = _profiler.beginTimeMemoryAction(#descr)
#define END_PROFILING(descr) _profiler.endTimeMemoryAction(_actions[#descr])

#define BEGIN_PROFILING_COND(descr, condition) if(condition) _actions[#descr] = _profiler.beginTimeMemoryAction(#descr)
#define END_PROFILING_COND(descr, condition) if(condition) _profiler.endTimeMemoryAction(_actions[#descr])

#else

#define INIT_PROFILING(Logger, out)
#define INIT_PROFILING_JSON(out)
#define INIT_PROFILING_TSV(out)
#define INIT_PROFILING_XML(out)
#define INIT_PROFILING_PLAIN_TEXT(out)
#define INIT_PROFILING_TIME(out) 
#define SCOPED_PROFILING(descr)
#define BEGIN_PROFILING(descr)
#define END_PROFILING(descr)
#define SCOPED_PROFILING_COND(descr, condition)
#define BEGIN_PROFILING_COND(descr, condition)
#define END_PROFILING_COND(descr, condition)

#endif // PROFILING_ENABLED

namespace Util {
namespace Profiling {
typedef Util::GenericAttributeMap Action;
class Logger;

/**
 * @brief Profiling code (measure running time, memory consumption and log it)
 * 
 * The profiler manages actions. An action is an extendable object that can be
 * annotated by arbitrary attributes. The profile contains convenience
 * functions to create actions and to annotate some standard attributes.
 * Different loggers can be attached to the profiler that are used to write
 * the actions to streams (e.g. a file, stdout). For example, the profiler can
 * be used to measure the duration and memory consumption of a code section:
 * @code
 * Util::Profiling::Profiler profiler;
 * Util::Profiling::LoggerPlainText logger(std::cout);
{'fraction_non_alphanumeric': 0.10079805690492713, 'fraction_numerical': 0.004163775156141568, 'mean_word_length': 6.918956043956044, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5731425', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1985, 'n_words': 524}