Chromium Code Reviews| 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 total_ += elapsed; |
|
hausner
2016/03/30 19:49:20
What if another thread does AddTotal at the same t
srdjan
2016/03/30 20:14:04
Using Atomic increment.
| |
| 38 running_ = false; | 39 running_ = false; |
| 39 } | 40 } |
| 40 | 41 |
| 41 // Get total cummulative elapsed time in micros. | 42 // Get total cummulative elapsed time in micros. |
| 42 int64_t TotalElapsedTime() const { | 43 int64_t TotalElapsedTime() const { |
| 43 int64_t result = total_; | 44 int64_t result = total_; |
| 44 if (running_) { | 45 if (running_) { |
| 45 int64_t now = OS::GetCurrentTimeMicros(); | 46 int64_t now = OS::GetCurrentTimeMicros(); |
| 46 result += (now - start_); | 47 result += (now - start_); |
| 47 } | 48 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 58 } | 59 } |
| 59 | 60 |
| 60 void Reset() { | 61 void Reset() { |
| 61 start_ = 0; | 62 start_ = 0; |
| 62 stop_ = 0; | 63 stop_ = 0; |
| 63 total_ = 0; | 64 total_ = 0; |
| 64 max_contiguous_ = 0; | 65 max_contiguous_ = 0; |
| 65 running_ = false; | 66 running_ = false; |
| 66 } | 67 } |
| 67 | 68 |
| 69 bool IsReset() const { | |
| 70 return (start_ == 0) && (stop_ == 0) && (total_ == 0) && | |
| 71 (max_contiguous_ == 0) && !running_; | |
| 72 } | |
| 73 | |
| 74 void AddTotal(const Timer& other) { | |
| 75 AtomicOperations::IncrementInt64By(&total_, other.total_); | |
| 76 } | |
| 77 | |
| 68 // Accessors. | 78 // Accessors. |
| 69 bool report() const { return report_; } | 79 bool report() const { return report_; } |
| 70 bool running() const { return running_; } | 80 bool running() const { return running_; } |
| 71 const char* message() const { return message_; } | 81 const char* message() const { return message_; } |
| 72 | 82 |
| 73 private: | 83 private: |
| 74 int64_t ElapsedMicros() const { | 84 int64_t ElapsedMicros() const { |
| 75 ASSERT(start_ != 0); | 85 ASSERT(start_ != 0); |
| 76 ASSERT(stop_ != 0); | 86 ASSERT(stop_ != 0); |
| 77 return stop_ - start_; | 87 return stop_ - start_; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 | 170 |
| 161 DISALLOW_ALLOCATION(); | 171 DISALLOW_ALLOCATION(); |
| 162 DISALLOW_COPY_AND_ASSIGN(PauseTimerScope); | 172 DISALLOW_COPY_AND_ASSIGN(PauseTimerScope); |
| 163 }; | 173 }; |
| 164 | 174 |
| 165 | 175 |
| 166 | 176 |
| 167 } // namespace dart | 177 } // namespace dart |
| 168 | 178 |
| 169 #endif // VM_TIMER_H_ | 179 #endif // VM_TIMER_H_ |
| OLD | NEW |