| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VM_TIMER_H_ | 5 #ifndef VM_TIMER_H_ |
| 6 #define VM_TIMER_H_ | 6 #define VM_TIMER_H_ |
| 7 | 7 |
| 8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
| 9 #include "vm/allocation.h" | 9 #include "vm/allocation.h" |
| 10 #include "vm/atomic.h" |
| 10 #include "vm/flags.h" | 11 #include "vm/flags.h" |
| 11 #include "vm/os.h" | 12 #include "vm/os.h" |
| 12 | 13 |
| 13 namespace dart { | 14 namespace dart { |
| 14 | 15 |
| 15 // Timer class allows timing of specific operations in the VM. | 16 // Timer class allows timing of specific operations in the VM. |
| 16 class Timer : public ValueObject { | 17 class Timer : public ValueObject { |
| 17 public: | 18 public: |
| 18 Timer(bool report, const char* message) | 19 Timer(bool report, const char* message) |
| 19 : report_(report), message_(message) { | 20 : report_(report), message_(message) { |
| 20 Reset(); | 21 Reset(); |
| 21 } | 22 } |
| 22 ~Timer() {} | 23 ~Timer() {} |
| 23 | 24 |
| 24 // Start timer. | 25 // Start timer. |
| 25 void Start() { | 26 void Start() { |
| 26 start_ = OS::GetCurrentTimeMicros(); | 27 start_ = OS::GetCurrentTimeMicros(); |
| 27 running_ = true; | 28 running_ = true; |
| 28 } | 29 } |
| 29 | 30 |
| 30 // Stop timer. | 31 // Stop timer. |
| 31 void Stop() { | 32 void Stop() { |
| 32 ASSERT(start_ != 0); | 33 ASSERT(start_ != 0); |
| 33 ASSERT(running()); | 34 ASSERT(running()); |
| 34 stop_ = OS::GetCurrentTimeMicros(); | 35 stop_ = OS::GetCurrentTimeMicros(); |
| 35 int64_t elapsed = ElapsedMicros(); | 36 int64_t elapsed = ElapsedMicros(); |
| 36 max_contiguous_ = Utils::Maximum(max_contiguous_, elapsed); | 37 max_contiguous_ = Utils::Maximum(max_contiguous_, elapsed); |
| 37 total_ += elapsed; | 38 // Make increment atomic in case it occurs in parallel with aggregation. |
| 39 AtomicOperations::IncrementInt64By(&total_, elapsed); |
| 38 running_ = false; | 40 running_ = false; |
| 39 } | 41 } |
| 40 | 42 |
| 41 // Get total cummulative elapsed time in micros. | 43 // Get total cummulative elapsed time in micros. |
| 42 int64_t TotalElapsedTime() const { | 44 int64_t TotalElapsedTime() const { |
| 43 int64_t result = total_; | 45 int64_t result = total_; |
| 44 if (running_) { | 46 if (running_) { |
| 45 int64_t now = OS::GetCurrentTimeMicros(); | 47 int64_t now = OS::GetCurrentTimeMicros(); |
| 46 result += (now - start_); | 48 result += (now - start_); |
| 47 } | 49 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 58 } | 60 } |
| 59 | 61 |
| 60 void Reset() { | 62 void Reset() { |
| 61 start_ = 0; | 63 start_ = 0; |
| 62 stop_ = 0; | 64 stop_ = 0; |
| 63 total_ = 0; | 65 total_ = 0; |
| 64 max_contiguous_ = 0; | 66 max_contiguous_ = 0; |
| 65 running_ = false; | 67 running_ = false; |
| 66 } | 68 } |
| 67 | 69 |
| 70 bool IsReset() const { |
| 71 return (start_ == 0) && (stop_ == 0) && (total_ == 0) && |
| 72 (max_contiguous_ == 0) && !running_; |
| 73 } |
| 74 |
| 75 void AddTotal(const Timer& other) { |
| 76 AtomicOperations::IncrementInt64By(&total_, other.total_); |
| 77 } |
| 78 |
| 68 // Accessors. | 79 // Accessors. |
| 69 bool report() const { return report_; } | 80 bool report() const { return report_; } |
| 70 bool running() const { return running_; } | 81 bool running() const { return running_; } |
| 71 const char* message() const { return message_; } | 82 const char* message() const { return message_; } |
| 72 | 83 |
| 73 private: | 84 private: |
| 74 int64_t ElapsedMicros() const { | 85 int64_t ElapsedMicros() const { |
| 75 ASSERT(start_ != 0); | 86 ASSERT(start_ != 0); |
| 76 ASSERT(stop_ != 0); | 87 ASSERT(stop_ != 0); |
| 77 return stop_ - start_; | 88 return stop_ - start_; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 171 |
| 161 DISALLOW_ALLOCATION(); | 172 DISALLOW_ALLOCATION(); |
| 162 DISALLOW_COPY_AND_ASSIGN(PauseTimerScope); | 173 DISALLOW_COPY_AND_ASSIGN(PauseTimerScope); |
| 163 }; | 174 }; |
| 164 | 175 |
| 165 | 176 |
| 166 | 177 |
| 167 } // namespace dart | 178 } // namespace dart |
| 168 | 179 |
| 169 #endif // VM_TIMER_H_ | 180 #endif // VM_TIMER_H_ |
| OLD | NEW |