1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package compiler.codecache.cli.common; 24 25 import sun.hotspot.code.BlobType; 26 27 import java.util.Arrays; 28 29 public class CodeCacheInfoFormatter { 30 private static final String DEFAULT_SIZE_FORMAT = "[0-9]+Kb"; 31 private BlobType heap = null; 32 private String size = DEFAULT_SIZE_FORMAT; 33 private String used = DEFAULT_SIZE_FORMAT; 34 private String maxUsed = DEFAULT_SIZE_FORMAT; 35 private String free = DEFAULT_SIZE_FORMAT; 36 37 public static CodeCacheInfoFormatter forHeap(BlobType heap) { 38 return new CodeCacheInfoFormatter(heap); 39 } 40 41 public static String[] forHeaps(BlobType... heaps) { 42 return Arrays.stream(heaps) 43 .map(CodeCacheInfoFormatter::forHeap) 44 .map(CodeCacheInfoFormatter::getInfoString) 45 .toArray(String[]::new); 46 } 47 48 private static String formatSize(long suffix) { 49 return String.format("%dKb", suffix / 1024); 50 } 51 52 private CodeCacheInfoFormatter(BlobType heap) { 53 this.heap = heap; 54 } 55 56 public CodeCacheInfoFormatter withSize(long size) { 57 this.size = CodeCacheInfoFormatter.formatSize(size); 58 return this; 59 } 60 61 public CodeCacheInfoFormatter withUsed(long used) { 62 this.used = CodeCacheInfoFormatter.formatSize(used); 63 return this; 64 } 65 66 public CodeCacheInfoFormatter withMaxUsed(long maxUsed) { 67 this.maxUsed = CodeCacheInfoFormatter.formatSize(maxUsed); 68 return this; 69 } 70 71 public CodeCacheInfoFormatter withFree(long free) { 72 this.free = CodeCacheInfoFormatter.formatSize(free); 73 return this; 74 } 75 76 public String getInfoString() { 77 return String.format("%s: size=%s used=%s max_used=%s free=%s", 78 heap.beanName, size, used, maxUsed, free); 79 } 80 }