interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / technical / racy-git.adoc

202 lines · 9207 bytes Raw Blame History
1Use of index and Racy Git problem
2=================================
3
4Background
5----------
6
7The index is one of the most important data structures in Git.
8It represents a virtual working tree state by recording list of
9paths and their object names and serves as a staging area to
10write out the next tree object to be committed. The state is
11"virtual" in the sense that it does not necessarily have to, and
12often does not, match the files in the working tree.
13
14There are cases where Git needs to examine the differences between the
15virtual working tree state in the index and the files in the
16working tree. The most obvious case is when the user asks `git
17diff` (or its low level implementation, `git diff-files`) or
18`git-ls-files --modified`. In addition, Git internally checks
19if the files in the working tree are different from what are
20recorded in the index to avoid stomping on local changes in them
21during patch application, switching branches, and merging.
22
23In order to speed up this comparison between the files in the
24working tree and the index entries, the index entries record the
25information obtained from the filesystem via `lstat(2)` system
26call when they were last updated. When checking if they differ,
27Git first runs `lstat(2)` on the files and compares the result
28with this information (this is what was originally done by the
29`ce_match_stat()` function, but the current code does it in
30`ce_match_stat_basic()` function). If some of these "cached
31stat information" fields do not match, Git can tell that the
32files are modified without even looking at their contents.
33
34Note: not all members in `struct stat` obtained via `lstat(2)`
35are used for this comparison. For example, `st_atime` obviously
36is not useful. Currently, Git compares the file type (regular
37files vs symbolic links) and executable bits (only for regular
38files) from `st_mode` member, `st_mtime` and `st_ctime`
39timestamps, `st_uid`, `st_gid`, `st_ino`, and `st_size` members.
40With a `USE_STDEV` compile-time option, `st_dev` is also
41compared, but this is not enabled by default because this member
42is not stable on network filesystems. With 'core.useNanosec'
43config setting, `st_mtim.tv_nsec` and `st_ctim.tv_nsec`
44members are also compared. On Linux, this is not enabled by default
45because in-core timestamps can have finer granularity than
46on-disk timestamps, resulting in meaningless changes when an
47inode is evicted from the inode cache. See commit 8ce13b0
48of git://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
49([PATCH] Sync in core time granularity with filesystems,
502005-01-04). This patch is included in kernel 2.6.11 and newer, but
51only fixes the issue for file systems with exactly 1 ns or 1 s
52resolution. As of kernel 4.3, other file systems (CEPH, CIFS, NTFS, UFS, FUSE)
53were fixed; see https://public-inbox.org/git/5605D88A.20104%40gmail.com/. FAT
54has been fixed since 2015. The usual suspects (ext2, ext4, XFS) are known to
55work, too.
56
57Racy Git
58--------
59
60There is one slight problem with the optimization based on the
61cached stat information. Consider this sequence:
62
63 : modify 'foo'
64 $ git update-index 'foo'
65 : modify 'foo' again, in-place, without changing its size
66
67The first `update-index` computes the object name of the
68contents of file `foo` and updates the index entry for `foo`
69along with the `struct stat` information. If the modification
70that follows it happens very fast so that the file's `st_mtime`
71timestamp does not change, after this sequence, the cached stat
72information the index entry records still exactly match what you
73would see in the filesystem, even though the file `foo` is now
74different.
75This way, Git can incorrectly think files in the working tree
76are unmodified even though they actually are. This is called
77the "racy Git" problem (discovered by Pasky), and the entries
78that appear clean when they may not be because of this problem
79are called "racily clean".
80
81To avoid this problem, Git does two things:
82
83. When the cached stat information says the file has not been
84 modified, and the `st_mtime` is the same as (or newer than)
85 the timestamp of the index file itself (which is the time `git
86 update-index foo` finished running in the above example), it
87 also compares the contents with the object registered in the
88 index entry to make sure they match.
89
90. When the index file is updated that contains racily clean
91 entries, cached `st_size` information is truncated to zero
92 before writing a new version of the index file.
93
94Because the index file itself is written after collecting all
95the stat information from updated paths, `st_mtime` timestamp of
96it is usually the same as or newer than any of the paths the
97index contains. And no matter how quick the modification that
98follows `git update-index foo` finishes, the resulting
99`st_mtime` timestamp on `foo` cannot get a value earlier
100than the index file. Therefore, index entries that can be
101racily clean are limited to the ones that have the same
102timestamp as the index file itself.
103
104The callers that want to check if an index entry matches the
105corresponding file in the working tree continue to call
106`ce_match_stat()`, but with this change, `ce_match_stat()` uses
107`ce_modified_check_fs()` to see if racily clean ones are
108actually clean after comparing the cached stat information using
109`ce_match_stat_basic()`.
110
111The problem the latter solves is this sequence:
112
113 $ git update-index 'foo'
114 : modify 'foo' in-place without changing its size
115 : wait for enough time
116 $ git update-index 'bar'
117
118Without the latter, the timestamp of the index file gets a newer
119value, and falsely clean entry `foo` would not be caught by the
120timestamp comparison check done with the former logic anymore.
121The latter makes sure that the cached stat information for `foo`
122would never match with the file in the working tree, so later
123checks by `ce_match_stat_basic()` would report that the index entry
124does not match the file and Git does not have to fall back on more
125expensive `ce_modified_check_fs()`.
126
127
128Runtime penalty
129---------------
130
131The runtime penalty of falling back to `ce_modified_check_fs()`
132from `ce_match_stat()` can be very expensive when there are many
133racily clean entries. An obvious way to artificially create
134this situation is to give the same timestamp to all the files in
135the working tree in a large project, run `git update-index` on
136them, and give the same timestamp to the index file:
137
138 $ date >.datestamp
139 $ git ls-files | xargs touch -r .datestamp
140 $ git ls-files | git update-index --stdin
141 $ touch -r .datestamp .git/index
142
143This will make all index entries racily clean. The linux project, for
144example, there are over 20,000 files in the working tree. On my
145Athlon 64 X2 3800+, after the above:
146
147 $ /usr/bin/time git diff-files
148 1.68user 0.54system 0:02.22elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k
149 0inputs+0outputs (0major+67111minor)pagefaults 0swaps
150 $ git update-index MAINTAINERS
151 $ /usr/bin/time git diff-files
152 0.02user 0.12system 0:00.14elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k
153 0inputs+0outputs (0major+935minor)pagefaults 0swaps
154
155Running `git update-index` in the middle checked the racily
156clean entries, and left the cached `st_mtime` for all the paths
157intact because they were actually clean (so this step took about
158the same amount of time as the first `git diff-files`). After
159that, they are not racily clean anymore but are truly clean, so
160the second invocation of `git diff-files` fully took advantage
161of the cached stat information.
162
163
164Avoiding runtime penalty
165------------------------
166
167In order to avoid the above runtime penalty, post 1.4.2 Git used
168to have a code that made sure the index file
169got a timestamp newer than the youngest files in the index when
170there were many young files with the same timestamp as the
171resulting index file otherwise would have by waiting
172before finishing writing the index file out.
173
174I suspected that in practice the situation where many paths in the
175index are all racily clean was quite rare. The only code paths
176that can record recent timestamp for large number of paths are:
177
178. Initial `git add .` of a large project.
179
180. `git checkout` of a large project from an empty index into an
181 unpopulated working tree.
182
183Note: switching branches with `git checkout` keeps the cached
184stat information of existing working tree files that are the
185same between the current branch and the new branch, which are
186all older than the resulting index file, and they will not
187become racily clean. Only the files that are actually checked
188out can become racily clean.
189
190In a large project where raciness avoidance cost really matters,
191however, the initial computation of all object names in the
192index takes more than one second, and the index file is written
193out after all that happens. Therefore the timestamp of the
194index file will be more than one second later than the
195youngest file in the working tree. This means that in these
196cases there actually will not be any racily clean entry in
197the resulting index.
198
199Based on this discussion, the current code does not use the
200"workaround" to avoid the runtime penalty that does not exist in
201practice anymore. This was done with commit 0fc82cff on Aug 15,
2022006.