File: platform_thread_metrics.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (114 lines) | stat: -rw-r--r-- 4,445 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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_THREADING_PLATFORM_THREAD_METRICS_H_
#define BASE_THREADING_PLATFORM_THREAD_METRICS_H_

#include <memory>
#include <optional>

#include "base/base_export.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_WIN)
#include "base/win/scoped_handle.h"
#endif

namespace base {

// Used to log metrics about a specific thread. Modeled on ProcessMetrics.
//
// On Android and Fuchsia the thread must belong to the current process. (This
// is due to the way Chrome spawns processes on Android, so uses of
// PlatformThreadMetrics outside of Chrome might not have this restriction.)
//
// CPU usage of the current thread can also be measured with base::ThreadTicks.
// However on most platforms ThreadTicks can't measure other threads.
//
// Depending on platform, measuring CPU usage needs either a thread ID (TID) or
// a thread handle:
//
// * Apple needs a thread handle, and there's no obvious way to convert a TID to
//   a handle, so it only supports CreateFromHandle().
// * Windows needs a thread handle, but it's possible to convert back and forth
//   between TID's and handles, so it supports both CreateFromHandle() and
//   CreateFromId().
// * Every other platform needs a TID, and there's no obvious way to convert a
//   handle to a TID, so they only support CreateFromId().
//
// TODO(crbug.com/420681350): There might be ways to support CreateFromHandle()
// and threads in other processes on Fuchsia. Improve the Fuchsia implementation
// if there's any demand.
class BASE_EXPORT PlatformThreadMetrics {
 public:
  static std::unique_ptr<PlatformThreadMetrics> CreateForCurrentThread();

#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN)
  static std::unique_ptr<PlatformThreadMetrics> CreateFromHandle(
      PlatformThreadHandle handle);
#endif

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) || \
    BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN)
  static std::unique_ptr<PlatformThreadMetrics> CreateFromId(
      PlatformThreadId tid);
#endif

  PlatformThreadMetrics(const PlatformThreadMetrics&) = delete;
  PlatformThreadMetrics& operator=(const PlatformThreadMetrics&) = delete;

  ~PlatformThreadMetrics();

  // Returns the cumulative CPU usage of the thread since process start, or
  // nullopt on error. Unlike ProcessMetrics::GetCumulativeCPUUsage() this
  // doesn't measure multiple concurrent threads, so it shouldn't return more
  // than 1 second of CPU utilization per 1 wall-clock second. However due to
  // rounding it may report slightly higher values under heavy load.
  std::optional<TimeDelta> GetCumulativeCPUUsage();

  // Returns the percentage of time spent executing the thread in the interval
  // since the last time the method was called, using the current
  // `cumulative_cpu`. Equivalent to ProcessMetrics::
  // GetPlatformIndependentCPUUsage(), but this doesn't measure multiple
  // concurrent threads, so it should always return a value in the range 0% to
  // 100%. However due to rounding it may report slightly more than 100% under
  // heavy load.
  //
  // Since this API measures usage over an interval, it will return zero on the
  // first call, and an actual value only on the second and subsequent calls.
  double GetCPUUsageProportion(TimeDelta cumulative_cpu);

  // Same as the above, but automatically calls GetCumulativeCPUUsage() to
  // determine the current cumulative CPU. Returns nullopt if
  // GetCumulativeCPUUsage() fails.
  std::optional<double> GetCPUUsageProportion();

 private:
#if BUILDFLAG(IS_APPLE)
  explicit PlatformThreadMetrics(PlatformThreadHandle handle)
      : handle_(handle) {}

  PlatformThreadHandle handle_;
#elif BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) || \
    BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_LINUX)
  explicit PlatformThreadMetrics(PlatformThreadId tid) : tid_(tid) {}

  PlatformThreadId tid_;
#elif BUILDFLAG(IS_WIN)
  explicit PlatformThreadMetrics(win::ScopedHandle&& handle);

  win::ScopedHandle handle_;
#endif

  // Previous measurements used to compute the CPU usage between calls to
  // GetCPUUsageProportion().
  std::optional<TimeTicks> last_cpu_time_;
  TimeDelta last_cumulative_cpu_;
};

}  // namespace base

#endif  // BASE_THREADING_PLATFORM_THREAD_METRICS_H_