blob: 263c92a9bf2e08ff1278a4419bf0db3b6928e5f2 [file] [log] [blame]
Colin Cross646d0012015-09-03 17:56:39 -07001// 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>
Colin Cross646d0012015-09-03 17:56:39 -070016#include <netlink/genl/ctrl.h>
17#include <netlink/genl/genl.h>
Thiébaud Weksteend0212c22020-10-26 13:40:38 +010018#include <netlink/socket.h>
Colin Cross646d0012015-09-03 17:56:39 -070019
20#include <algorithm>
21#include <memory>
22
Elliott Hughes66dd09e2015-12-04 14:00:57 -080023#include <android-base/logging.h>
Colin Cross646d0012015-09-03 17:56:39 -070024
25#include "taskstats.h"
26
Thiébaud Weksteend0212c22020-10-26 13:40:38 +010027TaskstatsSocket::TaskstatsSocket() : nl_(nullptr, nl_socket_free), family_id_(0) {}
Colin Cross646d0012015-09-03 17:56:39 -070028
29bool TaskstatsSocket::Open() {
Thiébaud Weksteend0212c22020-10-26 13:40:38 +010030 std::unique_ptr<nl_sock, decltype(&nl_socket_free)> nl(nl_socket_alloc(), nl_socket_free);
Colin Cross646d0012015-09-03 17:56:39 -070031 if (!nl.get()) {
Colin Cross5dff53f2017-10-20 10:17:35 -070032 LOG(ERROR) << "Failed to allocate netlink socket";
33 return false;
Colin Cross646d0012015-09-03 17:56:39 -070034 }
35
36 int ret = genl_connect(nl.get());
37 if (ret < 0) {
Colin Cross5dff53f2017-10-20 10:17:35 -070038 LOG(ERROR) << nl_geterror(ret) << std::endl << "Unable to open netlink socket (are you root?)";
39 return false;
Colin Cross646d0012015-09-03 17:56:39 -070040 }
41
42 int family_id = genl_ctrl_resolve(nl.get(), TASKSTATS_GENL_NAME);
43 if (family_id < 0) {
Thiébaud Weksteend0212c22020-10-26 13:40:38 +010044 LOG(ERROR) << nl_geterror(family_id) << std::endl
45 << "Unable to determine taskstats family id (does your kernel support taskstats?)";
Colin Cross5dff53f2017-10-20 10:17:35 -070046 return false;
Colin Cross646d0012015-09-03 17:56:39 -070047 }
48
49 nl_ = std::move(nl);
50 family_id_ = family_id;
51
52 return true;
53}
54
55void TaskstatsSocket::Close() {
56 nl_.reset();
57}
58
59struct TaskStatsRequest {
60 pid_t requested_pid;
61 taskstats stats;
62};
63
Thiébaud Weksteend0212c22020-10-26 13:40:38 +010064static pid_t ParseAggregateTaskStats(nlattr* attr, int attr_size, taskstats* stats) {
Colin Cross646d0012015-09-03 17:56:39 -070065 pid_t received_pid = -1;
66 nla_for_each_attr(attr, attr, attr_size, attr_size) {
67 switch (nla_type(attr)) {
Thiébaud Weksteend0212c22020-10-26 13:40:38 +010068 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;
Colin Cross646d0012015-09-03 17:56:39 -070081 }
82 }
83
84 return -1;
85}
86
87static 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);
Colin Cross1d0defe2015-09-16 16:28:11 -070091 int remaining = genlmsg_attrlen(gnlh, 0);
Colin Cross646d0012015-09-03 17:56:39 -070092
Colin Cross1d0defe2015-09-16 16:28:11 -070093 nla_for_each_attr(attr, attr, remaining, remaining) {
94 switch (nla_type(attr)) {
Thiébaud Weksteend0212c22020-10-26 13:40:38 +010095 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;
Colin Cross646d0012015-09-03 17:56:39 -0700100
Thiébaud Weksteend0212c22020-10-26 13:40:38 +0100101 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;
Colin Cross1d0defe2015-09-16 16:28:11 -0700111 }
Thiébaud Weksteend0212c22020-10-26 13:40:38 +0100112 case TASKSTATS_TYPE_NULL:
113 break;
114 default:
115 LOG(ERROR) << "unexpected attribute in taskstats";
Colin Cross646d0012015-09-03 17:56:39 -0700116 }
117 }
118 return NL_OK;
119}
120
121bool TaskstatsSocket::GetStats(int pid, int type, TaskStatistics& stats) {
122 TaskStatsRequest taskstats_request = TaskStatsRequest();
123 taskstats_request.requested_pid = pid;
124
Thiébaud Weksteend0212c22020-10-26 13:40:38 +0100125 std::unique_ptr<nl_msg, decltype(&nlmsg_free)> message(nlmsg_alloc(), nlmsg_free);
Colin Cross646d0012015-09-03 17:56:39 -0700126
Thiébaud Weksteend0212c22020-10-26 13:40:38 +0100127 genlmsg_put(message.get(), NL_AUTO_PID, NL_AUTO_SEQ, family_id_, 0, 0, TASKSTATS_CMD_GET,
128 TASKSTATS_VERSION);
Colin Cross646d0012015-09-03 17:56:39 -0700129 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
Thiébaud Weksteend0212c22020-10-26 13:40:38 +0100136 std::unique_ptr<nl_cb, decltype(&nl_cb_put)> callbacks(nl_cb_alloc(NL_CB_DEFAULT), nl_cb_put);
Colin Cross646d0012015-09-03 17:56:39 -0700137 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
151bool TaskstatsSocket::GetPidStats(int pid, TaskStatistics& stats) {
152 return GetStats(pid, TASKSTATS_CMD_ATTR_PID, stats);
153}
154
155bool 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
163TaskStatistics::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
Thiébaud Weksteend0212c22020-10-26 13:40:38 +0100184 total_delay_ns_ = cpu_delay_ns_ + block_io_delay_ns_ + swap_in_delay_ns_ + reclaim_delay_ns_;
Colin Cross646d0012015-09-03 17:56:39 -0700185
186 cpu_time_real_ = taskstats_stats.cpu_run_real_total;
187 cpu_time_virtual_ = taskstats_stats.cpu_run_virtual_total;
188
Minchan Kim4f74f322018-09-07 12:07:00 +0900189 majflt_ = taskstats_stats.ac_majflt;
190 minflt_ = taskstats_stats.ac_minflt;
191
Colin Cross646d0012015-09-03 17:56:39 -0700192 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
199void TaskStatistics::AddPidToTgid(const TaskStatistics& pid_statistics) {
200 // tgid statistics already contain delay values totalled across all pids
201 // only add IO statistics
Thiébaud Weksteend0212c22020-10-26 13:40:38 +0100202 read_bytes_ += pid_statistics.read_bytes_;
203 write_bytes_ += pid_statistics.write_bytes_;
204 read_write_bytes_ += pid_statistics.read_write_bytes_;
Colin Cross646d0012015-09-03 17:56:39 -0700205 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
217TaskStatistics TaskStatistics::Update(const TaskStatistics& new_statistics) {
218 TaskStatistics delta = new_statistics;
Thiébaud Weksteend0212c22020-10-26 13:40:38 +0100219 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_;
Colin Cross646d0012015-09-03 17:56:39 -0700235 delta.cancelled_write_bytes_ -= cancelled_write_bytes_;
236 *this = new_statistics;
237 return delta;
238}