Skip to content

Windows,Bazel client: multithreaded file writing - #5600

Closed
laszlocsomor wants to merge 3 commits into
bazelbuild:masterfrom
laszlocsomor:mt-extract
Closed

laszlocsomor wants to merge 3 commits into
bazelbuild:masterfrom
laszlocsomor:mt-extract

Conversation

@laszlocsomor

Copy link
Copy Markdown
Contributor

The Bazel client on Windows now writes extracted
binaries to disk in parallel. On all other systems
it writes them serially (as before).

This change makes blaze.cc:ActuallyExtractData()
about 3x faster when using a HDD. (In previous
experiments I saw no speedup with multi-threaded
writing on machines with an SSD.)

The Windows-specific code uses the native
Threadpool API of Windows, creating a pool of at
least 8 and at most 16 threads. (This seems to be
a good balance between speed and thread count.)

The OS manages everything about the pool; Bazel
submits callbacks and the pool executes them
asynchronously.

blaze.cc:ActuallyExtractData() speed, before:

  • Windows: 6.48s (avg) / 6.38s (median)
  • Linux (Debian): 4.78s (avg) / 4.79s (median)

blaze.cc:ActuallyExtractData() speed, after:

  • Windows (8-16 threads): 2.05s (avg) / 2.01s (md)
  • Windows (1 thread): 5.77s (avg) / 5.74s (median)

See #5444

Change-Id: I7211f3d28eb8b9837352c16ff8df0411d5a9ebe1

The Bazel client on Windows now writes extracted
binaries to disk in parallel. On all other systems
it writes them serially (as before).

This change makes blaze.cc:ActuallyExtractData()
about 3x faster when using a HDD. (In previous
experiments I saw no speedup with multi-threaded
writing on machines with an SSD.)

The Windows-specific code uses the native
Threadpool API of Windows, creating a pool of at
least 8 and at most 16 threads. (This seems to be
a good balance between speed and thread count.)

The OS manages everything about the pool; Bazel
submits callbacks and the pool executes them
asynchronously.

blaze.cc:ActuallyExtractData() speed, before:
- Windows: 6.48s (avg) / 6.38s (median)
- Linux (Debian): 4.78s (avg) / 4.79s (median)

blaze.cc:ActuallyExtractData() speed, after:
- Windows (8-16 threads): 2.05s (avg) / 2.01s (md)
- Windows (1 thread): 5.77s (avg) / 5.74s (median)

See bazelbuild#5444

Change-Id: I7211f3d28eb8b9837352c16ff8df0411d5a9ebe1
@rongjiecomputer

Copy link
Copy Markdown
Contributor

@laszlocsomor Very excited to see the huge speedup! How many cores are there for the machine you are testing? (Although I think it might not really matters since this is I/O bound task, not CPU bound task). My machine has 2 cores with 4 logical processors, HDD.

was_error_ as std::atomic_bool should be OK, but won't changing std::string error_msg_ without a lock introduces race condition?

Comment thread src/main/cpp/blaze_util_windows.cc Outdated

using std::atomic_bool;
using std::mutex;
using std::set;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try not to introduce more using std::type aliases. When reader sees mutex in a function, it is not immediately clear if it is std::mutex or Bazel's own custom mutex implementation.

using MagicMap = std::unordered_map<std::string, MagicItem> that saves significant amount of typing is strongly encouraged.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks for the advice!

Change-Id: I7e92c75a5602090e7278fe907e85a0bd116fcb9c
@laszlocsomor

Copy link
Copy Markdown
Contributor Author

@laszlocsomor Very excited to see the huge speedup! How many cores are there for the machine you are testing? (Although I think it might not really matters since this is I/O bound task, not CPU bound task). My machine has 2 cores with 4 logical processors, HDD.

Great! Good to hear someone's waiting for this PR to land :)

My Windows machine has 6 physical cores (12 logical ones). As you say this is I/O bound and this being a corporate workstation I probably have plenty of I/O work running in the background.

I experimented with different thread counts (1, 2, 4, 6, 8, 10, 20, 40, 100) but found around 8-10 to be the
sweet spot, at least at that time it was on that particular machine. Since static performance tuning is inherently flawed, I decided to play it safe, squint my eyes at the test results, and go for a low minimum and not too high maximum count, hence the 8..16 range.

was_error_ as std::atomic_bool should be OK, but won't changing std::string error_msg_ without a lock introduces race condition?

Good point! It won't. The race condition is benign, access is guarded by the one-time-flip of the atomic bool. I added comments to the code.

Change-Id: I7a74d62a563c92948a4dfa8ad5ac83eae018db10
@laszlocsomor
laszlocsomor requested a review from lberki July 17, 2018 08:25
@rongjiecomputer

Copy link
Copy Markdown
Contributor

My Windows machine has 6 physical cores (12 logical ones). As you say this is I/O bound and this being a corporate workstation I probably have plenty of I/O work running in the background.

After this PR is landed, I can tweak the constants and report back the result for my local machine.

Good point! It won't. The race condition is benign, access is guarded by the one-time-flip of the atomic bool. I added comments to the code.

👍

@laszlocsomor

Copy link
Copy Markdown
Contributor Author

After this PR is landed, I can tweak the constants and report back the result for my local machine.

Thanks, that'd be great! You can already experiment with it:

  1. Clone this PR (git clone origin refs/pull/5600/head && git checkout FETCH_HEAD)
  2. add //src/main/cpp/util:profiler to the deps of //src/main/cpp:client
  3. add #include "src/main/cpp/util/profiler.h" to src/main/cpp/blaze.cc
  4. change this line:
    ActuallyExtractData(self_path, tmp_binaries);

    ...to this:
  {
    blaze_util::profiler::Task task("ActuallyExtractData");
    blaze_util::profiler::ScopedTask profile(&task);
    ActuallyExtractData(self_path, tmp_binaries);
  }

@bazel-io bazel-io closed this in ae43043 Jul 17, 2018
@laszlocsomor
laszlocsomor deleted the mt-extract branch July 17, 2018 11:21
werkt pushed a commit to werkt/bazel that referenced this pull request Aug 2, 2018
The Bazel client on Windows now writes extracted
binaries to disk in parallel. On all other systems
it writes them serially (as before).

This change makes blaze.cc:ActuallyExtractData()
about 3x faster when using a HDD. (In previous
experiments I saw no speedup with multi-threaded
writing on machines with an SSD.)

The Windows-specific code uses the native
Threadpool API of Windows, creating a pool of at
least 8 and at most 16 threads. (This seems to be
a good balance between speed and thread count.)

The OS manages everything about the pool; Bazel
submits callbacks and the pool executes them
asynchronously.

blaze.cc:ActuallyExtractData() speed, before:
- Windows: 6.48s (avg) / 6.38s (median)
- Linux (Debian): 4.78s (avg) / 4.79s (median)

blaze.cc:ActuallyExtractData() speed, after:
- Windows (8-16 threads): 2.05s (avg) / 2.01s (md)
- Windows (1 thread): 5.77s (avg) / 5.74s (median)

See bazelbuild#5444

Change-Id: I7211f3d28eb8b9837352c16ff8df0411d5a9ebe1

Closes bazelbuild#5600.

Change-Id: I7a74d62a563c92948a4dfa8ad5ac83eae018db10
PiperOrigin-RevId: 204891217
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants