CodeCacheInfoFormatter.java revision 7640:a7adb22849d6
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 */
23package common;
24
25import sun.hotspot.code.BlobType;
26import java.util.Arrays;
27
28public class CodeCacheInfoFormatter {
29    private static final String DEFAULT_SIZE_FORMAT = "[0-9]+Kb";
30    private BlobType heap = null;
31    private String size = DEFAULT_SIZE_FORMAT;
32    private String used = DEFAULT_SIZE_FORMAT;
33    private String maxUsed = DEFAULT_SIZE_FORMAT;
34    private String free = DEFAULT_SIZE_FORMAT;
35
36    public static CodeCacheInfoFormatter forHeap(BlobType heap) {
37        return new CodeCacheInfoFormatter(heap);
38    }
39
40    public static String[] forHeaps(BlobType... heaps) {
41        return Arrays.stream(heaps)
42                .map(CodeCacheInfoFormatter::forHeap)
43                .map(CodeCacheInfoFormatter::getInfoString)
44                .toArray(String[]::new);
45    }
46
47    private static String formatSize(long suffix) {
48        return String.format("%dKb", suffix / 1024);
49    }
50
51    private CodeCacheInfoFormatter(BlobType heap) {
52        this.heap = heap;
53    }
54
55    public CodeCacheInfoFormatter withSize(long size) {
56        this.size = CodeCacheInfoFormatter.formatSize(size);
57        return this;
58    }
59
60    public CodeCacheInfoFormatter withUsed(long used) {
61        this.used = CodeCacheInfoFormatter.formatSize(used);
62        return this;
63    }
64
65    public CodeCacheInfoFormatter withMaxUsed(long maxUsed) {
66        this.maxUsed = CodeCacheInfoFormatter.formatSize(maxUsed);
67        return this;
68    }
69
70    public CodeCacheInfoFormatter withFree(long free) {
71        this.free = CodeCacheInfoFormatter.formatSize(free);
72        return this;
73    }
74
75    public String getInfoString() {
76        return String.format("%s: size=%s used=%s max_used=%s free=%s",
77                heap.beanName, size, used, maxUsed, free);
78    }
79}
80