Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions bottlecap/src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
collections::HashMap,
fs::{self, File},
io::{self, BufRead},
sync::LazyLock,
};

use constants::{
Expand All @@ -14,6 +15,14 @@ use constants::{
use regex::Regex;
use tracing::debug;

// Compiled once on first use rather than on every call. Both patterns capture
// the soft limit value (the first numeric value after the title) from a
// process `limits` file.
static MAX_OPEN_FILES_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^Max open files\s+(\d+)").expect("valid static regex"));
static MAX_PROCESSES_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^Max processes\s+(\d+)").expect("valid static regex"));

#[must_use]
pub fn get_pid_list() -> Vec<i64> {
get_pid_list_from_path(PROC_PATH)
Expand Down Expand Up @@ -222,8 +231,7 @@ pub fn get_fd_max_data(pids: &[i64]) -> f64 {

fn get_fd_max_data_from_path(path: &str, pids: &[i64]) -> f64 {
let mut fd_max = constants::LAMBDA_FILE_DESCRIPTORS_DEFAULT_LIMIT;
// regex to capture the soft limit value (first numeric value after the title)
let re = Regex::new(r"^Max open files\s+(\d+)").expect("Failed to create regex");
let re = &*MAX_OPEN_FILES_RE;

for &pid in pids {
let limits_path = format!("{path}/{pid}/limits");
Expand Down Expand Up @@ -273,8 +281,7 @@ pub fn get_threads_max_data(pids: &[i64]) -> f64 {

fn get_threads_max_data_from_path(path: &str, pids: &[i64]) -> f64 {
let mut threads_max = constants::LAMBDA_EXECUTION_PROCESSES_DEFAULT_LIMIT;
// regex to capture the soft limit value (first numeric value after the title)
let re = Regex::new(r"^Max processes\s+(\d+)").expect("Failed to create regex");
let re = &*MAX_PROCESSES_RE;

for &pid in pids {
let limits_path = format!("{path}/{pid}/limits");
Expand Down
36 changes: 24 additions & 12 deletions bottlecap/src/tags/lambda/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ const RESOURCE_KEY: &str = "resource";
#[derive(Debug, Clone)]
pub struct Lambda {
tags_map: HashMap<String, String>,
// Cached representations computed once at construction so that the hot
// getters below are O(1) reads instead of re-iterating `tags_map` and
// re-running `format!("{k}:{v}")`/`join(",")` on every call.
tags_vec: Vec<String>,
tags_string: String,
function_tags_map: HashMap<String, String>,
}

fn arch_to_platform<'a>() -> &'a str {
Expand Down Expand Up @@ -227,17 +233,29 @@ impl Lambda {
config: Arc<config::Config>,
metadata: &HashMap<String, String>,
) -> Self {
let tags_map = tags_from_env(HashMap::new(), config, metadata);
// Compute the derived representations once. The tag set is immutable
// after construction, so callers can read these cached values cheaply.
let tags_vec: Vec<String> = tags_map.iter().map(|(k, v)| format!("{k}:{v}")).collect();
let tags_string = tags_vec.join(",");
let function_tags_map =
HashMap::from_iter([(FUNCTION_TAGS_KEY.to_string(), tags_string.clone())]);
Lambda {
tags_map: tags_from_env(HashMap::new(), config, metadata),
tags_map,
tags_vec,
tags_string,
function_tags_map,
}
}

#[must_use]
pub fn get_tags_vec(&self) -> Vec<String> {
self.tags_map
.iter()
.map(|(k, v)| format!("{k}:{v}"))
.collect()
self.tags_vec.clone()
}

#[must_use]
pub fn get_tags_string(&self) -> &str {
&self.tags_string
}

#[must_use]
Expand All @@ -257,13 +275,7 @@ impl Lambda {

#[must_use]
pub fn get_function_tags_map(&self) -> HashMap<String, String> {
let tags = self
.tags_map
.iter()
.map(|(k, v)| format!("{k}:{v}"))
.collect::<Vec<String>>()
.join(",");
HashMap::from_iter([(FUNCTION_TAGS_KEY.to_string(), tags)])
self.function_tags_map.clone()
}
}

Expand Down
9 changes: 8 additions & 1 deletion bottlecap/src/tags/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Provider {

#[must_use]
pub fn get_tags_string(&self) -> String {
self.get_tags_vec().join(",")
self.tag_provider.get_tags_string().to_string()
}

#[must_use]
Expand All @@ -65,6 +65,7 @@ impl Provider {

trait GetTags {
fn get_tags_vec(&self) -> Vec<String>;
fn get_tags_string(&self) -> &str;
fn get_canonical_id(&self) -> Option<String>;
fn get_canonical_resource_name(&self) -> Option<String>;
fn get_tags_map(&self) -> &hash_map::HashMap<String, String>;
Expand All @@ -78,6 +79,12 @@ impl GetTags for TagProvider {
}
}

fn get_tags_string(&self) -> &str {
match self {
TagProvider::Lambda(lambda_tags) => lambda_tags.get_tags_string(),
}
}

fn get_canonical_id(&self) -> Option<String> {
match self {
TagProvider::Lambda(lambda_tags) => lambda_tags.get_function_arn().cloned(),
Expand Down
Loading