File size: 9,468 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::{
    error::Error,
    fs,
    path::PathBuf,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
};

use call_resolver::CallResolver;
use clap::Parser;
use identifier::{Identifier, IdentifierReference};
use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet};
use syn::visit::Visit;
use visitor::CallingStyleVisitor;

use crate::visitor::CallingStyle;

mod call_resolver;
mod identifier;
mod lsp_client;
mod visitor;

#[derive(Parser)]
struct Opt {
    #[clap(required = true)]
    paths: Vec<PathBuf>,

    /// reparse all files
    #[clap(long)]
    reparse: bool,

    /// reindex all files
    #[clap(long)]
    reindex: bool,
}

fn main() -> Result<(), Box<dyn Error>> {
    tracing_subscriber::fmt::init();
    let opt = Opt::parse();

    let mut connection = lsp_client::RAClient::new();
    connection.start(&opt.paths);

    let call_resolver = CallResolver::new(&mut connection, Some("call_resolver.bincode".into()));
    let mut call_resolver = if opt.reindex {
        call_resolver.cleared()
    } else {
        call_resolver
    };

    let halt = Arc::new(AtomicBool::new(false));
    let halt_clone = halt.clone();
    ctrlc::set_handler({
        move || {
            halt_clone.store(true, Ordering::SeqCst);
        }
    })?;

    tracing::info!("getting tasks");
    let mut tasks = get_all_tasks(&opt.paths);
    let dep_tree = resolve_tasks(&mut tasks, &mut call_resolver, halt.clone());
    let concurrency = resolve_concurrency(&tasks, &dep_tree, halt.clone());

    write_dep_tree(&tasks, concurrency, std::path::Path::new("graph.cypherl"));

    if halt.load(Ordering::Relaxed) {
        tracing::info!("ctrl-c detected, exiting");
    }

    Ok(())
}

/// search the given folders recursively and attempt to find all tasks inside
#[tracing::instrument(skip_all)]
fn get_all_tasks(folders: &[PathBuf]) -> FxHashMap<Identifier, Vec<String>> {
    let mut out = FxHashMap::default();

    for folder in folders {
        let walker = ignore::Walk::new(folder);
        for entry in walker {
            let entry = entry.unwrap();
            let rs_file = if let Some(true) = entry.file_type().map(|t| t.is_file()) {
                let path = entry.path();
                let ext = path.extension().unwrap_or_default();
                if ext == "rs" {
                    std::fs::canonicalize(path).unwrap()
                } else {
                    continue;
                }
            } else {
                continue;
            };

            let file = fs::read_to_string(&rs_file).unwrap();
            let lines = file.lines();
            let mut occurrences = vec![];

            tracing::debug!("processing {}", rs_file.display());

            for ((_, line), (line_no, _)) in lines.enumerate().tuple_windows() {
                if line.contains("turbo_tasks::function") {
                    tracing::debug!("found at {:?}:L{}", rs_file, line_no);
                    occurrences.push(line_no + 1);
                }
            }

            if occurrences.is_empty() {
                continue;
            }

            // parse the file using syn and get the span of the functions
            let file = syn::parse_file(&file).unwrap();
            let occurrences_count = occurrences.len();
            let mut visitor = visitor::TaskVisitor::new();
            syn::visit::visit_file(&mut visitor, &file);
            if visitor.results.len() != occurrences_count {
                tracing::warn!(
                    "file {:?} passed the heuristic with {:?} but the visitor found {:?}",
                    rs_file,
                    occurrences_count,
                    visitor.results.len()
                );
            }

            out.extend(
                visitor
                    .results
                    .into_iter()
                    .map(move |(ident, tags)| ((rs_file.clone(), ident).into(), tags)),
            )
        }
    }

    out
}

/// Given a list of tasks, get all the tasks that call that one
fn resolve_tasks(
    tasks: &mut FxHashMap<Identifier, Vec<String>>,
    client: &mut CallResolver,
    halt: Arc<AtomicBool>,
) -> FxHashMap<Identifier, Vec<IdentifierReference>> {
    tracing::info!(
        "found {} tasks, of which {} cached",
        tasks.len(),
        client.cached_count()
    );

    let mut unresolved = tasks.keys().cloned().collect::<FxHashSet<_>>();
    let mut resolved = FxHashMap::default();

    while let Some(top) = unresolved.iter().next().cloned() {
        unresolved.remove(&top);

        let callers = client.resolve(&top);

        // add all non-task callers to the unresolved list if they are not in the
        // resolved list
        for caller in callers.iter() {
            if !resolved.contains_key(&caller.identifier)
                && !unresolved.contains(&caller.identifier)
            {
                tracing::debug!("adding {} to unresolved", caller.identifier);
                unresolved.insert(caller.identifier.to_owned());
            }
        }
        resolved.insert(top.to_owned(), callers);

        if halt.load(Ordering::Relaxed) {
            break;
        }
    }

    resolved
}

/// given a map of tasks and functions that call it, produce a map of tasks and
/// those tasks that it calls
///
/// returns a list of pairs with a task, the task that calls it, and the calling
/// style
fn resolve_concurrency(
    task_list: &FxHashMap<Identifier, Vec<String>>,
    dep_tree: &FxHashMap<Identifier, Vec<IdentifierReference>>, // pairs of tasks and call trees
    halt: Arc<AtomicBool>,
) -> Vec<(Identifier, Identifier, CallingStyle)> {
    // println!("{:?}", dep_tree);
    // println!("{:#?}", task_list);

    let mut edges = vec![];

    for (ident, references) in dep_tree {
        for reference in references {
            #[allow(clippy::map_entry)] // This doesn't insert into dep_tree, so entry isn't useful
            if !dep_tree.contains_key(&reference.identifier) {
                // this is a task that is not in the task list
                // so we can't resolve it
                tracing::error!("missing task for {}: {}", ident, reference.identifier);
                for task in task_list.keys() {
                    if task.name == reference.identifier.name {
                        // we found a task that is not in the task list
                        // so we can't resolve it
                        tracing::trace!("- found {}", task);
                        continue;
                    }
                }
                continue;
            } else {
                // load the source file and get the calling style
                let target = IdentifierReference {
                    identifier: ident.clone(),
                    references: reference.references.clone(),
                };
                let mut visitor = CallingStyleVisitor::new(target);
                tracing::info!("looking for {} from {}", ident, reference.identifier);
                let file =
                    syn::parse_file(&fs::read_to_string(&reference.identifier.path).unwrap())
                        .unwrap();
                visitor.visit_file(&file);

                edges.push((
                    ident.clone(),
                    reference.identifier.clone(),
                    visitor.result().unwrap_or(CallingStyle::Once),
                ));
            }

            if halt.load(Ordering::Relaxed) {
                break;
            }
        }
    }

    // parse each fn between parent and child and get the max calling style

    edges
}

/// Write the dep tree into the given file using cypher syntax
fn write_dep_tree(
    task_list: &FxHashMap<Identifier, Vec<String>>,
    dep_tree: Vec<(Identifier, Identifier, CallingStyle)>,
    out: &std::path::Path,
) {
    use std::io::Write;

    let mut node_ids = FxHashMap::default();
    let mut counter = 0;

    let mut file = std::fs::File::create(out).unwrap();

    let empty = vec![];

    // collect all tasks as well as all intermediate nodes
    // tasks come last to ensure the tags are preserved
    let node_list = dep_tree
        .iter()
        .flat_map(|(dest, src, _)| [(src, &empty), (dest, &empty)])
        .chain(task_list)
        .collect::<FxHashMap<_, _>>();

    for (ident, tags) in node_list {
        counter += 1;

        let label = if !task_list.contains_key(ident) {
            "Function"
        } else if tags.contains(&"fs".to_string()) || tags.contains(&"network".to_string()) {
            "ImpureTask"
        } else {
            "Task"
        };

        _ = writeln!(
            file,
            "CREATE (n_{}:{} {{name: '{}', file: '{}', line: {}, tags: [{}]}})",
            counter,
            label,
            ident.name,
            ident.path,
            ident.range.start.line,
            tags.iter().map(|t| format!("\"{t}\"")).join(",")
        );
        node_ids.insert(ident, counter);
    }

    for (dest, src, style) in &dep_tree {
        let style = match style {
            CallingStyle::Once => "ONCE",
            CallingStyle::ZeroOrOnce => "ZERO_OR_ONCE",
            CallingStyle::ZeroOrMore => "ZERO_OR_MORE",
            CallingStyle::OneOrMore => "ONE_OR_MORE",
        };

        let src_id = *node_ids.get(src).unwrap();
        let dst_id = *node_ids.get(dest).unwrap();

        _ = writeln!(file, "CREATE (n_{src_id})-[:{style}]->(n_{dst_id})",);
    }
}