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