Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: webrtc/base/cpu_time.cc

Issue 2695743003: Added GetCpuTime to base/ to get total CPU time consumed by process for perf tests. (Closed)
Patch Set: implemented Sprang@ comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #if defined(WEBRTC_LINUX)
Taylor Brandstetter 2017/02/16 18:49:19 Should these be "WEBRTC_POSIX" instead?
ilnik 2017/02/17 09:17:10 No. I think POSIX is also defined for MAC, but it
12 #include <time.h>
13 #elif defined(WEBRTC_MAC)
14 #include <sys/resource.h>
15 #include <sys/types.h>
16 #include <sys/times.h>
17 #include <mach/thread_info.h>
18 #include <mach/thread_act.h>
19 #include <mach/mach_init.h>
20 #include <unistd.h>
21 #elif defined(WEBRTC_WIN)
22 #include <windows.h>
23 #endif
24
25 #include "webrtc/base/cpu_time.h"
Taylor Brandstetter 2017/02/16 18:49:19 nit: Usually we put the include for the matching h
ilnik 2017/02/17 09:17:10 Done.
26 #include "webrtc/base/logging.h"
27 #include "webrtc/base/timeutils.h"
28
29 #if defined(WEBRTC_WIN)
30 namespace {
31 // FILETIME resolution is 100 nanosecs.
32 const int64_t kNanosecsPerFiletime = 100;
33 } // namespace
34 #endif
35
36 namespace rtc {
37
38 int64_t GetProcessCpuTime() {
39 #if defined(WEBRTC_LINUX)
40 struct timespec ts;
41 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
42 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
43 } else {
44 LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
45 }
46 #elif defined(WEBRTC_MAC)
47 struct rusage rusage;
48 if (getrusage(RUSAGE_SELF, &rusage) == 0) {
49 return rusage.ru_utime.tv_sec * kNumNanosecsPerSec +
50 rusage.ru_utime.tv_usec * kNumNanosecsPerMicrosec;
51 } else {
52 LOG_ERR(LS_ERROR) << "getrusage() failed.";
53 }
54 #elif defined(WEBRTC_WIN)
55 FILETIME createTime;
56 FILETIME exitTime;
57 FILETIME kernelTime;
58 FILETIME userTime;
59 if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime,
60 &userTime) != 0) {
61 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
62 userTime.dwLowDateTime) *
63 kNanosecsPerFiletime;
64 } else {
65 LOG_ERR(LS_ERROR) << "GetProcessTimes() failed.";
66 }
67 #else
68 RTC_NOT_REACHED();
Taylor Brandstetter 2017/02/16 18:49:19 This will only trigger an error at run time. Shoul
ilnik 2017/02/17 09:17:10 Done.
69 #endif
70 return -1;
71 }
72
73 int64_t GetThreadCpuTime() {
74 #if defined(WEBRTC_LINUX)
75 struct timespec ts;
76 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
77 return ts.tv_sec * kNumNanosecsPerSec + ts.tv_nsec;
78 } else {
79 LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
80 }
81 #elif defined(WEBRTC_MAC)
82 thread_basic_info_data_t info;
83 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
84 if (thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&info,
85 &count) == KERN_SUCCESS) {
86 return info.user_time.seconds * kNumNanosecsPerSec +
87 info.user_time.microseconds * kNumNanosecsPerMicrosec;
88 } else {
89 LOG_ERR(LS_ERROR) << "thread_info() failed.";
90 }
91 #elif defined(WEBRTC_WIN)
92 FILETIME createTime;
93 FILETIME exitTime;
94 FILETIME kernelTime;
95 FILETIME userTime;
96 if (GetThreadTimes(GetCurrentThread(), &createTime, &exitTime, &kernelTime,
97 &userTime) != 0) {
98 return ((static_cast<uint64_t>(userTime.dwHighDateTime) << 32) +
99 userTime.dwLowDateTime) *
100 kNanosecsPerFiletime;
101 } else {
102 LOG_ERR(LS_ERROR) << "GetProcessTimes() failed.";
103 }
104 #else
105 RTC_NOT_REACHED();
106 #endif
107 return -1;
108 }
109
110 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698