1 /* 2 * Copyright (c) 2021 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 #include "segment.h" 16 17 #include "string_util.h" 18 using namespace std; 19 namespace OHOS { 20 namespace HiviewDFX { GetId(void) const21int Segment::GetId(void) const 22 { 23 return StringUtil::StrToInt(GetValue(SEGMENT_ID)); 24 } 25 GetSysTid(void) const26int Segment::GetSysTid(void) const 27 { 28 return StringUtil::StrToInt(GetValue(SEGMENT_SYSTID)); 29 } 30 GetName(void) const31const string Segment::GetName(void) const 32 { 33 return GetValue(SEGMENT_NAME); 34 } 35 SetValue(const string & name,const string & value)36void Segment::SetValue(const string& name, const string& value) 37 { 38 if (!name.empty() && !value.empty()) { 39 bundle_[name] = value; 40 } 41 } 42 GetValue(const string & name) const43const string Segment::GetValue(const string& name) const 44 { 45 auto it = bundle_.find(name); 46 return (it != bundle_.end()) ? it->second : string(""); 47 } 48 49 // example: proc_name:thread_name(sysTid=1234) GetFullName(void) const50const string Segment::GetFullName(void) const 51 { 52 string fullName; 53 if (!GetValue(SEGMENT_PARENT_NAME).empty()) { 54 fullName += GetValue(SEGMENT_PARENT_NAME) + ":"; 55 } 56 57 if (!GetName().empty()) { 58 fullName += GetName(); 59 } else if (GetId() > 0) { 60 fullName += to_string(GetId()); 61 } else { 62 fullName += "unknown"; 63 } 64 65 int sysTid = GetSysTid(); 66 if (sysTid > 0) { 67 fullName += "(sysTid=" + to_string(sysTid) + ")"; 68 } 69 return fullName; 70 } 71 72 // example: proc_name:thread_name(sysTid=1234) waiting on lock held by GetDesc(const map<string,vector<string>> & segStatusCfg) const73const string Segment::GetDesc(const map<string, vector<string>>& segStatusCfg) const 74 { 75 bool found = false; 76 string desc = GetFullName(); 77 for (const auto& cfg : segStatusCfg) { 78 if (!GetValue(cfg.first).empty() && !cfg.second[1].empty()) { 79 desc += cfg.second[1]; 80 found = true; 81 break; 82 } 83 } 84 if (!found) { 85 desc += " blocked"; 86 } 87 return desc; 88 } 89 } // namespace HiviewDFX 90 } // namespace OHOS