File: gpu_log_message_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (156 lines) | stat: -rw-r--r-- 5,725 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
// 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.

#include "components/viz/service/gl/gpu_log_message_manager.h"

#include "base/process/process.h"
#include "base/synchronization/waitable_event.h"

namespace viz {

namespace {
bool PreInitializeLogHandler(int severity,
                             const char* file,
                             int line,
                             size_t message_start,
                             const std::string& message) {
  GpuLogMessageManager::GetInstance()->AddDeferredMessage(
      severity, message.substr(0, message_start),
      message.substr(message_start));
  return false;
}

bool PostInitializeLogHandler(int severity,
                              const char* file,
                              int line,
                              size_t message_start,
                              const std::string& message) {
  GpuLogMessageManager::GetInstance()->RouteMessage(
      severity, message.substr(0, message_start),
      message.substr(message_start));
  return false;
}
}  // namespace

GpuLogMessageManager* GpuLogMessageManager::GetInstance() {
  static base::NoDestructor<GpuLogMessageManager> message_manager;
  return message_manager.get();
}

GpuLogMessageManager::GpuLogMessageManager() = default;

void GpuLogMessageManager::AddDeferredMessage(int severity,
                                              const std::string& header,
                                              const std::string& message) {
  base::AutoLock lock(message_lock_);
  deferred_messages_.emplace_back(severity, header, message);
}

void GpuLogMessageManager::RouteMessage(int severity,
                                        const std::string& header,
                                        const std::string& message) {
  // This can be run from any thread, but mojo messages are sent on the IO
  // thread.
  if (io_task_runner_->BelongsToCurrentThread()) {
    if (!gpu_logging_.is_bound()) {
      // |InstallPostInitializeLogHandler| set a new log message handler, which
      // will call |RouteMessage|. When |RouteMessage| handling log message
      // GPULogging may not yet be bound. Cache those log messages until the
      // Remote is bound.
      base::AutoLock lock(message_lock_);
      deferred_messages_.emplace_back(severity, header, message);
      return;
    }

    gpu_logging_->RecordLogMessage(severity, header, message);
  } else {
    // Unretained is safe because |this| is valid until the process exits.
    io_task_runner_->PostTask(
        FROM_HERE, base::BindOnce(&GpuLogMessageManager::RouteMessage,
                                  base::Unretained(this), severity,
                                  std::move(header), std::move(message)));
  }
}

void GpuLogMessageManager::FlushMessages(mojom::GpuLogging* gpu_logging) {
  base::AutoLock lock(message_lock_);
  for (auto& log : deferred_messages_) {
    gpu_logging->RecordLogMessage(log.severity, std::move(log.header),
                                  std::move(log.message));
  }
  deferred_messages_.clear();
}

void GpuLogMessageManager::InstallPreInitializeLogHandler() {
  logging::SetLogMessageHandler(PreInitializeLogHandler);
}

void GpuLogMessageManager::InstallPostInitializeLogHandler(
    mojo::PendingRemote<mojom::GpuLogging> pending_remote,
    scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) {
  Bind(std::move(pending_remote), std::move(io_task_runner));

  logging::SetLogMessageHandler(PostInitializeLogHandler);
}

void GpuLogMessageManager::ShutdownLogging() {
  logging::SetLogMessageHandler(nullptr);

  // |io_task_runner_| may be null if GPULogMessageManager hasn't been bound.
  // Destroy the remote on the IO thread.
  // base::Unretained is safe because this is a singleton.
  if (io_task_runner_) {
    io_task_runner_->PostTask(
        FROM_HERE, base::BindOnce(&GpuLogMessageManager::ResetLoggingOnIOThread,
                                  base::Unretained(this)));
  }
}

void GpuLogMessageManager::Bind(
    mojo::PendingRemote<mojom::GpuLogging> pending_remote,
    scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) {
  DCHECK(!io_task_runner->BelongsToCurrentThread());
  io_task_runner_ = std::move(io_task_runner);

  // base::Unretained is safe because this is a singleton.
  io_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&GpuLogMessageManager::BindOnIOThread,
                     base::Unretained(this), std::move(pending_remote)));
}

void GpuLogMessageManager::BindOnIOThread(
    mojo::PendingRemote<mojom::GpuLogging> pending_remote) {
  DCHECK(io_task_runner_->BelongsToCurrentThread());
  DCHECK(!gpu_logging_.is_bound());
  gpu_logging_.Bind(std::move(pending_remote));

  FlushMessages(gpu_logging_.get());
}

void GpuLogMessageManager::ResetLoggingOnIOThread() {
  DCHECK(io_task_runner_->BelongsToCurrentThread());
  gpu_logging_.reset();
}

void GpuLogMessageManager::TerminateProcess(int exit_code) {
  // Block the calling thread so it doesn't execute any more code before the
  // process exits. This function cannot be called from the IO thread.
  CHECK(!io_task_runner_->BelongsToCurrentThread());
  base::WaitableEvent wait;

  // base::Unretained is safe because this is a singleton.
  io_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&GpuLogMessageManager::TerminateProcessOnIO,
                                base::Unretained(this), exit_code));

  wait.Wait();
}

void GpuLogMessageManager::TerminateProcessOnIO(int exit_code) {
  DCHECK(io_task_runner_->BelongsToCurrentThread());
  base::Process::TerminateCurrentProcessImmediately(exit_code);
}

}  // namespace viz