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