Blender V5.0
gpu_profile_report.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "BLI_map.hh"
8#include "BLI_mutex.hh"
9#include "BLI_string_ref.hh"
10
11#include <fmt/format.h>
12#include <fstream>
13#include <iostream>
14#include <mutex>
15#include <sstream>
16#include <thread>
17
18namespace blender::gpu {
19
20class ProfileReport {
21 private:
22 std::fstream _report;
23 Mutex _mutex;
24 Map<uint64_t, int> _thread_ids;
25
26 ProfileReport()
27 {
28 _report.open("profile.json", std::ios::out);
29 _report << R"([{"name":"process_name","ph":"M","pid":1,"args":{"name":"GPU"}})"
30 ",\n";
31 _report << R"({"name":"process_name","ph":"M","pid":2,"args":{"name":"CPU"}})";
32 }
33
34 ~ProfileReport()
35 {
36 _report << "\n]\n";
37 _report.close();
38 }
39
40 public:
41 static ProfileReport &get()
42 {
43 static ProfileReport singleton;
44 return singleton;
45 }
46
48 uint64_t gpu_start,
49 uint64_t gpu_end,
50 uint64_t cpu_start,
51 uint64_t cpu_end)
52 {
53 std::scoped_lock lock(_mutex);
54
55 size_t thread_hash = std::hash<std::thread::id>()(std::this_thread::get_id());
56 int thread_id = _thread_ids.lookup_or_add(thread_hash, _thread_ids.size());
57
58 _report << fmt::format(
59 ",\n"
60 R"({{"name":"{}","ph":"X","ts":{},"dur":{},"pid":1,"tid":{}}})",
61 name.c_str(),
62 gpu_start / uint64_t(1000),
63 (gpu_end - gpu_start) / uint64_t(1000),
64 thread_id);
65
66 _report << fmt::format(
67 ",\n"
68 R"({{"name":"{}","ph":"X","ts":{},"dur":{},"pid":2,"tid":{}}})",
69 name.c_str(),
70 cpu_start / uint64_t(1000),
71 (cpu_end - cpu_start) / uint64_t(1000),
72 thread_id);
73 }
74
76 {
77 std::scoped_lock lock(_mutex);
78
79 size_t thread_hash = std::hash<std::thread::id>()(std::this_thread::get_id());
80 int thread_id = _thread_ids.lookup_or_add(thread_hash, _thread_ids.size());
81
82 _report << fmt::format(
83 ",\n"
84 R"({{"name":"{}","ph":"X","ts":{},"dur":{},"pid":2,"tid":{}}})",
85 name.c_str(),
86 cpu_start / uint64_t(1000),
87 (cpu_end - cpu_start) / uint64_t(1000),
88 thread_id);
89 }
90};
91
92} // namespace blender::gpu
volatile int lock
unsigned long long int uint64_t
static ProfileReport & get()
void add_group(StringRefNull name, uint64_t gpu_start, uint64_t gpu_end, uint64_t cpu_start, uint64_t cpu_end)
void add_group_cpu(StringRefNull name, uint64_t cpu_start, uint64_t cpu_end)
std::mutex Mutex
Definition BLI_mutex.hh:47
const char * name