Cap every language server logs (#3134)

* on opening a language server's logs, a new editor for server logs is
now created from `\n`-joined `VecDeque` elements instead of a buffer, as
before
* every `VecDeque` entry is a log line we receiver out of stderr or LSP
server, and their general amount is capped with `let
MAX_STORED_LOG_ENTRIES: usize = 2000;`
* currently opened editor with logs (`Editor::multi_line`) keeps getting
log lines appended and may get over this cap, but only last stored 2000
entries will be restored on reopen
* similarly, cap rpc message logs

Release Notes:

- Improved memory usage by storing less language LSP server and rpc logs
This commit is contained in:
Kirill Bulatov 2023-10-17 21:51:21 +03:00 committed by GitHub
commit c380d437c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,5 @@
use collections::HashMap; use collections::{HashMap, VecDeque};
use editor::Editor; use editor::{Editor, MoveToEnd};
use futures::{channel::mpsc, StreamExt}; use futures::{channel::mpsc, StreamExt};
use gpui::{ use gpui::{
actions, actions,
@ -11,7 +11,7 @@ use gpui::{
AnyElement, AppContext, Element, Entity, ModelContext, ModelHandle, Subscription, View, AnyElement, AppContext, Element, Entity, ModelContext, ModelHandle, Subscription, View,
ViewContext, ViewHandle, WeakModelHandle, ViewContext, ViewHandle, WeakModelHandle,
}; };
use language::{Buffer, LanguageServerId, LanguageServerName}; use language::{LanguageServerId, LanguageServerName};
use lsp::IoKind; use lsp::IoKind;
use project::{search::SearchQuery, Project}; use project::{search::SearchQuery, Project};
use std::{borrow::Cow, sync::Arc}; use std::{borrow::Cow, sync::Arc};
@ -22,8 +22,9 @@ use workspace::{
ToolbarItemLocation, ToolbarItemView, Workspace, WorkspaceCreated, ToolbarItemLocation, ToolbarItemView, Workspace, WorkspaceCreated,
}; };
const SEND_LINE: &str = "// Send:\n"; const SEND_LINE: &str = "// Send:";
const RECEIVE_LINE: &str = "// Receive:\n"; const RECEIVE_LINE: &str = "// Receive:";
const MAX_STORED_LOG_ENTRIES: usize = 2000;
pub struct LogStore { pub struct LogStore {
projects: HashMap<WeakModelHandle<Project>, ProjectState>, projects: HashMap<WeakModelHandle<Project>, ProjectState>,
@ -36,24 +37,25 @@ struct ProjectState {
} }
struct LanguageServerState { struct LanguageServerState {
log_buffer: ModelHandle<Buffer>, log_messages: VecDeque<String>,
rpc_state: Option<LanguageServerRpcState>, rpc_state: Option<LanguageServerRpcState>,
_io_logs_subscription: Option<lsp::Subscription>, _io_logs_subscription: Option<lsp::Subscription>,
_lsp_logs_subscription: Option<lsp::Subscription>, _lsp_logs_subscription: Option<lsp::Subscription>,
} }
struct LanguageServerRpcState { struct LanguageServerRpcState {
buffer: ModelHandle<Buffer>, rpc_messages: VecDeque<String>,
last_message_kind: Option<MessageKind>, last_message_kind: Option<MessageKind>,
} }
pub struct LspLogView { pub struct LspLogView {
pub(crate) editor: ViewHandle<Editor>, pub(crate) editor: ViewHandle<Editor>,
editor_subscription: Subscription,
log_store: ModelHandle<LogStore>, log_store: ModelHandle<LogStore>,
current_server_id: Option<LanguageServerId>, current_server_id: Option<LanguageServerId>,
is_showing_rpc_trace: bool, is_showing_rpc_trace: bool,
project: ModelHandle<Project>, project: ModelHandle<Project>,
_log_store_subscription: Subscription, _log_store_subscriptions: Vec<Subscription>,
} }
pub struct LspLogToolbarItemView { pub struct LspLogToolbarItemView {
@ -122,10 +124,9 @@ impl LogStore {
io_tx, io_tx,
}; };
cx.spawn_weak(|this, mut cx| async move { cx.spawn_weak(|this, mut cx| async move {
while let Some((project, server_id, io_kind, mut message)) = io_rx.next().await { while let Some((project, server_id, io_kind, message)) = io_rx.next().await {
if let Some(this) = this.upgrade(&cx) { if let Some(this) = this.upgrade(&cx) {
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
message.push('\n');
this.on_io(project, server_id, io_kind, &message, cx); this.on_io(project, server_id, io_kind, &message, cx);
}); });
} }
@ -168,15 +169,13 @@ impl LogStore {
project: &ModelHandle<Project>, project: &ModelHandle<Project>,
id: LanguageServerId, id: LanguageServerId,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Option<ModelHandle<Buffer>> { ) -> Option<&mut LanguageServerState> {
let project_state = self.projects.get_mut(&project.downgrade())?; let project_state = self.projects.get_mut(&project.downgrade())?;
let server_state = project_state.servers.entry(id).or_insert_with(|| { let server_state = project_state.servers.entry(id).or_insert_with(|| {
cx.notify(); cx.notify();
LanguageServerState { LanguageServerState {
rpc_state: None, rpc_state: None,
log_buffer: cx log_messages: VecDeque::with_capacity(MAX_STORED_LOG_ENTRIES),
.add_model(|cx| Buffer::new(0, cx.model_id() as u64, ""))
.clone(),
_io_logs_subscription: None, _io_logs_subscription: None,
_lsp_logs_subscription: None, _lsp_logs_subscription: None,
} }
@ -186,7 +185,7 @@ impl LogStore {
if let Some(server) = server.as_deref() { if let Some(server) = server.as_deref() {
if server.has_notification_handler::<lsp::notification::LogMessage>() { if server.has_notification_handler::<lsp::notification::LogMessage>() {
// Another event wants to re-add the server that was already added and subscribed to, avoid doing it again. // Another event wants to re-add the server that was already added and subscribed to, avoid doing it again.
return Some(server_state.log_buffer.clone()); return Some(server_state);
} }
} }
@ -215,7 +214,7 @@ impl LogStore {
} }
}) })
}); });
Some(server_state.log_buffer.clone()) Some(server_state)
} }
fn add_language_server_log( fn add_language_server_log(
@ -225,24 +224,26 @@ impl LogStore {
message: &str, message: &str,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Option<()> { ) -> Option<()> {
let buffer = match self let language_server_state = match self
.projects .projects
.get_mut(&project.downgrade())? .get_mut(&project.downgrade())?
.servers .servers
.get(&id) .get_mut(&id)
.map(|state| state.log_buffer.clone())
{ {
Some(existing_buffer) => existing_buffer, Some(existing_state) => existing_state,
None => self.add_language_server(&project, id, cx)?, None => self.add_language_server(&project, id, cx)?,
}; };
buffer.update(cx, |buffer, cx| {
let len = buffer.len(); let log_lines = &mut language_server_state.log_messages;
let has_newline = message.ends_with("\n"); while log_lines.len() >= MAX_STORED_LOG_ENTRIES {
buffer.edit([(len..len, message)], None, cx); log_lines.pop_front();
if !has_newline { }
let len = buffer.len(); let message = message.trim();
buffer.edit([(len..len, "\n")], None, cx); log_lines.push_back(message.to_string());
} cx.emit(Event::NewServerLogEntry {
id,
entry: message.to_string(),
is_rpc: false,
}); });
cx.notify(); cx.notify();
Some(()) Some(())
@ -260,46 +261,32 @@ impl LogStore {
Some(()) Some(())
} }
pub fn log_buffer_for_server( fn server_logs(
&self, &self,
project: &ModelHandle<Project>, project: &ModelHandle<Project>,
server_id: LanguageServerId, server_id: LanguageServerId,
) -> Option<ModelHandle<Buffer>> { ) -> Option<&VecDeque<String>> {
let weak_project = project.downgrade(); let weak_project = project.downgrade();
let project_state = self.projects.get(&weak_project)?; let project_state = self.projects.get(&weak_project)?;
let server_state = project_state.servers.get(&server_id)?; let server_state = project_state.servers.get(&server_id)?;
Some(server_state.log_buffer.clone()) Some(&server_state.log_messages)
} }
fn enable_rpc_trace_for_language_server( fn enable_rpc_trace_for_language_server(
&mut self, &mut self,
project: &ModelHandle<Project>, project: &ModelHandle<Project>,
server_id: LanguageServerId, server_id: LanguageServerId,
cx: &mut ModelContext<Self>, ) -> Option<&mut LanguageServerRpcState> {
) -> Option<ModelHandle<Buffer>> {
let weak_project = project.downgrade(); let weak_project = project.downgrade();
let project_state = self.projects.get_mut(&weak_project)?; let project_state = self.projects.get_mut(&weak_project)?;
let server_state = project_state.servers.get_mut(&server_id)?; let server_state = project_state.servers.get_mut(&server_id)?;
let rpc_state = server_state.rpc_state.get_or_insert_with(|| { let rpc_state = server_state
let language = project.read(cx).languages().language_for_name("JSON"); .rpc_state
let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, "")); .get_or_insert_with(|| LanguageServerRpcState {
cx.spawn_weak({ rpc_messages: VecDeque::with_capacity(MAX_STORED_LOG_ENTRIES),
let buffer = buffer.clone();
|_, mut cx| async move {
let language = language.await.ok();
buffer.update(&mut cx, |buffer, cx| {
buffer.set_language(language, cx);
});
}
})
.detach();
LanguageServerRpcState {
buffer,
last_message_kind: None, last_message_kind: None,
} });
}); Some(rpc_state)
Some(rpc_state.buffer.clone())
} }
pub fn disable_rpc_trace_for_language_server( pub fn disable_rpc_trace_for_language_server(
@ -328,7 +315,7 @@ impl LogStore {
IoKind::StdIn => false, IoKind::StdIn => false,
IoKind::StdErr => { IoKind::StdErr => {
let project = project.upgrade(cx)?; let project = project.upgrade(cx)?;
let message = format!("stderr: {}\n", message.trim()); let message = format!("stderr: {}", message.trim());
self.add_language_server_log(&project, language_server_id, &message, cx); self.add_language_server_log(&project, language_server_id, &message, cx);
return Some(()); return Some(());
} }
@ -341,24 +328,37 @@ impl LogStore {
.get_mut(&language_server_id)? .get_mut(&language_server_id)?
.rpc_state .rpc_state
.as_mut()?; .as_mut()?;
state.buffer.update(cx, |buffer, cx| { let kind = if is_received {
let kind = if is_received { MessageKind::Receive
MessageKind::Receive } else {
} else { MessageKind::Send
MessageKind::Send };
let rpc_log_lines = &mut state.rpc_messages;
if state.last_message_kind != Some(kind) {
let line_before_message = match kind {
MessageKind::Send => SEND_LINE,
MessageKind::Receive => RECEIVE_LINE,
}; };
if state.last_message_kind != Some(kind) { rpc_log_lines.push_back(line_before_message.to_string());
let len = buffer.len(); cx.emit(Event::NewServerLogEntry {
let line = match kind { id: language_server_id,
MessageKind::Send => SEND_LINE, entry: line_before_message.to_string(),
MessageKind::Receive => RECEIVE_LINE, is_rpc: true,
}; });
buffer.edit([(len..len, line)], None, cx); }
state.last_message_kind = Some(kind);
} while rpc_log_lines.len() >= MAX_STORED_LOG_ENTRIES {
let len = buffer.len(); rpc_log_lines.pop_front();
buffer.edit([(len..len, message)], None, cx); }
let message = message.trim();
rpc_log_lines.push_back(message.to_string());
cx.emit(Event::NewServerLogEntry {
id: language_server_id,
entry: message.to_string(),
is_rpc: true,
}); });
cx.notify();
Some(()) Some(())
} }
} }
@ -374,8 +374,7 @@ impl LspLogView {
.projects .projects
.get(&project.downgrade()) .get(&project.downgrade())
.and_then(|project| project.servers.keys().copied().next()); .and_then(|project| project.servers.keys().copied().next());
let buffer = cx.add_model(|cx| Buffer::new(0, cx.model_id() as u64, "")); let model_changes_subscription = cx.observe(&log_store, |this, store, cx| {
let _log_store_subscription = cx.observe(&log_store, |this, store, cx| {
(|| -> Option<()> { (|| -> Option<()> {
let project_state = store.read(cx).projects.get(&this.project.downgrade())?; let project_state = store.read(cx).projects.get(&this.project.downgrade())?;
if let Some(current_lsp) = this.current_server_id { if let Some(current_lsp) = this.current_server_id {
@ -411,13 +410,31 @@ impl LspLogView {
cx.notify(); cx.notify();
}); });
let events_subscriptions = cx.subscribe(&log_store, |log_view, _, e, cx| match e {
Event::NewServerLogEntry { id, entry, is_rpc } => {
if log_view.current_server_id == Some(*id) {
if (*is_rpc && log_view.is_showing_rpc_trace)
|| (!*is_rpc && !log_view.is_showing_rpc_trace)
{
log_view.editor.update(cx, |editor, cx| {
editor.set_read_only(false);
editor.handle_input(entry.trim(), cx);
editor.handle_input("\n", cx);
editor.set_read_only(true);
});
}
}
}
});
let (editor, editor_subscription) = Self::editor_for_logs(String::new(), cx);
let mut this = Self { let mut this = Self {
editor: Self::editor_for_buffer(project.clone(), buffer, cx), editor,
editor_subscription,
project, project,
log_store, log_store,
current_server_id: None, current_server_id: None,
is_showing_rpc_trace: false, is_showing_rpc_trace: false,
_log_store_subscription, _log_store_subscriptions: vec![model_changes_subscription, events_subscriptions],
}; };
if let Some(server_id) = server_id { if let Some(server_id) = server_id {
this.show_logs_for_server(server_id, cx); this.show_logs_for_server(server_id, cx);
@ -425,20 +442,19 @@ impl LspLogView {
this this
} }
fn editor_for_buffer( fn editor_for_logs(
project: ModelHandle<Project>, log_contents: String,
buffer: ModelHandle<Buffer>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> ViewHandle<Editor> { ) -> (ViewHandle<Editor>, Subscription) {
let editor = cx.add_view(|cx| { let editor = cx.add_view(|cx| {
let mut editor = Editor::for_buffer(buffer, Some(project), cx); let mut editor = Editor::multi_line(None, cx);
editor.set_text(log_contents, cx);
editor.move_to_end(&MoveToEnd, cx);
editor.set_read_only(true); editor.set_read_only(true);
editor.move_to_end(&Default::default(), cx);
editor editor
}); });
cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone())) let editor_subscription = cx.subscribe(&editor, |_, _, event, cx| cx.emit(event.clone()));
.detach(); (editor, editor_subscription)
editor
} }
pub(crate) fn menu_items<'a>(&'a self, cx: &'a AppContext) -> Option<Vec<LogMenuItem>> { pub(crate) fn menu_items<'a>(&'a self, cx: &'a AppContext) -> Option<Vec<LogMenuItem>> {
@ -487,14 +503,17 @@ impl LspLogView {
} }
fn show_logs_for_server(&mut self, server_id: LanguageServerId, cx: &mut ViewContext<Self>) { fn show_logs_for_server(&mut self, server_id: LanguageServerId, cx: &mut ViewContext<Self>) {
let buffer = self let log_contents = self
.log_store .log_store
.read(cx) .read(cx)
.log_buffer_for_server(&self.project, server_id); .server_logs(&self.project, server_id)
if let Some(buffer) = buffer { .map(log_contents);
if let Some(log_contents) = log_contents {
self.current_server_id = Some(server_id); self.current_server_id = Some(server_id);
self.is_showing_rpc_trace = false; self.is_showing_rpc_trace = false;
self.editor = Self::editor_for_buffer(self.project.clone(), buffer, cx); let (editor, editor_subscription) = Self::editor_for_logs(log_contents, cx);
self.editor = editor;
self.editor_subscription = editor_subscription;
cx.notify(); cx.notify();
} }
} }
@ -504,13 +523,37 @@ impl LspLogView {
server_id: LanguageServerId, server_id: LanguageServerId,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
let buffer = self.log_store.update(cx, |log_set, cx| { let rpc_log = self.log_store.update(cx, |log_store, _| {
log_set.enable_rpc_trace_for_language_server(&self.project, server_id, cx) log_store
.enable_rpc_trace_for_language_server(&self.project, server_id)
.map(|state| log_contents(&state.rpc_messages))
}); });
if let Some(buffer) = buffer { if let Some(rpc_log) = rpc_log {
self.current_server_id = Some(server_id); self.current_server_id = Some(server_id);
self.is_showing_rpc_trace = true; self.is_showing_rpc_trace = true;
self.editor = Self::editor_for_buffer(self.project.clone(), buffer, cx); let (editor, editor_subscription) = Self::editor_for_logs(rpc_log, cx);
let language = self.project.read(cx).languages().language_for_name("JSON");
editor
.read(cx)
.buffer()
.read(cx)
.as_singleton()
.expect("log buffer should be a singleton")
.update(cx, |_, cx| {
cx.spawn_weak({
let buffer = cx.handle();
|_, mut cx| async move {
let language = language.await.ok();
buffer.update(&mut cx, |buffer, cx| {
buffer.set_language(language, cx);
});
}
})
.detach();
});
self.editor = editor;
self.editor_subscription = editor_subscription;
cx.notify(); cx.notify();
} }
} }
@ -523,7 +566,7 @@ impl LspLogView {
) { ) {
self.log_store.update(cx, |log_store, cx| { self.log_store.update(cx, |log_store, cx| {
if enabled { if enabled {
log_store.enable_rpc_trace_for_language_server(&self.project, server_id, cx); log_store.enable_rpc_trace_for_language_server(&self.project, server_id);
} else { } else {
log_store.disable_rpc_trace_for_language_server(&self.project, server_id, cx); log_store.disable_rpc_trace_for_language_server(&self.project, server_id, cx);
} }
@ -535,6 +578,16 @@ impl LspLogView {
} }
} }
fn log_contents(lines: &VecDeque<String>) -> String {
let (a, b) = lines.as_slices();
let log_contents = a.join("\n");
if b.is_empty() {
log_contents
} else {
log_contents + "\n" + &b.join("\n")
}
}
impl View for LspLogView { impl View for LspLogView {
fn ui_name() -> &'static str { fn ui_name() -> &'static str {
"LspLogView" "LspLogView"
@ -949,8 +1002,16 @@ impl LspLogToolbarItemView {
} }
} }
pub enum Event {
NewServerLogEntry {
id: LanguageServerId,
entry: String,
is_rpc: bool,
},
}
impl Entity for LogStore { impl Entity for LogStore {
type Event = (); type Event = Event;
} }
impl Entity for LspLogView { impl Entity for LspLogView {