1 /* 2 * Copyright (c) 2025 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 "perf_utils.h" 17 18 namespace OHOS { 19 namespace HiviewDFX { 20 GetCurrentRealTimeNs()21int64_t GetCurrentRealTimeNs() 22 { 23 struct timespec ts = { 0, 0 }; 24 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { 25 return 0; 26 } 27 return (ts.tv_sec * NS_TO_S + ts.tv_nsec); 28 } 29 GetCurrentSystimeMs()30int64_t GetCurrentSystimeMs() 31 { 32 auto timeNow = std::chrono::system_clock::now(); 33 auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(timeNow.time_since_epoch()); 34 int64_t curSystime = tmp.count(); 35 return curSystime; 36 } 37 ConvertRealtimeToSystime(int64_t realTime,int64_t & sysTime)38void ConvertRealtimeToSystime(int64_t realTime, int64_t& sysTime) 39 { 40 int64_t curRealTime = GetCurrentRealTimeNs(); 41 if (curRealTime == 0) { 42 sysTime = 0; 43 return; 44 } 45 int64_t curSysTime = GetCurrentSystimeMs(); 46 sysTime = curSysTime - (curRealTime - realTime) / NS_TO_MS; 47 } 48 GetSourceTypeName(PerfSourceType sourceType)49std::string GetSourceTypeName(PerfSourceType sourceType) 50 { 51 std::string type = ""; 52 switch (sourceType) { 53 case PERF_TOUCH_EVENT: 54 type = "TOUCHSCREEN"; 55 break; 56 case PERF_MOUSE_EVENT: 57 type = "MOUSE"; 58 break; 59 case PERF_TOUCH_PAD: 60 type = "TOUCHPAD"; 61 break; 62 case PERF_JOY_STICK: 63 type = "JOYSTICK"; 64 break; 65 case PERF_KEY_EVENT: 66 type = "KEY_EVENT"; 67 break; 68 default : 69 type = "UNKNOWN_SOURCE"; 70 break; 71 } 72 return type; 73 } 74 75 } 76 }