File: cpu_usage_sampler.cc

package info (click to toggle)
android-platform-tools-base 2.2.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 113,928 kB
  • sloc: java: 696,396; xml: 45,920; cpp: 2,526; ansic: 1,432; sh: 508; lisp: 110; javascript: 108; makefile: 17
file content (238 lines) | stat: -rw-r--r-- 8,680 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright (C) 2016 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 "cpu_usage_sampler.h"

#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <unistd.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <mutex>
#include <sstream>  // for std::ostringstream
#include <string>
#include <vector>

#include "cpu/cpu_cache.h"
#include "proto/cpu_profiler_data.pb.h"
#include "proto/profiler.pb.h"
#include "utils/file_reader.h"
#include "utils/token.h"

using profiler::proto::CpuProfilerData;
using profiler::proto::CpuStartResponse;
using profiler::proto::CpuStopResponse;
using profiler::proto::CpuUsageData;
using profiler::proto::ProfilerData;
using profiler::FileReader;
using std::string;
using std::vector;

namespace {
// Entity knowing the time unit (used by /proc/* files) in milliseconds.
class TimeUnitInMillis {
 public:
  TimeUnitInMillis() : time_unit_in_millis_(-1) {
    int64_t user_hz = sysconf(_SC_CLK_TCK);
    // TODO: Handle other USER_HZ values.
    if (user_hz == 100) {
      time_unit_in_millis_ = 10;
    } else if (user_hz == 1000) {
      time_unit_in_millis_ = 1;
    }
  }

  // Returns the operating system's time unit in milliseconds.
  int64_t get() const { return time_unit_in_millis_; }

 private:
  int64_t time_unit_in_millis_;
};
const TimeUnitInMillis time_unit_in_millis;

const char* const proc_stat_filename = "/proc/stat";

// Reads /proc/stat file. Returns true on success.
// TODO: Mock this file on non-Linux platforms.
bool ReadProcStat(string* content) {
  return FileReader::Read(proc_stat_filename, content);
}

// Parses /proc/stat content in |content| and calculates
// |system_cpu_time_in_millisec| and |elapsed_time_in_millisec|. Returns true
// on success.
//
// |elapsed_time_in_millisec| is the combination of every
// state; while |system_cpu_time_in_millisec| is anything but 'idle'.
//
// Only the first line of /proc/stat is used.
// See more details at http://man7.org/linux/man-pages/man5/proc.5.html.
bool ParseProcStatForUsageData(const string& content, CpuUsageData* data) {
  int64_t user, nice, system, idle, iowait, irq, softirq, steal, guest,
      guest_nice;
  // TODO: figure out why sscanf_s cannot compile.
  if (sscanf(content.c_str(),
             "cpu  %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64
             " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
             &user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal,
             &guest, &guest_nice) == 10) {
    int64_t load = user + nice + system + iowait + irq + softirq + steal +
                   guest + guest_nice;
    data->set_system_cpu_time_in_millisec(load * time_unit_in_millis.get());
    int64_t elapsed = load + idle;
    data->set_elapsed_time_in_millisec(elapsed * time_unit_in_millis.get());
    return true;
  }
  return false;
}

// Collects system-wide data by reading /proc/stat. Returns true on success.
bool CollectSystemUsageData(CpuUsageData* data) {
  string buffer;
  if (ReadProcStat(&buffer)) {
    return ParseProcStatForUsageData(buffer, data);
  }
  return false;
}

// Reads /proc/[pid]/stat file. Returns true on success.
// TODO: Mock this file on non-Linux platforms.
bool ReadProcPidStat(int32_t pid, std::string* content) {
  // TODO: Use std::to_string() after we use libc++. NDK doesn't support itoa().
  std::ostringstream os;
  os << "/proc/" << pid << "/stat";
  return FileReader::Read(os.str(), content);
}

// Parses a process's stat file (proc/[pid]/stat) to collect info. Returns
// true on success.
// The file has only one line, including a number of fields. The fields are
// numbered from 1. A process usage is the sum of the following fields.
//    (14) utime  %lu
//    (15) stime  %lu
//    (16) cutime  %ld
//    (17) cstime  %ld
//
// The following fields are read, although they are not part of usage.
//    (1) pid  %d       -- Used by this function for sanity check.
//    (2) comm  %s      -- Used to map fields to tokens.
//
// The following fields are part of usage, but they are included by utime
// and cutime, respectively. Therefore, they are not read.
//    (43) guest_time  %lu  (since Linux 2.6.24)
//    (44) cguest_time  %ld  (since Linux 2.6.24)
// See more details at http://man7.org/linux/man-pages/man5/proc.5.html.
bool ParseProcPidStatForUsageData(int32_t pid, const string& content,
                                  CpuUsageData* data) {
  // Find the start and end positions of the second field.
  // The number of words in the file is variable. The second field is the
  // file name of the executable, in parentheses. The file name could include
  // spaces, so if we blindly split the entire line, it would be hard to map
  // words to fields.
  size_t left_parentheses = content.find_first_of('(');
  size_t right_parentheses = content.find_first_of(')');
  if (left_parentheses == string::npos || right_parentheses == string::npos ||
      right_parentheses <= left_parentheses || left_parentheses == 0)
    return false;

  // Sanity check on pid.
  // TODO: Use std::stoi() after we use libc++, and remove '.c_str()'.
  int32_t pid_from_file = atoi(content.substr(0, left_parentheses - 1).c_str());
  if (pid_from_file != pid) return false;

  // Each token after the right parenthesis is a field, either a charactor or a
  // number. The first token is field #3.
  vector<string> tokens =
      profiler::GetTokens(content.substr(right_parentheses + 1), " \n");
  if (tokens.size() >= 15) {
    // TODO: Use std::stoll() after we use libc++, and remove '.c_str()'.
    int64_t utime = atol(tokens[11].c_str());
    int64_t stime = atol(tokens[12].c_str());
    int64_t cutime = atol(tokens[13].c_str());
    int64_t cstime = atol(tokens[14].c_str());
    int64_t usage_in_time_units = utime + stime + cutime + cstime;
    data->set_app_cpu_time_in_millisec(usage_in_time_units *
                                       time_unit_in_millis.get());
    return true;
  }
  return false;
}

// TODO:
// Parses a thread's stat file (proc/[pid]/task/[tid]/stat) to collect info.
// Returns true on success.
// For a thread, the following fields are read (the first field is numbered as
// 1).
//    (1) id  %d                      => For sanity checking.
//    (2) comm  %s (in parentheses)   => Output |name|.
//    (3) state  %c                   => Output |state|.

bool CollectProcessUsageData(int32_t pid, CpuUsageData* data) {
  string buffer;
  if (ReadProcPidStat(pid, &buffer)) {
    return ParseProcPidStatForUsageData(pid, buffer, data);
  }
  return false;
}

}  // namespace

namespace profiler {

CpuStartResponse::Status CpuUsageSampler::AddProcess(int32_t pid) {
  std::lock_guard<std::mutex> lock(pids_mutex_);
  pids_.insert(pid);
  return CpuStartResponse::SUCCESS;
}

CpuStopResponse::Status CpuUsageSampler::RemoveProcess(int32_t pid) {
  std::lock_guard<std::mutex> lock(pids_mutex_);
  pids_.erase(pid);
  return CpuStopResponse::SUCCESS;
}

bool CpuUsageSampler::Sample() {
  std::unordered_set<int32_t> pids;
  {
    // Make a copy of all processes that need a sample. We want to be
    // thread-safe, and we don't want to hold the lock for too long.
    std::lock_guard<std::mutex> lock(pids_mutex_);
    pids = pids_;
  }
  bool all_succeeded = true;
  for (const int32_t pid : pids) {
    bool process_succeeded = SampleAProcess(pid);
    if (!process_succeeded) all_succeeded = false;
  }
  return all_succeeded;
}

// We sample system-wide usage data each time when we sample a process's usage
// data. This is not a waste. It takes non-trial amount of time to sample
// a process's usage data (> 1 millisecond), and therefore it is better to get
// the up-to-date system-wide data each time.
bool CpuUsageSampler::SampleAProcess(int32_t pid) {
  CpuProfilerData data;
  if (!CollectSystemUsageData(data.mutable_cpu_usage())) return false;
  if (!CollectProcessUsageData(pid, data.mutable_cpu_usage())) return false;
  data.mutable_basic_info()->set_app_id(pid);
  data.mutable_basic_info()->set_end_timestamp(clock_.GetCurrentTime());
  cache_.Add(data);
  return true;
}

}  // namespace profiler