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