24 #include <glog/logging.h>
25 #include <glog/vlog_is_on.h>
26 #ifdef HAVE_GOOGLEPERFTOOLS
28 #include <gperftools/profiler.h>
29 #endif // HAVE_GOOGLEPERFTOOLS
63 void DebuggingSupports::initialize_glog() {
64 std::lock_guard<std::mutex> guard(static_glog_initialize_lock);
65 ASSERT_ND(static_glog_initialize_counter >= 0);
66 if (static_glog_initialize_counter == 0) {
87 const char* logfile_name;
89 logfile_name =
"libfoedus";
95 logfile_name =
"libfoedus_soc";
97 google::InitGoogleLogging(logfile_name);
98 LOG(INFO) <<
"initialize_glog(): Initialized GLOG";
100 LOG(INFO) <<
"initialize_glog(): Observed that someone else has initialized GLOG";
105 void DebuggingSupports::uninitialize_glog() {
106 std::lock_guard<std::mutex> guard(static_glog_initialize_lock);
107 ASSERT_ND(static_glog_initialize_counter >= 1);
108 if (static_glog_initialize_counter == 1) {
109 LOG(INFO) <<
"uninitialize_glog(): Uninitializing GLOG...";
110 google::ShutdownGoogleLogging();
112 LOG(INFO) <<
"uninitialize_glog(): There are still some other GLOG user.";
123 std::memset(&papi_counters_, 0,
sizeof(papi_counters_));
124 papi_enabled_ =
false;
136 FLAGS_logtostderr = value;
137 LOG(INFO) <<
"Changed glog's FLAGS_logtostderr to " << value;
140 FLAGS_stderrthreshold =
static_cast<int>(level);
141 LOG(INFO) <<
"Changed glog's FLAGS_stderrthreshold to " << level;
144 FLAGS_minloglevel =
static_cast<int>(level);
145 LOG(INFO) <<
"Changed glog's FLAGS_minloglevel to " << level;
149 LOG(INFO) <<
"Changed glog's FLAGS_v to " << verbose;
154 google::SetVLOGLevel(module.c_str(), verbose);
155 LOG(INFO) <<
"Invoked google::SetVLOGLevel for " << module <<
", level=" << verbose;
162 int kPapiEvents[] = {
166 const uint16_t kPapiEventCount =
sizeof(kPapiEvents) /
sizeof(
int);
168 #define X_QUOTE(str) #str
169 #define X_EXPAND_AND_QUOTE(str) X_QUOTE(str)
170 #define X(a, b) X_EXPAND_AND_QUOTE(a) ": " b,
171 const char* kPapiEventNames[] = {
175 #undef X_EXPAND_AND_QUOTE
179 papi_enabled_ =
false;
180 int version = ::PAPI_library_init(PAPI_VER_CURRENT);
181 int total_counters = ::PAPI_num_counters();
182 if (total_counters <= PAPI_OK) {
183 LOG(ERROR) <<
"PAPI is not supported in this environment. PAPI runtime version=" << version
184 <<
", PAPI_VER_CURRENT=" << PAPI_VER_CURRENT;
188 LOG(INFO) <<
"PAPI has " << total_counters <<
" counters. PAPI runtime version=" << version
189 <<
", PAPI_VER_CURRENT=" << PAPI_VER_CURRENT;
190 int ret = ::PAPI_start_counters(kPapiEvents, kPapiEventCount);
191 if (ret != PAPI_OK) {
192 LOG(ERROR) <<
"PAPI_start_counters failed. retval=" << ret;
195 LOG(INFO) <<
"Started counting " << kPapiEventCount <<
" performance events via PAPI";
196 papi_enabled_ =
true;
201 int ret = ::PAPI_stop_counters(papi_counters_.
counters_, kPapiEventCount);
202 if (ret != PAPI_OK) {
203 LOG(ERROR) <<
"PAPI_stop_counters failed. retval=" << ret;
209 std::vector<std::string> ret;
210 for (uint16_t i = 0; i < kPapiEventCount; ++i) {
211 ret.emplace_back(std::string(kPapiEventNames[i]) +
":" + std::to_string(counters.counters_[i]));
217 LOG(WARNING) <<
"libpapi was not linked. No PAPI profile is collected.";
222 std::vector<std::string> ret;
223 ret.emplace_back(
"libpapi was not linked. No PAPI profile is collected");
230 #ifdef HAVE_GOOGLEPERFTOOLS
231 int ret = ::ProfilerStart(output_file.c_str());
233 LOG(ERROR) <<
"ProfilerStart() returned zero (an error). os_error=" <<
assorted::os_error();
236 #else // HAVE_GOOGLEPERFTOOLS
237 LOG(WARNING) <<
"Google perftools was not linked. No profile is provided. " << output_file;
238 #endif // HAVE_GOOGLEPERFTOOLS
243 #ifdef HAVE_GOOGLEPERFTOOLS
245 #endif // HAVE_GOOGLEPERFTOOLS
void set_debug_log_to_stderr(bool value)
Whether to write debug logs to stderr rather than log file.
void start_papi_counters()
Start collecting performance counters via PAPI if it's available.
ErrorStack uninitialize_once() override
long long int counters_[128]
wanna use int64_t, but to align with PAPI...
ErrorStack start_profile(const std::string &output_file)
Start running a CPU profiler (gperftools/PAPI).
bool debug_log_to_stderr_
Whether to write debug logs to stderr rather than log file.
#define ERROR_STACK(e)
Instantiates ErrorStack with the given foedus::error_code, creating an error stack with the current f...
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
ErrorStack initialize_once() override
assorted::FixedString< 252 > verbose_modules_
Per-module verbose level.
debugging::DebuggingOptions debugging_
Brings error stacktrace information as return value of functions.
void set_debug_log_min_threshold(DebuggingOptions::DebugLogLevel level)
Debug logs below this level will be completely ignored.
const EngineOptions & get_options() const
bool is_master() const
Returns if this engine object is a master instance.
static std::vector< std::string > describe_papi_counters(const PapiCounters &counters)
Returns a human-readable explanation of PAPI counters.
void set_debug_log_stderr_threshold(DebuggingOptions::DebugLogLevel level)
Debug logs at or above this level will be copied to stderr.
Analogue of boost::filesystem::path.
bool is_emulated_child() const
Returns if this engine object is a child instance running just as a thread.
int static_glog_initialize_counter
This and static_glog_initialize_lock are the only static variables we have in the entire code base...
void stop_profile()
Stop CPU profiling.
std::basic_string< CHAR > str() const
Convert to a std::string object.
bool create_directories(const Path &p, bool sync=false)
Recursive mkdir (mkdirs).
bool exists(const Path &p)
Returns if the file exists.
void set_verbose_module(const std::string &module, int verbose)
Per-module verbose level.
fs::FixedPath debug_log_dir_
Path of the folder to write debug logs.
std::mutex static_glog_initialize_lock
Exclusive lock variable for Google-logging's initialization/uninitialization.
std::string os_error()
Thread-safe strerror(errno).
void stop_papi_counters()
Stop collecting performance counters via PAPI.
DebugLogLevel debug_log_min_threshold_
Debug logs below this level will be completely ignored.
DebugLogLevel
Defines debug logging levels.
void set_verbose_log_level(int verbose)
Verbose debug logs (VLOG(m)) at or less than this number will be shown.
const ErrorStack kRetOk
Normal return value for no-error case.
Atomic fence methods and load/store with fences that work for both C++11/non-C++11 code...
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
uint32_t size() const noexcept
Returns the length of this string.
Set of options for debugging support.
0x0B01 : "DEBUG : Gperftools reported an error" .
DebugLogLevel debug_log_stderr_threshold_
Debug logs at or above this level will be copied to stderr.
int16_t verbose_log_level_
Verbose debug logs (VLOG(m)) at or less than this number will be shown.