Project import
diff --git a/iotop/Android.mk b/iotop/Android.mk
new file mode 100644
index 0000000..414967c
--- /dev/null
+++ b/iotop/Android.mk
@@ -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.
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_CLANG := true
+
+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 0000000..e69de29
--- /dev/null
+++ b/iotop/MODULE_LICENSE_APACHE2
diff --git a/iotop/NOTICE b/iotop/NOTICE
new file mode 100644
index 0000000..8530865
--- /dev/null
+++ b/iotop/NOTICE
@@ -0,0 +1,190 @@
+
+ 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.
+
+ 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.
+
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
diff --git a/iotop/iotop.cpp b/iotop/iotop.cpp
new file mode 100644
index 0000000..e2582e8
--- /dev/null
+++ b/iotop/iotop.cpp
@@ -0,0 +1,282 @@
+// 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 <android-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"
+ " -a Show byte count instead of rate\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 accumulated = false;
+ 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[] = {
+ {"accumulated", 0, 0, 'a'},
+ {"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, "ad:hm:n:Ps:", longopts, NULL);
+ if (c < 0) {
+ break;
+ }
+ switch (c) {
+ case 'a':
+ accumulated = true;
+ break;
+ 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");
+ }
+ if (accumulated) {
+ printf("%6s %-16s %20s %34s\n", "", "",
+ "---- IO (KiB) ----", "----------- delayed on ----------");
+ } else {
+ 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;
+ const int delay_div = accumulated ? 1 : delay;
+ uint64_t total_read = 0;
+ uint64_t total_write = 0;
+ uint64_t total_read_write = 0;
+ for (const TaskStatistics& statistics : stats) {
+ total_read += statistics.read();
+ total_write += statistics.write();
+ total_read_write += statistics.read_write();
+
+ if (n == 0) {
+ continue;
+ } else if (n > 0) {
+ n--;
+ }
+
+ 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()) / delay_div,
+ BytesToKB(statistics.write()) / delay_div,
+ BytesToKB(statistics.read_write()) / delay_div,
+ 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));
+ }
+ printf("%6s %-16s %6" PRIu64 " %6" PRIu64 " %6" PRIu64 "\n", "", "TOTAL",
+ BytesToKB(total_read) / delay_div,
+ BytesToKB(total_write) / delay_div,
+ BytesToKB(total_read_write) / delay_div);
+
+ 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 0000000..0708741
--- /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 <android-base/stringprintf.h>
+
+#include "tasklist.h"
+
+template<typename Func>
+static bool ScanPidsInDir(const 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 0000000..1a19c8f
--- /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 0000000..60c933d
--- /dev/null
+++ b/iotop/taskstats.cpp
@@ -0,0 +1,238 @@
+// 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 <android-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);
+ int remaining = genlmsg_attrlen(gnlh, 0);
+
+ nla_for_each_attr(attr, attr, remaining, remaining) {
+ 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...";
+ }
+ break;
+ }
+ case TASKSTATS_TYPE_NULL:
+ break;
+ default:
+ LOG(ERROR) << "unexpected attribute in taskstats";
+ }
+ }
+ 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 0000000..bbf7e9e
--- /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:
+ explicit 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