1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "test/cpp/qps/usage_timer.h"
20
21 #include <fstream>
22 #include <sstream>
23 #include <string>
24
25 #include <grpc/support/log.h>
26 #include <grpc/support/time.h>
27
28 #include "src/core/lib/gprpp/crash.h"
29 #ifdef __linux__
30 #include <sys/resource.h>
31 #include <sys/time.h>
32
time_double(struct timeval * tv)33 static double time_double(struct timeval* tv) {
34 return tv->tv_sec + 1e-6 * tv->tv_usec;
35 }
36 #endif
37
UsageTimer()38 UsageTimer::UsageTimer() : start_(Sample()) {}
39
Now()40 double UsageTimer::Now() {
41 auto ts = gpr_now(GPR_CLOCK_REALTIME);
42 return ts.tv_sec + 1e-9 * ts.tv_nsec;
43 }
44
get_resource_usage(double * utime,double * stime)45 static void get_resource_usage(double* utime, double* stime) {
46 #ifdef __linux__
47 struct rusage usage;
48 getrusage(RUSAGE_SELF, &usage);
49 *utime = time_double(&usage.ru_utime);
50 *stime = time_double(&usage.ru_stime);
51 #else
52 *utime = 0;
53 *stime = 0;
54 #endif
55 }
56
get_cpu_usage(unsigned long long * total_cpu_time,unsigned long long * idle_cpu_time)57 static void get_cpu_usage(unsigned long long* total_cpu_time,
58 unsigned long long* idle_cpu_time) {
59 #ifdef __linux__
60 std::ifstream proc_stat("/proc/stat");
61 proc_stat.ignore(5);
62 std::string cpu_time_str;
63 std::string first_line;
64 std::getline(proc_stat, first_line);
65 std::stringstream first_line_s(first_line);
66 for (int i = 0; i < 10; ++i) {
67 std::getline(first_line_s, cpu_time_str, ' ');
68 *total_cpu_time += std::stol(cpu_time_str);
69 if (i == 3) {
70 *idle_cpu_time = std::stol(cpu_time_str);
71 }
72 }
73 #else
74 // Use the parameters to avoid unused-parameter warning
75 (void)total_cpu_time;
76 (void)idle_cpu_time;
77 gpr_log(GPR_INFO, "get_cpu_usage(): Non-linux platform is not supported.");
78 #endif
79 }
80
Sample()81 UsageTimer::Result UsageTimer::Sample() {
82 Result r;
83 r.wall = Now();
84 get_resource_usage(&r.user, &r.system);
85 r.total_cpu_time = 0;
86 r.idle_cpu_time = 0;
87 get_cpu_usage(&r.total_cpu_time, &r.idle_cpu_time);
88 return r;
89 }
90
Mark() const91 UsageTimer::Result UsageTimer::Mark() const {
92 Result s = Sample();
93 Result r;
94 r.wall = s.wall - start_.wall;
95 r.user = s.user - start_.user;
96 r.system = s.system - start_.system;
97 r.total_cpu_time = s.total_cpu_time - start_.total_cpu_time;
98 r.idle_cpu_time = s.idle_cpu_time - start_.idle_cpu_time;
99
100 return r;
101 }
102