1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CGROUP_SCHED_COMMON_INCLUDE_CGROUP_SCHED_COMMON_H_ 17 #define CGROUP_SCHED_COMMON_INCLUDE_CGROUP_SCHED_COMMON_H_ 18 19 #include <chrono> 20 #include <ctime> 21 22 #include "cgroup_sched_log.h" 23 24 #undef LOG_TAG 25 #define LOG_TAG "ChronoScope" 26 27 namespace OHOS { 28 namespace ResourceSchedule { 29 using Clock = std::chrono::high_resolution_clock; 30 using MilliSecondsType = std::chrono::duration<double, std::milli>; 31 32 class ChronoScope { 33 public: ChronoScope(const std::string outmsg)34 explicit ChronoScope(const std::string outmsg) : outmsg_(outmsg) 35 { 36 out_ = nullptr; 37 t1 = Clock::now(); 38 } 39 ChronoScope(const std::string outmsg,double * out)40 ChronoScope(const std::string outmsg, double* out) : outmsg_(outmsg), out_(out) 41 { 42 t1 = Clock::now(); 43 } 44 ~ChronoScope()45 ~ChronoScope() 46 { 47 Clock::time_point t2 = Clock::now(); 48 MilliSecondsType time_span = std::chrono::duration_cast<MilliSecondsType>(t2 - t1); 49 CGS_LOGD("[%{public}s] cost %{public}lf milliseconds.", outmsg_.c_str(), time_span.count()); 50 if (out_) { 51 *out_ = time_span.count(); 52 } 53 } 54 55 private: 56 ChronoScope(const ChronoScope&) = delete; 57 ChronoScope& operator=(const ChronoScope &) = delete; 58 ChronoScope(ChronoScope&&) = delete; 59 ChronoScope& operator=(ChronoScope&&) = delete; 60 61 std::string outmsg_; 62 double* out_; 63 Clock::time_point t1; 64 }; 65 } // namespace ResourceSchedule 66 } // namespace OHOS 67 #endif // CGROUP_SCHED_COMMON_INCLUDE_CGROUP_SCHED_COMMON_H_ 68