summaryrefslogtreecommitdiff
path: root/iotop
diff options
context:
space:
mode:
authorColin Cross <ccross@android.com>2015-09-03 17:56:39 -0700
committerColin Cross <ccross@android.com>2015-09-04 18:02:03 -0700
commit646d001e5551e091ea2c5d1397c77ffa24c01953 (patch)
tree20167875094d24972bbc232c5ad335ce4812a8ff /iotop
parent32802ecf57a6047f7008f9be8ba5072b9d458e57 (diff)
downloadextras-646d001e5551e091ea2c5d1397c77ffa24c01953.tar.gz
Implement iotop
iotop provides per process statistics on IO rates and kernel delays. Requires root, and a kernel compiled with CONFIG_TASKSTATS, CONFIG_TASK_DELAY_ACCT, and CONFIG_TASK_IO_ACCOUNTING. Change-Id: I111c55a1492e9ea33d8d7b3ab620080625b84346
Diffstat (limited to 'iotop')
-rw-r--r--iotop/Android.mk28
-rw-r--r--iotop/MODULE_LICENSE_APACHE20
-rw-r--r--iotop/iotop.cpp253
-rw-r--r--iotop/tasklist.cpp65
-rw-r--r--iotop/tasklist.h30
-rw-r--r--iotop/taskstats.cpp228
-rw-r--r--iotop/taskstats.h94
7 files changed, 698 insertions, 0 deletions
diff --git a/iotop/Android.mk b/iotop/Android.mk
new file mode 100644
index 00000000..b8a146df
--- /dev/null
+++ b/iotop/Android.mk
@@ -0,0 +1,28 @@
+# Copyright (C) 2015 The Android Open Source Project
+#
+# 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.
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := iotop.cpp tasklist.cpp taskstats.cpp
+
+LOCAL_MODULE := iotop
+
+LOCAL_MODULE_TAGS := debug
+
+LOCAL_SHARED_LIBRARIES := libnl libbase
+
+LOCAL_CFLAGS := -Wall -Werror
+
+include $(BUILD_EXECUTABLE)
diff --git a/iotop/MODULE_LICENSE_APACHE2 b/iotop/MODULE_LICENSE_APACHE2
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/iotop/MODULE_LICENSE_APACHE2
diff --git a/iotop/iotop.cpp b/iotop/iotop.cpp
new file mode 100644
index 00000000..e2d7e0b8
--- /dev/null
+++ b/iotop/iotop.cpp
@@ -0,0 +1,253 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// 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.
+
+#include <getopt.h>
+#include <inttypes.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <algorithm>
+#include <map>
+#include <unordered_map>
+#include <vector>
+
+#include <base/logging.h>
+
+#include "tasklist.h"
+#include "taskstats.h"
+
+constexpr uint64_t NSEC_PER_SEC = 1000000000;
+
+static uint64_t BytesToKB(uint64_t bytes) {
+ return (bytes + 1024-1) / 1024;
+}
+
+static float TimeToTgidPercent(uint64_t ns, int time, const TaskStatistics& stats) {
+ float percent = ns / stats.threads() / (time * NSEC_PER_SEC / 100.0f);
+ return std::min(percent, 99.99f);
+}
+
+static void usage(char* myname) {
+ printf(
+ "Usage: %s [-h] [-P] [-d <delay>] [-n <cycles>] [-s <column>]\n"
+ " -d Set the delay between refreshes in seconds.\n"
+ " -h Display this help screen.\n"
+ " -m Set the number of processes or threads to show\n"
+ " -n Set the number of refreshes before exiting.\n"
+ " -P Show processes instead of the default threads.\n"
+ " -s Set the column to sort by:\n"
+ " pid, read, write, total, io, swap, sched, mem or delay.\n",
+ myname);
+}
+
+using Sorter = std::function<void(std::vector<TaskStatistics>&)>;
+static Sorter GetSorter(const std::string field) {
+ // Generic comparator
+ static auto comparator = [](auto& lhs, auto& rhs, auto field, bool ascending) -> bool {
+ auto a = (lhs.*field)();
+ auto b = (rhs.*field)();
+ if (a != b) {
+ // Sort by selected field
+ return ascending ^ (a < b);
+ } else {
+ // And then fall back to sorting by pid
+ return lhs.pid() < rhs.pid();
+ }
+ };
+
+ auto make_sorter = [](auto field, bool ascending) {
+ // Make closure for comparator on a specific field
+ using namespace std::placeholders;
+ auto bound_comparator = std::bind(comparator, _1, _2, field, ascending);
+
+ // Return closure to std::sort with specialized comparator
+ return [bound_comparator](auto& vector) {
+ return std::sort(vector.begin(), vector.end(), bound_comparator);
+ };
+ };
+
+ static const std::map<std::string, Sorter> sorters{
+ {"pid", make_sorter(&TaskStatistics::pid, false)},
+ {"read", make_sorter(&TaskStatistics::read, true)},
+ {"write", make_sorter(&TaskStatistics::write, true)},
+ {"total", make_sorter(&TaskStatistics::read_write, true)},
+ {"io", make_sorter(&TaskStatistics::delay_io, true)},
+ {"swap", make_sorter(&TaskStatistics::delay_swap, true)},
+ {"sched", make_sorter(&TaskStatistics::delay_sched, true)},
+ {"mem", make_sorter(&TaskStatistics::delay_mem, true)},
+ {"delay", make_sorter(&TaskStatistics::delay_total, true)},
+ };
+
+ auto it = sorters.find(field);
+ if (it == sorters.end()) {
+ return nullptr;
+ }
+ return it->second;
+}
+
+int main(int argc, char* argv[]) {
+ bool processes = false;
+ int delay = 1;
+ int cycles = -1;
+ int limit = -1;
+ Sorter sorter = GetSorter("total");
+
+ android::base::InitLogging(argv, android::base::StderrLogger);
+
+ while (1) {
+ int c;
+ static const option longopts[] = {
+ {"delay", required_argument, 0, 'd'},
+ {"help", 0, 0, 'h'},
+ {"limit", required_argument, 0, 'm'},
+ {"iter", required_argument, 0, 'n'},
+ {"sort", required_argument, 0, 's'},
+ {"processes", 0, 0, 'P'},
+ {0, 0, 0, 0},
+ };
+ c = getopt_long(argc, argv, "d:hm:n:Ps:", longopts, NULL);
+ if (c < 0) {
+ break;
+ }
+ switch (c) {
+ case 'd':
+ delay = atoi(optarg);
+ break;
+ case 'h':
+ usage(argv[0]);
+ return(EXIT_SUCCESS);
+ case 'm':
+ limit = atoi(optarg);
+ break;
+ case 'n':
+ cycles = atoi(optarg);
+ break;
+ case 's': {
+ sorter = GetSorter(optarg);
+ if (sorter == nullptr) {
+ LOG(ERROR) << "Invalid sort column \"" << optarg << "\"";
+ usage(argv[0]);
+ return(EXIT_FAILURE);
+ }
+ break;
+ }
+ case 'P':
+ processes = true;
+ break;
+ case '?':
+ usage(argv[0]);
+ return(EXIT_FAILURE);
+ default:
+ abort();
+ }
+ }
+
+ std::map<pid_t, std::vector<pid_t>> tgid_map;
+
+ TaskstatsSocket taskstats_socket;
+ taskstats_socket.Open();
+
+ std::unordered_map<pid_t, TaskStatistics> pid_stats;
+ std::unordered_map<pid_t, TaskStatistics> tgid_stats;
+ std::vector<TaskStatistics> stats;
+
+ bool first = true;
+ bool second = true;
+
+ while (true) {
+ stats.clear();
+ if (!TaskList::Scan(tgid_map)) {
+ LOG(FATAL) << "failed to scan tasks";
+ }
+ for (auto& tgid_it : tgid_map) {
+ pid_t tgid = tgid_it.first;
+ std::vector<pid_t>& pid_list = tgid_it.second;
+
+ TaskStatistics tgid_stats_new;
+ TaskStatistics tgid_stats_delta;
+
+ if (processes) {
+ // If printing processes, collect stats for the tgid which will
+ // hold delay accounting data across all threads, including
+ // ones that have exited.
+ if (!taskstats_socket.GetTgidStats(tgid, tgid_stats_new)) {
+ continue;
+ }
+ tgid_stats_delta = tgid_stats[tgid].Update(tgid_stats_new);
+ }
+
+ // Collect per-thread stats
+ for (pid_t pid : pid_list) {
+ TaskStatistics pid_stats_new;
+ if (!taskstats_socket.GetPidStats(pid, pid_stats_new)) {
+ continue;
+ }
+
+ TaskStatistics pid_stats_delta = pid_stats[pid].Update(pid_stats_new);
+
+ if (processes) {
+ tgid_stats_delta.AddPidToTgid(pid_stats_delta);
+ } else {
+ stats.push_back(pid_stats_delta);
+ }
+ }
+
+ if (processes) {
+ stats.push_back(tgid_stats_delta);
+ }
+ }
+
+ if (!first) {
+ sorter(stats);
+ if (!second) {
+ printf("\n");
+ }
+ printf("%6s %-16s %20s %34s\n", "", "",
+ "--- IO (KiB/s) ---", "----------- delayed on ----------");
+ printf("%6s %-16s %6s %6s %6s %-5s %-5s %-5s %-5s %-5s\n",
+ "PID",
+ "Command",
+ "read",
+ "write",
+ "total",
+ "IO",
+ "swap",
+ "sched",
+ "mem",
+ "total");
+ int n = limit;
+ for (const TaskStatistics& statistics : stats) {
+ printf("%6d %-16s %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %5.2f%% %5.2f%% %5.2f%% %5.2f%% %5.2f%%\n",
+ statistics.pid(),
+ statistics.comm().c_str(),
+ BytesToKB(statistics.read()),
+ BytesToKB(statistics.write()),
+ BytesToKB(statistics.read_write()),
+ TimeToTgidPercent(statistics.delay_io(), delay, statistics),
+ TimeToTgidPercent(statistics.delay_swap(), delay, statistics),
+ TimeToTgidPercent(statistics.delay_sched(), delay, statistics),
+ TimeToTgidPercent(statistics.delay_mem(), delay, statistics),
+ TimeToTgidPercent(statistics.delay_total(), delay, statistics));
+ if (n > 0 && --n == 0) break;
+ }
+ second = false;
+
+ if (cycles > 0 && --cycles == 0) break;
+ }
+ first = false;
+ sleep(delay);
+ }
+
+ return 0;
+}
diff --git a/iotop/tasklist.cpp b/iotop/tasklist.cpp
new file mode 100644
index 00000000..c1041a46
--- /dev/null
+++ b/iotop/tasklist.cpp
@@ -0,0 +1,65 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// 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.
+
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <map>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <base/stringprintf.h>
+
+#include "tasklist.h"
+
+template<typename Func>
+static bool ScanPidsInDir(std::string name, Func f) {
+ std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(name.c_str()), closedir);
+ if (!dir) {
+ return false;
+ }
+
+ dirent* entry;
+ while ((entry = readdir(dir.get())) != nullptr) {
+ if (isdigit(entry->d_name[0])) {
+ pid_t pid = atoi(entry->d_name);
+ f(pid);
+ }
+ }
+
+ return true;
+}
+
+bool TaskList::Scan(std::map<pid_t, std::vector<pid_t>>& tgid_map) {
+ tgid_map.clear();
+
+ return ScanPidsInDir("/proc", [&tgid_map](pid_t tgid) {
+ std::vector<pid_t> pid_list;
+ if (ScanPid(tgid, pid_list)) {
+ tgid_map.insert({tgid, pid_list});
+ }
+ });
+}
+
+bool TaskList::ScanPid(pid_t tgid, std::vector<pid_t>& pid_list) {
+ std::string filename = android::base::StringPrintf("/proc/%d/task", tgid);
+
+ return ScanPidsInDir(filename, [&pid_list](pid_t pid) {
+ pid_list.push_back(pid);
+ });
+}
diff --git a/iotop/tasklist.h b/iotop/tasklist.h
new file mode 100644
index 00000000..1a19c8fb
--- /dev/null
+++ b/iotop/tasklist.h
@@ -0,0 +1,30 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// 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.
+
+#include <map>
+#include <vector>
+
+#ifndef _IOTOP_TASKLIST_H
+#define _IOTOP_TASKLIST_H
+
+class TaskList {
+public:
+ static bool Scan(std::map<pid_t, std::vector<pid_t>>&);
+
+private:
+ TaskList() {}
+ static bool ScanPid(pid_t pid, std::vector<pid_t>&);
+};
+
+#endif // _IOTOP_TASKLIST_H
diff --git a/iotop/taskstats.cpp b/iotop/taskstats.cpp
new file mode 100644
index 00000000..0c397dc5
--- /dev/null
+++ b/iotop/taskstats.cpp
@@ -0,0 +1,228 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// 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.
+
+#include <linux/taskstats.h>
+#include <netlink/socket.h>
+#include <netlink/genl/ctrl.h>
+#include <netlink/genl/genl.h>
+
+#include <algorithm>
+#include <memory>
+
+#include <base/logging.h>
+
+#include "taskstats.h"
+
+TaskstatsSocket::TaskstatsSocket()
+ : nl_(nullptr, nl_socket_free), family_id_(0) {
+}
+
+bool TaskstatsSocket::Open() {
+ std::unique_ptr<nl_sock, decltype(&nl_socket_free)> nl(
+ nl_socket_alloc(), nl_socket_free);
+ if (!nl.get()) {
+ LOG(FATAL) << "Failed to allocate netlink socket";
+ }
+
+ int ret = genl_connect(nl.get());
+ if (ret < 0) {
+ LOG(FATAL) << nl_geterror(ret) << std::endl << "Unable to open netlink socket (are you root?)";
+ }
+
+ int family_id = genl_ctrl_resolve(nl.get(), TASKSTATS_GENL_NAME);
+ if (family_id < 0) {
+ LOG(FATAL) << nl_geterror(family_id) << std::endl << "Unable to determine taskstats family id (does your kernel support taskstats?)";
+ }
+
+ nl_ = std::move(nl);
+ family_id_ = family_id;
+
+ return true;
+}
+
+void TaskstatsSocket::Close() {
+ nl_.reset();
+}
+
+struct TaskStatsRequest {
+ pid_t requested_pid;
+ taskstats stats;
+};
+
+static pid_t ParseAggregateTaskStats(nlattr* attr, int attr_size,
+ taskstats* stats) {
+ pid_t received_pid = -1;
+ nla_for_each_attr(attr, attr, attr_size, attr_size) {
+ switch (nla_type(attr)) {
+ case TASKSTATS_TYPE_PID:
+ case TASKSTATS_TYPE_TGID:
+ received_pid = nla_get_u32(attr);
+ break;
+ case TASKSTATS_TYPE_STATS:
+ {
+ int len = static_cast<int>(sizeof(*stats));
+ len = std::min(len, nla_len(attr));
+ nla_memcpy(stats, attr, len);
+ return received_pid;
+ }
+ default:
+ LOG(ERROR) << "unexpected attribute inside AGGR";
+ return -1;
+ }
+ }
+
+ return -1;
+}
+
+static int ParseTaskStats(nl_msg* msg, void* arg) {
+ TaskStatsRequest* taskstats_request = static_cast<TaskStatsRequest*>(arg);
+ genlmsghdr* gnlh = static_cast<genlmsghdr*>(nlmsg_data(nlmsg_hdr(msg)));
+ nlattr* attr = genlmsg_attrdata(gnlh, 0);
+
+ switch (nla_type(attr)) {
+ case TASKSTATS_TYPE_AGGR_PID:
+ case TASKSTATS_TYPE_AGGR_TGID:
+ nlattr* nested_attr = static_cast<nlattr*>(nla_data(attr));
+ taskstats stats;
+ pid_t ret;
+
+ ret = ParseAggregateTaskStats(nested_attr, nla_len(attr), &stats);
+ if (ret < 0) {
+ LOG(ERROR) << "Bad AGGR_PID contents";
+ } else if (ret == taskstats_request->requested_pid) {
+ taskstats_request->stats = stats;
+ } else {
+ LOG(WARNING) << "got taskstats for unexpected pid " << ret <<
+ " (expected " << taskstats_request->requested_pid << ", continuing...";
+ }
+ }
+ return NL_OK;
+}
+
+bool TaskstatsSocket::GetStats(int pid, int type, TaskStatistics& stats) {
+ TaskStatsRequest taskstats_request = TaskStatsRequest();
+ taskstats_request.requested_pid = pid;
+
+ std::unique_ptr<nl_msg, decltype(&nlmsg_free)> message(nlmsg_alloc(),
+ nlmsg_free);
+
+ genlmsg_put(message.get(), NL_AUTO_PID, NL_AUTO_SEQ, family_id_, 0, 0,
+ TASKSTATS_CMD_GET, TASKSTATS_VERSION);
+ nla_put_u32(message.get(), type, pid);
+
+ int result = nl_send_auto_complete(nl_.get(), message.get());
+ if (result < 0) {
+ return false;
+ }
+
+ std::unique_ptr<nl_cb, decltype(&nl_cb_put)> callbacks(
+ nl_cb_alloc(NL_CB_DEFAULT), nl_cb_put);
+ nl_cb_set(callbacks.get(), NL_CB_VALID, NL_CB_CUSTOM, &ParseTaskStats,
+ static_cast<void*>(&taskstats_request));
+
+ result = nl_recvmsgs(nl_.get(), callbacks.get());
+ if (result < 0) {
+ return false;
+ }
+ nl_wait_for_ack(nl_.get());
+
+ stats = TaskStatistics(taskstats_request.stats);
+
+ return true;
+}
+
+bool TaskstatsSocket::GetPidStats(int pid, TaskStatistics& stats) {
+ return GetStats(pid, TASKSTATS_CMD_ATTR_PID, stats);
+}
+
+bool TaskstatsSocket::GetTgidStats(int tgid, TaskStatistics& stats) {
+ bool ret = GetStats(tgid, TASKSTATS_CMD_ATTR_TGID, stats);
+ if (ret) {
+ stats.set_pid(tgid);
+ }
+ return ret;
+}
+
+TaskStatistics::TaskStatistics(const taskstats& taskstats_stats) {
+ comm_ = std::string(taskstats_stats.ac_comm);
+ pid_ = taskstats_stats.ac_pid;
+
+ uid_ = taskstats_stats.ac_uid;
+ gid_ = taskstats_stats.ac_gid;
+ pid_ = taskstats_stats.ac_pid;
+ ppid_ = taskstats_stats.ac_ppid;
+
+ cpu_delay_count_ = taskstats_stats.cpu_count;
+ cpu_delay_ns_ = taskstats_stats.cpu_delay_total;
+
+ block_io_delay_count_ = taskstats_stats.blkio_count;
+ block_io_delay_ns_ = taskstats_stats.blkio_delay_total;
+
+ swap_in_delay_count_ = taskstats_stats.swapin_count;
+ swap_in_delay_ns_ = taskstats_stats.swapin_delay_total;
+
+ reclaim_delay_count_ = taskstats_stats.freepages_count;
+ reclaim_delay_ns_ = taskstats_stats.freepages_delay_total;
+
+ total_delay_ns_ =
+ cpu_delay_ns_ + block_io_delay_ns_ + swap_in_delay_ns_ + reclaim_delay_ns_;
+
+ cpu_time_real_ = taskstats_stats.cpu_run_real_total;
+ cpu_time_virtual_ = taskstats_stats.cpu_run_virtual_total;
+
+ read_bytes_ = taskstats_stats.read_bytes;
+ write_bytes_ = taskstats_stats.write_bytes;
+ read_write_bytes_ = read_bytes_ + write_bytes_;
+ cancelled_write_bytes_ = taskstats_stats.cancelled_write_bytes;
+ threads_ = 1;
+}
+
+void TaskStatistics::AddPidToTgid(const TaskStatistics& pid_statistics) {
+ // tgid statistics already contain delay values totalled across all pids
+ // only add IO statistics
+ read_bytes_ += pid_statistics.read_bytes_;
+ write_bytes_ += pid_statistics.write_bytes_;
+ read_write_bytes_ += pid_statistics.read_write_bytes_;
+ cancelled_write_bytes_ += pid_statistics.cancelled_write_bytes_;
+ if (pid_ == pid_statistics.pid_) {
+ comm_ = pid_statistics.comm_;
+ uid_ = pid_statistics.uid_;
+ gid_ = pid_statistics.pid_;
+ ppid_ = pid_statistics.ppid_;
+ } else {
+ threads_++;
+ }
+}
+
+// Store new statistics and return the delta from the old statistics
+TaskStatistics TaskStatistics::Update(const TaskStatistics& new_statistics) {
+ TaskStatistics delta = new_statistics;
+ delta.cpu_delay_count_ -= cpu_delay_count_;
+ delta.cpu_delay_ns_ -= cpu_delay_ns_;
+ delta.block_io_delay_count_ -= block_io_delay_count_;
+ delta.block_io_delay_ns_ -= block_io_delay_ns_;
+ delta.swap_in_delay_count_ -= swap_in_delay_count_;
+ delta.swap_in_delay_ns_ -= swap_in_delay_ns_;
+ delta.reclaim_delay_count_ -= reclaim_delay_count_;
+ delta.reclaim_delay_ns_ -= reclaim_delay_ns_;
+ delta.total_delay_ns_ -= total_delay_ns_;
+ delta.cpu_time_real_ -= cpu_time_real_;
+ delta.cpu_time_virtual_ -= cpu_time_virtual_;
+ delta.read_bytes_ -= read_bytes_;
+ delta.write_bytes_ -= write_bytes_;
+ delta.read_write_bytes_ -= read_write_bytes_;
+ delta.cancelled_write_bytes_ -= cancelled_write_bytes_;
+ *this = new_statistics;
+ return delta;
+}
diff --git a/iotop/taskstats.h b/iotop/taskstats.h
new file mode 100644
index 00000000..f5ad2d20
--- /dev/null
+++ b/iotop/taskstats.h
@@ -0,0 +1,94 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// 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.
+
+#include <memory>
+#include <string>
+
+#include <stdint.h>
+
+#ifndef _IOTOP_TASKSTATS_H
+#define _IOTOP_TASKSTATS_H
+
+struct nl_sock;
+struct taskstats;
+
+class TaskStatistics {
+public:
+ TaskStatistics(const taskstats&);
+ TaskStatistics() = default;
+ TaskStatistics(const TaskStatistics&) = default;
+ void AddPidToTgid(const TaskStatistics&);
+ TaskStatistics Update(const TaskStatistics&);
+
+ pid_t pid() const { return pid_; }
+ const std::string& comm() const { return comm_; }
+ uint64_t read() const { return read_bytes_; }
+ uint64_t write() const { return write_bytes_; }
+ uint64_t read_write() const { return read_write_bytes_; }
+ uint64_t delay_io() const { return block_io_delay_ns_; }
+ uint64_t delay_swap() const { return swap_in_delay_ns_; }
+ uint64_t delay_sched() const { return cpu_delay_ns_; }
+ uint64_t delay_mem() const { return reclaim_delay_ns_; }
+ uint64_t delay_total() const { return total_delay_ns_; }
+ int threads() const { return threads_; }
+
+ void set_pid(pid_t pid) { pid_ = pid; }
+
+private:
+ std::string comm_;
+ uid_t uid_;
+ gid_t gid_;
+ pid_t pid_;
+ pid_t ppid_;
+
+ uint64_t cpu_delay_count_;
+ uint64_t cpu_delay_ns_;
+
+ uint64_t block_io_delay_count_;
+ uint64_t block_io_delay_ns_;
+
+ uint64_t swap_in_delay_count_;
+ uint64_t swap_in_delay_ns_;
+
+ uint64_t reclaim_delay_count_;
+ uint64_t reclaim_delay_ns_;
+
+ uint64_t total_delay_ns_;
+
+ uint64_t cpu_time_real_;
+ uint64_t cpu_time_virtual_;
+
+ uint64_t read_bytes_;
+ uint64_t write_bytes_;
+ uint64_t read_write_bytes_;
+ uint64_t cancelled_write_bytes_;
+
+ int threads_;
+};
+
+class TaskstatsSocket {
+public:
+ TaskstatsSocket();
+ bool Open();
+ void Close();
+
+ bool GetPidStats(int, TaskStatistics&);
+ bool GetTgidStats(int, TaskStatistics&);
+private:
+ bool GetStats(int, int, TaskStatistics& stats);
+ std::unique_ptr<nl_sock, void(*)(nl_sock*)> nl_;
+ int family_id_;
+};
+
+#endif // _IOTOP_TASKSTATS_H