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 { info } from '../../../log/Log.js'; 17 18export const initMemoryAggStrategy = (metricData: Array<{ 19 processName: string; 20 name: string; 21 value: string; 22 ts: string; 23}>): ProcessValuesListItem => { 24 info('Memory Agg Strategy data length is:', metricData.length); 25 let processValuesListItems: Array<ProcessValuesItem> = []; 26 const splitChar: string = ','; 27 for (let sqlIndex = 0; sqlIndex < metricData.length; sqlIndex++) { 28 let processNames = metricData[sqlIndex].processName; 29 let processInfoSource: ProcessValuesItem = { 30 processName: processNames, 31 }; 32 if (metricData[sqlIndex].name == null) { 33 let values = metricData[sqlIndex].value.split(splitChar); 34 let times = metricData[sqlIndex].ts.split(splitChar); 35 let oomScoreValue = 0; 36 for (let index = 0; index < values.length; index++) { 37 if (!processInfoSource) continue; 38 processValuesListItems?.push(processInfoSource); 39 } 40 } else { 41 let names = metricData[sqlIndex].name.split(splitChar); 42 let values = metricData[sqlIndex].value.split(splitChar); 43 let times = metricData[sqlIndex].ts.split(splitChar); 44 let oomScoreValue = '0'; 45 for (let indexScore = 0; indexScore < names.length; indexScore++) { 46 if ('oom_score_adj' === names[indexScore]) { 47 oomScoreValue = values[indexScore]; 48 break; 49 } 50 } 51 for (let index = 0; index < names.length; index++) { 52 let typeItem: TypeItem = { 53 ts: times[index], 54 oom_score: oomScoreValue, 55 value: values[index], 56 }; 57 if (!processInfoSource) continue; 58 if ('mem.rss.anon' === names[index]) { 59 processInfoSource.anonRss = typeItem; 60 } 61 if ('mem.swap' === names[index]) { 62 processInfoSource.swap = typeItem; 63 } 64 if ('mem.rss.file' === names[index]) { 65 processInfoSource.fileRss = typeItem; 66 } 67 if ('oom_score_adj' === names[index]) { 68 processInfoSource.anonAndSwap = typeItem; 69 } 70 } 71 } 72 processValuesListItems?.push(processInfoSource); 73 } 74 return { 75 processValues: processValuesListItems, 76 }; 77}; 78 79export interface ProcessValuesListItem { 80 processValues: Array<ProcessValuesItem>; 81} 82 83export interface ProcessValuesItem { 84 processName: string; 85 anonRss?: TypeItem; 86 swap?: TypeItem; 87 fileRss?: TypeItem; 88 anonAndSwap?: TypeItem; 89} 90 91export interface TypeItem { 92 ts: string; 93 oom_score: string; 94 value: string; 95} 96