brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
4 / 34
11.8%
Branches:
3 / 42
7.1%
Functions:
1 / 5
20.0%

src/profile.cpp
Line Branch Exec Source
1 #include <brotensor/profile.h>
2
3 #include "profile_report.h"
4
5 #include <cstdio>
6 #include <cstdlib>
7 #include <fstream>
8 #include <string>
9
10 #if defined(BROTENSOR_HAS_CUDA)
11 #include "cuda/cuda_profile.h"
12 #endif
13 #if defined(BROTENSOR_HAS_VULKAN)
14 #include "vulkan/vk_profile.h"
15 #endif
16 #if defined(BROTENSOR_HAS_METAL)
17 #include "metal/mtl_profile.h"
18 #endif
19
20 namespace brotensor {
21
22 ✗ void profile_begin() {
23 #if defined(BROTENSOR_HAS_CUDA)
24 detail::cuda::prof::session_begin();
25 #endif
26 #if defined(BROTENSOR_HAS_VULKAN)
27 detail::vulkan::prof::session_begin();
28 #endif
29 #if defined(BROTENSOR_HAS_METAL)
30 ✗ detail::metal::prof::session_begin();
31 #endif
32 ✗ }
33
34 ✗ std::string profile_end(int iterations) {
35 ✗ std::string out;
36
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ detail::prof::appendf(out, "brotensor profile (per iteration, %d iteration%s)\n", iterations,
37 ✗ iterations == 1 ? "" : "s");
38 ✗ std::string body;
39 #if defined(BROTENSOR_HAS_CUDA)
40 body += detail::cuda::prof::session_end(iterations);
41 #endif
42 #if defined(BROTENSOR_HAS_VULKAN)
43 body += detail::vulkan::prof::session_end(iterations);
44 #endif
45 #if defined(BROTENSOR_HAS_METAL)
46
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ body += detail::metal::prof::session_end(iterations);
47 #endif
48
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ out += body.empty() ? std::string(" no GPU work was recorded\n") : body;
49 ✗ return out;
50 ✗ }
51
52 namespace detail {
53
54 namespace {
55
56 ✗ std::string& env_target() {
57
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ static std::string t;
58 ✗ return t;
59 }
60
61 ✗ void report_at_exit() {
62 ✗ const std::string report = profile_end(1);
63 ✗ const std::string& target = env_target();
64
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (target == "1") {
65
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ std::fputs(report.c_str(), stderr);
66 ✗ return;
67 }
68
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ std::ofstream f(target, std::ios::binary);
69
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (f) {
70
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ f << report;
71 ✗ } else {
72
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ std::fputs(report.c_str(), stderr);
73 }
74 ✗ }
75
76 } // namespace
77
78 // BROTENSOR_PROFILE=1 profiles the whole process from init() and prints the
79 // report to stderr at exit; any other value (but 0) is a file to write it to.
80 172 void profile_from_env() {
81 172 const char* v = std::getenv("BROTENSOR_PROFILE");
82
3/8
✗ Branch 0 not taken.
✓ Branch 1 taken 172 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 172 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 172 times.
✗ Branch 7 not taken.
172 if (!v || !*v || std::string(v) == "0") return;
83 ✗ env_target() = v;
84 ✗ profile_begin();
85 ✗ std::atexit(report_at_exit);
86 172 }
87
88 } // namespace detail
89
90 } // namespace brotensor
91