1// Copyright (C) 2015 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15#include <linux/taskstats.h> 16#include <netlink/socket.h> 17#include <netlink/genl/ctrl.h> 18#include <netlink/genl/genl.h> 19 20#include <algorithm> 21#include <memory> 22 23#include <android-base/logging.h> 24 25#include "taskstats.h" 26 27TaskstatsSocket::TaskstatsSocket() 28 : nl_(nullptr, nl_socket_free), family_id_(0) { 29} 30 31bool TaskstatsSocket::Open() { 32 std::unique_ptr<nl_sock, decltype(&nl_socket_free)> nl( 33 nl_socket_alloc(), nl_socket_free); 34 if (!nl.get()) { 35 LOG(ERROR) << "Failed to allocate netlink socket"; 36 return false; 37 } 38 39 int ret = genl_connect(nl.get()); 40 if (ret < 0) { 41 LOG(ERROR) << nl_geterror(ret) << std::endl << "Unable to open netlink socket (are you root?)"; 42 return false; 43 } 44 45 int family_id = genl_ctrl_resolve(nl.get(), TASKSTATS_GENL_NAME); 46 if (family_id < 0) { 47 LOG(ERROR) << nl_geterror(family_id) << std::endl << "Unable to determine taskstats family id (does your kernel support taskstats?)"; 48 return false; 49 } 50 51 nl_ = std::move(nl); 52 family_id_ = family_id; 53 54 return true; 55} 56 57void TaskstatsSocket::Close() { 58 nl_.reset(); 59} 60 61struct TaskStatsRequest { 62 pid_t requested_pid; 63 taskstats stats; 64}; 65 66static pid_t ParseAggregateTaskStats(nlattr* attr, int attr_size, 67 taskstats* stats) { 68 pid_t received_pid = -1; 69 nla_for_each_attr(attr, attr, attr_size, attr_size) { 70 switch (nla_type(attr)) { 71 case TASKSTATS_TYPE_PID: 72 case TASKSTATS_TYPE_TGID: 73 received_pid = nla_get_u32(attr); 74 break; 75 case TASKSTATS_TYPE_STATS: 76 { 77 int len = static_cast<int>(sizeof(*stats)); 78 len = std::min(len, nla_len(attr)); 79 nla_memcpy(stats, attr, len); 80 return received_pid; 81 } 82 default: 83 LOG(ERROR) << "unexpected attribute inside AGGR"; 84 return -1; 85 } 86 } 87 88 return -1; 89} 90 91static int ParseTaskStats(nl_msg* msg, void* arg) { 92 TaskStatsRequest* taskstats_request = static_cast<TaskStatsRequest*>(arg); 93 genlmsghdr* gnlh = static_cast<genlmsghdr*>(nlmsg_data(nlmsg_hdr(msg))); 94 nlattr* attr = genlmsg_attrdata(gnlh, 0); 95 int remaining = genlmsg_attrlen(gnlh, 0); 96 97 nla_for_each_attr(attr, attr, remaining, remaining) { 98 switch (nla_type(attr)) { 99 case TASKSTATS_TYPE_AGGR_PID: 100 case TASKSTATS_TYPE_AGGR_TGID: 101 { 102 nlattr* nested_attr = static_cast<nlattr*>(nla_data(attr)); 103 taskstats stats; 104 pid_t ret; 105 106 ret = ParseAggregateTaskStats(nested_attr, nla_len(attr), &stats); 107 if (ret < 0) { 108 LOG(ERROR) << "Bad AGGR_PID contents"; 109 } else if (ret == taskstats_request->requested_pid) { 110 taskstats_request->stats = stats; 111 } else { 112 LOG(WARNING) << "got taskstats for unexpected pid " << ret << 113 " (expected " << taskstats_request->requested_pid << ", continuing..."; 114 } 115 break; 116 } 117 case TASKSTATS_TYPE_NULL: 118 break; 119 default: 120 LOG(ERROR) << "unexpected attribute in taskstats"; 121 } 122 } 123 return NL_OK; 124} 125 126bool TaskstatsSocket::GetStats(int pid, int type, TaskStatistics& stats) { 127 TaskStatsRequest taskstats_request = TaskStatsRequest(); 128 taskstats_request.requested_pid = pid; 129 130 std::unique_ptr<nl_msg, decltype(&nlmsg_free)> message(nlmsg_alloc(), 131 nlmsg_free); 132 133 genlmsg_put(message.get(), NL_AUTO_PID, NL_AUTO_SEQ, family_id_, 0, 0, 134 TASKSTATS_CMD_GET, TASKSTATS_VERSION); 135 nla_put_u32(message.get(), type, pid); 136 137 int result = nl_send_auto_complete(nl_.get(), message.get()); 138 if (result < 0) { 139 return false; 140 } 141 142 std::unique_ptr<nl_cb, decltype(&nl_cb_put)> callbacks( 143 nl_cb_alloc(NL_CB_DEFAULT), nl_cb_put); 144 nl_cb_set(callbacks.get(), NL_CB_VALID, NL_CB_CUSTOM, &ParseTaskStats, 145 static_cast<void*>(&taskstats_request)); 146 147 result = nl_recvmsgs(nl_.get(), callbacks.get()); 148 if (result < 0) { 149 return false; 150 } 151 nl_wait_for_ack(nl_.get()); 152 153 stats = TaskStatistics(taskstats_request.stats); 154 155 return true; 156} 157 158bool TaskstatsSocket::GetPidStats(int pid, TaskStatistics& stats) { 159 return GetStats(pid, TASKSTATS_CMD_ATTR_PID, stats); 160} 161 162bool TaskstatsSocket::GetTgidStats(int tgid, TaskStatistics& stats) { 163 bool ret = GetStats(tgid, TASKSTATS_CMD_ATTR_TGID, stats); 164 if (ret) { 165 stats.set_pid(tgid); 166 } 167 return ret; 168} 169 170TaskStatistics::TaskStatistics(const taskstats& taskstats_stats) { 171 comm_ = std::string(taskstats_stats.ac_comm); 172 pid_ = taskstats_stats.ac_pid; 173 174 uid_ = taskstats_stats.ac_uid; 175 gid_ = taskstats_stats.ac_gid; 176 pid_ = taskstats_stats.ac_pid; 177 ppid_ = taskstats_stats.ac_ppid; 178 179 cpu_delay_count_ = taskstats_stats.cpu_count; 180 cpu_delay_ns_ = taskstats_stats.cpu_delay_total; 181 182 block_io_delay_count_ = taskstats_stats.blkio_count; 183 block_io_delay_ns_ = taskstats_stats.blkio_delay_total; 184 185 swap_in_delay_count_ = taskstats_stats.swapin_count; 186 swap_in_delay_ns_ = taskstats_stats.swapin_delay_total; 187 188 reclaim_delay_count_ = taskstats_stats.freepages_count; 189 reclaim_delay_ns_ = taskstats_stats.freepages_delay_total; 190 191 total_delay_ns_ = 192 cpu_delay_ns_ + block_io_delay_ns_ + swap_in_delay_ns_ + reclaim_delay_ns_; 193 194 cpu_time_real_ = taskstats_stats.cpu_run_real_total; 195 cpu_time_virtual_ = taskstats_stats.cpu_run_virtual_total; 196 197 read_bytes_ = taskstats_stats.read_bytes; 198 write_bytes_ = taskstats_stats.write_bytes; 199 read_write_bytes_ = read_bytes_ + write_bytes_; 200 cancelled_write_bytes_ = taskstats_stats.cancelled_write_bytes; 201 threads_ = 1; 202} 203 204void TaskStatistics::AddPidToTgid(const TaskStatistics& pid_statistics) { 205 // tgid statistics already contain delay values totalled across all pids 206 // only add IO statistics 207 read_bytes_ += pid_statistics.read_bytes_; 208 write_bytes_ += pid_statistics.write_bytes_; 209 read_write_bytes_ += pid_statistics.read_write_bytes_; 210 cancelled_write_bytes_ += pid_statistics.cancelled_write_bytes_; 211 if (pid_ == pid_statistics.pid_) { 212 comm_ = pid_statistics.comm_; 213 uid_ = pid_statistics.uid_; 214 gid_ = pid_statistics.pid_; 215 ppid_ = pid_statistics.ppid_; 216 } else { 217 threads_++; 218 } 219} 220 221// Store new statistics and return the delta from the old statistics 222TaskStatistics TaskStatistics::Update(const TaskStatistics& new_statistics) { 223 TaskStatistics delta = new_statistics; 224 delta.cpu_delay_count_ -= cpu_delay_count_; 225 delta.cpu_delay_ns_ -= cpu_delay_ns_; 226 delta.block_io_delay_count_ -= block_io_delay_count_; 227 delta.block_io_delay_ns_ -= block_io_delay_ns_; 228 delta.swap_in_delay_count_ -= swap_in_delay_count_; 229 delta.swap_in_delay_ns_ -= swap_in_delay_ns_; 230 delta.reclaim_delay_count_ -= reclaim_delay_count_; 231 delta.reclaim_delay_ns_ -= reclaim_delay_ns_; 232 delta.total_delay_ns_ -= total_delay_ns_; 233 delta.cpu_time_real_ -= cpu_time_real_; 234 delta.cpu_time_virtual_ -= cpu_time_virtual_; 235 delta.read_bytes_ -= read_bytes_; 236 delta.write_bytes_ -= write_bytes_; 237 delta.read_write_bytes_ -= read_write_bytes_; 238 delta.cancelled_write_bytes_ -= cancelled_write_bytes_; 239 *this = new_statistics; 240 return delta; 241} 242