presto-main/src/main/java/io/prestosql/execution/resourcegroups/InternalResourceGroup.java/InternalResourceGroup/updateResourceUsageAndGetDelta
class InternalResourceGroup:    protected ResourceUsage updateResourceUsageAndGetDelta()
    {
        checkState(Thread.holdsLock(root), "Must hold lock to refresh stats");
        synchronized (root) {
            ResourceUsage groupUsageDelta = new ResourceUsage(0, 0);

            if (subGroups.isEmpty()) {
                // Leaf resource group
                for (Map.Entry<ManagedQueryExecution, ResourceUsage> entry : runningQueries.entrySet()) {
                    ManagedQueryExecution query = entry.getKey();
                    ResourceUsage oldResourceUsage = entry.getValue();

                    ResourceUsage newResourceUsage = new ResourceUsage(
                            query.getTotalCpuTime().toMillis(),
                            query.getTotalMemoryReservation().toBytes());

                    // Compute delta and update usage
                    ResourceUsage queryUsageDelta = newResourceUsage.subtract(oldResourceUsage);
                    entry.setValue(newResourceUsage);
                    groupUsageDelta = groupUsageDelta.add(queryUsageDelta);
                }

                cachedResourceUsage = cachedResourceUsage.add(groupUsageDelta);
            }
            else {
                // Intermediate resource group
                for (Iterator<InternalResourceGroup> iterator = dirtySubGroups.iterator(); iterator.hasNext(); ) {
                    InternalResourceGroup subGroup = iterator.next();

                    ResourceUsage subGroupUsageDelta = subGroup.updateResourceUsageAndGetDelta();
                    groupUsageDelta = groupUsageDelta.add(subGroupUsageDelta);
                    cachedResourceUsage = cachedResourceUsage.add(subGroupUsageDelta);

                    if (!subGroupUsageDelta.equals(new ResourceUsage(0, 0))) {
                        subGroup.updateEligibility();
                    }
                }
            }

            return groupUsageDelta;
        }
    }