Skip to content

Commit c4b9d5a

Browse files
Make collector call home to perf server
Processing now calls home so that we can display the current status on the status page.
1 parent 057d350 commit c4b9d5a

File tree

10 files changed

+259
-22
lines changed

10 files changed

+259
-22
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collector/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cargo_metadata = "0.5"
1818
lazy_static = "1"
1919
semver = "0.9"
2020
reqwest = "0.8.6"
21+
futures = "0.1"
2122

2223
[dependencies.rustup]
2324
git = "https://github.com/rust-lang-nursery/rustup.rs.git"

collector/src/api.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pub mod collected {
2+
use Commit;
3+
4+
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
5+
pub enum Request {
6+
// Will benchmark commit with these benchmarks
7+
BenchmarkCommit {
8+
commit: Commit,
9+
benchmarks: Vec<String>,
10+
},
11+
// benchmark finished for this benchmark/commit
12+
BenchmarkDone {
13+
benchmark: String,
14+
commit: Commit,
15+
}
16+
}
17+
18+
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
19+
pub struct Response {
20+
// nothing
21+
}
22+
}

collector/src/bin/rustc-perf-collector/main.rs

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ extern crate tempfile;
2121
extern crate rustup;
2222
extern crate semver;
2323
extern crate reqwest;
24+
extern crate futures;
25+
26+
use reqwest::header::{Authorization, Bearer};
2427

2528
use failure::{Error, ResultExt, SyncFailure};
2629

@@ -33,10 +36,14 @@ use std::path::{Path, PathBuf};
3336
use std::io::{stderr, Write};
3437
use std::collections::BTreeMap;
3538
use std::sync::Arc;
39+
use std::thread::{self, JoinHandle};
3640

3741
use chrono::{Timelike, Utc};
42+
use futures::sync::mpsc::{unbounded as unbounded_channel, UnboundedSender, UnboundedReceiver};
43+
use futures::stream::Stream;
3844

3945
use collector::{Commit, ArtifactData, CommitData, Date};
46+
use collector::api::collected;
4047
use rust_sysroot::git::Commit as GitCommit;
4148
use rust_sysroot::sysroot::Sysroot;
4249

@@ -144,6 +151,52 @@ fn kinds_from_arg<K>(strings_and_kinds: &[(&str, K)], arg: &str)
144151
Ok(v)
145152
}
146153

154+
lazy_static! {
155+
static ref BG_THREAD: Arc<(UnboundedSender<Option<collected::Request>>, JoinHandle<()>)> = start_bg_thread();
156+
}
157+
158+
fn start_bg_thread() -> Arc<(UnboundedSender<Option<collected::Request>>, JoinHandle<()>)> {
159+
let (sender, receiver): (_, UnboundedReceiver<Option<collected::Request>>) = unbounded_channel();
160+
let sender_thread = sender.clone();
161+
let handle = thread::spawn(move || {
162+
for request in receiver.wait() {
163+
let request = request.expect("result is Ok");
164+
if request.is_none() {
165+
// termination requested
166+
break;
167+
}
168+
let request = request.unwrap();
169+
let ret = (|| {
170+
let client = reqwest::ClientBuilder::new().build()?;
171+
let resp = client.post(&format!(
172+
"{}/perf/collected",
173+
env::var("SITE_URL").expect("SITE_URL defined")
174+
))
175+
.header(Authorization(Bearer { token: env::var("PERF_SECRET_KEY").unwrap() }))
176+
.json(&request)
177+
.send()?;
178+
if !resp.status().is_success() {
179+
bail!("ping home failed: {:?}", resp);
180+
}
181+
Ok(())
182+
})();
183+
match ret {
184+
Ok(()) => {},
185+
Err(err) => {
186+
warn!("call home failed: {:?}", err);
187+
thread::sleep(std::time::Duration::from_secs(10));
188+
sender_thread.unbounded_send(Some(request)).expect("can send on thread channel");
189+
}
190+
}
191+
}
192+
});
193+
Arc::new((sender, handle))
194+
}
195+
196+
fn send_home(d: &collected::Request) {
197+
BG_THREAD.0.unbounded_send(Some(d.clone())).unwrap();
198+
}
199+
147200
fn bench_commit(
148201
repo: Option<&outrepo::Repo>,
149202
commit: &GitCommit,
@@ -154,18 +207,37 @@ fn bench_commit(
154207
cargo_path: &Path,
155208
benchmarks: &[Benchmark],
156209
iterations: usize,
210+
call_home: bool,
157211
) -> CommitData {
158212
info!(
159213
"benchmarking commit {} ({}) for triple {}",
160214
commit.sha, commit.date, triple
161215
);
162216

217+
if call_home {
218+
send_home(&collected::Request::BenchmarkCommit {
219+
commit: Commit {
220+
sha: commit.sha.clone(),
221+
date: Date(commit.date),
222+
},
223+
benchmarks: benchmarks.iter().map(|b| b.name.clone()).collect(),
224+
});
225+
}
163226
let existing_data = repo.and_then(|r| r.load_commit_data(&commit, &triple).ok());
164227

165228
let mut results = BTreeMap::new();
166229
if let Some(ref data) = existing_data {
167230
for benchmark in benchmarks {
168231
if let Some(result) = data.benchmarks.get(&benchmark.name) {
232+
if call_home {
233+
send_home(&collected::Request::BenchmarkDone {
234+
benchmark: benchmark.name.clone(),
235+
commit: Commit {
236+
sha: commit.sha.clone(),
237+
date: Date(commit.date),
238+
},
239+
});
240+
}
169241
results.insert(benchmark.name.clone(), result.clone());
170242
}
171243
}
@@ -187,6 +259,16 @@ fn bench_commit(
187259
}
188260
};
189261

262+
if call_home {
263+
send_home(&collected::Request::BenchmarkDone {
264+
benchmark: benchmark.name.clone(),
265+
commit: Commit {
266+
sha: commit.sha.clone(),
267+
date: Date(commit.date),
268+
},
269+
});
270+
}
271+
190272
results.insert(benchmark.name.clone(), result);
191273
info!("{} benchmarks left", benchmarks.len() - results.len());
192274
}
@@ -332,7 +414,7 @@ fn main_result() -> Result<i32, Error> {
332414
rust_sysroot::get_commits(rust_sysroot::EPOCH_COMMIT, "master").map_err(SyncFailure::new)
333415
};
334416

335-
match matches.subcommand() {
417+
let ret = match matches.subcommand() {
336418
("bench_commit", Some(sub_m)) => {
337419
let commit = sub_m.value_of("COMMIT").unwrap();
338420
let commit = get_commits()?
@@ -362,6 +444,7 @@ fn main_result() -> Result<i32, Error> {
362444
&sysroot.cargo,
363445
&benchmarks,
364446
3,
447+
false,
365448
))?;
366449
Ok(0)
367450
}
@@ -398,6 +481,7 @@ fn main_result() -> Result<i32, Error> {
398481
&cargo_path,
399482
&benchmarks,
400483
1,
484+
false,
401485
);
402486
get_out_repo(true)?.add_commit_data(&result)?;
403487
Ok(0)
@@ -437,6 +521,7 @@ fn main_result() -> Result<i32, Error> {
437521
&toolchain.binary_file("cargo"),
438522
&benchmarks,
439523
3,
524+
false,
440525
);
441526
repo.success_artifact(&ArtifactData { id: id.to_string(), benchmarks: benchmark_data })?;
442527
Ok(0)
@@ -473,6 +558,7 @@ fn main_result() -> Result<i32, Error> {
473558
&sysroot.cargo,
474559
&benchmarks,
475560
3,
561+
true,
476562
));
477563
if let Err(err) = result {
478564
out_repo.write_broken_commit(&commit, err)?;
@@ -556,6 +642,7 @@ fn main_result() -> Result<i32, Error> {
556642
&sysroot.cargo,
557643
&benchmarks,
558644
1,
645+
false,
559646
);
560647
} else {
561648
panic!("no commits");
@@ -567,5 +654,8 @@ fn main_result() -> Result<i32, Error> {
567654
let _ = writeln!(stderr(), "{}", matches.usage());
568655
Ok(2)
569656
}
570-
}
657+
};
658+
BG_THREAD.0.unbounded_send(None).unwrap();
659+
thread::sleep(std::time::Duration::from_secs(1));
660+
ret
571661
}

collector/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use chrono::{DateTime, Datelike, Duration, TimeZone, Utc};
1919
use chrono::naive::NaiveDate;
2020
use serde::{Deserialize, Serialize};
2121

22+
pub mod api;
23+
2224
#[derive(Debug, Clone, Deserialize, Serialize)]
2325
pub struct Commit {
2426
pub sha: String,

site/src/api.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ pub mod nll_dashboard {
190190

191191
pub mod status {
192192
use collector::Commit;
193+
use load::CurrentState;
193194

194195
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
195196
pub struct BenchmarkStatus {
@@ -198,10 +199,12 @@ pub mod status {
198199
pub error: Option<String>,
199200
}
200201

201-
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
202+
#[derive(Debug, Clone, Serialize, Deserialize)]
202203
pub struct Response {
203204
pub last_commit: Commit,
204205
pub benchmarks: Vec<BenchmarkStatus>,
205206
pub missing: Vec<Commit>,
207+
pub current: Option<CurrentState>,
206208
}
207209
}
210+

site/src/load.rs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use std::io::Read;
1414
use std::env;
1515

1616
use serde_json;
17-
use failure::Error;
17+
use antidote::Mutex;
18+
use failure::{ResultExt, Error};
1819
use failure::SyncFailure;
1920
use rust_sysroot;
2021
use rust_sysroot::git::Commit as GitCommit;
@@ -26,6 +27,40 @@ use collector::Date;
2627

2728
pub use collector::{Commit, CommitData, ArtifactData, Patch, Run, Stat};
2829

30+
#[derive(Clone, Deserialize, Serialize, Debug)]
31+
pub struct CurrentState {
32+
pub commit: Commit,
33+
pub benchmarks: Vec<String>,
34+
}
35+
36+
#[derive(Clone, Deserialize, Serialize, Debug)]
37+
pub struct Persistent {
38+
pub try_commits: Vec<String>,
39+
pub current: Option<CurrentState>,
40+
}
41+
42+
impl Persistent {
43+
pub fn write(&self) -> Result<(), Error> {
44+
let s = serde_json::to_string(self)?;
45+
fs::write("persistent.json", &s).with_context(|_| format!("failed to write persistent DB"))?;
46+
Ok(())
47+
}
48+
49+
fn load() -> Persistent {
50+
Persistent::load_().unwrap_or_else(|| Persistent {
51+
try_commits: Vec::new(),
52+
current: None,
53+
})
54+
}
55+
56+
fn load_() -> Option<Persistent> {
57+
let s = fs::read_to_string("persistent.json").ok()?;
58+
let persistent: Persistent = serde_json::from_str(&s).ok()?;
59+
60+
Some(persistent)
61+
}
62+
}
63+
2964
#[derive(Debug)]
3065
pub struct InputData {
3166
/// A set containing all crate names of the bootstrap kind.
@@ -43,9 +78,9 @@ pub struct InputData {
4378

4479
pub artifact_data: BTreeMap<String, ArtifactData>,
4580

46-
pub try_commits: Vec<String>,
47-
4881
pub commits: Vec<GitCommit>,
82+
83+
pub persistent: Mutex<Persistent>,
4984
}
5085

5186
impl InputData {
@@ -56,10 +91,6 @@ impl InputData {
5691
let mut artifact_data = BTreeMap::new();
5792
let mut data = BTreeMap::new();
5893

59-
let try_commits = fs::read_to_string("try-commit-queue")
60-
.expect("could not open try-commit-queue file")
61-
.lines().map(|s| s.to_owned()).collect::<Vec<_>>();
62-
6394
if !repo_loc.exists() {
6495
// If the repository doesn't yet exist, simplify clone it to the given location.
6596
info!(
@@ -135,13 +166,12 @@ impl InputData {
135166
info!("{} skipped files", skipped);
136167
info!("{} measured", data.len());
137168

138-
InputData::new(data, artifact_data, try_commits)
169+
InputData::new(data, artifact_data)
139170
}
140171

141172
pub fn new(
142173
data: BTreeMap<Commit, CommitData>,
143174
artifact_data: BTreeMap<String, ArtifactData>,
144-
try_commits: Vec<String>,
145175
) -> Result<InputData, Error> {
146176
let mut last_date = None;
147177
let mut crate_list = BTreeSet::new();
@@ -177,7 +207,7 @@ impl InputData {
177207
data: data,
178208
artifact_data,
179209
commits,
180-
try_commits,
210+
persistent: Mutex::new(Persistent::load()),
181211
})
182212
}
183213

@@ -205,8 +235,9 @@ impl InputData {
205235
})
206236
.collect::<Vec<_>>();
207237
missing.reverse();
208-
Ok(self.try_commits.iter()
238+
Ok(self.persistent.lock().try_commits.iter()
209239
.map(|sha| Commit { sha: sha.clone(), date: Date::ymd_hms(2001, 01, 01, 0, 0, 0) })
240+
.filter(|c| !have.contains_key(&c.sha)) // we may have not updated the try-commits file
210241
.chain(missing)
211242
.collect())
212243
}

site/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ fn main() {
2424
.unwrap_or(2346);
2525
println!("Starting server with port={:?}", port);
2626

27-
server::start(data, port);
27+
let key = env::var("PERF_SECRET_KEY").unwrap_or(String::from("QuiteSecret"));
28+
29+
server::start(data, port, key);
2830
}

0 commit comments

Comments
 (0)