diff options
author | ThiƩbaud Weksteen <tweek@google.com> | 2020-10-26 13:40:38 +0100 |
---|---|---|
committer | ThiƩbaud Weksteen <tweek@google.com> | 2020-10-26 15:53:10 +0100 |
commit | d0212c246586fcca5b8d3a88cd6b37b7de771921 (patch) | |
tree | e60f3c90a54f26c0af53870cde0a21439620cac1 /iotop | |
parent | 7ebc9e6cd61d5b167ccccbea85f42387370f51a7 (diff) | |
download | extras-d0212c246586fcca5b8d3a88cd6b37b7de771921.tar.gz |
iotop: format files
Format *.cpp according to the new .clang-format-2. The following command
was used to generate this change:
$ find . \( -name \*.cpp -o -name \*.h \) -exec clang-format \
--style=file -i {} \;
Test: mm
Bug: 171699326
Change-Id: I50a4305d77a672fbc7f9fdd895da9013f6c86cec
Diffstat (limited to 'iotop')
l--------- | iotop/.clang-format | 1 | ||||
-rw-r--r-- | iotop/iotop.cpp | 116 | ||||
-rw-r--r-- | iotop/tasklist.cpp | 8 | ||||
-rw-r--r-- | iotop/tasklist.h | 6 | ||||
-rw-r--r-- | iotop/taskstats.cpp | 134 | ||||
-rw-r--r-- | iotop/taskstats.h | 13 |
6 files changed, 128 insertions, 150 deletions
diff --git a/iotop/.clang-format b/iotop/.clang-format new file mode 120000 index 00000000..fd0645fd --- /dev/null +++ b/iotop/.clang-format @@ -0,0 +1 @@ +../.clang-format-2
\ No newline at end of file diff --git a/iotop/iotop.cpp b/iotop/iotop.cpp index fe476bf5..06b82b1b 100644 --- a/iotop/iotop.cpp +++ b/iotop/iotop.cpp @@ -30,7 +30,7 @@ constexpr uint64_t NSEC_PER_SEC = 1000000000; static uint64_t BytesToKB(uint64_t bytes) { - return (bytes + 1024-1) / 1024; + return (bytes + 1024 - 1) / 1024; } static float TimeToTgidPercent(uint64_t ns, int time, const TaskStatistics& stats) { @@ -125,38 +125,38 @@ int main(int argc, char* argv[]) { 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 << "\""; + case 'a': + accumulated = true; + break; + case 'd': + delay = atoi(optarg); + break; + case 'h': usage(argv[0]); - return EXIT_FAILURE; + 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; } - break; - } - case 'P': - processes = true; - break; - case '?': - usage(argv[0]); - return EXIT_FAILURE; - default: - abort(); + case 'P': + processes = true; + break; + case '?': + usage(argv[0]); + return EXIT_FAILURE; + default: + abort(); } } @@ -224,25 +224,14 @@ int main(int argc, char* argv[]) { printf("\n"); } if (accumulated) { - printf("%6s %-16s %20s %14s %34s\n", "", "", - "---- IO (KiB) ----", "--- faults ---", "----------- delayed on ----------"); + printf("%6s %-16s %20s %14s %34s\n", "", "", "---- IO (KiB) ----", "--- faults ---", + "----------- delayed on ----------"); } else { - printf("%6s %-16s %20s %14s %34s\n", "", "", - "--- IO (KiB/s) ---", "--- faults ---", "----------- delayed on ----------"); + printf("%6s %-16s %20s %14s %34s\n", "", "", "--- IO (KiB/s) ---", "--- faults ---", + "----------- delayed on ----------"); } - printf("%6s %-16s %6s %6s %6s %6s %6s %-5s %-5s %-5s %-5s %-5s\n", - "PID", - "Command", - "read", - "write", - "total", - "major", - "minor", - "IO", - "swap", - "sched", - "mem", - "total"); + printf("%6s %-16s %6s %6s %6s %6s %6s %-5s %-5s %-5s %-5s %-5s\n", "PID", "Command", + "read", "write", "total", "major", "minor", "IO", "swap", "sched", "mem", "total"); int n = limit; const int delay_div = accumulated ? 1 : delay; uint64_t total_read = 0; @@ -263,24 +252,21 @@ int main(int argc, char* argv[]) { n--; } - printf("%6d %-16s %6" PRIu64 " %6" PRIu64 " %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, - statistics.majflt(), statistics.minflt(), - 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("%6d %-16s %6" PRIu64 " %6" PRIu64 " %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, statistics.majflt(), + statistics.minflt(), 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 " %6" PRIu64 " %6" PRIu64"\n", "", "TOTAL", - BytesToKB(total_read) / delay_div, - BytesToKB(total_write) / delay_div, - BytesToKB(total_read_write) / delay_div, - total_majflt / delay_div, total_minflt / delay_div); + printf("%6s %-16s %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %6" PRIu64 " %6" PRIu64 "\n", "", + "TOTAL", BytesToKB(total_read) / delay_div, BytesToKB(total_write) / delay_div, + BytesToKB(total_read_write) / delay_div, total_majflt / delay_div, + total_minflt / delay_div); second = false; diff --git a/iotop/tasklist.cpp b/iotop/tasklist.cpp index 07087414..dc649265 100644 --- a/iotop/tasklist.cpp +++ b/iotop/tasklist.cpp @@ -12,11 +12,11 @@ // 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 <sys/types.h> #include <map> #include <memory> @@ -27,7 +27,7 @@ #include "tasklist.h" -template<typename Func> +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) { @@ -59,7 +59,5 @@ bool TaskList::Scan(std::map<pid_t, std::vector<pid_t>>& tgid_map) { 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); - }); + return ScanPidsInDir(filename, [&pid_list](pid_t pid) { pid_list.push_back(pid); }); } diff --git a/iotop/tasklist.h b/iotop/tasklist.h index 1a19c8fb..071dc591 100644 --- a/iotop/tasklist.h +++ b/iotop/tasklist.h @@ -19,12 +19,12 @@ #define _IOTOP_TASKLIST_H class TaskList { -public: + public: static bool Scan(std::map<pid_t, std::vector<pid_t>>&); -private: + private: TaskList() {} static bool ScanPid(pid_t pid, std::vector<pid_t>&); }; -#endif // _IOTOP_TASKLIST_H +#endif // _IOTOP_TASKLIST_H diff --git a/iotop/taskstats.cpp b/iotop/taskstats.cpp index eff1d4df..263c92a9 100644 --- a/iotop/taskstats.cpp +++ b/iotop/taskstats.cpp @@ -13,9 +13,9 @@ // limitations under the License. #include <linux/taskstats.h> -#include <netlink/socket.h> #include <netlink/genl/ctrl.h> #include <netlink/genl/genl.h> +#include <netlink/socket.h> #include <algorithm> #include <memory> @@ -24,13 +24,10 @@ #include "taskstats.h" -TaskstatsSocket::TaskstatsSocket() - : nl_(nullptr, nl_socket_free), family_id_(0) { -} +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); + std::unique_ptr<nl_sock, decltype(&nl_socket_free)> nl(nl_socket_alloc(), nl_socket_free); if (!nl.get()) { LOG(ERROR) << "Failed to allocate netlink socket"; return false; @@ -44,7 +41,8 @@ bool TaskstatsSocket::Open() { int family_id = genl_ctrl_resolve(nl.get(), TASKSTATS_GENL_NAME); if (family_id < 0) { - LOG(ERROR) << nl_geterror(family_id) << std::endl << "Unable to determine taskstats family id (does your kernel support taskstats?)"; + LOG(ERROR) << nl_geterror(family_id) << std::endl + << "Unable to determine taskstats family id (does your kernel support taskstats?)"; return false; } @@ -63,25 +61,23 @@ struct TaskStatsRequest { taskstats stats; }; -static pid_t ParseAggregateTaskStats(nlattr* attr, int attr_size, - 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; + 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; } } @@ -96,28 +92,27 @@ static int ParseTaskStats(nl_msg* msg, void* arg) { 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..."; + 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; } - break; - } - case TASKSTATS_TYPE_NULL: - break; - default: - LOG(ERROR) << "unexpected attribute in taskstats"; + case TASKSTATS_TYPE_NULL: + break; + default: + LOG(ERROR) << "unexpected attribute in taskstats"; } } return NL_OK; @@ -127,11 +122,10 @@ 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); + 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); + 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()); @@ -139,8 +133,7 @@ bool TaskstatsSocket::GetStats(int pid, int type, TaskStatistics& stats) { return false; } - std::unique_ptr<nl_cb, decltype(&nl_cb_put)> callbacks( - nl_cb_alloc(NL_CB_DEFAULT), nl_cb_put); + 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)); @@ -188,8 +181,7 @@ TaskStatistics::TaskStatistics(const taskstats& taskstats_stats) { 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_; + 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; @@ -207,9 +199,9 @@ TaskStatistics::TaskStatistics(const taskstats& taskstats_stats) { 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_; + 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_; @@ -224,22 +216,22 @@ void TaskStatistics::AddPidToTgid(const TaskStatistics& pid_statistics) { // Store new statistics and return the delta from the old statistics TaskStatistics TaskStatistics::Update(const TaskStatistics& new_statistics) { TaskStatistics delta = new_statistics; - delta.minflt_ -= minflt_; - delta.majflt_ -= majflt_; - 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.minflt_ -= minflt_; + delta.majflt_ -= majflt_; + 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 index 7aaae10a..9a82abdd 100644 --- a/iotop/taskstats.h +++ b/iotop/taskstats.h @@ -24,7 +24,7 @@ struct nl_sock; struct taskstats; class TaskStatistics { -public: + public: explicit TaskStatistics(const taskstats&); TaskStatistics() = default; TaskStatistics(const TaskStatistics&) = default; @@ -48,7 +48,7 @@ public: void set_pid(pid_t pid) { pid_ = pid; } -private: + private: std::string comm_; uid_t uid_; gid_t gid_; @@ -84,17 +84,18 @@ private: }; class TaskstatsSocket { -public: + public: TaskstatsSocket(); bool Open(); void Close(); bool GetPidStats(int, TaskStatistics&); bool GetTgidStats(int, TaskStatistics&); -private: + + private: bool GetStats(int, int, TaskStatistics& stats); - std::unique_ptr<nl_sock, void(*)(nl_sock*)> nl_; + std::unique_ptr<nl_sock, void (*)(nl_sock*)> nl_; int family_id_; }; -#endif // _IOTOP_TASKSTATS_H +#endif // _IOTOP_TASKSTATS_H |