Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-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 #ifndef V8_LOG_UTILS_H_ | 28 #ifndef V8_LOG_UTILS_H_ |
| 29 #define V8_LOG_UTILS_H_ | 29 #define V8_LOG_UTILS_H_ |
| 30 | 30 |
| 31 namespace v8 { | 31 namespace v8 { |
| 32 namespace internal { | 32 namespace internal { |
| 33 | 33 |
| 34 #ifdef ENABLE_LOGGING_AND_PROFILING | 34 #ifdef ENABLE_LOGGING_AND_PROFILING |
| 35 | 35 |
| 36 // A memory buffer that increments its size as you write in it. Size | 36 // A memory buffer that increments its size as you write in it. Size |
| 37 // is incremented with 'block_size' steps, never exceeding 'max_size'. | 37 // is incremented with 'block_size' steps, never exceeding 'max_size'. |
| 38 // During growth, memory contents are never copied. | 38 // During growth, memory contents are never copied. At the end of the |
| 39 // buffer an amount of memory specified in 'seal_size' is reserved. | |
| 40 // When writing position reaches max_size - seal_size, buffer auto-seals | |
| 41 // itself with 'seal' and allows no further writes. Data pointed by | |
| 42 // 'seal' must be available during entire LogDynamicBuffer lifetime. | |
| 39 // | 43 // |
| 40 // An instance of this class is created dynamically by Log. | 44 // An instance of this class is created dynamically by Log. |
| 41 class LogDynamicBuffer { | 45 class LogDynamicBuffer { |
| 42 public: | 46 public: |
| 43 LogDynamicBuffer(int block_size, int max_size); | 47 LogDynamicBuffer( |
| 48 int block_size, int max_size, const char* seal, int seal_size); | |
| 44 | 49 |
| 45 ~LogDynamicBuffer(); | 50 ~LogDynamicBuffer(); |
| 46 | 51 |
| 47 // Reads contents of the buffer starting from 'from_pos'. Upon | 52 // Reads contents of the buffer starting from 'from_pos'. Upon |
| 48 // return, 'dest_buf' is filled with the data. Actual amount of data | 53 // return, 'dest_buf' is filled with the data. Actual amount of data |
| 49 // filled is returned, it is <= 'buf_size'. | 54 // filled is returned, it is <= 'buf_size'. |
| 50 int Read(int from_pos, char* dest_buf, int buf_size); | 55 int Read(int from_pos, char* dest_buf, int buf_size); |
| 51 | 56 |
| 52 // Writes 'data' to the buffer, making it larger if necessary. If | 57 // Writes 'data' to the buffer, making it larger if necessary. If |
| 53 // data is too big to fit in the buffer, it doesn't get written at | 58 // data is too big to fit in the buffer, it doesn't get written at |
| 54 // all. Returns amount of data written (it is either 'data_size', or | 59 // all. In that case, buffer auto-seals itself and stops to accept |
| 55 // 0, if 'data' is too big). | 60 // any incoming writes. Returns amount of data written (it is either |
| 61 // 'data_size', or 0, if 'data' is too big). | |
| 56 int Write(const char* data, int data_size); | 62 int Write(const char* data, int data_size); |
| 57 | 63 |
| 58 private: | 64 private: |
| 59 void AllocateBlock(int index) { | 65 void AllocateBlock(int index) { |
| 60 blocks_[index] = NewArray<char>(block_size_); | 66 blocks_[index] = NewArray<char>(block_size_); |
| 61 } | 67 } |
| 62 | 68 |
| 63 int BlockIndex(int pos) const { return pos / block_size_; } | 69 int BlockIndex(int pos) const { return pos / block_size_; } |
| 64 | 70 |
| 65 int BlocksCount() const { return BlockIndex(max_size_) + 1; } | 71 int BlocksCount() const { return BlockIndex(max_size_) + 1; } |
| 66 | 72 |
| 67 int PosInBlock(int pos) const { return pos % block_size_; } | 73 int PosInBlock(int pos) const { return pos % block_size_; } |
| 68 | 74 |
| 75 int Seal(); | |
| 76 | |
| 77 int WriteInternal(const char* data, int data_size); | |
| 78 | |
| 69 const int block_size_; | 79 const int block_size_; |
| 70 const int max_size_; | 80 const int max_size_; |
| 81 const char* seal_; | |
| 82 const int seal_size_; | |
| 71 ScopedVector<char*> blocks_; | 83 ScopedVector<char*> blocks_; |
| 72 int write_pos_; | 84 int write_pos_; |
| 73 int block_index_; | 85 int block_index_; |
| 74 int block_write_pos_; | 86 int block_write_pos_; |
| 87 bool is_sealed_; | |
| 75 }; | 88 }; |
| 76 | 89 |
| 77 | 90 |
| 78 // Functions and data for performing output of log messages. | 91 // Functions and data for performing output of log messages. |
| 79 class Log : public AllStatic { | 92 class Log : public AllStatic { |
| 80 public: | 93 public: |
| 81 // Opens stdout for logging. | 94 // Opens stdout for logging. |
| 82 static void OpenStdout(); | 95 static void OpenStdout(); |
| 83 | 96 |
| 84 // Opens file for logging. | 97 // Opens file for logging. |
| 85 static void OpenFile(const char* name); | 98 static void OpenFile(const char* name); |
| 86 | 99 |
| 87 // Opens memory buffer for logging. | 100 // Opens memory buffer for logging. |
| 88 static void OpenMemoryBuffer(); | 101 static void OpenMemoryBuffer(); |
| 89 | 102 |
| 103 // Disables logging, but preserves acquired resources. | |
| 104 static void stop() { is_stopped_ = true; } | |
| 105 | |
| 90 // Frees all resources acquired in Open... functions. | 106 // Frees all resources acquired in Open... functions. |
| 91 static void Close(); | 107 static void Close(); |
| 92 | 108 |
| 93 // See description in include/v8.h. | 109 // See description in include/v8.h. |
| 94 static int GetLogLines(int from_pos, char* dest_buf, int max_size); | 110 static int GetLogLines(int from_pos, char* dest_buf, int max_size); |
| 95 | 111 |
| 96 // Returns whether logging is enabled. | 112 // Returns whether logging is enabled. |
| 97 static bool IsEnabled() { | 113 static bool IsEnabled() { |
| 98 return output_handle_ != NULL || output_buffer_ != NULL; | 114 return !is_stopped_ && (output_handle_ != NULL || output_buffer_ != NULL); |
| 99 } | 115 } |
| 100 | 116 |
| 101 private: | 117 private: |
| 102 typedef int (*WritePtr)(const char* msg, int length); | 118 typedef int (*WritePtr)(const char* msg, int length); |
| 103 | 119 |
| 104 // Initialization function called from Open... functions. | 120 // Initialization function called from Open... functions. |
| 105 static void Init(); | 121 static void Init(); |
| 106 | 122 |
| 107 // Write functions assume that mutex_ is acquired by the caller. | 123 // Write functions assume that mutex_ is acquired by the caller. |
| 108 static WritePtr Write; | 124 static WritePtr Write; |
| 109 | 125 |
| 110 // Implementation of writing to a log file. | 126 // Implementation of writing to a log file. |
| 111 static int WriteToFile(const char* msg, int length) { | 127 static int WriteToFile(const char* msg, int length) { |
| 112 ASSERT(output_handle_ != NULL); | 128 ASSERT(output_handle_ != NULL); |
| 113 int rv = fwrite(msg, 1, length, output_handle_); | 129 int rv = fwrite(msg, 1, length, output_handle_); |
| 114 ASSERT(length == rv); | 130 ASSERT(length == rv); |
| 115 return rv; | 131 return rv; |
| 116 } | 132 } |
| 117 | 133 |
| 118 // Implementation of writing to a memory buffer. | 134 // Implementation of writing to a memory buffer. |
| 119 static int WriteToMemory(const char* msg, int length) { | 135 static int WriteToMemory(const char* msg, int length) { |
| 120 ASSERT(output_buffer_ != NULL); | 136 ASSERT(output_buffer_ != NULL); |
| 121 return output_buffer_->Write(msg, length); | 137 return output_buffer_->Write(msg, length); |
| 122 } | 138 } |
| 123 | 139 |
| 140 // Whether logging is stopped (e.g. due to insufficient resources). | |
| 141 static bool is_stopped_; | |
| 142 | |
| 124 // When logging is active, either output_handle_ or output_buffer_ is used | 143 // When logging is active, either output_handle_ or output_buffer_ is used |
| 125 // to store a pointer to log destination. If logging was opened via OpenStdout | 144 // to store a pointer to log destination. If logging was opened via OpenStdout |
| 126 // or OpenFile, then output_handle_ is used. If logging was opened | 145 // or OpenFile, then output_handle_ is used. If logging was opened |
| 127 // via OpenMemoryBuffer, then output_buffer_ is used. | 146 // via OpenMemoryBuffer, then output_buffer_ is used. |
| 128 // mutex_ should be acquired before using output_handle_ or output_buffer_. | 147 // mutex_ should be acquired before using output_handle_ or output_buffer_. |
| 129 static FILE* output_handle_; | 148 static FILE* output_handle_; |
| 130 | 149 |
| 131 static LogDynamicBuffer* output_buffer_; | 150 static LogDynamicBuffer* output_buffer_; |
| 132 | 151 |
| 133 // Size of dynamic buffer block (and dynamic buffer initial size). | 152 // Size of dynamic buffer block (and dynamic buffer initial size). |
| 134 static const int kDynamicBufferBlockSize = 65536; | 153 static const int kDynamicBufferBlockSize = 65536; |
| 135 | 154 |
| 136 // Maximum size of dynamic buffer. | 155 // Maximum size of dynamic buffer. |
| 137 static const int kMaxDynamicBufferSize = 50 * 1024 * 1024; | 156 static const int kMaxDynamicBufferSize = 50 * 1024 * 1024; |
| 138 | 157 |
| 158 // Message to "seal" dynamic buffer with. | |
| 159 static const char* kDynamicBufferSeal; | |
| 160 | |
| 139 // mutex_ is a Mutex used for enforcing exclusive | 161 // mutex_ is a Mutex used for enforcing exclusive |
| 140 // access to the formatting buffer and the log file or log memory buffer. | 162 // access to the formatting buffer and the log file or log memory buffer. |
| 141 static Mutex* mutex_; | 163 static Mutex* mutex_; |
| 142 | 164 |
| 143 // Size of buffer used for formatting log messages. | 165 // Size of buffer used for formatting log messages. |
| 144 static const int kMessageBufferSize = 2048; | 166 static const int kMessageBufferSize = 2048; |
| 145 | 167 |
| 146 // Buffer used for formatting log messages. This is a singleton buffer and | 168 // Buffer used for formatting log messages. This is a singleton buffer and |
| 147 // mutex_ should be acquired before using it. | 169 // mutex_ should be acquired before using it. |
| 148 static char* message_buffer_; | 170 static char* message_buffer_; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 173 void Append(String* str); | 195 void Append(String* str); |
| 174 | 196 |
| 175 void AppendDetailed(String* str, bool show_impl_info); | 197 void AppendDetailed(String* str, bool show_impl_info); |
| 176 | 198 |
| 177 // Write the log message to the log file currently opened. | 199 // Write the log message to the log file currently opened. |
| 178 void WriteToLogFile(); | 200 void WriteToLogFile(); |
| 179 | 201 |
| 180 // Write a null-terminated string to to the log file currently opened. | 202 // Write a null-terminated string to to the log file currently opened. |
| 181 void WriteCStringToLogFile(const char* str); | 203 void WriteCStringToLogFile(const char* str); |
| 182 | 204 |
| 205 // A handler that is called when Log::Write fails. | |
| 206 typedef void (*WriteFailureHandler)(); | |
| 207 static WriteFailureHandler write_failure_handler; | |
|
Søren Thygesen Gjesse
2009/05/28 13:23:35
Please make the write_failure_handler member priva
Mikhail Naganov
2009/05/28 13:52:52
Done.
| |
| 208 | |
| 183 private: | 209 private: |
| 184 ScopedLock sl; | 210 ScopedLock sl; |
| 185 int pos_; | 211 int pos_; |
| 186 }; | 212 }; |
| 187 | 213 |
| 188 #endif // ENABLE_LOGGING_AND_PROFILING | 214 #endif // ENABLE_LOGGING_AND_PROFILING |
| 189 | 215 |
| 190 } } // namespace v8::internal | 216 } } // namespace v8::internal |
| 191 | 217 |
| 192 #endif // V8_LOG_UTILS_H_ | 218 #endif // V8_LOG_UTILS_H_ |
| OLD | NEW |