posix_logger.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. //
  10. // Logger implementation that can be shared by all environments
  11. // where enough posix functionality is available.
  12. #pragma once
  13. #include <algorithm>
  14. #include <stdio.h>
  15. #include "port/sys_time.h"
  16. #include <time.h>
  17. #include <fcntl.h>
  18. #ifdef OS_LINUX
  19. #ifndef FALLOC_FL_KEEP_SIZE
  20. #include <linux/falloc.h>
  21. #endif
  22. #endif
  23. #include <atomic>
  24. #include "env/io_posix.h"
  25. #include "monitoring/iostats_context_imp.h"
  26. #include "rocksdb/env.h"
  27. #include "test_util/sync_point.h"
  28. namespace ROCKSDB_NAMESPACE {
  29. class PosixLogger : public Logger {
  30. private:
  31. Status PosixCloseHelper() {
  32. int ret;
  33. ret = fclose(file_);
  34. if (ret) {
  35. return IOError("Unable to close log file", "", ret);
  36. }
  37. return Status::OK();
  38. }
  39. FILE* file_;
  40. uint64_t (*gettid_)(); // Return the thread id for the current thread
  41. std::atomic_size_t log_size_;
  42. int fd_;
  43. const static uint64_t flush_every_seconds_ = 5;
  44. std::atomic_uint_fast64_t last_flush_micros_;
  45. Env* env_;
  46. std::atomic<bool> flush_pending_;
  47. protected:
  48. virtual Status CloseImpl() override { return PosixCloseHelper(); }
  49. public:
  50. PosixLogger(FILE* f, uint64_t (*gettid)(), Env* env,
  51. const InfoLogLevel log_level = InfoLogLevel::ERROR_LEVEL)
  52. : Logger(log_level),
  53. file_(f),
  54. gettid_(gettid),
  55. log_size_(0),
  56. fd_(fileno(f)),
  57. last_flush_micros_(0),
  58. env_(env),
  59. flush_pending_(false) {}
  60. virtual ~PosixLogger() {
  61. if (!closed_) {
  62. closed_ = true;
  63. PosixCloseHelper();
  64. }
  65. }
  66. virtual void Flush() override {
  67. TEST_SYNC_POINT("PosixLogger::Flush:Begin1");
  68. TEST_SYNC_POINT("PosixLogger::Flush:Begin2");
  69. if (flush_pending_) {
  70. flush_pending_ = false;
  71. fflush(file_);
  72. }
  73. last_flush_micros_ = env_->NowMicros();
  74. }
  75. using Logger::Logv;
  76. virtual void Logv(const char* format, va_list ap) override {
  77. IOSTATS_TIMER_GUARD(logger_nanos);
  78. const uint64_t thread_id = (*gettid_)();
  79. // We try twice: the first time with a fixed-size stack allocated buffer,
  80. // and the second time with a much larger dynamically allocated buffer.
  81. char buffer[500];
  82. for (int iter = 0; iter < 2; iter++) {
  83. char* base;
  84. int bufsize;
  85. if (iter == 0) {
  86. bufsize = sizeof(buffer);
  87. base = buffer;
  88. } else {
  89. bufsize = 65536;
  90. base = new char[bufsize];
  91. }
  92. char* p = base;
  93. char* limit = base + bufsize;
  94. struct timeval now_tv;
  95. gettimeofday(&now_tv, nullptr);
  96. const time_t seconds = now_tv.tv_sec;
  97. struct tm t;
  98. localtime_r(&seconds, &t);
  99. p += snprintf(p, limit - p,
  100. "%04d/%02d/%02d-%02d:%02d:%02d.%06d %llx ",
  101. t.tm_year + 1900,
  102. t.tm_mon + 1,
  103. t.tm_mday,
  104. t.tm_hour,
  105. t.tm_min,
  106. t.tm_sec,
  107. static_cast<int>(now_tv.tv_usec),
  108. static_cast<long long unsigned int>(thread_id));
  109. // Print the message
  110. if (p < limit) {
  111. va_list backup_ap;
  112. va_copy(backup_ap, ap);
  113. p += vsnprintf(p, limit - p, format, backup_ap);
  114. va_end(backup_ap);
  115. }
  116. // Truncate to available space if necessary
  117. if (p >= limit) {
  118. if (iter == 0) {
  119. continue; // Try again with larger buffer
  120. } else {
  121. p = limit - 1;
  122. }
  123. }
  124. // Add newline if necessary
  125. if (p == base || p[-1] != '\n') {
  126. *p++ = '\n';
  127. }
  128. assert(p <= limit);
  129. const size_t write_size = p - base;
  130. #ifdef ROCKSDB_FALLOCATE_PRESENT
  131. const int kDebugLogChunkSize = 128 * 1024;
  132. // If this write would cross a boundary of kDebugLogChunkSize
  133. // space, pre-allocate more space to avoid overly large
  134. // allocations from filesystem allocsize options.
  135. const size_t log_size = log_size_;
  136. const size_t last_allocation_chunk =
  137. ((kDebugLogChunkSize - 1 + log_size) / kDebugLogChunkSize);
  138. const size_t desired_allocation_chunk =
  139. ((kDebugLogChunkSize - 1 + log_size + write_size) /
  140. kDebugLogChunkSize);
  141. if (last_allocation_chunk != desired_allocation_chunk) {
  142. fallocate(
  143. fd_, FALLOC_FL_KEEP_SIZE, 0,
  144. static_cast<off_t>(desired_allocation_chunk * kDebugLogChunkSize));
  145. }
  146. #endif
  147. size_t sz = fwrite(base, 1, write_size, file_);
  148. flush_pending_ = true;
  149. if (sz > 0) {
  150. log_size_ += write_size;
  151. }
  152. uint64_t now_micros = static_cast<uint64_t>(now_tv.tv_sec) * 1000000 +
  153. now_tv.tv_usec;
  154. if (now_micros - last_flush_micros_ >= flush_every_seconds_ * 1000000) {
  155. Flush();
  156. }
  157. if (base != buffer) {
  158. delete[] base;
  159. }
  160. break;
  161. }
  162. }
  163. size_t GetLogFileSize() const override { return log_size_; }
  164. };
  165. } // namespace ROCKSDB_NAMESPACE