| ... | @@ -1,8 +1,9 @@ | ... | @@ -1,8 +1,9 @@ |
| 1 | gpa: Allocator, | 1 | gpa: Allocator, |
| 2 | manifest_dir: fs.Dir, | 2 | manifest_dir: fs.Dir, |
| 3 | hash: HashHelper = .{}, | 3 | hash: HashHelper = .{}, |
| | 4 | /// This value is accessed from multiple threads, protected by mutex. |
| 4 | recent_problematic_timestamp: i128 = 0, | 5 | recent_problematic_timestamp: i128 = 0, |
| 5 | want_refresh_timestamp: bool = true, | 6 | mutex: std.Thread.Mutex = .{}, |
| 6 | | 7 | |
| 7 | const Cache = @This(); | 8 | const Cache = @This(); |
| 8 | const std = @import("std"); | 9 | const std = @import("std"); |
| ... | @@ -183,11 +184,18 @@ pub const Manifest = struct { | ... | @@ -183,11 +184,18 @@ pub const Manifest = struct { |
| 183 | /// the same cache directory at the same time. | 184 | /// the same cache directory at the same time. |
| 184 | want_shared_lock: bool = true, | 185 | want_shared_lock: bool = true, |
| 185 | have_exclusive_lock: bool = false, | 186 | 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, |
| 186 | files: std.ArrayListUnmanaged(File) = .{}, | 191 | files: std.ArrayListUnmanaged(File) = .{}, |
| 187 | hex_digest: [hex_digest_len]u8, | 192 | hex_digest: [hex_digest_len]u8, |
| 188 | /// Populated when hit() returns an error because of one | 193 | /// Populated when hit() returns an error because of one |
| 189 | /// of the files listed in the manifest. | 194 | /// of the files listed in the manifest. |
| 190 | failed_file_index: ?usize = null, | 195 | 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, |
| 191 | | 199 | |
| 192 | /// Add a file as a dependency of process being cached. When `hit` is | 200 | /// Add a file as a dependency of process being cached. When `hit` is |
| 193 | /// called, the file's contents will be checked to ensure that it matches | 201 | /// called, the file's contents will be checked to ensure that it matches |
| ... | @@ -347,11 +355,7 @@ pub const Manifest = struct { | ... | @@ -347,11 +355,7 @@ pub const Manifest = struct { |
| 347 | } | 355 | } |
| 348 | } | 356 | } |
| 349 | | 357 | |
| 350 | // Indicate that we want isProblematicTimestamp to perform a filesystem write in | 358 | self.want_refresh_timestamp = true; |
| 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); | | |
| 355 | | 359 | |
| 356 | const file_contents = try self.manifest_file.?.reader().readAllAlloc(self.cache.gpa, manifest_file_size_max); | 360 | const file_contents = try self.manifest_file.?.reader().readAllAlloc(self.cache.gpa, manifest_file_size_max); |
| 357 | defer self.cache.gpa.free(file_contents); | 361 | defer self.cache.gpa.free(file_contents); |
| ... | @@ -422,7 +426,7 @@ pub const Manifest = struct { | ... | @@ -422,7 +426,7 @@ pub const Manifest = struct { |
| 422 | | 426 | |
| 423 | cache_hash_file.stat = actual_stat; | 427 | cache_hash_file.stat = actual_stat; |
| 424 | | 428 | |
| 425 | if (self.cache.isProblematicTimestamp(cache_hash_file.stat.mtime)) { | 429 | if (self.isProblematicTimestamp(cache_hash_file.stat.mtime)) { |
| 426 | // The actual file has an unreliable timestamp, force it to be hashed | 430 | // The actual file has an unreliable timestamp, force it to be hashed |
| 427 | cache_hash_file.stat.mtime = 0; | 431 | cache_hash_file.stat.mtime = 0; |
| 428 | cache_hash_file.stat.inode = 0; | 432 | cache_hash_file.stat.inode = 0; |
| ... | @@ -487,6 +491,40 @@ pub const Manifest = struct { | ... | @@ -487,6 +491,40 @@ pub const Manifest = struct { |
| 487 | } | 491 | } |
| 488 | } | 492 | } |
| 489 | | 493 | |
| | 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 | |
| 490 | fn populateFileHash(self: *Manifest, ch_file: *File) !void { | 528 | fn populateFileHash(self: *Manifest, ch_file: *File) !void { |
| 491 | log.debug("populateFileHash {s}", .{ch_file.path.?}); | 529 | log.debug("populateFileHash {s}", .{ch_file.path.?}); |
| 492 | const file = try fs.cwd().openFile(ch_file.path.?, .{}); | 530 | const file = try fs.cwd().openFile(ch_file.path.?, .{}); |
| ... | @@ -494,7 +532,7 @@ pub const Manifest = struct { | ... | @@ -494,7 +532,7 @@ pub const Manifest = struct { |
| 494 | | 532 | |
| 495 | ch_file.stat = try file.stat(); | 533 | ch_file.stat = try file.stat(); |
| 496 | | 534 | |
| 497 | if (self.cache.isProblematicTimestamp(ch_file.stat.mtime)) { | 535 | if (self.isProblematicTimestamp(ch_file.stat.mtime)) { |
| 498 | // The actual file has an unreliable timestamp, force it to be hashed | 536 | // The actual file has an unreliable timestamp, force it to be hashed |
| 499 | ch_file.stat.mtime = 0; | 537 | ch_file.stat.mtime = 0; |
| 500 | ch_file.stat.inode = 0; | 538 | ch_file.stat.inode = 0; |
| ... | @@ -751,36 +789,6 @@ fn hashFile(file: fs.File, bin_digest: *[Hasher.mac_length]u8) !void { | ... | @@ -751,36 +789,6 @@ fn hashFile(file: fs.File, bin_digest: *[Hasher.mac_length]u8) !void { |
| 751 | hasher.final(bin_digest); | 789 | hasher.final(bin_digest); |
| 752 | } | 790 | } |
| 753 | | 791 | |
| 754 | /// Create/Write a file, grab its stat.mtime timestamp, then close it. | | |
| 755 | /// If any filesystem errors occur, this function returns `true`. | | |
| 756 | fn 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 | | | |
| 784 | // Create/Write a file, close it, then grab its stat.mtime timestamp. | 792 | // Create/Write a file, close it, then grab its stat.mtime timestamp. |
| 785 | fn testGetCurrentFileTimestamp() !i128 { | 793 | fn testGetCurrentFileTimestamp() !i128 { |
| 786 | var file = try fs.cwd().createFile("test-filetimestamp.tmp", .{ | 794 | var file = try fs.cwd().createFile("test-filetimestamp.tmp", .{ |