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 16import { fileOpen, travelFile } from '../profiler/base/BaseProfilerUtils'; 17import { ProcessRunningInfo } from '../utils/../entity/SystemEntity' 18import appManager from '@ohos.application.appManager'; 19 20export function getCpuCoreInfo(): Array<String> { 21 const basePath = "/sys/devices/system/cpu/cpufreq" 22 let defaultPolicy = new Array( 23 "policy0", "policy1", "policy2" 24 ) 25 var supportPolicy = [] 26 let policyArr = travelFile(basePath, "policy") 27 28 policyArr.forEach((policy) => { 29 defaultPolicy.forEach((defaultItem) => { 30 if (defaultItem == policy) { 31 supportPolicy.push(defaultItem) 32 } 33 }) 34 }) 35 var coreArr = new Array() 36 for (var index = 0; index < supportPolicy.length; index++) { 37 const policy = supportPolicy[index]; 38 var affected_cpus = fileOpen(basePath + "/" + policy + "/affected_cpus") 39 coreArr.push(affected_cpus.charAt(0)) 40 } 41 return coreArr 42} 43 44export function getPidOfAbility(processName: string): Promise<String> { 45 return appManager.getProcessRunningInfos().then(data => { 46 let processArr: Array<ProcessRunningInfo> = data 47 var pid = "-1" 48 processArr.forEach(process=>{ 49 if (process.processName == processName) { 50 pid = process.pid.toString() 51 } 52 }) 53 return pid 54 }) 55} 56export function bubbleSort(arr: Array<number>): Array<number> { 57 let final: Array<number> = new Array(...arr); 58 59 for (let i = 0; i < final.length; i++) { 60 let no_swap: boolean = true; 61 for (let j = 0; j < final.length - i - 1; j++) { 62 // 如果前一个更大,则和后面的交换 63 if (final[j] > final[j + 1]) { 64 let temp = final[j + 1]; 65 final[j + 1] = final[j]; 66 final[j] = temp; 67 no_swap = false; 68 } 69 } 70 // 如果未冒泡,则已经排序完毕 71 if (no_swap) { 72 break; 73 } 74 } 75 return final; 76} 77 78