21
21
/**
22
22
* Record representing the heap usage for a single cluster node
23
23
*/
24
- public record HeapUsage (String nodeId , String nodeName , long totalBytes , long freeBytes ) implements ToXContentFragment , Writeable {
24
+ public record HeapUsage (String nodeId , String nodeName , long totalBytes , long usedBytes ) implements ToXContentFragment , Writeable {
25
+
26
+ public HeapUsage {
27
+ assert usedBytes <= totalBytes ;
28
+ }
25
29
26
30
public HeapUsage (StreamInput in ) throws IOException {
27
31
this (in .readString (), in .readString (), in .readVLong (), in .readVLong ());
@@ -32,14 +36,14 @@ public void writeTo(StreamOutput out) throws IOException {
32
36
out .writeString (this .nodeId );
33
37
out .writeString (this .nodeName );
34
38
out .writeVLong (this .totalBytes );
35
- out .writeVLong (this .freeBytes );
39
+ out .writeVLong (this .usedBytes );
36
40
}
37
41
38
42
public XContentBuilder toShortXContent (XContentBuilder builder ) throws IOException {
39
43
builder .field ("node_name" , this .nodeName );
40
44
builder .humanReadableField ("total_heap_bytes" , "total" , ByteSizeValue .ofBytes (this .totalBytes ));
41
- builder .humanReadableField ("used_heap_bytes" , "used" , ByteSizeValue .ofBytes (this .usedBytes () ));
42
- builder .humanReadableField ("free_heap_bytes" , "free" , ByteSizeValue .ofBytes (this .freeBytes ));
45
+ builder .humanReadableField ("used_heap_bytes" , "used" , ByteSizeValue .ofBytes (this .usedBytes ));
46
+ builder .humanReadableField ("free_heap_bytes" , "free" , ByteSizeValue .ofBytes (this .freeBytes () ));
43
47
builder .field ("free_heap_percent" , truncatePercent (this .freeHeapAsPercentage ()));
44
48
builder .field ("used_heap_percent" , truncatePercent (this .usedHeapAsPercentage ()));
45
49
return builder ;
@@ -53,15 +57,15 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
53
57
}
54
58
55
59
public double freeHeapAsPercentage () {
56
- return 100.0 * freeBytes / ( double ) totalBytes ;
60
+ return 100.0 - usedHeapAsPercentage () ;
57
61
}
58
62
59
63
public double usedHeapAsPercentage () {
60
- return 100.0 - freeHeapAsPercentage () ;
64
+ return 100.0 * usedBytes / ( double ) totalBytes ;
61
65
}
62
66
63
- public long usedBytes () {
64
- return totalBytes - freeBytes ;
67
+ public long freeBytes () {
68
+ return totalBytes - usedBytes ;
65
69
}
66
70
67
71
private static double truncatePercent (double pct ) {
0 commit comments