// Path: src/main/java/com/metamx/common/RE.java
// public class RE extends RuntimeException
// {
//   public RE(String formatText, Object... arguments)
//   {
//     super(StringUtils.safeFormat(formatText, arguments));
//   }
// 
//   public RE(Throwable cause, String formatText, Object... arguments)
//   {
//     super(StringUtils.safeFormat(formatText, arguments), cause);
//   }
// }
// 
// Path: src/main/java/com/metamx/metrics/CgroupUtil.java
// public class CgroupUtil
// {
//   public static final String SPACE_MATCH = Pattern.quote(" ");
//   public static final String COMMA_MATCH = Pattern.quote(",");
//   public static final String COLON_MATCH = Pattern.quote(":");
// }


import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import com.metamx.common.RE;
import com.metamx.metrics.CgroupUtil;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
/*
 * Copyright 2017 Metamarkets Group Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.metamx.metrics.cgroups;



public class ProcCgroupDiscoverer implements CgroupDiscoverer
{
  private static final String CGROUP_TYPE = "cgroup";

  private final File procDir;

  /**
   * Create a proc discovery mechanism based on a `/proc` directory.
   *
   * @param procDir The directory under proc. This is usually `/proc/self` or `/proc/#pid`
   */
  public ProcCgroupDiscoverer(Path procDir)
  {
    this.procDir = Preconditions.checkNotNull(procDir, "procDir").toFile();
    Preconditions.checkArgument(this.procDir.isDirectory(), "Not a directory: [%s]", procDir);
  }

  @Override
  public Path discover(final String cgroup)
  {
    Preconditions.checkNotNull(cgroup, "cgroup required");
    final File procMounts = new File(procDir, "mounts");
    final File pidCgroups = new File(procDir, "cgroup");
    final PidCgroupEntry pidCgroupsEntry = getCgroupEntry(pidCgroups, cgroup);
    final ProcMountsEntry procMountsEntry = getMountEntry(procMounts, cgroup);
    final File cgroupDir = new File(
// Path: src/main/java/com/metamx/common/RE.java
// public class RE extends RuntimeException
// {
//   public RE(String formatText, Object... arguments)
//   {
//     super(StringUtils.safeFormat(formatText, arguments));
//   }
// 
//   public RE(Throwable cause, String formatText, Object... arguments)
//   {
//     super(StringUtils.safeFormat(formatText, arguments), cause);
//   }
// }
// 
// Path: src/main/java/com/metamx/metrics/CgroupUtil.java
// public class CgroupUtil
// {
//   public static final String SPACE_MATCH = Pattern.quote(" ");
//   public static final String COMMA_MATCH = Pattern.quote(",");
//   public static final String COLON_MATCH = Pattern.quote(":");
// }


// Path: src/main/java/com/metamx/metrics/cgroups/ProcCgroupDiscoverer.java
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import com.metamx.common.RE;
import com.metamx.metrics.CgroupUtil;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

/*
 * Copyright 2017 Metamarkets Group Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.metamx.metrics.cgroups;



public class ProcCgroupDiscoverer implements CgroupDiscoverer
{
  private static final String CGROUP_TYPE = "cgroup";

  private final File procDir;

  /**
   * Create a proc discovery mechanism based on a `/proc` directory.
   *
   * @param procDir The directory under proc. This is usually `/proc/self` or `/proc/#pid`
   */
  public ProcCgroupDiscoverer(Path procDir)
  {
    this.procDir = Preconditions.checkNotNull(procDir, "procDir").toFile();
    Preconditions.checkArgument(this.procDir.isDirectory(), "Not a directory: [%s]", procDir);
  }

  @Override
  public Path discover(final String cgroup)
  {
    Preconditions.checkNotNull(cgroup, "cgroup required");
    final File procMounts = new File(procDir, "mounts");
    final File pidCgroups = new File(procDir, "cgroup");
    final PidCgroupEntry pidCgroupsEntry = getCgroupEntry(pidCgroups, cgroup);
    final ProcMountsEntry procMountsEntry = getMountEntry(procMounts, cgroup);
    final File cgroupDir = new File(
        procMountsEntry.path.toString(),