blob: fcdce342ca96c7bf9512f4fb6a785a307f831a14 [file] [log] [blame]
Po-Hsien Wang00777b22019-04-24 16:37:09 -07001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <getopt.h>
Po-Hsien Wang00777b22019-04-24 16:37:09 -07006#include <unistd.h>
7#include <algorithm>
8#include <iterator>
9#include <map>
10#include <vector>
11
Po-Hsien Wang5372c812020-10-02 09:41:01 +080012#include "clearTest.h"
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070013#include "constant.h"
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080014#include "drawSizeTest.h"
Po-Hsien Wanga956e652020-11-11 16:29:04 -080015#include "filepath.h"
Po-Hsien Wang00777b22019-04-24 16:37:09 -070016#include "submitTest.h"
17#include "utils.h"
18
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070019// g_list determines should we show the test list.
20int g_list = false;
Po-Hsien Wang00777b22019-04-24 16:37:09 -070021// g_iteration determines the total iteration to run for each test
22int g_iteration = 1;
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070023// g_verbose determines the logging level to print into the screen.
Po-Hsien Wang00777b22019-04-24 16:37:09 -070024int g_verbose = false;
25// g_vlayer enables the vulkan verification layer if it is set.
26int g_vlayer = false;
Po-Hsien Wang513fe192020-10-02 11:40:07 +080027// g_hasty enables the hasty mode. Tests would tries to reuse vulkan instance if
28// possible.
29int g_hasty = false;
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070030// g_spirv_dir is the path to the folder contains spirv code for test.
Po-Hsien Wanga956e652020-11-11 16:29:04 -080031FilePath g_spirv_dir = FilePath("shaders");
32// g_out_dir is the path to the folder to store the output image.
33FilePath g_out_dir = FilePath("");
Po-Hsien Wang00777b22019-04-24 16:37:09 -070034
Po-Hsien Wang00777b22019-04-24 16:37:09 -070035// kLongOptions defines the options for argument options.
36const static struct option kLongOptions[] = {
37 {"iterations", required_argument, nullptr, 'i'},
38 {"tests", required_argument, nullptr, 't'},
39 {"blacklist", required_argument, nullptr, 'b'},
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070040 {"spirv_dir", required_argument, nullptr, 's'},
Po-Hsien Wanga956e652020-11-11 16:29:04 -080041 {"out_dir", required_argument, nullptr, 'o'},
Po-Hsien Wang00777b22019-04-24 16:37:09 -070042 {"help", no_argument, nullptr, 'h'},
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070043 {"list", no_argument, &g_list, 1},
Po-Hsien Wang00777b22019-04-24 16:37:09 -070044 {"vlayer", no_argument, &g_vlayer, 1},
45 {"verbose", no_argument, &g_verbose, 1},
Po-Hsien Wang513fe192020-10-02 11:40:07 +080046 {"hasty", no_argument, &g_hasty, 1},
Po-Hsien Wang00777b22019-04-24 16:37:09 -070047 {0, 0, 0, 0}};
48
49// TimeTest times the test by looping it iteration times.
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070050// @param test: test to be excuted.
51// @param iteration: how many times should the test be executed.
Po-Hsien Wang00777b22019-04-24 16:37:09 -070052inline uint64_t TimeTest(vkbench::testBase* test, uint64_t iteration) {
53 uint64_t start = GetUTime();
Po-Hsien Wang00777b22019-04-24 16:37:09 -070054 try {
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070055 test->Setup();
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070056 } catch (const vk::SystemError& err) {
57 LOG("Setup failed: %s", err.what());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080058 test->Cleanup();
59 throw;
Po-Hsien Wang00777b22019-04-24 16:37:09 -070060 }
Po-Hsien Wangaafe13e2020-09-24 14:29:58 +080061 DEFER(test->Cleanup());
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070062
63 try {
64 for (int i = 0; i < iteration; i++) {
65 test->Run();
66 }
67 } catch (const vk::SystemError& err) {
68 LOG("TestRun failed: %s", err.what());
69 throw TEST_FAIL;
Po-Hsien Wang00777b22019-04-24 16:37:09 -070070 }
71 return GetUTime() - start;
72}
73
74// Run the test and pre/post processes.
Po-Hsien Wang00777b22019-04-24 16:37:09 -070075// @param duration_us: The test would to iterate till duration_us is reached.
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070076void Run(vkbench::testBase* test, const uint64_t duration_us = 1000000) {
Po-Hsien Wang00777b22019-04-24 16:37:09 -070077 try {
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070078 test->Initialize();
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070079 } catch (const vk::SystemError& err) {
80 LOG("Test failed to initialize: %s", err.what());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080081 test->Destroy();
82 throw;
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070083 }
84 DEFER(test->Destroy());
85
86 // Do some iterations since initial timings may vary.
87 TimeTest(test, 2);
88 // Target minimum iteration is 1s for each test.
89 uint64_t time = 0;
90 uint64_t iteration = 1;
91 double score = -1.f;
92 do {
93 time = TimeTest(test, iteration);
94 DEBUG("iterations: %llu, time: %llu us, time/iter: %llu us", iteration,
95 time, time / iteration)
96 if (time > duration_us) {
97 score = time / iteration;
98 break;
Po-Hsien Wang00777b22019-04-24 16:37:09 -070099 }
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700100 iteration = iteration * 2;
101 } while ((1ull << 40) > iteration);
102 // Returns 0.0 if it ran max iterations in less than test time.
103 if (score <= 0.01f)
104 LOG("%s: measurement may not be accurate.", test->Name())
105 score = test->FormatMeasurement(score);
106 LOG("@RESULT: %46s = %10.2f %-15s", test->Name(), score, test->Unit());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800107 try {
Po-Hsien Wanga956e652020-11-11 16:29:04 -0800108 test->SaveImage(g_out_dir.Append(FilePath(test->Name())));
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800109 } catch (std::runtime_error err) {
110 DEBUG("Get runtime_error while SaveImage: %s.", err.what());
111 }
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700112}
113
114void PrintHelp() {
115 LOG(R",(
116Usage: vkbench [OPTIONS]
117 -i, --iterations=N Specify the iterations to run the tests.
118 -t, --tests=TESTS Tests to run in colon separated form.
119 -b, --blacklist=TESTS Tests to not run in colon separated form.
120 --list List the tests available.
121 --verbose Show verbose messages.
Po-Hsien Wang513fe192020-10-02 11:40:07 +0800122 --vlayer Enable vulkan verification layer.
Po-Hsien Wang93cc6892020-10-28 17:01:28 -0700123 --hasty Enable hasty mode.
Po-Hsien Wanga956e652020-11-11 16:29:04 -0800124 --spirv_dir Path to SPIRV code for test.(default: shaders/)
125 --out_dir Path to the output directory.),")
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700126}
127
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700128bool prefixFind(std::vector<std::string> list, std::string name) {
129 for (const std::string item : list) {
130 if (name.rfind(item, 0) == 0) {
131 return true;
132 }
133 }
134 return false;
135}
136
137bool ParseArgv(int argc,
138 char** argv,
139 std::vector<vkbench::testBase*>& all_tests) {
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700140 std::vector<std::string> enabled_tests;
141 std::vector<std::string> disabled_tests;
142 int c;
143 int option_index = 0;
144 while ((c = getopt_long(argc, argv, "i:t:b:", kLongOptions, &option_index)) !=
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700145 -1) {
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700146 if (c == 'i') {
147 g_iteration = atoi(optarg);
148 } else if (c == 't') {
149 enabled_tests = SplitString(std::string(optarg), ':');
150 } else if (c == 'b') {
151 disabled_tests = SplitString(std::string(optarg), ':');
Po-Hsien Wang93cc6892020-10-28 17:01:28 -0700152 } else if (c == 's') {
Po-Hsien Wanga956e652020-11-11 16:29:04 -0800153 g_spirv_dir = FilePath(optarg);
154 } else if (c == 'o') {
155 g_out_dir = FilePath(optarg);
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700156 } else if (c == '?' || c == 'h') {
157 PrintHelp();
158 return false;
159 }
160 }
161
162 if (optind < argc) {
163 ERROR("Unknown argv: ")
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700164 while (optind < argc)
165 ERROR("%s ", argv[optind++])
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700166 return false;
167 }
168
169 all_tests.erase(
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700170 remove_if(all_tests.begin(), all_tests.end(),
171 [enabled_tests, disabled_tests](const vkbench::testBase* test) {
172 bool should_run = enabled_tests.empty() ||
173 prefixFind(enabled_tests, test->Name());
174 should_run &= !prefixFind(disabled_tests, test->Name());
175 if (!should_run)
176 delete test;
177 return !should_run;
178 }),
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700179 all_tests.end());
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700180
Po-Hsien Wang93cc6892020-10-28 17:01:28 -0700181 if (g_list) {
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700182 for (const auto& test : all_tests) {
183 LOG("%s: %s", test->Name(), test->Desp())
184 }
185 return false;
186 }
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700187 return true;
188}
189
190int main(int argc, char* argv[]) {
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700191 vkbench::vkBase simpleVulkan;
192 // all_tests list all the available tests.
193 std::vector<vkbench::testBase*> all_tests = {
194 new vkbench::SubmitTest(10, &simpleVulkan),
195 new vkbench::SubmitTest(100, &simpleVulkan),
196 new vkbench::SubmitTest(1000, &simpleVulkan),
197 new vkbench::SubmitTest(10000, &simpleVulkan),
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800198 new vkbench::SubmitTest(100000, &simpleVulkan),
199 new vkbench::DrawSizeTest(16, &simpleVulkan),
200 new vkbench::DrawSizeTest(64, &simpleVulkan),
201 new vkbench::DrawSizeTest(128, &simpleVulkan),
202 new vkbench::DrawSizeTest(512, &simpleVulkan),
203 new vkbench::DrawSizeTest(1024, &simpleVulkan),
Po-Hsien Wang5372c812020-10-02 09:41:01 +0800204 new vkbench::ClearTest(&simpleVulkan),
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700205 };
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700206
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800207 // Sort to bundle tests using same vulkan instance together.
208 std::stable_sort(all_tests.begin(), all_tests.end(),
209 [](vkbench::testBase* a, vkbench::testBase* b) -> bool {
210 return a->vk < b->vk;
211 });
212
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700213 if (!ParseArgv(argc, argv, all_tests)) {
214 return 0;
215 }
216
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700217 std::map<const char*, int> failed_test;
218 LOG("@TEST_BEGIN")
219 PrintDateTime();
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700220 for (auto i = 0; i < all_tests.size(); i++) {
221 auto& test = all_tests[i];
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800222 for (auto iter = 0; iter < g_iteration; iter++) {
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700223 try {
Po-Hsien Wang513fe192020-10-02 11:40:07 +0800224 if (!test->vk->IsInitialized())
225 test->vk->Initialize();
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700226 Run(test, 1000000);
Po-Hsien Wang513fe192020-10-02 11:40:07 +0800227 if (!g_hasty)
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700228 test->vk->Destroy();
229 } catch (const ERROR_TYPE& type) {
230 switch (type) {
231 case TEST_PASS:
232 break;
233 case TEST_FAIL:
234 failed_test[test->Name()] += 1;
235 break;
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700236 default:
237 ERROR("Unimplemented error type");
238 throw;
239 }
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800240 } catch (const std::runtime_error error) {
241 failed_test[test->Name()] += 1;
242 LOG("Runtime Error: %s", error.what());
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700243 }
244 }
Po-Hsien Wang513fe192020-10-02 11:40:07 +0800245
246 // keep test->vk initialized for the next test.
247 if (g_hasty && test->vk->IsInitialized()) {
248 if (i + 1 >= all_tests.size() || test->vk != all_tests[i + 1]->vk) {
249 test->vk->Destroy();
250 }
251 }
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700252 }
253 PrintDateTime();
254 LOG("@TEST_END")
255
256 for (auto& keyval : failed_test) {
257 LOG("%s failed %d times.", keyval.first, keyval.second)
258 }
259
260 for (auto& test : all_tests) {
261 delete test;
262 }
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700263}