File: viz_debugger_multithread_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (419 lines) | stat: -rw-r--r-- 15,428 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <stddef.h>

#include <cstdint>
#include <cstdio>
#include <unordered_map>
#include <utility>
#include <vector>

#include "base/strings/string_number_conversions.h"
#include "base/threading/platform_thread.h"
#include "components/viz/service/debugger/viz_debugger.h"
#include "components/viz/service/debugger/viz_debugger_unittests/viz_debugger_internal.h"
#include "components/viz/service/debugger/viz_debugger_unittests/viz_debugger_unittest_base.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/vector2d_f.h"

#if VIZ_DEBUGGER_IS_ON()
using testing::_;
using testing::Mock;

namespace viz {

namespace {

class VizDebuggerMultithreadTest : public VisualDebuggerTestBase {};

static_assert(sizeof(VizDebuggerInternal) == sizeof(VizDebugger),
              "This test code exposes the internals of |VizDebugger| via an "
              "upcast; thus they must be the same size.");

enum class VizDebugCommandType { kDbgDrawRectCommand, kDbgLogCommand };

// Data structure to hold pre-determined commands for unit test.
struct DbgTestConfig {
  std::vector<std::vector<VizDebugCommandType>> dbg_commands;
};

struct DbgCommandIdentifier {
  int dbg_command_thread_id;
  int dbg_command_id;
  int dbg_command_frame_id;
};

// Reader thread.
class ReaderTestThread : public base::PlatformThread::Delegate {
 public:
  ReaderTestThread() = default;

  ReaderTestThread(const ReaderTestThread&) = delete;
  ReaderTestThread& operator=(const ReaderTestThread&) = delete;

  // Index variables to keep track of which debug command calls are being
  // checked in each called debug command cache.
  int rect_command_idx = 0;
  int log_command_idx = 0;

  // Identification pairs consisting of debug command ID and type of command.
  std::vector<DbgCommandIdentifier> draw_rect_command_history;
  std::vector<DbgCommandIdentifier> log_command_history;

  // Initialize debug commands before threads run.
  void Init(DbgTestConfig& config, uint32_t num_frames, uint32_t spin_amount) {
    thread_test_config_ = config;
    num_frames_ = num_frames;
    spin_amount_ = spin_amount;
  }

  void ThreadMain() override {
    // NOTE: Thread ID's are not unique on Fuchsia. Also, thread ID's on
    // Fuchsia are very large and thread_id_ may not hold the true thread ID
    // value. Regardless, the unit tests will run successfully.
    thread_id_ = static_cast<uint32_t>(
        base::PlatformThread::CurrentId().truncate_to_int32_for_display_only());
    for (int i = 0;
         static_cast<uint32_t>(i) < thread_test_config_.dbg_commands[0].size();
         ++i) {
      // Spin to allow writer to jump in once in a while.
      for (uint32_t spinner = 0; spinner < spin_amount_; ++spinner) {
        ++spin_inc_var_;
      }
      // The position of the rect is used as a unique identifier
      // for draw rect and text command calls for cross-checking later.
      // The x-position represents the thread ID and the
      // y-position represents the debug command ID per thread.
      // gfx::Rect(int x, int y, int width, int height)
      const gfx::RectF testRect = gfx::RectF(thread_id_, i, 12, 34);
      if (thread_test_config_.dbg_commands[0][i] ==
          VizDebugCommandType::kDbgDrawRectCommand) {
        DBG_DRAW_RECTANGLE_OPT_BUFF_UV_TEXT(
            "multithread rect", DBG_OPT_BLACK, testRect.OffsetFromOrigin(),
            testRect.size(), nullptr, DBG_DEFAULT_UV, "text");
        draw_rect_command_history.push_back({static_cast<int>(thread_id_), i});
      } else {  // kDbgLogCommand case
        DBG_LOG("multithread log", "%d:%d", thread_id_, i);
        log_command_history.push_back({static_cast<int>(thread_id_), i});
      }
    }
  }

 private:
  DbgTestConfig thread_test_config_;
  uint32_t thread_id_;
  uint32_t num_frames_;
  uint32_t spin_amount_;
  std::atomic<int> spin_inc_var_ = 0;
};

// Writer thread.
class WriterTestThread : public base::PlatformThread::Delegate {
 public:
  WriterTestThread() = default;

  WriterTestThread(const WriterTestThread&) = delete;
  WriterTestThread& operator=(const WriterTestThread&) = delete;

  // Initialize parameters before threads run.
  void Init(VizDebuggerMultithreadTest* test_fixture_ptr,
            uint32_t num_writer_tries,
            uint32_t spin_amount) {
    spin_amount_ = spin_amount;
    test_fixture_ptr_ = test_fixture_ptr;
    num_writer_tries_ = num_writer_tries;
  }

  void ThreadMain() override {
    for (uint32_t writer_try = 0; writer_try < num_writer_tries_;
         ++writer_try) {
      // Spin to add delays before writer tries again.
      for (uint32_t spinner = 0; spinner < spin_amount_; ++spinner) {
        ++spin_inc_var_;
      }
      test_fixture_ptr_->GetFrameData(false);
    }
  }

 private:
  uint32_t spin_amount_;
  std::atomic<int> spin_inc_var_ = 0;
  raw_ptr<VizDebuggerMultithreadTest> test_fixture_ptr_;
  uint32_t num_writer_tries_;
};

// Tests k-number of READ operations from k different threads.
//
// NOTE: The test assumes that thread ID's are unique
// (Exception: Fuchsia thread ID's are not unique).
TEST_F(VizDebuggerMultithreadTest, kReadersTest) {
  static const unsigned kNumReaderThreads = 3;

  ReaderTestThread threads[kNumReaderThreads];
  base::PlatformThreadHandle handles[kNumReaderThreads];

  SetFilter({TestFilter("")});
  // Enable viz debugger
  GetInternal()->ForceEnabled();

  DbgTestConfig test_config;

  static const unsigned kNumDrawRectCommandsPerFrame = 64;
  static const unsigned kNumLogCommandsPerFrame = 64;
  uint32_t kNumCommandsPerFrame =
      kNumDrawRectCommandsPerFrame + kNumLogCommandsPerFrame;

  // pre-process test commands vector
  test_config.dbg_commands.resize(1);
  test_config.dbg_commands[0].reserve(kNumCommandsPerFrame);

  for (uint32_t i = 0; i < kNumDrawRectCommandsPerFrame; ++i) {
    test_config.dbg_commands[0].push_back(
        VizDebugCommandType::kDbgDrawRectCommand);
  }

  for (uint32_t i = 0; i < kNumLogCommandsPerFrame; ++i) {
    test_config.dbg_commands[0].push_back(VizDebugCommandType::kDbgLogCommand);
  }

  // Initialize each thread and start thread
  for (uint32_t i = 0; i < kNumReaderThreads; ++i) {
    threads[i].Init(test_config, 1, 0);
  }
  for (uint32_t i = 0; i < kNumReaderThreads; ++i) {
    ASSERT_TRUE(base::PlatformThread::Create(0, &threads[i], &handles[i]));
  }

  // Collect all threads
  for (auto& handle : handles) {
    base::PlatformThread::Join(handle);
  }

  int expected_submission_count = kNumCommandsPerFrame * kNumReaderThreads;

  EXPECT_EQ(GetInternal()->GetSubmissionCount(), expected_submission_count);

  std::vector<VizDebuggerInternal::DrawCall> rect_calls_result =
      GetInternal()->GetDrawRectCalls();
  std::vector<VizDebuggerInternal::LogCall> logs_result =
      GetInternal()->GetLogs();

  size_t const kNumDrawCallSubmission = static_cast<size_t>(std::min(
      GetInternal()->GetRectCallsTailIdx(), GetInternal()->GetRectCallsSize()));
  size_t const kNumLogSubmission = static_cast<size_t>(
      std::min(GetInternal()->GetLogsTailIdx(), GetInternal()->GetLogsSize()));

  bool valid_command_found;

  // Final cross-checking for draw rect calls.
  for (size_t i = 0; i < kNumDrawCallSubmission; ++i) {
    valid_command_found = false;
    for (auto&& thread : threads) {
      // If debug calls from a thread is exhausted, skip this thread.
      if (static_cast<uint32_t>(thread.rect_command_idx) >=
          thread.draw_rect_command_history.size()) {
        continue;
      }
      DbgCommandIdentifier cur_thread_command_identifier =
          thread.draw_rect_command_history[thread.rect_command_idx];

      // Check if debug command ID matches. Refer to ReaderTestThread's
      // ThreadMain() function to see how command ID's are formatted.
      if (rect_calls_result[i].pos.x() ==
              cur_thread_command_identifier.dbg_command_thread_id &&
          rect_calls_result[i].pos.y() ==
              cur_thread_command_identifier.dbg_command_id) {
        valid_command_found = true;
        ++thread.rect_command_idx;
        break;
      }
    }
    EXPECT_TRUE(valid_command_found);
  }

  // Final cross-checking for logs.
  for (size_t i = 0; i < kNumLogSubmission; ++i) {
    valid_command_found = false;
    for (auto&& thread : threads) {
      // If debug calls from a thread is exhausted, skip this thread.
      if (static_cast<uint32_t>(thread.log_command_idx) >=
          thread.log_command_history.size()) {
        continue;
      }
      DbgCommandIdentifier cur_thread_command_identifier =
          thread.log_command_history[thread.log_command_idx];

      // Extract debug command identifier information from log result.
      // Information is being extracted froma string below. Refer to
      // ReaderTestThread's ThreadMain() function to see how command ID's
      // are formatted.
      std::string log_result_identifier = logs_result[i].value;
      int log_split_position = log_result_identifier.find(":");
      int log_thread_id;
      int log_command_id;
      base::StringToInt(log_result_identifier.substr(0, log_split_position),
                        &log_thread_id);
      base::StringToInt(log_result_identifier.substr(log_split_position + 1),
                        &log_command_id);

      // Check if debug command ID matches.
      if (log_thread_id ==
              cur_thread_command_identifier.dbg_command_thread_id &&
          log_command_id == cur_thread_command_identifier.dbg_command_id) {
        valid_command_found = true;
        ++thread.log_command_idx;
        break;
      }
    }
    EXPECT_TRUE(valid_command_found);
  }
}

// Tests k-number of READ operations from k different reader threads while
// ONE writer thread comes in and writes once in a while.
//
// NOTE: The test assumes that thread ID's are unique
// (Exception: Fuchsia thread ID's are not unique).
TEST_F(VizDebuggerMultithreadTest, kReadersOneWriterCommandsSequenceTest) {
  // Number of Reader Threads
  static const unsigned kNumReaderThreads = 3;
  // Number of times writer thread tries to run
  static const unsigned kNumWriterTries = 20;
  // Number of DBG calls per frame for each type
  static const unsigned kNumDrawRectCommandsPerFrame = 64;
  static const unsigned kNumLogCommandsPerFrame = 64;
  // Amount of spin delay for each type of thread
  static const unsigned kWriterSpinAmount = 900;
  static const unsigned kReaderSpinAmount = 100;

  WriterTestThread writer_thread;
  base::PlatformThreadHandle writer_thread_handle;
  ReaderTestThread reader_threads[kNumReaderThreads];
  base::PlatformThreadHandle reader_thread_handles[kNumReaderThreads];

  SetFilter({TestFilter("")});
  // Enable viz debugger
  GetInternal()->ForceEnabled();

  DbgTestConfig test_config;

  uint32_t kNumCommandsPerFrame =
      kNumDrawRectCommandsPerFrame + kNumLogCommandsPerFrame;

  // Pre-process test commands vector.
  test_config.dbg_commands.resize(1);
  test_config.dbg_commands[0].reserve(kNumCommandsPerFrame);

  // Populate test commands vectors with enums representing command types.
  for (uint32_t i = 0; i < kNumDrawRectCommandsPerFrame; ++i) {
    test_config.dbg_commands[0].push_back(
        VizDebugCommandType::kDbgDrawRectCommand);
  }

  for (uint32_t i = 0; i < kNumLogCommandsPerFrame; ++i) {
    test_config.dbg_commands[0].push_back(VizDebugCommandType::kDbgLogCommand);
  }

  // Initialize and start each thread. Each thread will start making VizDebugger
  // debug calls simultaneously upon starting.
  for (uint32_t i = 0; i < kNumReaderThreads; ++i) {
    reader_threads[i].Init(test_config, 1, kReaderSpinAmount);
  }
  for (uint32_t i = 0; i < kNumReaderThreads; ++i) {
    ASSERT_TRUE(base::PlatformThread::Create(0, &reader_threads[i],
                                             &reader_thread_handles[i]));
  }
  writer_thread.Init(this, kNumWriterTries, kWriterSpinAmount);
  ASSERT_TRUE(
      base::PlatformThread::Create(0, &writer_thread, &writer_thread_handle));

  // Collect all threads.
  base::PlatformThread::Join(writer_thread_handle);
  for (auto& reader_thread_handle : reader_thread_handles) {
    base::PlatformThread::Join(reader_thread_handle);
  }

  // One last call to GetFrameData() to collect any remaining calls not yet
  // added to the debug calls cache.
  GetFrameData(false);
  int expected_submission_count = kNumCommandsPerFrame * kNumReaderThreads;
  int calls_cache_total_size =
      draw_calls_cache_.size() + log_calls_cache_.size();
  EXPECT_EQ(calls_cache_total_size, expected_submission_count);

  bool valid_command_found;

  // Final cross-checking for draw rect calls.
  for (auto&& rect_call : draw_calls_cache_) {
    valid_command_found = false;
    for (auto&& thread : reader_threads) {
      // If debug calls from a thread is exhausted, skip this thread.
      if (static_cast<uint32_t>(thread.rect_command_idx) >=
          thread.draw_rect_command_history.size()) {
        continue;
      }
      DbgCommandIdentifier cur_thread_command_identifier =
          thread.draw_rect_command_history[thread.rect_command_idx];

      // Check if debug command ID matches. Refer to ReaderTestThread's
      // ThreadMain() function to see how command ID's are formatted.
      if (rect_call.pos.x() ==
              cur_thread_command_identifier.dbg_command_thread_id &&
          rect_call.pos.y() == cur_thread_command_identifier.dbg_command_id) {
        valid_command_found = true;
        ++thread.rect_command_idx;
        break;
      }
    }
    EXPECT_TRUE(valid_command_found);
  }

  // Final cross-checking for logs.
  for (auto&& log_call : log_calls_cache_) {
    valid_command_found = false;
    for (auto&& thread : reader_threads) {
      // If debug calls from a thread is exhausted, skip this thread.
      if (static_cast<uint32_t>(thread.log_command_idx) >=
          thread.log_command_history.size()) {
        continue;
      }
      DbgCommandIdentifier cur_thread_command_identifier =
          thread.log_command_history[thread.log_command_idx];

      // Extract debug command identifier information from log result.
      // Information is being extracted from a string below. Refer to
      // ReaderTestThread's ThreadMain() function to see how command ID's
      // are formatted.
      std::string log_result_identifier = log_call.value;
      int log_split_position = log_result_identifier.find(":");
      int log_thread_id;
      int log_command_id;
      base::StringToInt(log_result_identifier.substr(0, log_split_position),
                        &log_thread_id);
      base::StringToInt(log_result_identifier.substr(log_split_position + 1),
                        &log_command_id);

      // Check if debug command ID matches.
      if (log_thread_id ==
              cur_thread_command_identifier.dbg_command_thread_id &&
          log_command_id == cur_thread_command_identifier.dbg_command_id) {
        valid_command_found = true;
        ++thread.log_command_idx;
        break;
      }
    }
    EXPECT_TRUE(valid_command_found);
  }
}
}  // namespace
}  // namespace viz

#endif  // VIZ_DEBUGGER_IS_ON()