1 /* 2 * Copyright (c) 2024 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 #include "record_mutex.h" 17 #include "sched/execute_ctx.h" 18 #include "sched/workgroup_internal.h" 19 #include "tm/cpu_task.h" 20 21 namespace { 22 constexpr uint64_t MUTEX_TIMEOUT_THRESHOLD_US = 30 * 1000 * 1000; 23 } 24 25 namespace ffrt { IsTimeout()26bool RecordMutex::IsTimeout() 27 { 28 return GetDuration() > MUTEX_TIMEOUT_THRESHOLD_US; 29 } 30 LoadInfo()31void RecordMutex::LoadInfo() 32 { 33 if (ExecuteCtx::Cur()->task) { 34 owner_.id = ExecuteCtx::Cur()->task->gid; 35 owner_.type = MutexOwnerType::MUTEX_OWNER_TYPE_TASK; 36 } else { 37 owner_.id = static_cast<uint64_t>(gettid()); 38 owner_.type = MutexOwnerType::MUTEX_OWNER_TYPE_THREAD; 39 } 40 41 owner_.timestamp = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>( 42 std::chrono::steady_clock::now().time_since_epoch()).count()); 43 } 44 GetDuration()45uint64_t RecordMutex::GetDuration() 46 { 47 uint64_t now = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>( 48 std::chrono::steady_clock::now().time_since_epoch()).count()); 49 uint64_t past = owner_.timestamp; 50 return (now > past) ? now - past : 0; 51 } 52 } 53