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