aboutsummaryrefslogtreecommitdiffhomepage
path: root/autoload.cpp
blob: a886df52290485e5fa847d3e0f34aef5d7fd0abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/** \file autoload.cpp
	
The classes responsible for autoloading functions and completions.
*/

#include "config.h"
#include "autoload.h"
#include "wutil.h"
#include <assert.h>

const size_t kLRULimit = 256;

file_access_attempt_t access_file(const wcstring &path, int mode) {
    file_access_attempt_t result = {0};
    struct stat statbuf;
    if (wstat(path.c_str(), &statbuf)) {
        result.error = errno;
    } else {
        result.mod_time = statbuf.st_mtime;
        if (waccess(path.c_str(), mode)) {
            result.error = errno;
        } else {
            result.accessible = true;
        }
    }
    
    // Note that we record the last checked time after the call, on the assumption that in a slow filesystem, the lag comes before the kernel check, not after.
    result.stale = false;
    result.last_checked = time(NULL);
    return result;
}

/** A node in our LRU map */
class file_access_node_t {
public:
    file_access_node_t *prev, *next;
    const wcstring path;
    file_access_attempt_t attempt;
    
    file_access_node_t(const wcstring &pathVar) : path(pathVar) { }
    
    bool operator<(const file_access_node_t &other) const { return path < other.path; }
};

/* By default, things are stale after 60 seconds */
const time_t kFishDefaultStalenessInterval = 60;

access_tracker_t::access_tracker_t(time_t stale, int the_mode) :
        node_count(0),
        mouth(NULL),
        stale_interval(stale),
        mode(the_mode)
{
    VOMIT_ON_FAILURE(pthread_mutex_init(&lock, NULL));
}

access_tracker_t::~access_tracker_t() {
    VOMIT_ON_FAILURE(pthread_mutex_destroy(&lock));
}

void access_tracker_t::vacuum_one_node(void) {
    /* Removes the least recently used access */
    assert(mouth && mouth->prev != mouth);
    
    /* Remove us from the linked list */
    file_access_node_t *condemned_node = mouth->prev;
    condemned_node->prev->next = condemned_node->next;
    condemned_node->next->prev = condemned_node->prev;
    
    /* Remove us from the set */
    access_set.erase(condemned_node);
    
    /* Deleted */
    node_count--;
    delete condemned_node;
}

void access_tracker_t::promote_node(file_access_node_t *node) {
    /* Promotes a node to the mouth, unless we're already there... */
    if (node == mouth) return;
    
    /* First unhook us */
    node->prev->next = node->next;
    node->next->prev = node->prev;
    
    /* Now become the mouth */
    node->next = mouth;
    node->prev = mouth->prev;
    mouth = node;
}

/* Return the node referenced by the given string, or NULL */
file_access_node_t *access_tracker_t::while_locked_find_node(const wcstring &path) const {
    file_access_node_t key(path);
    access_set_t::iterator iter = access_set.find(&key);
    return iter != access_set.end() ? *iter : NULL;
}

file_access_attempt_t access_tracker_t::attempt_access(const wcstring& path) const {
    return ::access_file(path, this->mode);
}

bool access_tracker_t::access_file_only_cached(const wcstring &path, file_access_attempt_t &attempt) {
    bool result = false;
    
    /* Lock our lock */
    scoped_lock locker(lock);
    
    /* Search for the node */
    file_access_node_t *node = while_locked_find_node(path);
    if (node) {
        promote_node(node);
        attempt = node->attempt;
        attempt.stale = (time(NULL) - node->attempt.last_checked > this->stale_interval);
        result = true;
    }    
    return result;
}

file_access_attempt_t access_tracker_t::access_file(const wcstring &path) {
    file_access_attempt_t result;
    
    /* Try just using our cache */
    if (access_file_only_cached(path, result) && ! result.stale) {
        return result;
    }
    
    /* Really access the file. Note we are not yet locked, and don't want to be, since this may be slow. */
    result = attempt_access(path);
    
    /* Take the lock so we can insert. */
    scoped_lock locker(lock);
    
    /* Maybe we had it cached and it was stale, or maybe someone else may have put it in the cache while we were unlocked */
    file_access_node_t *node = while_locked_find_node(path);
    
    if (node != NULL) {
        /* Someone else put it in. Promote and overwrite it. */
        node->attempt = result;
        promote_node(node);
    } else {
        /* We did not find this node. Add it. */
        file_access_node_t *node = new file_access_node_t(path);
        node->attempt = result;
        
        /* Insert into the set */
        access_set.insert(node);
        
        /* Insert it into the linked list */
        if (mouth == NULL) {
            /* One element circularly linked list! */
            mouth = node->next = node->prev = node;
        } else {
            /* Normal circularly linked list operation */
            node->next = mouth;
            node->prev = mouth->prev;
            mouth = node;
        }
        
        /* We have one more node now */
        ++node_count;
        
        /* Clean up if we're over our limit */
        while (node_count > kLRULimit)
            vacuum_one_node();
    }
    return result;
}

lru_cache_impl_t::lru_cache_impl_t() : node_count(0), mouth(L"") {
    /* Hook up the mouth to itself: a one node circularly linked list */
    mouth.prev = mouth.next = &mouth;
}

void lru_cache_impl_t::evict_node(lru_node_t *condemned_node) {
    /* We should never evict the mouth */
    assert(condemned_node != NULL && condemned_node != &mouth);

    /* Remove it from the linked list */
    condemned_node->prev->next = condemned_node->next;
    condemned_node->next->prev = condemned_node->prev;

    /* Remove us from the set */
    node_set.erase(condemned_node);
    node_count--;

    /* Tell it */
    condemned_node->evicted();
}

void lru_cache_impl_t::evict_last_node(void) {
    /* Simple */
    evict_node(mouth.prev);
}

bool lru_cache_impl_t::evict_node(const wcstring &key) {
    /* Construct a fake node as our key */
    lru_node_t node_key(key);
    
    /* Look for it in the set */
    node_set_t::iterator iter = node_set.find(&node_key);
    if (iter == node_set.end())
        return false;
    
    /* Evict the given node */
    evict_node(*iter);
    return true;
}

void lru_cache_impl_t::promote_node(lru_node_t *node) {
    /* We should never promote the mouth */
    assert(node != &mouth);
    
    /* First unhook us */
    node->prev->next = node->next;
    node->next->prev = node->prev;
    
    /* Put us after the mouth */
    node->next = mouth.next;
    node->prev = &mouth;
    mouth.next = node;
}

bool lru_cache_impl_t::add_node(lru_node_t *node) {
    assert(node != NULL && node != &mouth);
    
    /* Try inserting; return false if it was already in the set */
    if (! node_set.insert(node).second)
        return false;
    
    /* Update the count */
    node_count++;
    
    /* Add the node after the mouth */
    node->next = mouth.next;
    node->prev = &mouth;
    mouth.next = node;
    
    /* Success */
    return true;
}

lru_node_t *lru_cache_impl_t::get_node(const wcstring &key) {
    lru_node_t *result = NULL;
    
    /* Construct a fake node as our key */
    lru_node_t node_key(key);
    
    /* Look for it in the set */
    node_set_t::iterator iter = node_set.find(&node_key);
    
    /* If we found a node, promote and return it */
    if (iter != node_set.end()) {
        result = *iter;
        promote_node(result);
    }
    return result;
}

void lru_cache_impl_t::evict_all_nodes() {
    while (node_count > 0) {
        evict_last_node();
    }
}