Skip to content

Commit 2d72f1a

Browse files
committed
Write stats to cache_stats_file after compile
Signed-off-by: Zack Cerza <[email protected]>
1 parent a888c16 commit 2d72f1a

File tree

6 files changed

+74
-9
lines changed

6 files changed

+74
-9
lines changed

docs/Configuration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# If specified, wait this long for the server to start up.
77
server_startup_timeout_ms = 10000
88

9+
# If specified, write JSON-formatted stats to this file after each compile operation.
10+
cache_stats_file = "/home/user/.cache/sccache-stats.json"
11+
912
[dist]
1013
# where to find the scheduler
1114
scheduler_url = "http://1.2.3.4:10600"

src/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ pub struct FileConfig {
585585
pub cache: CacheConfigs,
586586
pub dist: DistConfig,
587587
pub server_startup_timeout_ms: Option<u64>,
588+
pub cache_stats_file: Option<PathBuf>,
588589
}
589590

590591
// If the file doesn't exist or we can't read it, log the issue and proceed. If the
@@ -977,6 +978,7 @@ pub struct Config {
977978
pub fallback_cache: DiskCacheConfig,
978979
pub dist: DistConfig,
979980
pub server_startup_timeout: Option<std::time::Duration>,
981+
pub cache_stats_file: Option<PathBuf>,
980982
}
981983

982984
impl Config {
@@ -998,6 +1000,7 @@ impl Config {
9981000
cache,
9991001
dist,
10001002
server_startup_timeout_ms,
1003+
cache_stats_file,
10011004
} = file_conf;
10021005
conf_caches.merge(cache);
10031006

@@ -1013,6 +1016,7 @@ impl Config {
10131016
fallback_cache,
10141017
dist,
10151018
server_startup_timeout,
1019+
cache_stats_file,
10161020
}
10171021
}
10181022
}
@@ -1312,6 +1316,7 @@ fn config_overrides() {
13121316
},
13131317
dist: Default::default(),
13141318
server_startup_timeout_ms: None,
1319+
cache_stats_file: None,
13151320
};
13161321

13171322
assert_eq!(
@@ -1334,6 +1339,7 @@ fn config_overrides() {
13341339
},
13351340
dist: Default::default(),
13361341
server_startup_timeout: None,
1342+
cache_stats_file: None,
13371343
}
13381344
);
13391345
}
@@ -1463,6 +1469,7 @@ fn test_gcs_service_account() {
14631469
fn full_toml_parse() {
14641470
const CONFIG_STR: &str = r#"
14651471
server_startup_timeout_ms = 10000
1472+
cache_stats_file = "/home/user/.cache/sccache-stats.json"
14661473
14671474
[dist]
14681475
# where to find the scheduler
@@ -1623,6 +1630,7 @@ no_credentials = true
16231630
rewrite_includes_only: false,
16241631
},
16251632
server_startup_timeout_ms: Some(10000),
1633+
cache_stats_file: Some(PathBuf::from("/home/user/.cache/sccache-stats.json"))
16261634
}
16271635
)
16281636
}
@@ -1715,6 +1723,7 @@ size = "7g"
17151723
..Default::default()
17161724
},
17171725
server_startup_timeout_ms: None,
1726+
cache_stats_file: None,
17181727
}
17191728
);
17201729
}

src/server.rs

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use anyhow::Context as _;
3131
use bytes::{buf::BufMut, Bytes, BytesMut};
3232
use filetime::FileTime;
3333
use fs::metadata;
34+
use fs::File;
3435
use fs_err as fs;
3536
use futures::channel::mpsc;
3637
use futures::future::FutureExt;
@@ -42,15 +43,15 @@ use std::collections::{HashMap, HashSet};
4243
use std::env;
4344
use std::ffi::OsString;
4445
use std::future::Future;
45-
use std::io::{self, Write};
46+
use std::io::{self, BufWriter, Write};
4647
use std::marker::Unpin;
4748
#[cfg(feature = "dist-client")]
4849
use std::mem;
4950
#[cfg(target_os = "android")]
5051
use std::os::android::net::SocketAddrExt;
5152
#[cfg(target_os = "linux")]
5253
use std::os::linux::net::SocketAddrExt;
53-
use std::path::PathBuf;
54+
use std::path::{Path, PathBuf};
5455
use std::pin::Pin;
5556
use std::process::{ExitStatus, Output};
5657
use std::sync::Arc;
@@ -488,8 +489,14 @@ pub fn start_server(config: &Config, addr: &crate::net::SocketAddr) -> Result<()
488489
crate::net::SocketAddr::Net(addr) => {
489490
trace!("binding TCP {addr}");
490491
let l = runtime.block_on(tokio::net::TcpListener::bind(addr))?;
491-
let srv =
492-
SccacheServer::<_>::with_listener(l, runtime, client, dist_client, storage);
492+
let srv = SccacheServer::<_>::with_listener(
493+
l,
494+
runtime,
495+
client,
496+
dist_client,
497+
storage,
498+
config.cache_stats_file.clone(),
499+
);
493500
Ok((
494501
srv.local_addr().unwrap(),
495502
Box::new(move |f| srv.run(f)) as Box<dyn FnOnce(_) -> _>,
@@ -504,8 +511,14 @@ pub fn start_server(config: &Config, addr: &crate::net::SocketAddr) -> Result<()
504511
let _guard = runtime.enter();
505512
tokio::net::UnixListener::bind(path)?
506513
};
507-
let srv =
508-
SccacheServer::<_>::with_listener(l, runtime, client, dist_client, storage);
514+
let srv = SccacheServer::<_>::with_listener(
515+
l,
516+
runtime,
517+
client,
518+
dist_client,
519+
storage,
520+
config.cache_stats_file.clone(),
521+
);
509522
Ok((
510523
srv.local_addr().unwrap(),
511524
Box::new(move |f| srv.run(f)) as Box<dyn FnOnce(_) -> _>,
@@ -579,6 +592,7 @@ impl<C: CommandCreatorSync> SccacheServer<tokio::net::TcpListener, C> {
579592
client: Client,
580593
dist_client: DistClientContainer,
581594
storage: Arc<dyn Storage>,
595+
cache_stats_file: Option<PathBuf>,
582596
) -> Result<Self> {
583597
let addr = crate::net::SocketAddr::with_port(port);
584598
let listener = runtime.block_on(tokio::net::TcpListener::bind(addr.as_net().unwrap()))?;
@@ -589,6 +603,7 @@ impl<C: CommandCreatorSync> SccacheServer<tokio::net::TcpListener, C> {
589603
client,
590604
dist_client,
591605
storage,
606+
cache_stats_file,
592607
))
593608
}
594609
}
@@ -600,13 +615,22 @@ impl<A: crate::net::Acceptor, C: CommandCreatorSync> SccacheServer<A, C> {
600615
client: Client,
601616
dist_client: DistClientContainer,
602617
storage: Arc<dyn Storage>,
618+
cache_stats_file: Option<PathBuf>,
603619
) -> Self {
604620
// Prepare the service which we'll use to service all incoming TCP
605621
// connections.
606622
let (tx, rx) = mpsc::channel(1);
607623
let (wait, info) = WaitUntilZero::new();
608624
let pool = runtime.handle().clone();
609-
let service = SccacheService::new(dist_client, storage, &client, pool, tx, info);
625+
let service = SccacheService::new(
626+
dist_client,
627+
storage,
628+
&client,
629+
pool,
630+
tx,
631+
info,
632+
cache_stats_file.clone(),
633+
);
610634

611635
SccacheServer {
612636
runtime,
@@ -818,6 +842,10 @@ where
818842
/// This field causes [WaitUntilZero] to wait until this struct drops.
819843
#[allow(dead_code)]
820844
info: ActiveInfo,
845+
846+
/// A file that will contain JSON-formatted stats output, written after
847+
/// each compile operation.
848+
cache_stats_file: Option<PathBuf>,
821849
}
822850

823851
type SccacheRequest = Message<Request, Body<()>>;
@@ -857,7 +885,11 @@ where
857885
Request::Compile(compile) => {
858886
debug!("handle_client: compile");
859887
me.stats.lock().await.compile_requests += 1;
860-
me.handle_compile(compile).await
888+
let resp = me.handle_compile(compile).await;
889+
if let Some(val) = &me.cache_stats_file {
890+
me.stats.lock().await.clone().write(val)?
891+
}
892+
resp
861893
}
862894
Request::GetStats => {
863895
debug!("handle_client: get_stats");
@@ -916,6 +948,7 @@ where
916948
rt: tokio::runtime::Handle,
917949
tx: mpsc::Sender<ServerMessage>,
918950
info: ActiveInfo,
951+
cache_stats_file: Option<PathBuf>,
919952
) -> SccacheService<C> {
920953
SccacheService {
921954
stats: Arc::default(),
@@ -927,6 +960,7 @@ where
927960
creator: C::new(client),
928961
tx,
929962
info,
963+
cache_stats_file,
930964
}
931965
}
932966

@@ -938,6 +972,7 @@ where
938972
let (_, info) = WaitUntilZero::new();
939973
let client = Client::new_num(1);
940974
let dist_client = DistClientContainer::new_disabled();
975+
let cache_stats_file = None;
941976
SccacheService {
942977
stats: Arc::default(),
943978
dist_client: Arc::new(dist_client),
@@ -948,6 +983,7 @@ where
948983
creator: C::new(&client),
949984
tx,
950985
info,
986+
cache_stats_file,
951987
}
952988
}
953989

@@ -960,6 +996,7 @@ where
960996
let (tx, _) = mpsc::channel(1);
961997
let (_, info) = WaitUntilZero::new();
962998
let client = Client::new_num(1);
999+
let cache_stats_file = None;
9631000
SccacheService {
9641001
stats: Arc::default(),
9651002
dist_client: Arc::new(DistClientContainer::new_with_state(DistClientState::Some(
@@ -981,6 +1018,7 @@ where
9811018
creator: C::new(&client),
9821019
tx,
9831020
info,
1021+
cache_stats_file,
9841022
}
9851023
}
9861024

@@ -1909,6 +1947,19 @@ impl ServerStats {
19091947
);
19101948
}
19111949
}
1950+
1951+
/// Write stats in JSON format to a file.
1952+
fn write(&self, path: &Path) -> Result<()> {
1953+
let file = match File::create(path) {
1954+
Ok(f) => f,
1955+
Err(e) => {
1956+
debug!("Couldn't open stats file for writing: {}", e);
1957+
return Ok(());
1958+
}
1959+
};
1960+
let mut writer = BufWriter::new(file);
1961+
Ok(serde_json::to_writer(&mut writer, self)?)
1962+
}
19121963
}
19131964

19141965
fn set_percentage_stat(

src/test/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ where
8989
));
9090

9191
let client = Client::new();
92-
let srv = SccacheServer::new(0, runtime, client, dist_client, storage).unwrap();
92+
let srv = SccacheServer::new(0, runtime, client, dist_client, storage, None).unwrap();
9393
let mut srv: SccacheServer<_, Arc<Mutex<MockCommandCreator>>> = srv;
9494
let addr = srv.local_addr().unwrap();
9595
assert!(matches!(addr, crate::net::SocketAddr::Net(a) if a.port() > 0));

tests/harness/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ pub fn sccache_client_cfg(
189189
rewrite_includes_only: false, // TODO
190190
},
191191
server_startup_timeout_ms: None,
192+
cache_stats_file: None,
192193
}
193194
}
194195

tests/oauth.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn config_with_dist_auth(
6060
rewrite_includes_only: true,
6161
},
6262
server_startup_timeout_ms: None,
63+
cache_stats_file: None,
6364
}
6465
}
6566

0 commit comments

Comments
 (0)