1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <fcntl.h>
18 #include <inttypes.h>
19 #include <stdint.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <time.h>
23 #include <unistd.h>
24
25 #include <string>
26
27 #include "perfetto/base/logging.h"
28 #include "perfetto/base/time.h"
29 #include "perfetto/ext/base/file_utils.h"
30 #include "perfetto/ext/base/getopt.h"
31 #include "perfetto/ext/base/scoped_file.h"
32
33 // Periodically prints an un-normalized cpu usage ratio (full use of a single
34 // core = 1.0) of a target process. Based on /proc/pid/stat utime (userspace) &
35 // stime (kernel space).
36
37 namespace perfetto {
38 namespace {
39
TimespecToMs(struct timespec ts)40 uint64_t TimespecToMs(struct timespec ts) {
41 PERFETTO_CHECK(ts.tv_sec >= 0 && ts.tv_nsec >= 0);
42 return static_cast<uint64_t>(ts.tv_sec) * 1000 +
43 static_cast<uint64_t>(ts.tv_nsec) / 1000 / 1000;
44 }
45
ReadWallTimeMs(clockid_t clk)46 uint64_t ReadWallTimeMs(clockid_t clk) {
47 struct timespec ts = {};
48 PERFETTO_CHECK(clock_gettime(clk, &ts) == 0);
49 return TimespecToMs(ts);
50 }
51
ReadUtimeStime(const base::ScopedFile & stat_fd,unsigned long * utime_out,unsigned long * stime_out)52 void ReadUtimeStime(const base::ScopedFile& stat_fd,
53 unsigned long* utime_out,
54 unsigned long* stime_out) {
55 char buf[1024] = {};
56 lseek(stat_fd.get(), 0, SEEK_SET);
57 PERFETTO_CHECK(read(stat_fd.get(), buf, sizeof(buf)) > 0);
58 buf[sizeof(buf) - 1] = '\0';
59
60 PERFETTO_CHECK(
61 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu",
62 utime_out, stime_out) == 2);
63 }
64
CpuUtilizationMain(int argc,char ** argv)65 int CpuUtilizationMain(int argc, char** argv) {
66 unsigned sleep_duration_us = 10 * 1000 * 1000; // 10s
67 int sleep_intervals = 6;
68 int target_pid = -1;
69
70 static option long_options[] = {
71 {"pid", required_argument, nullptr, 'p'},
72 {"sleep-duration-us", required_argument, nullptr, 't'},
73 {"sleep-intervals", required_argument, nullptr, 'n'},
74 {nullptr, 0, nullptr, 0}};
75 int c;
76 while ((c = getopt_long(argc, argv, "", long_options, nullptr)) != -1) {
77 switch (c) {
78 case 'p':
79 target_pid = atoi(optarg);
80 break;
81 case 't':
82 sleep_duration_us = static_cast<unsigned>(atoi(optarg));
83 break;
84 case 'n':
85 sleep_intervals = atoi(optarg);
86 break;
87 default:
88 break;
89 }
90 }
91
92 if (target_pid < 1) {
93 PERFETTO_ELOG(
94 "Usage: %s --pid=target_pid [--sleep-duration-us=N] "
95 "[--sleep-intervals=N]",
96 argv[0]);
97 return 1;
98 }
99
100 // Resolution of utime/stime from procfs, at least 10 ms.
101 long ticks = sysconf(_SC_CLK_TCK);
102 PERFETTO_CHECK(ticks >= 100);
103 unsigned long ticks_per_s = static_cast<unsigned long>(ticks);
104
105 // Resolution of wallclock time, at least 1 ms. Should be O(ns) in practice.
106 auto clk = CLOCK_MONOTONIC_RAW;
107 struct timespec ts = {};
108 PERFETTO_CHECK(clock_getres(clk, &ts) == 0);
109 PERFETTO_CHECK(ts.tv_sec == 0 && ts.tv_nsec <= 1 * 1000 * 1000);
110
111 PERFETTO_LOG("--- setup: ---");
112 PERFETTO_LOG("target pid: %d", target_pid);
113 PERFETTO_LOG("intervals: %d x %uus", sleep_intervals, sleep_duration_us);
114 PERFETTO_LOG("utime/stime ticks per sec: %ld", ticks_per_s);
115 PERFETTO_LOG("wall clock resolution (ns): %ld", ts.tv_nsec);
116 PERFETTO_LOG("--- timings: ---");
117
118 base::ScopedFile fd = base::OpenFile(
119 std::string("/proc/") + std::to_string(target_pid) + std::string("/stat"),
120 O_RDONLY);
121 PERFETTO_CHECK(fd);
122
123 // Read the base times.
124 unsigned long first_utime = 0;
125 unsigned long first_stime = 0;
126 ReadUtimeStime(fd, &first_utime, &first_stime);
127 uint64_t first_walltime_ms = ReadWallTimeMs(clk);
128
129 uint64_t last_walltime_ms = first_walltime_ms;
130 unsigned long last_utime = first_utime;
131 unsigned long last_stime = first_stime;
132
133 // Report the utilization for each fixed duration chunk.
134 for (int i = 0; i < sleep_intervals; i++) {
135 base::SleepMicroseconds(sleep_duration_us);
136
137 unsigned long utime = 0;
138 unsigned long stime = 0;
139 ReadUtimeStime(fd, &utime, &stime);
140 uint64_t walltime_ms = ReadWallTimeMs(clk);
141
142 uint64_t wall_diff_ms = walltime_ms - last_walltime_ms;
143 PERFETTO_LOG("wall_ms : [%" PRIu64 "] - [%" PRIu64 "] = [%" PRIu64 "]",
144 walltime_ms, last_walltime_ms, wall_diff_ms);
145
146 unsigned long utime_diff = utime - last_utime;
147 unsigned long stime_diff = stime - last_stime;
148 PERFETTO_LOG("utime_ticks: [%lu] - [%lu] = [%lu]", utime, last_utime,
149 utime_diff);
150 PERFETTO_LOG("stime_ticks: [%lu] - [%lu] = [%lu]", stime, last_stime,
151 stime_diff);
152
153 // Calculate the utilization, resolution of inputs will be no worse than
154 // 10ms due to the above assert. At the default 10s wall time, we therefore
155 // get a resolution of at least 0.1%.
156 double utime_diff_ms = static_cast<double>(utime_diff * 1000 / ticks_per_s);
157 double stime_diff_ms = static_cast<double>(stime_diff * 1000 / ticks_per_s);
158
159 double utime_ratio = utime_diff_ms / static_cast<double>(wall_diff_ms);
160 double stime_ratio = stime_diff_ms / static_cast<double>(wall_diff_ms);
161
162 PERFETTO_LOG("utime ratio : %f", utime_ratio);
163 PERFETTO_LOG("stime ratio : %f", stime_ratio);
164 PERFETTO_LOG("combined ratio: %f\n", utime_ratio + stime_ratio);
165
166 last_walltime_ms = walltime_ms;
167 last_utime = utime;
168 last_stime = stime;
169 }
170
171 PERFETTO_LOG("--- timings over the whole period: ---");
172 unsigned long utime_diff = last_utime - first_utime;
173 unsigned long stime_diff = last_stime - first_stime;
174 uint64_t wall_diff_ms = last_walltime_ms - first_walltime_ms;
175 double utime_diff_ms = static_cast<double>(utime_diff * 1000 / ticks_per_s);
176 double stime_diff_ms = static_cast<double>(stime_diff * 1000 / ticks_per_s);
177 double utime_ratio = utime_diff_ms / static_cast<double>(wall_diff_ms);
178 double stime_ratio = stime_diff_ms / static_cast<double>(wall_diff_ms);
179 PERFETTO_LOG("utime ratio : %f", utime_ratio);
180 PERFETTO_LOG("stime ratio : %f", stime_ratio);
181 PERFETTO_LOG("combined ratio: %f\n", utime_ratio + stime_ratio);
182
183 return 0;
184 }
185
186 } // namespace
187 } // namespace perfetto
188
main(int argc,char ** argv)189 int main(int argc, char** argv) {
190 return perfetto::CpuUtilizationMain(argc, argv);
191 }
192