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.trace.util; 17 18 import org.apache.commons.collections.map.HashedMap; 19 20 import java.awt.Point; 21 import java.awt.Rectangle; 22 import java.security.MessageDigest; 23 import java.security.NoSuchAlgorithmException; 24 import java.text.DecimalFormat; 25 import java.util.Map; 26 import java.util.concurrent.ExecutorService; 27 import java.util.concurrent.LinkedBlockingQueue; 28 import java.util.concurrent.ThreadPoolExecutor; 29 import java.util.concurrent.TimeUnit; 30 31 /** 32 * Tools 33 * 34 * @since 2021/04/22 12:25 35 */ 36 public final class Utils { 37 private static final String[] UNITS = new String[] {"", "K", "M", "G", "T", "E"}; 38 private static Map<String, String> statusMap = new HashedMap(); 39 private static Utils instance = new Utils(); 40 private static ExecutorService pool = new ThreadPoolExecutor(8, 8, 41 0L, TimeUnit.MILLISECONDS, 42 new LinkedBlockingQueue<Runnable>()); 43 Utils()44 private Utils() { 45 statusMap.put("D", "Uninterruptible Sleep"); 46 statusMap.put("S", "Sleeping"); 47 statusMap.put("R", "Runnable"); 48 statusMap.put("Running", "Running"); 49 statusMap.put("R+", "Runnable (Preempted)"); 50 statusMap.put("DK", "Uninterruptible Sleep + Wake Kill"); 51 statusMap.put("I", "Task Dead"); 52 statusMap.put("T", "Stopped"); 53 statusMap.put("t", "Traced"); 54 statusMap.put("X", "Exit (Dead)"); 55 statusMap.put("Z", "Exit (Zombie)"); 56 statusMap.put("K", "Wake Kill"); 57 statusMap.put("W", "Waking"); 58 statusMap.put("P", "Parked"); 59 statusMap.put("N", "No Load"); 60 } 61 62 /** 63 * Get singleton object 64 * 65 * @return Utils 66 */ getInstance()67 public static Utils getInstance() { 68 return instance; 69 } 70 71 /** 72 * Calculate whether it is within the range of Rectangle according to the x and y coordinates 73 * 74 * @param rect rect 75 * @param xAxis xAxis 76 * @param yAxis yAxis 77 * @return boolean 78 */ pointInRect(final Rectangle rect, final int xAxis, final int yAxis)79 public static boolean pointInRect(final Rectangle rect, final int xAxis, final int yAxis) { 80 if (rect == null) { 81 return false; 82 } 83 return xAxis >= Utils.getX(rect) && xAxis <= Utils.getX(rect) + rect.width && yAxis >= Utils.getY(rect) 84 && yAxis < Utils.getY(rect) + rect.height; 85 } 86 87 /** 88 * Calculate whether it is within the range of Rectangle according to the x and y coordinates 89 * 90 * @param rect rect 91 * @param point event point 92 * @return boolean 93 */ pointInRect(final Rectangle rect, final Point point)94 public static boolean pointInRect(final Rectangle rect, final Point point) { 95 if (rect == null || point == null) { 96 return false; 97 } 98 int xAxis = Utils.getX(point); 99 int yAxis = Utils.getY(point); 100 return xAxis >= Utils.getX(rect) && xAxis <= Utils.getX(rect) + rect.width && yAxis >= Utils.getY(rect) 101 && yAxis < Utils.getY(rect) + rect.height; 102 } 103 104 /** 105 * Get the last status description 106 * 107 * @param state state 108 * @return String 109 */ getEndState(final String state)110 public static String getEndState(final String state) { 111 if (Utils.getInstance().getStatusMap().containsKey(state)) { 112 return Utils.getInstance().getStatusMap().get(state); 113 } else { 114 if ("".equals(state) || state == null) { 115 return ""; 116 } 117 return "Unknown State"; 118 } 119 } 120 121 /** 122 * transform time unit ns to ms 123 * 124 * @param data time 125 * @return String 126 */ transformTimeToMs(Number data)127 public static String transformTimeToMs(Number data) { 128 double ms; 129 if (data instanceof Long) { 130 ms = (Long) data / 1000000.0; 131 } else if (data instanceof Double) { 132 ms = (Double) data / 1000000.0; 133 } else { 134 ms = 0.0; 135 } 136 return new DecimalFormat("#.#######").format(ms); 137 } 138 139 /** 140 * transform str to md5 141 * 142 * @param str str 143 * @return String str to md5 string 144 */ md5String(String str)145 public static String md5String(String str) { 146 String result = ""; 147 try { 148 MessageDigest md5 = MessageDigest.getInstance("MD5"); 149 byte[] digest = md5.digest(str.getBytes()); 150 StringBuilder builder = new StringBuilder(); 151 for (byte val : digest) { 152 builder.append(Integer.toHexString((0x000000FF & val) | 0xFFFFFF00).substring(6)); 153 } 154 result = builder.toString(); 155 } catch (NoSuchAlgorithmException exception) { 156 exception.printStackTrace(); 157 } 158 return result; 159 } 160 161 /** 162 * get the db pool 163 * 164 * @return ExecutorService db pool 165 */ getPool()166 public static ExecutorService getPool() { 167 return pool; 168 } 169 170 /** 171 * reset the db pool 172 */ resetPool()173 public static void resetPool() { 174 pool.shutdownNow(); 175 pool = new ThreadPoolExecutor(8, 8, 176 0L, TimeUnit.MILLISECONDS, 177 new LinkedBlockingQueue<Runnable>()); 178 } 179 180 /** 181 * reset the db pool 182 * 183 * @param num pool size 184 */ resetPool(int num)185 public static void resetPool(int num) { 186 pool.shutdownNow(); 187 pool = new ThreadPoolExecutor(num, num, 188 0L, TimeUnit.MILLISECONDS, 189 new LinkedBlockingQueue<Runnable>()); 190 } 191 192 /** 193 * get rectangle x 194 * 195 * @param rectangle rectangle 196 * @return rectangleX 197 */ getX(Rectangle rectangle)198 public static int getX(Rectangle rectangle) { 199 return rectangle.x; 200 } 201 202 /** 203 * get point x 204 * 205 * @param point point 206 * @return pointX 207 */ getX(Point point)208 public static int getX(Point point) { 209 return point.x; 210 } 211 212 /** 213 * set rectangle x 214 * 215 * @param rectangle rectangle 216 * @param xVal xVal 217 */ setX(Rectangle rectangle, int xVal)218 public static void setX(Rectangle rectangle, int xVal) { 219 rectangle.x = xVal; 220 } 221 222 /** 223 * set point x 224 * 225 * @param point rectangle 226 * @param xVal xVal 227 */ setX(Point point, int xVal)228 public static void setX(Point point, int xVal) { 229 point.x = xVal; 230 } 231 232 /** 233 * set rectangle y 234 * 235 * @param rectangle rectangle 236 * @param yVal yVal 237 */ setY(Rectangle rectangle, int yVal)238 public static void setY(Rectangle rectangle, int yVal) { 239 rectangle.y = yVal; 240 } 241 242 /** 243 * set point y 244 * 245 * @param point point 246 * @param yVal yVal 247 */ setY(Point point, int yVal)248 public static void setY(Point point, int yVal) { 249 point.y = yVal; 250 } 251 252 /** 253 * get rectangle y 254 * 255 * @param rectangle rectangle 256 * @return rectangleY 257 */ getY(Rectangle rectangle)258 public static int getY(Rectangle rectangle) { 259 return rectangle.y; 260 } 261 262 /** 263 * get point y 264 * 265 * @param point point 266 * @return rectangleY 267 */ getY(Point point)268 public static int getY(Point point) { 269 return point.y; 270 } 271 272 /** 273 * Gets the value of statusMap . 274 * 275 * @return Get state collection 276 */ getStatusMap()277 public Map<String, String> getStatusMap() { 278 return statusMap; 279 } 280 281 /** 282 * get the unit cpu freq 283 * 284 * @param maxFreq maxFreq 285 * @return CpuFreq 286 */ getHzUnit(Long maxFreq)287 public static String getHzUnit(Long maxFreq) { 288 StringBuilder builder = new StringBuilder(); 289 if (maxFreq > 1000000000L) { 290 builder.append(String.format("%.2f", maxFreq / 1000000000D)); 291 builder.append("G"); 292 } else if (maxFreq > 1000000L) { 293 builder.append(String.format("%.2f", maxFreq / 1000000D)); 294 builder.append("M"); 295 } else if (maxFreq > 1000L) { 296 builder.append(String.format("%.2f", maxFreq / 1000D)); 297 builder.append("K"); 298 } else { 299 builder.toString(); 300 } 301 builder.append("hz"); 302 return builder.toString(); 303 } 304 } 305