Skip to content

Commit abbd88e

Browse files
glandiumsylvestre
authored andcommitted
Avoid spawning a new server for --show-stats
Fixes #2042
1 parent dedab5d commit abbd88e

5 files changed

Lines changed: 63 additions & 28 deletions

File tree

src/commands.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use crate::cache::storage_from_config;
1516
use crate::client::{connect_to_server, connect_with_retry, ServerConnection};
1617
use crate::cmdline::{Command, StatsFormat};
1718
use crate::compiler::ColorMode;
1819
use crate::config::{default_disk_cache_dir, Config};
1920
use crate::jobserver::Client;
2021
use crate::mock_command::{CommandChild, CommandCreatorSync, ProcessCommandCreator, RunCommand};
2122
use crate::protocol::{Compile, CompileFinished, CompileResponse, Request, Response};
22-
use crate::server::{self, DistInfo, ServerInfo, ServerStartup};
23+
use crate::server::{self, DistInfo, ServerInfo, ServerStartup, ServerStats};
2324
use crate::util::daemonize;
2425
use byteorder::{BigEndian, ByteOrder};
2526
use fs::{File, OpenOptions};
@@ -607,8 +608,16 @@ pub fn run_command(cmd: Command) -> Result<i32> {
607608
match cmd {
608609
Command::ShowStats(fmt, advanced) => {
609610
trace!("Command::ShowStats({:?})", fmt);
610-
let srv = connect_or_start_server(get_port(), startup_timeout)?;
611-
let stats = request_stats(srv).context("failed to get stats from server")?;
611+
let stats = match connect_to_server(get_port()) {
612+
Ok(srv) => request_stats(srv).context("failed to get stats from server")?,
613+
// If there is no server, spawning a new server would start with zero stats
614+
// anyways, so we can just return (mostly) empty stats directly.
615+
Err(_) => {
616+
let runtime = Runtime::new()?;
617+
let storage = storage_from_config(config, runtime.handle()).ok();
618+
runtime.block_on(ServerInfo::new(ServerStats::default(), storage.as_deref()))?
619+
}
620+
};
612621
match fmt {
613622
StatsFormat::Text => stats.print(advanced),
614623
StatsFormat::Json => serde_json::to_writer(&mut io::stdout(), &stats)?,

src/server.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -910,22 +910,7 @@ where
910910
/// Get info and stats about the cache.
911911
async fn get_info(&self) -> Result<ServerInfo> {
912912
let stats = self.stats.lock().await.clone();
913-
let cache_location = self.storage.location();
914-
let use_preprocessor_cache_mode = self
915-
.storage
916-
.preprocessor_cache_mode_config()
917-
.use_preprocessor_cache_mode;
918-
let version = env!("CARGO_PKG_VERSION").to_string();
919-
futures::try_join!(self.storage.current_size(), self.storage.max_size()).map(
920-
move |(cache_size, max_cache_size)| ServerInfo {
921-
stats,
922-
cache_location,
923-
cache_size,
924-
max_cache_size,
925-
use_preprocessor_cache_mode,
926-
version,
927-
},
928-
)
913+
ServerInfo::new(stats, Some(&*self.storage)).await
929914
}
930915

931916
/// Zero stats about the cache.
@@ -1678,6 +1663,35 @@ impl ServerStats {
16781663
}
16791664

16801665
impl ServerInfo {
1666+
pub async fn new(stats: ServerStats, storage: Option<&dyn Storage>) -> Result<Self> {
1667+
let cache_location;
1668+
let use_preprocessor_cache_mode;
1669+
let cache_size;
1670+
let max_cache_size;
1671+
if let Some(storage) = storage {
1672+
cache_location = storage.location();
1673+
use_preprocessor_cache_mode = storage
1674+
.preprocessor_cache_mode_config()
1675+
.use_preprocessor_cache_mode;
1676+
(cache_size, max_cache_size) =
1677+
futures::try_join!(storage.current_size(), storage.max_size())?;
1678+
} else {
1679+
cache_location = String::new();
1680+
use_preprocessor_cache_mode = false;
1681+
cache_size = None;
1682+
max_cache_size = None;
1683+
}
1684+
let version = env!("CARGO_PKG_VERSION").to_string();
1685+
Ok(ServerInfo {
1686+
stats,
1687+
cache_location,
1688+
cache_size,
1689+
max_cache_size,
1690+
use_preprocessor_cache_mode,
1691+
version,
1692+
})
1693+
}
1694+
16811695
/// Print info to stdout in a human-readable format.
16821696
pub fn print(&self, advanced: bool) {
16831697
let (name_width, stat_width) = self.stats.print(advanced);

tests/harness/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,14 @@ pub fn start_local_daemon(cfg_path: &Path, cached_cfg_path: &Path) {
6161
}
6262
}
6363

64-
pub fn stop_local_daemon() {
64+
pub fn stop_local_daemon() -> bool {
6565
trace!("sccache --stop-server");
66-
drop(
67-
sccache_command()
68-
.arg("--stop-server")
69-
.stdout(Stdio::null())
70-
.stderr(Stdio::null())
71-
.status(),
72-
);
66+
sccache_command()
67+
.arg("--stop-server")
68+
.stdout(Stdio::null())
69+
.stderr(Stdio::null())
70+
.status()
71+
.map_or(false, |status| status.success())
7372
}
7473

7574
pub fn get_stats<F: 'static + Fn(ServerInfo)>(f: F) {

tests/sccache_cargo.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,12 @@ fn test_run_log() -> Result<()> {
166166
.context("Failed to create tempdir")?;
167167
let tmppath = tempdir.path().join("perm.log");
168168
let mut cmd = Command::new(SCCACHE_BIN.as_os_str());
169-
cmd.arg("--show-stats")
169+
cmd.arg("--start-server")
170170
.env("SCCACHE_ERROR_LOG", &tmppath) // Should not work
171171
.env("SCCACHE_LOG", "debug");
172172

173173
cmd.assert().success();
174+
stop_sccache()?;
174175
assert!(Path::new(&tmppath).is_file());
175176
Ok(())
176177
}

tests/system.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,18 @@ fn test_sccache_command(preprocessor_cache_mode: bool) {
881881
}
882882
}
883883

884+
#[test]
885+
#[serial]
886+
fn test_stats_no_server() {
887+
// Ensure there's no existing sccache server running.
888+
stop_local_daemon();
889+
get_stats(|_| {});
890+
assert!(
891+
!stop_local_daemon(),
892+
"Server shouldn't be running after --show-stats"
893+
);
894+
}
895+
884896
#[test_case(true ; "with preprocessor cache")]
885897
#[test_case(false ; "without preprocessor cache")]
886898
#[serial]

0 commit comments

Comments
 (0)