mmap-sync icon indicating copy to clipboard operation
mmap-sync copied to clipboard

Add optional support for write locks

Open austinhartzheim opened this issue 1 year ago • 1 comments

Write locks prevent accidental misuse of Synchronizers by establishing an advisory lock on the state file using the flock system call. Correctly configured writers will check this lock to prevent duplicate writers.

The new write locking feature is configured through a new builder type, which provides an alternate way to configure Synchronizer options. We can extend this pattern in the future.

Write locking functionality is gated behind the write-lock flag and therefore has no performance impact when disabled. Benchmarks show an ~1% increase in write latency when the feature is enabled (the first write may experience additional latency as the lock is acquired.)

Design

Write locks are configured with the WriteLockMode. Using an enum allows us to reserve space for future locking modes. Currently, we only support Disabled and SingleWriter modes, where SingleWriter makes a non-blocking call to flock to establish a lock. Future work might add support for multiple writers (i.e., where the lock is released after every write so other threads/processes can write), in addition to blocking/non-blocking variations of single- and multiple-writer modes.

Internally, the state method has been divided into state_read and state_write to avoid an additional branch condition for reads. This design allows read throughout to remain unchanged.

Benchmarks

synchronizer/write      time:   [451.21 ns 452.36 ns 453.79 ns]
                        thrpt:  [2.2037 Melem/s 2.2106 Melem/s 2.2163 Melem/s]
                 change:
                        time:   [+0.9181% +1.3393% +1.7429%] (p = 0.00 < 0.05)
                        thrpt:  [-1.7130% -1.3216% -0.9097%]
                        Change within noise threshold.
Found 4 outliers among 100 measurements (4.00%)
  1 (1.00%) high mild
  3 (3.00%) high severe
synchronizer/write_raw  time:   [306.60 ns 306.89 ns 307.17 ns]
                        thrpt:  [3.2555 Melem/s 3.2585 Melem/s 3.2616 Melem/s]
                 change:
                        time:   [+0.3092% +0.4819% +0.6496%] (p = 0.00 < 0.05)
                        thrpt:  [-0.6454% -0.4796% -0.3083%]
                        Change within noise threshold.
Found 4 outliers among 100 measurements (4.00%)
  3 (3.00%) low mild
  1 (1.00%) high severe
synchronizer/read/check_bytes_true
                        time:   [48.643 ns 48.728 ns 48.814 ns]
                        thrpt:  [20.486 Melem/s 20.522 Melem/s 20.558 Melem/s]
                 change:
                        time:   [+0.5895% +0.8119% +1.0184%] (p = 0.00 < 0.05)
                        thrpt:  [-1.0081% -0.8053% -0.5861%]
                        Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) low mild
synchronizer/read/check_bytes_false
                        time:   [23.008 ns 23.048 ns 23.087 ns]
                        thrpt:  [43.314 Melem/s 43.387 Melem/s 43.463 Melem/s]
                 change:
                        time:   [-0.5080% -0.2852% -0.0539%] (p = 0.01 < 0.05)
                        thrpt:  [+0.0539% +0.2860% +0.5106%]
                        Change within noise threshold.
Found 4 outliers among 100 measurements (4.00%)
  3 (3.00%) low mild
  1 (1.00%) high mild

austinhartzheim avatar Sep 12 '24 03:09 austinhartzheim