blob: 84b509af3ac719829e877574979800c6b79bd31c [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 Wanga956e652020-11-11 16:29:04 -080012#include "filepath.h"
Po-Hsien Wang6765a852020-11-19 17:48:29 -080013#include "tests/clearTest.h"
14#include "tests/drawSizeTest.h"
15#include "tests/submitTest.h"
Po-Hsien Wang00777b22019-04-24 16:37:09 -070016#include "utils.h"
17
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070018// g_list determines should we show the test list.
19int g_list = false;
Po-Hsien Wang00777b22019-04-24 16:37:09 -070020// g_iteration determines the total iteration to run for each test
21int g_iteration = 1;
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070022// g_verbose determines the logging level to print into the screen.
Po-Hsien Wang00777b22019-04-24 16:37:09 -070023int g_verbose = false;
24// g_vlayer enables the vulkan verification layer if it is set.
25int g_vlayer = false;
Po-Hsien Wang513fe192020-10-02 11:40:07 +080026// g_hasty enables the hasty mode. Tests would tries to reuse vulkan instance if
27// possible.
28int g_hasty = false;
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070029// g_spirv_dir is the path to the folder contains spirv code for test.
Po-Hsien Wanga956e652020-11-11 16:29:04 -080030FilePath g_spirv_dir = FilePath("shaders");
31// g_out_dir is the path to the folder to store the output image.
32FilePath g_out_dir = FilePath("");
Po-Hsien Wang00777b22019-04-24 16:37:09 -070033
Po-Hsien Wang00777b22019-04-24 16:37:09 -070034// kLongOptions defines the options for argument options.
35const static struct option kLongOptions[] = {
36 {"iterations", required_argument, nullptr, 'i'},
37 {"tests", required_argument, nullptr, 't'},
38 {"blacklist", required_argument, nullptr, 'b'},
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070039 {"spirv_dir", required_argument, nullptr, 's'},
Po-Hsien Wanga956e652020-11-11 16:29:04 -080040 {"out_dir", required_argument, nullptr, 'o'},
Po-Hsien Wang00777b22019-04-24 16:37:09 -070041 {"help", no_argument, nullptr, 'h'},
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070042 {"list", no_argument, &g_list, 1},
Po-Hsien Wang00777b22019-04-24 16:37:09 -070043 {"vlayer", no_argument, &g_vlayer, 1},
44 {"verbose", no_argument, &g_verbose, 1},
Po-Hsien Wang513fe192020-10-02 11:40:07 +080045 {"hasty", no_argument, &g_hasty, 1},
Po-Hsien Wang00777b22019-04-24 16:37:09 -070046 {0, 0, 0, 0}};
47
48// TimeTest times the test by looping it iteration times.
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070049// @param test: test to be excuted.
50// @param iteration: how many times should the test be executed.
Po-Hsien Wang00777b22019-04-24 16:37:09 -070051inline uint64_t TimeTest(vkbench::testBase* test, uint64_t iteration) {
52 uint64_t start = GetUTime();
Po-Hsien Wang00777b22019-04-24 16:37:09 -070053 try {
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070054 test->Setup();
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070055 } catch (const vk::SystemError& err) {
56 LOG("Setup failed: %s", err.what());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080057 test->Cleanup();
58 throw;
Po-Hsien Wang00777b22019-04-24 16:37:09 -070059 }
Po-Hsien Wangaafe13e2020-09-24 14:29:58 +080060 DEFER(test->Cleanup());
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070061
Po-Hsien Wang6765a852020-11-19 17:48:29 -080062 for (int i = 0; i < iteration; i++) {
63 test->Run();
Po-Hsien Wang00777b22019-04-24 16:37:09 -070064 }
65 return GetUTime() - start;
66}
67
68// Run the test and pre/post processes.
Po-Hsien Wang00777b22019-04-24 16:37:09 -070069// @param duration_us: The test would to iterate till duration_us is reached.
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070070void Run(vkbench::testBase* test, const uint64_t duration_us = 1000000) {
Po-Hsien Wang00777b22019-04-24 16:37:09 -070071 try {
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070072 test->Initialize();
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070073 } catch (const vk::SystemError& err) {
74 LOG("Test failed to initialize: %s", err.what());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080075 test->Destroy();
76 throw;
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070077 }
78 DEFER(test->Destroy());
79
80 // Do some iterations since initial timings may vary.
81 TimeTest(test, 2);
82 // Target minimum iteration is 1s for each test.
83 uint64_t time = 0;
84 uint64_t iteration = 1;
85 double score = -1.f;
86 do {
87 time = TimeTest(test, iteration);
88 DEBUG("iterations: %llu, time: %llu us, time/iter: %llu us", iteration,
89 time, time / iteration)
90 if (time > duration_us) {
91 score = time / iteration;
92 break;
Po-Hsien Wang00777b22019-04-24 16:37:09 -070093 }
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070094 iteration = iteration * 2;
95 } while ((1ull << 40) > iteration);
96 // Returns 0.0 if it ran max iterations in less than test time.
97 if (score <= 0.01f)
98 LOG("%s: measurement may not be accurate.", test->Name())
99 score = test->FormatMeasurement(score);
100 LOG("@RESULT: %46s = %10.2f %-15s", test->Name(), score, test->Unit());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800101 try {
Po-Hsien Wang53230a22020-11-12 18:03:18 -0800102 char file_name[1024];
103 sprintf(file_name, "%s.png", test->Name());
104 FilePath file_path = g_out_dir.Append(FilePath(file_name));
105
106 vkbench::Image image = test->GetImage();
107 image.Save(file_path);
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800108 } catch (std::runtime_error err) {
109 DEBUG("Get runtime_error while SaveImage: %s.", err.what());
110 }
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700111}
112
113void PrintHelp() {
114 LOG(R",(
115Usage: vkbench [OPTIONS]
116 -i, --iterations=N Specify the iterations to run the tests.
117 -t, --tests=TESTS Tests to run in colon separated form.
118 -b, --blacklist=TESTS Tests to not run in colon separated form.
119 --list List the tests available.
120 --verbose Show verbose messages.
Po-Hsien Wang513fe192020-10-02 11:40:07 +0800121 --vlayer Enable vulkan verification layer.
Po-Hsien Wang93cc6892020-10-28 17:01:28 -0700122 --hasty Enable hasty mode.
Po-Hsien Wanga956e652020-11-11 16:29:04 -0800123 --spirv_dir Path to SPIRV code for test.(default: shaders/)
124 --out_dir Path to the output directory.),")
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700125}
126
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700127bool prefixFind(std::vector<std::string> list, std::string name) {
128 for (const std::string item : list) {
129 if (name.rfind(item, 0) == 0) {
130 return true;
131 }
132 }
133 return false;
134}
135
136bool ParseArgv(int argc,
137 char** argv,
138 std::vector<vkbench::testBase*>& all_tests) {
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700139 std::vector<std::string> enabled_tests;
140 std::vector<std::string> disabled_tests;
141 int c;
142 int option_index = 0;
143 while ((c = getopt_long(argc, argv, "i:t:b:", kLongOptions, &option_index)) !=
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700144 -1) {
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700145 if (c == 'i') {
146 g_iteration = atoi(optarg);
147 } else if (c == 't') {
148 enabled_tests = SplitString(std::string(optarg), ':');
149 } else if (c == 'b') {
150 disabled_tests = SplitString(std::string(optarg), ':');
Po-Hsien Wang93cc6892020-10-28 17:01:28 -0700151 } else if (c == 's') {
Po-Hsien Wanga956e652020-11-11 16:29:04 -0800152 g_spirv_dir = FilePath(optarg);
153 } else if (c == 'o') {
154 g_out_dir = FilePath(optarg);
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700155 } else if (c == '?' || c == 'h') {
156 PrintHelp();
157 return false;
158 }
159 }
160
161 if (optind < argc) {
162 ERROR("Unknown argv: ")
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700163 while (optind < argc)
164 ERROR("%s ", argv[optind++])
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700165 return false;
166 }
167
168 all_tests.erase(
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700169 remove_if(all_tests.begin(), all_tests.end(),
170 [enabled_tests, disabled_tests](const vkbench::testBase* test) {
171 bool should_run = enabled_tests.empty() ||
172 prefixFind(enabled_tests, test->Name());
173 should_run &= !prefixFind(disabled_tests, test->Name());
174 if (!should_run)
175 delete test;
176 return !should_run;
177 }),
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700178 all_tests.end());
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700179
Po-Hsien Wang93cc6892020-10-28 17:01:28 -0700180 if (g_list) {
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700181 for (const auto& test : all_tests) {
182 LOG("%s: %s", test->Name(), test->Desp())
183 }
184 return false;
185 }
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700186 return true;
187}
188
189int main(int argc, char* argv[]) {
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700190 vkbench::vkBase simpleVulkan;
191 // all_tests list all the available tests.
192 std::vector<vkbench::testBase*> all_tests = {
193 new vkbench::SubmitTest(10, &simpleVulkan),
194 new vkbench::SubmitTest(100, &simpleVulkan),
195 new vkbench::SubmitTest(1000, &simpleVulkan),
196 new vkbench::SubmitTest(10000, &simpleVulkan),
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800197 new vkbench::SubmitTest(100000, &simpleVulkan),
198 new vkbench::DrawSizeTest(16, &simpleVulkan),
199 new vkbench::DrawSizeTest(64, &simpleVulkan),
200 new vkbench::DrawSizeTest(128, &simpleVulkan),
201 new vkbench::DrawSizeTest(512, &simpleVulkan),
202 new vkbench::DrawSizeTest(1024, &simpleVulkan),
Po-Hsien Wang5372c812020-10-02 09:41:01 +0800203 new vkbench::ClearTest(&simpleVulkan),
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700204 };
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700205
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800206 // Sort to bundle tests using same vulkan instance together.
207 std::stable_sort(all_tests.begin(), all_tests.end(),
208 [](vkbench::testBase* a, vkbench::testBase* b) -> bool {
209 return a->vk < b->vk;
210 });
211
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700212 if (!ParseArgv(argc, argv, all_tests)) {
213 return 0;
214 }
215
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700216 std::map<const char*, int> failed_test;
217 LOG("@TEST_BEGIN")
218 PrintDateTime();
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700219 for (auto i = 0; i < all_tests.size(); i++) {
220 auto& test = all_tests[i];
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800221 for (auto iter = 0; iter < g_iteration; iter++) {
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700222 try {
Po-Hsien Wang513fe192020-10-02 11:40:07 +0800223 if (!test->vk->IsInitialized())
224 test->vk->Initialize();
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700225 Run(test, 1000000);
Po-Hsien Wang513fe192020-10-02 11:40:07 +0800226 if (!g_hasty)
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700227 test->vk->Destroy();
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +0800228 } catch (const std::runtime_error error) {
229 failed_test[test->Name()] += 1;
230 LOG("Runtime Error: %s", error.what());
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700231 }
232 }
Po-Hsien Wang513fe192020-10-02 11:40:07 +0800233
234 // keep test->vk initialized for the next test.
235 if (g_hasty && test->vk->IsInitialized()) {
236 if (i + 1 >= all_tests.size() || test->vk != all_tests[i + 1]->vk) {
237 test->vk->Destroy();
238 }
239 }
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700240 }
241 PrintDateTime();
242 LOG("@TEST_END")
243
244 for (auto& keyval : failed_test) {
245 LOG("%s failed %d times.", keyval.first, keyval.second)
246 }
247
248 for (auto& test : all_tests) {
249 delete test;
250 }
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700251}