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 namespace OHOS { 25 namespace ResourceSchedule { 26 using Clock = std::chrono::high_resolution_clock; 27 using MilliSecondsType = std::chrono::duration<double, std::milli>; 28 29 class ChronoScope { 30 public: ChronoScope(const std::string outmsg)31 explicit ChronoScope(const std::string outmsg) : outmsg_(outmsg) 32 { 33 out_ = nullptr; 34 t1 = Clock::now(); 35 } 36 ChronoScope(const std::string outmsg,double * out)37 ChronoScope(const std::string outmsg, double* out) : outmsg_(outmsg), out_(out) 38 { 39 t1 = Clock::now(); 40 } 41 ~ChronoScope()42 ~ChronoScope() 43 { 44 Clock::time_point t2 = Clock::now(); 45 MilliSecondsType time_span = std::chrono::duration_cast<MilliSecondsType>(t2 - t1); 46 HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, LOG_TAG_DOMAIN_ID_RMS, "ChronoScope"}; 47 CGS_LOGD("[%{public}s] cost %{public}lf milliseconds.", outmsg_.c_str(), time_span.count()); 48 if (out_) { 49 *out_ = time_span.count(); 50 } 51 } 52 53 private: 54 ChronoScope(const ChronoScope&) = delete; 55 ChronoScope& operator=(const ChronoScope &) = delete; 56 ChronoScope(ChronoScope&&) = delete; 57 ChronoScope& operator=(ChronoScope&&) = delete; 58 59 std::string outmsg_; 60 double* out_; 61 Clock::time_point t1; 62 }; 63 } // namespace ResourceSchedule 64 } // namespace OHOS 65 #endif // CGROUP_SCHED_COMMON_INCLUDE_CGROUP_SCHED_COMMON_H_ 66