swr/rast: Fix addPassesToEmitFile usage with llvm-7.0.
[mesa.git] / src / gallium / drivers / swr / rasterizer / common / rdtsc_buckets.cpp
1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * @file rdtsc_buckets.cpp
24 *
25 * @brief implementation of rdtsc buckets.
26 *
27 * Notes:
28 *
29 ******************************************************************************/
30 #include "rdtsc_buckets.h"
31 #include <inttypes.h>
32
33 #if defined(_WIN32)
34 #define PATH_SEPARATOR "\\"
35 #elif defined(__unix__) || defined(__APPLE__)
36 #define PATH_SEPARATOR "/"
37 #else
38 #error "Unsupported platform"
39 #endif
40
41 THREAD UINT tlsThreadId = 0;
42
43 BucketManager::~BucketManager()
44 {
45 }
46
47 void BucketManager::RegisterThread(const std::string& name)
48 {
49
50 BUCKET_THREAD newThread;
51 newThread.name = name;
52 newThread.root.children.reserve(mBuckets.size());
53 newThread.root.id = 0;
54 newThread.root.pParent = nullptr;
55 newThread.pCurrent = &newThread.root;
56
57 mThreadMutex.lock();
58
59 // assign unique thread id for this thread
60 size_t id = mThreads.size();
61 newThread.id = (UINT)id;
62 tlsThreadId = (UINT)id;
63
64 // store new thread
65 mThreads.push_back(newThread);
66
67 mThreadMutex.unlock();
68 }
69
70 UINT BucketManager::RegisterBucket(const BUCKET_DESC& desc)
71 {
72 mThreadMutex.lock();
73 size_t id = mBuckets.size();
74 mBuckets.push_back(desc);
75 mThreadMutex.unlock();
76 return (UINT)id;
77 }
78
79 void BucketManager::PrintBucket(
80 FILE* f, UINT level, uint64_t threadCycles, uint64_t parentCycles, const BUCKET& bucket)
81 {
82 const char* arrows[] = {
83 "",
84 "|-> ",
85 " |-> ",
86 " |-> ",
87 " |-> ",
88 " |-> ",
89 " |-> ",
90 " |-> ",
91 " |-> ",
92 };
93
94 // compute percent of total cycles used by this bucket
95 float percentTotal = (float)((double)bucket.elapsed / (double)threadCycles * 100.0);
96
97 // compute percent of parent cycles used by this bucket
98 float percentParent = (float)((double)bucket.elapsed / (double)parentCycles * 100.0);
99
100 // compute average cycle count per invocation
101 uint64_t CPE = bucket.elapsed / bucket.count;
102
103 BUCKET_DESC& desc = mBuckets[bucket.id];
104
105 // construct hierarchy visualization
106 char hier[80];
107 strcpy(hier, arrows[level]);
108 strcat(hier, desc.name.c_str());
109
110 // print out
111 fprintf(f,
112 "%6.2f %6.2f %-10" PRIu64 " %-10" PRIu64 " %-10u %-10lu %-10u %s\n",
113 percentTotal,
114 percentParent,
115 bucket.elapsed,
116 CPE,
117 bucket.count,
118 (unsigned long)0,
119 (uint32_t)0,
120 hier);
121
122 // dump all children of this bucket
123 for (const BUCKET& child : bucket.children)
124 {
125 if (child.count)
126 {
127 PrintBucket(f, level + 1, threadCycles, bucket.elapsed, child);
128 }
129 }
130 }
131
132 void BucketManager::PrintThread(FILE* f, const BUCKET_THREAD& thread)
133 {
134 // print header
135 fprintf(f, "\nThread %u (%s)\n", thread.id, thread.name.c_str());
136 fprintf(f, " %%Tot %%Par Cycles CPE NumEvent CPE2 NumEvent2 Bucket\n");
137
138 // compute thread level total cycle counts across all buckets from root
139 const BUCKET& root = thread.root;
140 uint64_t totalCycles = 0;
141 for (const BUCKET& child : root.children)
142 {
143 totalCycles += child.elapsed;
144 }
145
146 for (const BUCKET& child : root.children)
147 {
148 if (child.count)
149 {
150 PrintBucket(f, 0, totalCycles, totalCycles, child);
151 }
152 }
153 }
154
155 void BucketManager::PrintReport(const std::string& filename)
156 {
157 {
158 FILE* f = fopen(filename.c_str(), "w");
159
160 mThreadMutex.lock();
161 for (const BUCKET_THREAD& thread : mThreads)
162 {
163 PrintThread(f, thread);
164 fprintf(f, "\n");
165 }
166
167 mThreadMutex.unlock();
168
169 fclose(f);
170 }
171 }
172
173
174 void BucketManager::StartCapture()
175 {
176
177 printf("Capture Starting\n");
178
179 mCapturing = true;
180 }
181
182 void BucketManager_StartBucket(BucketManager* pBucketMgr, uint32_t id)
183 {
184 pBucketMgr->StartBucket(id);
185 }
186
187 void BucketManager_StopBucket(BucketManager* pBucketMgr, uint32_t id)
188 {
189 pBucketMgr->StopBucket(id);
190 }