authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-09 22:07:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-09 22:10:38-07:00
log3e618f8432533eaa1cf6dcb39bdcb1b2e5bc1e47
treebe4d87aea477a27bfb5d32d069df2dbee067407b
parentfdbb329d1009b4c7b23552e65c0344e3ce52b459

Cache: use mutex to protect recent_problematic_timestamp

The previous commit tried to use atomics but not many CPUs support 128-bit atomics. So we use a mutex. In order to avoid contention, we also store `recent_problematic_timestamp` locally on the `Manifest` which is only ever accessed from a single thread at a time, and only consult the global one if the local one is problematic. This commit was tested by running `zig build test-behavior` in two separate terminals at the same time.

1 files changed, 46 insertions(+), 38 deletions(-)

src/Cache.zig+46-38
......@@ -1,8 +1,9 @@
11gpa: Allocator,
22manifest_dir: fs.Dir,
33hash: HashHelper = .{},
4/// This value is accessed from multiple threads, protected by mutex.
45recent_problematic_timestamp: i128 = 0,
5want_refresh_timestamp: bool = true,
6mutex: std.Thread.Mutex = .{},
67
78const Cache = @This();
89const std = @import("std");
......@@ -183,11 +184,18 @@ pub const Manifest = struct {
183184 /// the same cache directory at the same time.
184185 want_shared_lock: bool = true,
185186 have_exclusive_lock: bool = false,
187 // Indicate that we want isProblematicTimestamp to perform a filesystem write in
188 // order to obtain a problematic timestamp for the next call. Calls after that
189 // will then use the same timestamp, to avoid unnecessary filesystem writes.
190 want_refresh_timestamp: bool = true,
186191 files: std.ArrayListUnmanaged(File) = .{},
187192 hex_digest: [hex_digest_len]u8,
188193 /// Populated when hit() returns an error because of one
189194 /// of the files listed in the manifest.
190195 failed_file_index: ?usize = null,
196 /// Keeps track of the last time we performed a file system write to observe
197 /// what time the file system thinks it is, according to its own granularity.
198 recent_problematic_timestamp: i128 = 0,
191199
192200 /// Add a file as a dependency of process being cached. When `hit` is
193201 /// called, the file's contents will be checked to ensure that it matches
......@@ -347,11 +355,7 @@ pub const Manifest = struct {
347355 }
348356 }
349357
350 // Indicate that we want isProblematicTimestamp to perform a filesystem write in
351 // order to obtain a problematic timestamp for the next call. Calls after that
352 // in this same hit() function call will then use the same timestamp, to avoid
353 // writing multiple times to the filesystem.
354 @atomicStore(bool, &self.cache.want_refresh_timestamp, true, .Monotonic);
358 self.want_refresh_timestamp = true;
355359
356360 const file_contents = try self.manifest_file.?.reader().readAllAlloc(self.cache.gpa, manifest_file_size_max);
357361 defer self.cache.gpa.free(file_contents);
......@@ -422,7 +426,7 @@ pub const Manifest = struct {
422426
423427 cache_hash_file.stat = actual_stat;
424428
425 if (self.cache.isProblematicTimestamp(cache_hash_file.stat.mtime)) {
429 if (self.isProblematicTimestamp(cache_hash_file.stat.mtime)) {
426430 // The actual file has an unreliable timestamp, force it to be hashed
427431 cache_hash_file.stat.mtime = 0;
428432 cache_hash_file.stat.inode = 0;
......@@ -487,6 +491,40 @@ pub const Manifest = struct {
487491 }
488492 }
489493
494 fn isProblematicTimestamp(man: *Manifest, file_time: i128) bool {
495 // If the file_time is prior to the most recent problematic timestamp
496 // then we don't need to access the filesystem.
497 if (file_time < man.recent_problematic_timestamp)
498 return false;
499
500 // Next we will check the globally shared Cache timestamp, which is accessed
501 // from multiple threads.
502 man.cache.mutex.lock();
503 defer man.cache.mutex.unlock();
504
505 // Save the global one to our local one to avoid locking next time.
506 man.recent_problematic_timestamp = man.cache.recent_problematic_timestamp;
507 if (file_time < man.recent_problematic_timestamp)
508 return false;
509
510 // This flag prevents multiple filesystem writes for the same hit() call.
511 if (man.want_refresh_timestamp) {
512 man.want_refresh_timestamp = false;
513
514 var file = man.cache.manifest_dir.createFile("timestamp", .{
515 .read = true,
516 .truncate = true,
517 }) catch return true;
518 defer file.close();
519
520 // Save locally and also save globally (we still hold the global lock).
521 man.recent_problematic_timestamp = (file.stat() catch return true).mtime;
522 man.cache.recent_problematic_timestamp = man.recent_problematic_timestamp;
523 }
524
525 return file_time >= man.recent_problematic_timestamp;
526 }
527
490528 fn populateFileHash(self: *Manifest, ch_file: *File) !void {
491529 log.debug("populateFileHash {s}", .{ch_file.path.?});
492530 const file = try fs.cwd().openFile(ch_file.path.?, .{});
......@@ -494,7 +532,7 @@ pub const Manifest = struct {
494532
495533 ch_file.stat = try file.stat();
496534
497 if (self.cache.isProblematicTimestamp(ch_file.stat.mtime)) {
535 if (self.isProblematicTimestamp(ch_file.stat.mtime)) {
498536 // The actual file has an unreliable timestamp, force it to be hashed
499537 ch_file.stat.mtime = 0;
500538 ch_file.stat.inode = 0;
......@@ -751,36 +789,6 @@ fn hashFile(file: fs.File, bin_digest: *[Hasher.mac_length]u8) !void {
751789 hasher.final(bin_digest);
752790}
753791
754/// Create/Write a file, grab its stat.mtime timestamp, then close it.
755/// If any filesystem errors occur, this function returns `true`.
756fn isProblematicTimestamp(cache: *Cache, file_time: i128) bool {
757 // If the file_time is prior to the most recent problematic timestamp
758 // then we don't need to access the filesystem.
759 var ts = @atomicLoad(i128, &cache.recent_problematic_timestamp, .Monotonic);
760 if (file_time < ts)
761 return false;
762
763 if (@atomicRmw(bool, &cache.want_refresh_timestamp, .Xchg, false, .Monotonic)) {
764 var file = cache.manifest_dir.createFile("timestamp", .{
765 .read = true,
766 .truncate = true,
767 }) catch return true;
768 defer file.close();
769
770 const new_ts = (file.stat() catch return true).mtime;
771 ts = if (@cmpxchgWeak(
772 i128,
773 &cache.recent_problematic_timestamp,
774 ts,
775 new_ts,
776 .Monotonic,
777 .Monotonic,
778 )) |race_ts| race_ts else new_ts;
779 }
780
781 return file_time >= ts;
782}
783
784792// Create/Write a file, close it, then grab its stat.mtime timestamp.
785793fn testGetCurrentFileTimestamp() !i128 {
786794 var file = try fs.cwd().createFile("test-filetimestamp.tmp", .{