diff --git a/frameworks/js/napi/common/src/datashare_js_utils.cpp b/frameworks/js/napi/common/src/datashare_js_utils.cpp index f55d034bdbc3915284891f290004542c6e5f599c..09eb7cdc9d14a7438aa9a8d15494d72dbe2188fd 100644 --- a/frameworks/js/napi/common/src/datashare_js_utils.cpp +++ b/frameworks/js/napi/common/src/datashare_js_utils.cpp @@ -21,6 +21,7 @@ #include "napi/native_common.h" #include "napi_datashare_values_bucket.h" #include "securec.h" +#include "time_threshold.h" namespace OHOS { namespace DataShare { diff --git a/frameworks/js/napi/common/src/datashare_result_set_proxy.cpp b/frameworks/js/napi/common/src/datashare_result_set_proxy.cpp index 14e53a8f6f79df40f9455a2658adb000b72f8915..b6663698af2060bae79f0fc5f67f638111771e13 100644 --- a/frameworks/js/napi/common/src/datashare_result_set_proxy.cpp +++ b/frameworks/js/napi/common/src/datashare_result_set_proxy.cpp @@ -23,6 +23,7 @@ #include "datashare_js_utils.h" #include "string_ex.h" #include "datashare_log.h" +#include "time_threshold.h" namespace OHOS { namespace DataShare { @@ -170,6 +171,7 @@ std::shared_ptr DataShareResultSetProxy::GetInnerResultSet(n napi_value DataShareResultSetProxy::GoToFirstRow(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int errCode = E_ERROR; std::shared_ptr innerResultSet = GetInnerResultSet(env, info); if (innerResultSet != nullptr) { @@ -180,11 +182,16 @@ napi_value DataShareResultSetProxy::GoToFirstRow(napi_env env, napi_callback_inf } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + } + } napi_value DataShareResultSetProxy::GoToLastRow(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int errCode = E_ERROR; std::shared_ptr innerResultSet = GetInnerResultSet(env, info); if (innerResultSet != nullptr) { @@ -195,11 +202,16 @@ napi_value DataShareResultSetProxy::GoToLastRow(napi_env env, napi_callback_info } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + } + } napi_value DataShareResultSetProxy::GoToNextRow(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int errCode = E_ERROR; std::shared_ptr innerResultSet = GetInnerResultSet(env, info); if (innerResultSet != nullptr) { @@ -210,11 +222,16 @@ napi_value DataShareResultSetProxy::GoToNextRow(napi_env env, napi_callback_info } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + } + } napi_value DataShareResultSetProxy::GoToPreviousRow(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int errCode = E_ERROR; std::shared_ptr innerResultSet = GetInnerResultSet(env, info); if (innerResultSet != nullptr) { @@ -225,11 +242,16 @@ napi_value DataShareResultSetProxy::GoToPreviousRow(napi_env env, napi_callback_ } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + } + } napi_value DataShareResultSetProxy::GoTo(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t offset = -1; size_t argc = MAX_INPUT_COUNT; napi_value args[MAX_INPUT_COUNT] = { 0 }; @@ -246,11 +268,16 @@ napi_value DataShareResultSetProxy::GoTo(napi_env env, napi_callback_info info) } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + } + } napi_value DataShareResultSetProxy::GoToRow(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t position = -1; size_t argc = MAX_INPUT_COUNT; napi_value args[MAX_INPUT_COUNT] = { 0 }; @@ -267,11 +294,16 @@ napi_value DataShareResultSetProxy::GoToRow(napi_env env, napi_callback_info inf } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + } + } napi_value DataShareResultSetProxy::GetBlob(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t columnIndex = -1; std::vector blob; size_t argc = MAX_INPUT_COUNT; @@ -291,11 +323,16 @@ napi_value DataShareResultSetProxy::GetBlob(napi_env env, napi_callback_info inf } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, blob); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, blob); + } + } napi_value DataShareResultSetProxy::GetString(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t columnIndex = -1; std::string value; size_t argc = MAX_INPUT_COUNT; @@ -312,11 +349,16 @@ napi_value DataShareResultSetProxy::GetString(napi_env env, napi_callback_info i } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, value); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, value); + } + } napi_value DataShareResultSetProxy::GetLong(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t columnIndex = -1; int64_t value = -1; size_t argc = MAX_INPUT_COUNT; @@ -336,11 +378,15 @@ napi_value DataShareResultSetProxy::GetLong(napi_env env, napi_callback_info inf } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, value); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, value); + } } napi_value DataShareResultSetProxy::GetDouble(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t columnIndex = -1; double value = 0.0; size_t argc = MAX_INPUT_COUNT; @@ -360,11 +406,15 @@ napi_value DataShareResultSetProxy::GetDouble(napi_env env, napi_callback_info i } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, value); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, value); + } } napi_value DataShareResultSetProxy::Close(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int errCode = E_ERROR; DataShareResultSetProxy *resultSet = nullptr; napi_value self = nullptr; @@ -383,11 +433,15 @@ napi_value DataShareResultSetProxy::Close(napi_env env, napi_callback_info info) } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, (errCode == E_OK)); + } } napi_value DataShareResultSetProxy::GetColumnIndex(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t columnIndex = -1; size_t argc = MAX_INPUT_COUNT; napi_value args[MAX_INPUT_COUNT] = { 0 }; @@ -406,11 +460,15 @@ napi_value DataShareResultSetProxy::GetColumnIndex(napi_env env, napi_callback_i } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, columnIndex); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, columnIndex); + } } napi_value DataShareResultSetProxy::GetColumnName(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t columnIndex = -1; std::string columnName; size_t argc = MAX_INPUT_COUNT; @@ -427,11 +485,15 @@ napi_value DataShareResultSetProxy::GetColumnName(napi_env env, napi_callback_in } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, columnName); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, columnName); + } } napi_value DataShareResultSetProxy::GetDataType(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t columnIndex = -1; DataType dataType = DataType::TYPE_NULL; size_t argc = MAX_INPUT_COUNT; @@ -451,11 +513,15 @@ napi_value DataShareResultSetProxy::GetDataType(napi_env env, napi_callback_info } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, int32_t(dataType)); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, int32_t(dataType)); + } } napi_value DataShareResultSetProxy::GetAllColumnNames(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); std::vector columnNames; std::shared_ptr innerResultSet = GetInnerResultSet(env, info); if (innerResultSet != nullptr) { @@ -466,11 +532,15 @@ napi_value DataShareResultSetProxy::GetAllColumnNames(napi_env env, napi_callbac } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, columnNames); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, columnNames); + } } napi_value DataShareResultSetProxy::GetColumnCount(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t count = -1; std::shared_ptr innerResultSet = GetInnerResultSet(env, info); if (innerResultSet != nullptr) { @@ -481,11 +551,15 @@ napi_value DataShareResultSetProxy::GetColumnCount(napi_env env, napi_callback_i } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, count); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, count); + } } napi_value DataShareResultSetProxy::GetRowCount(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int32_t count = -1; std::shared_ptr innerResultSet = GetInnerResultSet(env, info); if (innerResultSet != nullptr) { @@ -496,11 +570,15 @@ napi_value DataShareResultSetProxy::GetRowCount(napi_env env, napi_callback_info } else { LOG_ERROR("GetInnerResultSet failed."); } - return DataShareJSUtils::Convert2JSValue(env, count); + { + TimeThreshold time2(std::chrono::microseconds::zero(), __FUNCTION__); + return DataShareJSUtils::Convert2JSValue(env, count); + } } napi_value DataShareResultSetProxy::IsClosed(napi_env env, napi_callback_info info) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); bool result = false; std::shared_ptr innerResultSet = GetInnerResultSet(env, info); if (innerResultSet != nullptr) { 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/ishared_result_set_stub.cpp b/frameworks/native/common/src/ishared_result_set_stub.cpp index a837ade99600da9ac974eacb1ba8df703e7db848..d65657be9173dbfd099a8ea54ce1a14589373b6a 100644 --- a/frameworks/native/common/src/ishared_result_set_stub.cpp +++ b/frameworks/native/common/src/ishared_result_set_stub.cpp @@ -17,6 +17,7 @@ #include "datashare_log.h" #include "datashare_errno.h" #include "string_ex.h" +#include "time_threshold.h" namespace OHOS::DataShare { std::function(std::shared_ptr, @@ -74,6 +75,7 @@ int ISharedResultSetStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &da int ISharedResultSetStub::HandleGetRowCountRequest(MessageParcel &data, MessageParcel &reply) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int count = -1; int errCode = resultSet_->GetRowCount(count); reply.WriteInt32(errCode); @@ -86,6 +88,7 @@ int ISharedResultSetStub::HandleGetRowCountRequest(MessageParcel &data, MessageP int ISharedResultSetStub::HandleGetAllColumnNamesRequest(MessageParcel &data, MessageParcel &reply) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); std::vector names; int errCode = resultSet_->GetAllColumnNames(names); reply.WriteInt32(errCode); @@ -98,6 +101,7 @@ int ISharedResultSetStub::HandleGetAllColumnNamesRequest(MessageParcel &data, Me int ISharedResultSetStub::HandleOnGoRequest(MessageParcel &data, MessageParcel &reply) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int oldRow = data.ReadInt32(); int newRow = data.ReadInt32(); int cachedIndex = 0; @@ -113,6 +117,7 @@ int ISharedResultSetStub::HandleOnGoRequest(MessageParcel &data, MessageParcel & int ISharedResultSetStub::HandleCloseRequest(MessageParcel &data, MessageParcel &reply) { + TimeThreshold time(std::chrono::microseconds::zero(), __FUNCTION__); int errCode = resultSet_->Close(); reply.WriteInt32(errCode); LOG_DEBUG("errCode %{public}d", errCode); diff --git a/frameworks/native/common/src/time_threshold.cpp b/frameworks/native/common/src/time_threshold.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f1b46513947b2bb36140609ddd436dc567dd3fb --- /dev/null +++ b/frameworks/native/common/src/time_threshold.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 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. + */ +#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 = [