| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "v8.h" | 28 #include "v8.h" |
| 29 | 29 |
| 30 #include "log-utils.h" | 30 #include "log-utils.h" |
| 31 #include "string-stream.h" | 31 #include "string-stream.h" |
| 32 | 32 |
| 33 namespace v8 { | 33 namespace v8 { |
| 34 namespace internal { | 34 namespace internal { |
| 35 | 35 |
| 36 LogDynamicBuffer::LogDynamicBuffer( | 36 |
| 37 int block_size, int max_size, const char* seal, int seal_size) | 37 const char* Log::kLogToTemporaryFile = "&"; |
| 38 : block_size_(block_size), | |
| 39 max_size_(max_size - (max_size % block_size_)), | |
| 40 seal_(seal), | |
| 41 seal_size_(seal_size), | |
| 42 blocks_(max_size_ / block_size_ + 1), | |
| 43 write_pos_(0), block_index_(0), block_write_pos_(0), is_sealed_(false) { | |
| 44 ASSERT(BlocksCount() > 0); | |
| 45 AllocateBlock(0); | |
| 46 for (int i = 1; i < BlocksCount(); ++i) { | |
| 47 blocks_[i] = NULL; | |
| 48 } | |
| 49 } | |
| 50 | 38 |
| 51 | 39 |
| 52 LogDynamicBuffer::~LogDynamicBuffer() { | |
| 53 for (int i = 0; i < BlocksCount(); ++i) { | |
| 54 DeleteArray(blocks_[i]); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 | |
| 59 int LogDynamicBuffer::Read(int from_pos, char* dest_buf, int buf_size) { | |
| 60 if (buf_size == 0) return 0; | |
| 61 int read_pos = from_pos; | |
| 62 int block_read_index = BlockIndex(from_pos); | |
| 63 int block_read_pos = PosInBlock(from_pos); | |
| 64 int dest_buf_pos = 0; | |
| 65 // Read until dest_buf is filled, or write_pos_ encountered. | |
| 66 while (read_pos < write_pos_ && dest_buf_pos < buf_size) { | |
| 67 const int read_size = Min(write_pos_ - read_pos, | |
| 68 Min(buf_size - dest_buf_pos, block_size_ - block_read_pos)); | |
| 69 memcpy(dest_buf + dest_buf_pos, | |
| 70 blocks_[block_read_index] + block_read_pos, read_size); | |
| 71 block_read_pos += read_size; | |
| 72 dest_buf_pos += read_size; | |
| 73 read_pos += read_size; | |
| 74 if (block_read_pos == block_size_) { | |
| 75 block_read_pos = 0; | |
| 76 ++block_read_index; | |
| 77 } | |
| 78 } | |
| 79 return dest_buf_pos; | |
| 80 } | |
| 81 | |
| 82 | |
| 83 int LogDynamicBuffer::Seal() { | |
| 84 WriteInternal(seal_, seal_size_); | |
| 85 is_sealed_ = true; | |
| 86 return 0; | |
| 87 } | |
| 88 | |
| 89 | |
| 90 int LogDynamicBuffer::Write(const char* data, int data_size) { | |
| 91 if (is_sealed_) { | |
| 92 return 0; | |
| 93 } | |
| 94 if ((write_pos_ + data_size) <= (max_size_ - seal_size_)) { | |
| 95 return WriteInternal(data, data_size); | |
| 96 } else { | |
| 97 return Seal(); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 | |
| 102 int LogDynamicBuffer::WriteInternal(const char* data, int data_size) { | |
| 103 int data_pos = 0; | |
| 104 while (data_pos < data_size) { | |
| 105 const int write_size = | |
| 106 Min(data_size - data_pos, block_size_ - block_write_pos_); | |
| 107 memcpy(blocks_[block_index_] + block_write_pos_, data + data_pos, | |
| 108 write_size); | |
| 109 block_write_pos_ += write_size; | |
| 110 data_pos += write_size; | |
| 111 if (block_write_pos_ == block_size_) { | |
| 112 block_write_pos_ = 0; | |
| 113 AllocateBlock(++block_index_); | |
| 114 } | |
| 115 } | |
| 116 write_pos_ += data_size; | |
| 117 return data_size; | |
| 118 } | |
| 119 | |
| 120 // Must be the same message as in Logger::PauseProfiler. | |
| 121 const char* const Log::kDynamicBufferSeal = "profiler,\"pause\"\n"; | |
| 122 | |
| 123 Log::Log(Logger* logger) | 40 Log::Log(Logger* logger) |
| 124 : write_to_file_(false), | 41 : is_stopped_(false), |
| 125 is_stopped_(false), | |
| 126 output_handle_(NULL), | 42 output_handle_(NULL), |
| 127 ll_output_handle_(NULL), | 43 ll_output_handle_(NULL), |
| 128 output_buffer_(NULL), | |
| 129 mutex_(NULL), | 44 mutex_(NULL), |
| 130 message_buffer_(NULL), | 45 message_buffer_(NULL), |
| 131 logger_(logger) { | 46 logger_(logger) { |
| 132 } | 47 } |
| 133 | 48 |
| 134 | 49 |
| 135 static void AddIsolateIdIfNeeded(StringStream* stream) { | 50 static void AddIsolateIdIfNeeded(StringStream* stream) { |
| 136 Isolate* isolate = Isolate::Current(); | 51 Isolate* isolate = Isolate::Current(); |
| 137 if (isolate->IsDefaultIsolate()) return; | 52 if (isolate->IsDefaultIsolate()) return; |
| 138 stream->Add("isolate-%p-", isolate); | 53 stream->Add("isolate-%p-", isolate); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 156 | 71 |
| 157 // --prof implies --log-code. | 72 // --prof implies --log-code. |
| 158 if (FLAG_prof) FLAG_log_code = true; | 73 if (FLAG_prof) FLAG_log_code = true; |
| 159 | 74 |
| 160 // --prof_lazy controls --log-code, implies --noprof_auto. | 75 // --prof_lazy controls --log-code, implies --noprof_auto. |
| 161 if (FLAG_prof_lazy) { | 76 if (FLAG_prof_lazy) { |
| 162 FLAG_log_code = false; | 77 FLAG_log_code = false; |
| 163 FLAG_prof_auto = false; | 78 FLAG_prof_auto = false; |
| 164 } | 79 } |
| 165 | 80 |
| 166 bool start_logging = FLAG_log || FLAG_log_runtime || FLAG_log_api | 81 bool open_log_file = FLAG_log || FLAG_log_runtime || FLAG_log_api |
| 167 || FLAG_log_code || FLAG_log_gc || FLAG_log_handles || FLAG_log_suspect | 82 || FLAG_log_code || FLAG_log_gc || FLAG_log_handles || FLAG_log_suspect |
| 168 || FLAG_log_regexp || FLAG_log_state_changes || FLAG_ll_prof; | 83 || FLAG_log_regexp || FLAG_log_state_changes || FLAG_ll_prof; |
| 169 | 84 |
| 170 bool open_log_file = start_logging || FLAG_prof_lazy; | |
| 171 | |
| 172 // If we're logging anything, we need to open the log file. | 85 // If we're logging anything, we need to open the log file. |
| 173 if (open_log_file) { | 86 if (open_log_file) { |
| 174 if (strcmp(FLAG_logfile, "-") == 0) { | 87 if (strcmp(FLAG_logfile, "-") == 0) { |
| 175 OpenStdout(); | 88 OpenStdout(); |
| 176 } else if (strcmp(FLAG_logfile, "*") == 0) { | 89 } else if (strcmp(FLAG_logfile, "*") == 0) { |
| 177 OpenMemoryBuffer(); | 90 // Does nothing for now. Will be removed. |
| 178 } else { | 91 } else if (strcmp(FLAG_logfile, kLogToTemporaryFile) == 0) { |
| 92 OpenTemporaryFile(); |
| 93 } else { |
| 179 if (strchr(FLAG_logfile, '%') != NULL || | 94 if (strchr(FLAG_logfile, '%') != NULL || |
| 180 !Isolate::Current()->IsDefaultIsolate()) { | 95 !Isolate::Current()->IsDefaultIsolate()) { |
| 181 // If there's a '%' in the log file name we have to expand | 96 // If there's a '%' in the log file name we have to expand |
| 182 // placeholders. | 97 // placeholders. |
| 183 HeapStringAllocator allocator; | 98 HeapStringAllocator allocator; |
| 184 StringStream stream(&allocator); | 99 StringStream stream(&allocator); |
| 185 AddIsolateIdIfNeeded(&stream); | 100 AddIsolateIdIfNeeded(&stream); |
| 186 for (const char* p = FLAG_logfile; *p; p++) { | 101 for (const char* p = FLAG_logfile; *p; p++) { |
| 187 if (*p == '%') { | 102 if (*p == '%') { |
| 188 p++; | 103 p++; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 218 OpenFile(FLAG_logfile); | 133 OpenFile(FLAG_logfile); |
| 219 } | 134 } |
| 220 } | 135 } |
| 221 } | 136 } |
| 222 } | 137 } |
| 223 | 138 |
| 224 | 139 |
| 225 void Log::OpenStdout() { | 140 void Log::OpenStdout() { |
| 226 ASSERT(!IsEnabled()); | 141 ASSERT(!IsEnabled()); |
| 227 output_handle_ = stdout; | 142 output_handle_ = stdout; |
| 228 write_to_file_ = true; | |
| 229 } | 143 } |
| 230 | 144 |
| 231 | 145 |
| 146 void Log::OpenTemporaryFile() { |
| 147 ASSERT(!IsEnabled()); |
| 148 output_handle_ = i::OS::OpenTemporaryFile(); |
| 149 } |
| 150 |
| 151 |
| 232 // Extension added to V8 log file name to get the low-level log name. | 152 // Extension added to V8 log file name to get the low-level log name. |
| 233 static const char kLowLevelLogExt[] = ".ll"; | 153 static const char kLowLevelLogExt[] = ".ll"; |
| 234 | 154 |
| 235 // File buffer size of the low-level log. We don't use the default to | 155 // File buffer size of the low-level log. We don't use the default to |
| 236 // minimize the associated overhead. | 156 // minimize the associated overhead. |
| 237 static const int kLowLevelLogBufferSize = 2 * MB; | 157 static const int kLowLevelLogBufferSize = 2 * MB; |
| 238 | 158 |
| 239 | 159 |
| 240 void Log::OpenFile(const char* name) { | 160 void Log::OpenFile(const char* name) { |
| 241 ASSERT(!IsEnabled()); | 161 ASSERT(!IsEnabled()); |
| 242 output_handle_ = OS::FOpen(name, OS::LogFileOpenMode); | 162 output_handle_ = OS::FOpen(name, OS::LogFileOpenMode); |
| 243 write_to_file_ = true; | |
| 244 if (FLAG_ll_prof) { | 163 if (FLAG_ll_prof) { |
| 245 // Open the low-level log file. | 164 // Open the low-level log file. |
| 246 size_t len = strlen(name); | 165 size_t len = strlen(name); |
| 247 ScopedVector<char> ll_name(static_cast<int>(len + sizeof(kLowLevelLogExt))); | 166 ScopedVector<char> ll_name(static_cast<int>(len + sizeof(kLowLevelLogExt))); |
| 248 memcpy(ll_name.start(), name, len); | 167 memcpy(ll_name.start(), name, len); |
| 249 memcpy(ll_name.start() + len, kLowLevelLogExt, sizeof(kLowLevelLogExt)); | 168 memcpy(ll_name.start() + len, kLowLevelLogExt, sizeof(kLowLevelLogExt)); |
| 250 ll_output_handle_ = OS::FOpen(ll_name.start(), OS::LogFileOpenMode); | 169 ll_output_handle_ = OS::FOpen(ll_name.start(), OS::LogFileOpenMode); |
| 251 setvbuf(ll_output_handle_, NULL, _IOFBF, kLowLevelLogBufferSize); | 170 setvbuf(ll_output_handle_, NULL, _IOFBF, kLowLevelLogBufferSize); |
| 252 } | 171 } |
| 253 } | 172 } |
| 254 | 173 |
| 255 | 174 |
| 256 void Log::OpenMemoryBuffer() { | 175 FILE* Log::Close() { |
| 257 ASSERT(!IsEnabled()); | 176 FILE* result = NULL; |
| 258 output_buffer_ = new LogDynamicBuffer( | 177 if (output_handle_ != NULL) { |
| 259 kDynamicBufferBlockSize, kMaxDynamicBufferSize, | 178 if (strcmp(FLAG_logfile, kLogToTemporaryFile) != 0) { |
| 260 kDynamicBufferSeal, StrLength(kDynamicBufferSeal)); | 179 fclose(output_handle_); |
| 261 write_to_file_ = false; | 180 } else { |
| 262 } | 181 result = output_handle_; |
| 263 | 182 } |
| 264 | |
| 265 void Log::Close() { | |
| 266 if (write_to_file_) { | |
| 267 if (output_handle_ != NULL) fclose(output_handle_); | |
| 268 output_handle_ = NULL; | |
| 269 if (ll_output_handle_ != NULL) fclose(ll_output_handle_); | |
| 270 ll_output_handle_ = NULL; | |
| 271 } else { | |
| 272 delete output_buffer_; | |
| 273 output_buffer_ = NULL; | |
| 274 } | 183 } |
| 184 output_handle_ = NULL; |
| 185 if (ll_output_handle_ != NULL) fclose(ll_output_handle_); |
| 186 ll_output_handle_ = NULL; |
| 275 | 187 |
| 276 DeleteArray(message_buffer_); | 188 DeleteArray(message_buffer_); |
| 277 message_buffer_ = NULL; | 189 message_buffer_ = NULL; |
| 278 | 190 |
| 279 delete mutex_; | 191 delete mutex_; |
| 280 mutex_ = NULL; | 192 mutex_ = NULL; |
| 281 | 193 |
| 282 is_stopped_ = false; | 194 is_stopped_ = false; |
| 195 return result; |
| 283 } | 196 } |
| 284 | 197 |
| 285 | 198 |
| 286 int Log::GetLogLines(int from_pos, char* dest_buf, int max_size) { | |
| 287 if (write_to_file_) return 0; | |
| 288 ASSERT(output_buffer_ != NULL); | |
| 289 ASSERT(from_pos >= 0); | |
| 290 ASSERT(max_size >= 0); | |
| 291 int actual_size = output_buffer_->Read(from_pos, dest_buf, max_size); | |
| 292 ASSERT(actual_size <= max_size); | |
| 293 if (actual_size == 0) return 0; | |
| 294 | |
| 295 // Find previous log line boundary. | |
| 296 char* end_pos = dest_buf + actual_size - 1; | |
| 297 while (end_pos >= dest_buf && *end_pos != '\n') --end_pos; | |
| 298 actual_size = static_cast<int>(end_pos - dest_buf + 1); | |
| 299 // If the assertion below is hit, it means that there was no line end | |
| 300 // found --- something wrong has happened. | |
| 301 ASSERT(actual_size > 0); | |
| 302 ASSERT(actual_size <= max_size); | |
| 303 return actual_size; | |
| 304 } | |
| 305 | |
| 306 | |
| 307 LogMessageBuilder::LogMessageBuilder(Logger* logger) | 199 LogMessageBuilder::LogMessageBuilder(Logger* logger) |
| 308 : log_(logger->log_), | 200 : log_(logger->log_), |
| 309 sl(log_->mutex_), | 201 sl(log_->mutex_), |
| 310 pos_(0) { | 202 pos_(0) { |
| 311 ASSERT(log_->message_buffer_ != NULL); | 203 ASSERT(log_->message_buffer_ != NULL); |
| 312 } | 204 } |
| 313 | 205 |
| 314 | 206 |
| 315 void LogMessageBuilder::Append(const char* format, ...) { | 207 void LogMessageBuilder::Append(const char* format, ...) { |
| 316 Vector<char> buf(log_->message_buffer_ + pos_, | 208 Vector<char> buf(log_->message_buffer_ + pos_, |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 Vector<char> buf(log_->message_buffer_ + pos_, | 294 Vector<char> buf(log_->message_buffer_ + pos_, |
| 403 Log::kMessageBufferSize - pos_); | 295 Log::kMessageBufferSize - pos_); |
| 404 OS::StrNCpy(buf, str, len); | 296 OS::StrNCpy(buf, str, len); |
| 405 pos_ += len; | 297 pos_ += len; |
| 406 ASSERT(pos_ <= Log::kMessageBufferSize); | 298 ASSERT(pos_ <= Log::kMessageBufferSize); |
| 407 } | 299 } |
| 408 | 300 |
| 409 | 301 |
| 410 void LogMessageBuilder::WriteToLogFile() { | 302 void LogMessageBuilder::WriteToLogFile() { |
| 411 ASSERT(pos_ <= Log::kMessageBufferSize); | 303 ASSERT(pos_ <= Log::kMessageBufferSize); |
| 412 const int written = log_->write_to_file_ ? | 304 const int written = log_->WriteToFile(log_->message_buffer_, pos_); |
| 413 log_->WriteToFile(log_->message_buffer_, pos_) : | |
| 414 log_->WriteToMemory(log_->message_buffer_, pos_); | |
| 415 if (written != pos_) { | 305 if (written != pos_) { |
| 416 log_->stop(); | 306 log_->stop(); |
| 417 log_->logger_->LogFailure(); | 307 log_->logger_->LogFailure(); |
| 418 } | 308 } |
| 419 } | 309 } |
| 420 | 310 |
| 421 | 311 |
| 422 } } // namespace v8::internal | 312 } } // namespace v8::internal |
| OLD | NEW |