/** * Return the size of the provided array of {@link Accountable}s by summing * up the shallow size of the array and the * {@link Accountable#ramBytesUsed() memory usage} reported by each * {@link Accountable}. */ public static long sizeOf(Accountable[] accountables) { long size = shallowSizeOf(accountables); for (Accountable accountable : accountables) { if (accountable != null) { size += accountable.ramBytesUsed(); } } return size; } }
private static StringBuilder toString(StringBuilder dest, Accountable a, int depth) { for (int i = 1; i < depth; i++) { dest.append(" "); } if (depth > 0) { dest.append("|-- "); } dest.append(a.toString()); dest.append(": "); dest.append(RamUsageEstimator.humanReadableUnits(a.ramBytesUsed())); dest.append(System.lineSeparator()); for (Accountable child : a.getChildResources()) { toString(dest, child, depth + 1); } return dest; }
/** * Augments an existing accountable with the provided description. * <p> * The resource description is constructed in this format: * {@code description [toString()]} * <p> * This is a point-in-time type safe view: consumers * will not be able to cast or manipulate the resource in any way. */ public static Accountable namedAccountable(String description, Accountable in) { return namedAccountable(description + " [" + in + "]", in.getChildResources(), in.ramBytesUsed()); }
/** Returns 0 in the case where accountable is null, otherwise returns {@code ramBytesUsed()} */ protected static long guardedRamBytesUsed(Accountable a) { if (a == null) { return 0; } return a.ramBytesUsed(); }
@Override public long applyAsLong(Key key, Accountable ramUsage) { int weight = (int) Math.min(ramUsage.ramBytesUsed(), Integer.MAX_VALUE); return weight == 0 ? 1 : weight; } }
@Override public void onCache(ShardId shardId, String fieldName, Accountable ramUsage) { totalMetric.inc(ramUsage.ramBytesUsed()); CounterMetric total = perFieldTotals.get(fieldName); if (total != null) { total.inc(ramUsage.ramBytesUsed()); } else { total = new CounterMetric(); total.inc(ramUsage.ramBytesUsed()); CounterMetric prev = perFieldTotals.putIfAbsent(fieldName, total); if (prev != null) { prev.inc(ramUsage.ramBytesUsed()); } } }
public void onCached(Accountable key, BytesReference value) { totalMetric.inc(key.ramBytesUsed() + value.ramBytesUsed()); }
void writeRamTree(StreamOutput out, Accountable tree) throws IOException { out.writeString(tree.toString()); out.writeVLong(tree.ramBytesUsed()); Collection<Accountable> children = tree.getChildResources(); out.writeVInt(children.size()); for (Accountable child : children) { writeRamTree(out, child); } }
public void onRemoval(Accountable key, BytesReference value, boolean evicted) { if (evicted) { evictionsMetric.inc(); } long dec = 0; if (key != null) { dec += key.ramBytesUsed(); } if (value != null) { dec += value.ramBytesUsed(); } totalMetric.dec(dec); } }
private static void toXContent(XContentBuilder builder, Accountable tree) throws IOException { builder.startObject(); builder.field(Fields.DESCRIPTION, tree.toString()); builder.humanReadableField(Fields.SIZE_IN_BYTES, Fields.SIZE, new ByteSizeValue(tree.ramBytesUsed())); Collection<Accountable> children = tree.getChildResources(); if (children.isEmpty() == false) { builder.startArray(Fields.CHILDREN); for (Accountable child : children) { toXContent(builder, child); } builder.endArray(); } builder.endObject(); }
@Override public void onRemoval(RemovalNotification<Key, Accountable> notification) { Key key = notification.getKey(); assert key != null && key.listeners != null; IndexFieldCache indexCache = key.indexCache; final Accountable value = notification.getValue(); for (IndexFieldDataCache.Listener listener : key.listeners) { try { listener.onRemoval( key.shardId, indexCache.fieldName, notification.getRemovalReason() == RemovalNotification.RemovalReason.EVICTED, value.ramBytesUsed() ); } catch (Exception e) { // load anyway since listeners should not throw exceptions logger.error("Failed to call listener on field data cache unloading", e); } } }
@Override public void onCache(ShardId shardId, Accountable accountable) { if (shardId != null) { final IndexShard shard = indexService.getShardOrNull(shardId.id()); if (shard != null) { long ramBytesUsed = accountable != null ? accountable.ramBytesUsed() : 0L; shard.shardBitsetFilterCache().onCached(ramBytesUsed); } } }
@Override public void onRemoval(ShardId shardId, Accountable accountable) { if (shardId != null) { final IndexShard shard = indexService.getShardOrNull(shardId.id()); if (shard != null) { long ramBytesUsed = accountable != null ? accountable.ramBytesUsed() : 0L; shard.shardBitsetFilterCache().onRemoval(ramBytesUsed); } } } }
/** Returns 0 in the case where accountable is null, otherwise returns {@code ramBytesUsed()} */ protected static long guardedRamBytesUsed(Accountable a) { if (a == null) { return 0; } return a.ramBytesUsed(); }
/** * Return the number of bytes used by the given filter. The default * implementation returns {@link Accountable#ramBytesUsed()} if the filter * implements {@link Accountable} and <code>1024</code> otherwise. */ protected long ramBytesUsed(Filter filter) { if (filter instanceof Accountable) { return ((Accountable) filter).ramBytesUsed(); } return FILTER_DEFAULT_RAM_BYTES_USED; }
@Override public long applyAsLong(Key key, Accountable ramUsage) { int weight = (int) Math.min(ramUsage.ramBytesUsed(), Integer.MAX_VALUE); return weight == 0 ? 1 : weight; } }
/** * The most recently estimated size of the value, null unless * estimateSize has been called. */ public String getEstimatedSize() { long bytesUsed = value == null ? 0L : value.ramBytesUsed(); return RamUsageEstimator.humanReadableUnits(bytesUsed); }
/** * Augments an existing accountable with the provided description. * <p> * The resource description is constructed in this format: * {@code description [toString()]} * <p> * This is a point-in-time type safe view: consumers * will not be able to cast or manipulate the resource in any way. */ public static Accountable namedAccountable(String description, Accountable in) { return namedAccountable(description + " [" + in + "]", in.getChildResources(), in.ramBytesUsed()); }
void writeRamTree(StreamOutput out, Accountable tree) throws IOException { out.writeString(tree.toString()); out.writeVLong(tree.ramBytesUsed()); Collection<Accountable> children = tree.getChildResources(); out.writeVInt(children.size()); for (Accountable child : children) { writeRamTree(out, child); } }
@Override public void onCache(ShardId shardId, Accountable accountable) { if (shardId != null) { final IndexShard shard = indexService.getShardOrNull(shardId.id()); if (shard != null) { long ramBytesUsed = accountable != null ? accountable.ramBytesUsed() : 0L; shard.shardBitsetFilterCache().onCached(ramBytesUsed); } } }