Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_SRC_TIMING_LOGGER_H_ |
| 18 | #define ART_SRC_TIMING_LOGGER_H_ |
| 19 | |
| 20 | #include "logging.h" |
| 21 | #include "utils.h" |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | class TimingLogger { |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 31 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 32 | explicit TimingLogger(const char* name) : name_(name) { |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 33 | AddSplit(""); |
| 34 | } |
| 35 | |
| 36 | void AddSplit(const std::string& label) { |
| 37 | times_.push_back(NanoTime()); |
| 38 | labels_.push_back(label); |
| 39 | } |
| 40 | |
| 41 | void Dump() { |
| 42 | LOG(INFO) << name_ << ": begin"; |
| 43 | for (size_t i = 1; i < times_.size(); ++i) { |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 44 | LOG(INFO) << name_ << StringPrintf(": %8lld ms, ", NsToMs(times_[i] - times_[i-1])) << labels_[i]; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 45 | } |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 46 | LOG(INFO) << name_ << ": end, " << NsToMs(GetTotalNs()) << " ms"; |
| 47 | } |
| 48 | |
| 49 | uint64_t GetTotalNs() { |
| 50 | return times_.back() - times_.front(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 53 | private: |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 54 | std::string name_; |
| 55 | std::vector<uint64_t> times_; |
| 56 | std::vector<std::string> labels_; |
| 57 | }; |
| 58 | |
| 59 | } // namespace art |
| 60 | |
| 61 | #endif // ART_SRC_TIMING_LOGGER_H_ |