diff --git a/frameworks/native/common/include/time_threshold.h b/frameworks/native/common/include/time_threshold.h new file mode 100644 index 0000000000000000000000000000000000000000..1e02c02d47248fe22b58bf840172f992b30457ea --- /dev/null +++ b/frameworks/native/common/include/time_threshold.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DATA_SHARE_TIME_THRESHOLD_H +#define DATA_SHARE_TIME_THRESHOLD_H + +#include +#include + +namespace OHOS { +namespace DataShare { +class TimeThreshold { +public: + TimeThreshold(std::chrono::microseconds threshold, std::string message); + ~TimeThreshold(); + + std::chrono::microseconds threshold_; + std::string message_; + std::chrono::steady_clock::time_point start_; +}; +} // namespace DataShare +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/frameworks/native/common/src/time_threshold.cpp b/frameworks/native/common/src/time_threshold.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10cba2754bb28c7520521b8a44040c510a19ea8c --- /dev/null +++ b/frameworks/native/common/src/time_threshold.cpp @@ -0,0 +1,26 @@ +#include "time_threshold.h" + +#include +#include +#include "datashare_log.h" + +namespace OHOS { +namespace DataShare { +TimeThreshold::TimeThreshold(std::chrono::microseconds threshold, std::string message) +{ + threshold_ = threshold; + message_ = message; + start_ = std::chrono::steady_clock::now(); +} + +TimeThreshold::~TimeThreshold() +{ + auto finish = std::chrono::steady_clock::now(); + auto duration = std::chrono::duration_cast(finish - start_); + if (duration >= threshold_) { + int64_t milliseconds = duration.count(); + LOG_ERROR("%{public}s over time, cost:%{public}" PRIi64 "ms", message_.c_str(), milliseconds); + } +} +} // namespace DataShare +} // namespace DataShare \ No newline at end of file diff --git a/interfaces/inner_api/common/BUILD.gn b/interfaces/inner_api/common/BUILD.gn index dd28c64e98a709985d5617c439d58f8b2834a52b..da0a62e381a067bce340b3f832ebcd3e627da6e1 100644 --- a/interfaces/inner_api/common/BUILD.gn +++ b/interfaces/inner_api/common/BUILD.gn @@ -50,6 +50,7 @@ datashare_common_sources = [ "${datashare_common_native_path}/src/ishared_result_set_proxy.cpp", "${datashare_common_native_path}/src/ishared_result_set_stub.cpp", "${datashare_common_native_path}/src/shared_block.cpp", + "${datashare_common_native_path}/src/time_threshold.cpp", ] datashare_common_external_deps = [