Commit 16e70f20 authored by Matteo Melli's avatar Matteo Melli
Browse files

Fixed filtering of processes

parent 7eca329e
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package com.ongres.pgio.main; package com.ongres.pgio.main;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.ongres.pgio.main.config.Config; import com.ongres.pgio.main.config.Config;
...@@ -36,10 +37,12 @@ import java.io.File; ...@@ -36,10 +37,12 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Optional; import java.util.Optional;
import java.util.Scanner;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
...@@ -108,14 +111,6 @@ public class Main { ...@@ -108,14 +111,6 @@ public class Main {
.isForHelp(); .isForHelp();
parser.acceptsAll(Lists.newArrayList("v", "version"), parser.acceptsAll(Lists.newArrayList("v", "version"),
"Show version and quit"); "Show version and quit");
parser.acceptsAll(Lists.newArrayList("print-header"),
"Print header");
parser.acceptsAll(Lists.newArrayList("a", "advanced"),
"Enable advanced options");
parser.acceptsAll(Lists.newArrayList("s", "show-system"),
"Print read/write data for the whole system");
parser.acceptsAll(Lists.newArrayList("o", "show-other"),
"Print read/write data not accounted by any listed process");
parser.acceptsAll(Lists.newArrayList("D"), parser.acceptsAll(Lists.newArrayList("D"),
"Specifies the file system location of the database configuration files." "Specifies the file system location of the database configuration files."
+ " If this is omitted, the environment variable PGDATA is used.") + " If this is omitted, the environment variable PGDATA is used.")
...@@ -124,8 +119,19 @@ public class Main { ...@@ -124,8 +119,19 @@ public class Main {
"Interval in milliseconds to gather stats") "Interval in milliseconds to gather stats")
.withRequiredArg() .withRequiredArg()
.defaultsTo("3000"); .defaultsTo("3000");
parser.acceptsAll(Lists.newArrayList("s", "show-system"),
"Print read/write data for the whole system");
parser.acceptsAll(Lists.newArrayList("o", "show-other"),
"Print read/write data not accounted by any listed process");
parser.acceptsAll(Lists.newArrayList("no-print-header"),
"Suppress print of CSV header");
parser.acceptsAll(Lists.newArrayList("a", "advanced"),
"Enable advanced options");
parser.acceptsAll(Lists.newArrayList("prometheus-format"), parser.acceptsAll(Lists.newArrayList("prometheus-format"),
"Print output in prometheus format"); "Print output in prometheus format");
parser.acceptsAll(Lists.newArrayList("all-processes"),
"Enable advanced options")
.availableIf("advanced");
parser.acceptsAll(Lists.newArrayList("ppid"), parser.acceptsAll(Lists.newArrayList("ppid"),
"Parent pid of the process to scan" "Parent pid of the process to scan"
+ " (if not specified will collect stats from all processes)") + " (if not specified will collect stats from all processes)")
...@@ -138,7 +144,7 @@ public class Main { ...@@ -138,7 +144,7 @@ public class Main {
return parser; return parser;
} }
private static Config configOptionSet(OptionSet options) { private static Config configOptionSet(OptionSet options) throws Exception {
Config.Builder configBuilder = new Config.Builder(); Config.Builder configBuilder = new Config.Builder();
ConfigHelper configHelper = new ConfigHelper(options); ConfigHelper configHelper = new ConfigHelper(options);
if (System.getenv("PGDATA") != null) { if (System.getenv("PGDATA") != null) {
...@@ -152,8 +158,15 @@ public class Main { ...@@ -152,8 +158,15 @@ public class Main {
configHelper.setIf("show-other", configBuilder::withShowOther); configHelper.setIf("show-other", configBuilder::withShowOther);
configHelper.set("group", Main::readGroupConfig, configBuilder::appendProcessGroups); configHelper.set("group", Main::readGroupConfig, configBuilder::appendProcessGroups);
configHelper.setIf("prometheus-format", configBuilder::withPrometheusFormat); configHelper.setIf("prometheus-format", configBuilder::withPrometheusFormat);
Config config = configBuilder.build(); Config config = configBuilder.build();
return config;
if (!config.getPpid().isPresent()) {
int postgresqlPid = parsePostgresPidFile(configBuilder.build().getDataDir());
configBuilder.withPpid(postgresqlPid);
}
return configBuilder.build();
} }
private static ImmutableList<ProcessGroupInfo> readGroupConfig(String config) { private static ImmutableList<ProcessGroupInfo> readGroupConfig(String config) {
...@@ -172,6 +185,23 @@ public class Main { ...@@ -172,6 +185,23 @@ public class Main {
} }
} }
private static int parsePostgresPidFile(Path dataDir) {
Path postmasterPid = dataDir.resolve("postmaster.pid");
try (Scanner scanner = new Scanner(
new FileInputStream(postmasterPid.toFile()),
Charsets.UTF_8.name())) {
if (scanner.hasNext()) {
String line = scanner.nextLine();
return Integer.parseInt(line);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
throw new RuntimeException("Error reading pid from file " + postmasterPid);
}
private static class ConfigHelper { private static class ConfigHelper {
public final OptionSet options; public final OptionSet options;
......
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.ongres.pgio.main;
import com.google.common.base.Charsets;
import java.io.FileInputStream;
import java.nio.file.Path;
import java.util.Scanner;
public class PostgresPidFileParser {
private final Path dataDir;
public PostgresPidFileParser(Path dataDir) {
this.dataDir = dataDir;
}
public int parse() throws Exception {
Path postmasterPid = dataDir.resolve("postmaster.pid");
try (Scanner scanner = new Scanner(
new FileInputStream(postmasterPid.toFile()),
Charsets.UTF_8.name())) {
if (scanner.hasNext()) {
String line = scanner.nextLine();
return Integer.parseInt(line);
}
}
throw new IllegalStateException("Error reading pid from file " + postmasterPid);
}
}
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
*/ */
package com.ongres.pgio.main.stats; package com.ongres.pgio.main.stats;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Streams; import com.google.common.collect.Streams;
import com.ongres.pgio.main.config.Config; import com.ongres.pgio.main.config.Config;
...@@ -47,6 +48,9 @@ import java.util.stream.StreamSupport; ...@@ -47,6 +48,9 @@ import java.util.stream.StreamSupport;
public class StatProcessor { public class StatProcessor {
private static final ImmutableList<String> PROC_FILES_TO_READ =
ImmutableList.of("cmdline", "stat", "io");
private final Config config; private final Config config;
private final StatSerializer serializer; private final StatSerializer serializer;
private final PrintStream err; private final PrintStream err;
...@@ -134,9 +138,8 @@ public class StatProcessor { ...@@ -134,9 +138,8 @@ public class StatProcessor {
.filter(file -> file.canRead()) .filter(file -> file.canRead())
.filter(file -> file.isDirectory()) .filter(file -> file.isDirectory())
.filter(file -> reduceSubdirectory(file.toPath(), procStream -> procStream .filter(file -> reduceSubdirectory(file.toPath(), procStream -> procStream
.filter(entry -> entry.getName().equals("cmdline")) .filter(entry -> PROC_FILES_TO_READ.contains(entry.getName()))
.filter(entry -> entry.getName().equals("io")) .filter(entry -> entry.canRead()).count()) == PROC_FILES_TO_READ.size())
.filter(entry -> entry.canRead()).count()) == 2)
.map(file -> file.getName()) .map(file -> file.getName())
.map(name -> Integer.parseInt(name)) .map(name -> Integer.parseInt(name))
.map(pid -> getPreviousOrParseInfo(previousStats, pid)) .map(pid -> getPreviousOrParseInfo(previousStats, pid))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment