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 16 package ohos.devtools.views.applicationtrace.bean; 17 18 import com.intellij.ui.JBColor; 19 import ohos.devtools.views.applicationtrace.DataPanel; 20 import ohos.devtools.views.applicationtrace.analysis.AnalysisEnum; 21 import ohos.devtools.views.applicationtrace.util.TimeUtils; 22 import ohos.devtools.views.perftrace.PerfColorUtil; 23 import ohos.devtools.views.perftrace.bean.PrefSample; 24 import ohos.devtools.views.trace.AbstractNode; 25 import ohos.devtools.views.trace.Common; 26 import ohos.devtools.views.trace.util.Final; 27 import ohos.devtools.views.trace.util.Utils; 28 29 import java.awt.Color; 30 import java.awt.Font; 31 import java.awt.Graphics2D; 32 import java.awt.Rectangle; 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 import java.util.List; 36 37 /** 38 * TreeTableBean 39 * 40 * @since 2021/5/26 15:38 41 */ 42 public class TreeTableBean extends AbstractNode { 43 /** 44 * PERF_FLAME_VENDOR color 45 */ 46 public static final Color PERF_FLAME_VENDOR = new JBColor(0xFFC56F, 0xFFC56F); 47 private static final int PADDING_LEFT = 5; 48 private static final int PADDING_RIGHT = 5; 49 50 private long stackId; 51 private long parentStackId; 52 private String prefStackId; 53 private String prefParentStackId; 54 private String name; 55 private String total; 56 private long totalNum = 0L; 57 private double totalPercentNum = 0D; 58 private long selfNum = 0L; 59 private String selfPercent = "0"; 60 private double selfPercentNum = 0D; 61 private long childrenNum = 0L; 62 private double childrenPercentNum = 0D; 63 private long childrenNS; 64 private String childrenPercent; 65 private boolean isUserWrite = false; 66 private int containType = 0; // 0 OK 1 There are keywords 2 children there are keywords 3 there are no keywords 67 private List<Integer> childrens = new ArrayList<>(); 68 private long threadDur = 0L; 69 private String bloodId = ""; 70 private String parentBloodId = ""; 71 72 /** 73 * constructor 74 */ TreeTableBean()75 public TreeTableBean() { 76 this(0); 77 } 78 79 /** 80 * constructor 81 * 82 * @param threadDur duration 83 */ TreeTableBean(final long threadDur)84 public TreeTableBean(final long threadDur) { 85 this.threadDur = threadDur; 86 } 87 88 /** 89 * set current time 90 * 91 * @param timeBean timeBean 92 */ setTime(TreeTableBean timeBean)93 public void setTime(TreeTableBean timeBean) { 94 setTotalNum(timeBean.getTotalNum()); 95 setSelfNum(0); 96 setChildrenNum(timeBean.getTotalNum()); 97 } 98 99 /** 100 * merge current time 101 * 102 * @param timeBean timeBean 103 */ mergeTime(TreeTableBean timeBean)104 public void mergeTime(TreeTableBean timeBean) { 105 setTotalNum(getTotalNum() + timeBean.getTotalNum()); 106 setSelfNum(getSelfNum() + timeBean.getSelfNum()); 107 setChildrenNum(getChildrenNum() + timeBean.getChildrenNum()); 108 } 109 110 /** 111 * get pref stack id 112 * 113 * @return id 114 */ getPrefStackId()115 public String getPrefStackId() { 116 return prefStackId; 117 } 118 119 /** 120 * set stack id 121 * 122 * @param param stack id 123 */ setPrefStackId(final String param)124 public void setPrefStackId(final String param) { 125 this.prefStackId = param; 126 } 127 128 /** 129 * get parent stack id 130 * 131 * @return id 132 */ getPrefParentStackId()133 public String getPrefParentStackId() { 134 return prefParentStackId; 135 } 136 137 /** 138 * set parent stack id 139 * 140 * @param param id 141 */ setPrefParentStackId(final String param)142 public void setPrefParentStackId(final String param) { 143 this.prefParentStackId = param; 144 } 145 146 /** 147 * is user write 148 * 149 * @return boolean 150 */ isUserWrite()151 public boolean isUserWrite() { 152 return isUserWrite; 153 } 154 155 /** 156 * set user write 157 * 158 * @param param user write 159 */ setUserWrite(final boolean param)160 public void setUserWrite(final boolean param) { 161 isUserWrite = param; 162 } 163 164 /** 165 * get contain type 166 * 167 * @return type 168 */ getContainType()169 public int getContainType() { 170 return containType; 171 } 172 173 /** 174 * set contain type 175 * 176 * @param param type 177 */ setContainType(final int param)178 public void setContainType(final int param) { 179 this.containType = param; 180 } 181 182 /** 183 * get thread duration 184 * 185 * @return duration 186 */ getThreadDur()187 public long getThreadDur() { 188 return threadDur; 189 } 190 191 /** 192 * set thread duration 193 * 此方法需要在set total children self之前设置 否则百分数数据不会计算 194 * 195 * @param param duration 196 */ setThreadDur(final long param)197 public void setThreadDur(final long param) { 198 this.threadDur = param; 199 } 200 201 /** 202 * get total percent 203 * 204 * @return percent 205 */ getTotalPercentNum()206 public double getTotalPercentNum() { 207 return totalPercentNum; 208 } 209 210 /** 211 * get self percent 212 * 213 * @return percent 214 */ getSelfPercentNum()215 public double getSelfPercentNum() { 216 return selfPercentNum; 217 } 218 219 /** 220 * get child percent 221 * 222 * @return percent 223 */ getChildrenPercentNum()224 public double getChildrenPercentNum() { 225 return childrenPercentNum; 226 } 227 228 /** 229 * get total num 230 * 231 * @return num 232 */ getTotalNum()233 public long getTotalNum() { 234 return totalNum; 235 } 236 237 /** 238 * set total num 239 * 240 * @param param num 241 */ setTotalNum(final long param)242 public void setTotalNum(final long param) { 243 this.totalNum = param; 244 if (threadDur != 0) { 245 totalPercentNum = param * 1.0 / threadDur * 100; 246 } 247 } 248 249 /** 250 * get self num 251 * 252 * @return num 253 */ getSelfNum()254 public long getSelfNum() { 255 return selfNum; 256 } 257 258 /** 259 * set self num 260 * 261 * @param param num 262 */ setSelfNum(final long param)263 public void setSelfNum(final long param) { 264 this.selfNum = param; 265 if (threadDur != 0) { 266 selfPercentNum = param * 1.0 / threadDur * 100; 267 } 268 } 269 270 /** 271 * get child num 272 * 273 * @return num 274 */ getChildrenNum()275 public long getChildrenNum() { 276 return childrenNum; 277 } 278 279 /** 280 * set children num 281 * 282 * @param param num 283 */ setChildrenNum(final long param)284 public void setChildrenNum(final long param) { 285 this.childrenNum = param; 286 if (threadDur != 0) { 287 childrenPercentNum = param * 1.0 / threadDur * 100; 288 } 289 } 290 291 /** 292 * get children 293 * 294 * @return children list 295 */ getChildrens()296 public List<Integer> getChildrens() { 297 return childrens; 298 } 299 300 /** 301 * set children list 302 * 303 * @param param list 304 */ setChildrens(final List<Integer> param)305 public void setChildrens(final List<Integer> param) { 306 this.childrens = param; 307 } 308 309 /** 310 * Gets the value of childrenNS . 311 * 312 * @return the value of long 313 */ getChildrenNS()314 public long getChildrenNS() { 315 return childrenNS; 316 } 317 318 /** 319 * Sets the childrenNS . 320 * <p>You can use getChildrenNS() to get the value of childrenNS</p> 321 * 322 * @param param childrenNS 323 */ setChildrenNS(final long param)324 public void setChildrenNS(final long param) { 325 this.childrenNS = param; 326 } 327 328 /** 329 * get total 330 * 331 * @return total 332 */ getTotal()333 public String getTotal() { 334 return total; 335 } 336 337 /** 338 * set total 339 * 340 * @param param total 341 */ setTotal(final String param)342 public void setTotal(final String param) { 343 this.total = param; 344 } 345 346 /** 347 * get name 348 * 349 * @return name 350 */ getName()351 public String getName() { 352 return name; 353 } 354 355 /** 356 * set name 357 * 358 * @param param name 359 */ setName(final String param)360 public void setName(final String param) { 361 this.name = param; 362 } 363 364 /** 365 * Gets the value of stackId . 366 * 367 * @return the value of java.lang.Integer 368 */ getStackId()369 public long getStackId() { 370 return stackId; 371 } 372 373 /** 374 * Sets the stackId . 375 * <p>You can use getStackId() to get the value of stackId</p> 376 * 377 * @param param id 378 */ setStackId(final long param)379 public void setStackId(final long param) { 380 this.stackId = param; 381 } 382 383 /** 384 * Gets the value of parentStackId . 385 * 386 * @return the value of java.lang.Integer 387 */ getParentStackId()388 public long getParentStackId() { 389 return parentStackId; 390 } 391 392 /** 393 * Sets the parentStackId . 394 * <p>You can use getParentStackId() to get the value of parentStackId</p> 395 * 396 * @param param id 397 */ setParentStackId(final long param)398 public void setParentStackId(final long param) { 399 this.parentStackId = param; 400 } 401 402 /** 403 * get the self percent 404 * 405 * @return percent 406 */ getSelfPercent()407 public String getSelfPercent() { 408 return selfPercent; 409 } 410 411 /** 412 * set self percent 413 * 414 * @param param percent 415 */ setSelfPercent(final String param)416 public void setSelfPercent(final String param) { 417 this.selfPercent = param; 418 } 419 420 /** 421 * get children percent 422 * 423 * @return percent 424 */ getChildrenPercent()425 public String getChildrenPercent() { 426 return childrenPercent; 427 } 428 429 /** 430 * set children percent 431 * 432 * @param param percent 433 */ setChildrenPercent(final String param)434 public void setChildrenPercent(final String param) { 435 this.childrenPercent = param; 436 } 437 438 @Override toString()439 public String toString() { 440 return name; 441 } 442 443 @Override draw(Graphics2D paint)444 public void draw(Graphics2D paint) { 445 if (containType == 3) { 446 Common.setAlpha(paint, 0.2F); 447 } else { 448 if (isMouseIn) { 449 Common.setAlpha(paint, 0.7F); 450 } else { 451 Common.setAlpha(paint, 1.0F); 452 } 453 } 454 if (DataPanel.analysisEnum.equals(AnalysisEnum.APP)) { 455 paint.setColor(PerfColorUtil.PERF_FLAME_VENDOR); 456 } else { 457 if (name.contains("(") || name.contains(PrefSample.KERNEL) || name.contains(".so")) { 458 paint.setColor(PerfColorUtil.getPerfMethod(this)); 459 } else if (name.contains(".")) { 460 paint.setColor(PerfColorUtil.getJavaMethod(this)); 461 } else { 462 paint.setColor(PerfColorUtil.getPerfMethod(this)); 463 } 464 } 465 paint.fillRect(Utils.getX(rect), Utils.getY(rect), rect.width, rect.height); 466 if (rect.width > 1) { 467 paint.setColor(JBColor.background().darker()); 468 paint.drawRect(Utils.getX(rect), Utils.getY(rect), rect.width, rect.height); 469 } 470 Common.setAlpha(paint, 1.0f); 471 if (containType == 1) { 472 paint.setFont(new Font(Final.FONT_NAME, Font.BOLD, Final.NORMAL_FONT_SIZE)); 473 } else { 474 paint.setFont(new Font(Final.FONT_NAME, Font.PLAIN, Final.NORMAL_FONT_SIZE)); 475 } 476 Common.drawStringMiddleHeight(paint, name, 477 new Rectangle(Utils.getX(rect) + PADDING_LEFT, Utils.getY(rect), rect.width - PADDING_LEFT - PADDING_RIGHT, 478 rect.height)); 479 } 480 481 @Override getStringList(String time)482 public List<String> getStringList(String time) { 483 return Arrays.asList( 484 "" + getName(), 485 "", 486 "Total: " + TimeUtils.getTimeWithUnit(totalNum * 1000) 487 ); 488 } 489 490 /** 491 * get BloodId 492 * 493 * @return BloodId 494 */ getBloodId()495 public String getBloodId() { 496 return bloodId; 497 } 498 499 /** 500 * set BloodId 501 * 502 * @param bloodId bloodId 503 */ setBloodId(String bloodId)504 public void setBloodId(String bloodId) { 505 this.bloodId = bloodId; 506 } 507 508 /** 509 * get parentBloodId 510 * 511 * @return parentBloodId 512 */ getParentBloodId()513 public String getParentBloodId() { 514 return parentBloodId; 515 } 516 517 /** 518 * set BloodId 519 * 520 * @param parentBloodId parentBloodId 521 */ setParentBloodId(String parentBloodId)522 public void setParentBloodId(String parentBloodId) { 523 this.parentBloodId = parentBloodId; 524 } 525 } 526