Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions src/carnot/funcs/metadata/metadata_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ void RegisterMetadataOpsOrDie(px::carnot::udf::Registry* registry) {
registry->RegisterOrDie<UPIDToHostnameUDF>("upid_to_hostname");
registry->RegisterOrDie<UPIDToNamespaceUDF>("upid_to_namespace");
registry->RegisterOrDie<UPIDToNodeNameUDF>("upid_to_node_name");
registry->RegisterOrDie<UPIDToParentUPIDUDF>("upid_to_parent_upid");
registry->RegisterOrDie<UPIDToPIDUDF>("upid_to_pid");
registry->RegisterOrDie<UPIDToStartTSUDF>("upid_to_start_ts");
registry->RegisterOrDie<UPIDToPodIDUDF>("upid_to_pod_id");
Expand Down
38 changes: 28 additions & 10 deletions src/carnot/funcs/metadata/metadata_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,7 @@ class UPIDToContainerIDUDF : public ScalarUDF {

auto upid_uint128 = absl::MakeUint128(upid_value.High64(), upid_value.Low64());
auto upid = md::UPID(upid_uint128);
auto pid = md->GetPIDByUPID(upid);
if (pid == nullptr) {
return "";
}
return pid->cid();
return md->ContainerIDByUPID(upid);
}

static udf::ScalarUDFDocBuilder Doc() {
Expand All @@ -264,15 +260,37 @@ class UPIDToContainerIDUDF : public ScalarUDF {
static udfspb::UDFSourceExecutor Executor() { return udfspb::UDFSourceExecutor::UDF_PEM; }
};

class UPIDToParentUPIDUDF : public ScalarUDF {
public:
UInt128Value Exec(FunctionContext* ctx, UInt128Value upid_value) {
auto md = GetMetadataState(ctx);
auto upid_uint128 = absl::MakeUint128(upid_value.High64(), upid_value.Low64());
return md->ParentUPID(md::UPID(upid_uint128)).value();
}

static udf::ScalarUDFDocBuilder Doc() {
return udf::ScalarUDFDocBuilder("Get the UPID of the parent process from a UPID.")
.Details(
"Gets the Unique Process ID (UPID) of the parent of the process with the given UPID. "
"If the parent is not known, this function will return a UPID of zero.")
.Example("df.parent_upid = px.upid_to_parent_upid(df.upid)")
.Arg("upid", "The UPID of the process to get the parent for.")
.Returns("The UPID of the parent process.");
}

static udf::InfRuleVec SemanticInferenceRules() {
return {udf::ExplicitRule::Create<UPIDToParentUPIDUDF>(types::ST_UPID, {types::ST_NONE})};
}

// This UDF can currently only run on PEMs, because only PEMs have the UPID information.
static udfspb::UDFSourceExecutor Executor() { return udfspb::UDFSourceExecutor::UDF_PEM; }
};

inline const md::ContainerInfo* UPIDToContainer(const px::md::AgentMetadataState* md,
types::UInt128Value upid_value) {
auto upid_uint128 = absl::MakeUint128(upid_value.High64(), upid_value.Low64());
auto upid = md::UPID(upid_uint128);
auto pid = md->GetPIDByUPID(upid);
if (pid == nullptr) {
return nullptr;
}
return md->k8s_metadata_state().ContainerInfoByID(pid->cid());
return md->k8s_metadata_state().ContainerInfoByID(md->ContainerIDByUPID(upid));
}

class UPIDToContainerNameUDF : public ScalarUDF {
Expand Down
35 changes: 35 additions & 0 deletions src/carnot/funcs/metadata/metadata_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,41 @@ TEST_F(MetadataOpsTest, upid_to_container_id_test) {
udf_tester.ForInput(upid3).Expect("");
}

TEST_F(MetadataOpsTest, upid_to_container_id_resolves_through_parent) {
metadata_state_->AddProcessParent(md::UPID(123, 900, 5000), md::UPID(123, 567, 89101));
auto function_ctx = std::make_unique<FunctionContext>(metadata_state_, nullptr);
auto udf_tester = px::carnot::udf::UDFTester<UPIDToContainerIDUDF>(std::move(function_ctx));
auto child_upid = types::UInt128Value(528280978308, 5000);
udf_tester.ForInput(child_upid).Expect("pod1_container_1");
}

TEST_F(MetadataOpsTest, upid_to_pod_name_resolves_through_parent) {
metadata_state_->AddProcessParent(md::UPID(123, 900, 5000), md::UPID(123, 567, 89101));
auto function_ctx = std::make_unique<FunctionContext>(metadata_state_, nullptr);
auto udf_tester = px::carnot::udf::UDFTester<UPIDToPodNameUDF>(std::move(function_ctx));
auto child_upid = types::UInt128Value(528280978308, 5000);
udf_tester.ForInput(child_upid).Expect("pl/running_pod");
}

TEST_F(MetadataOpsTest, upid_to_pod_name_resolves_through_cgroup) {
metadata_state_->SetContainerCgroupID("pod1_container_1", 77);
metadata_state_->AddProcessCgroup(md::UPID(123, 900, 5000), 77);
auto function_ctx = std::make_unique<FunctionContext>(metadata_state_, nullptr);
auto udf_tester = px::carnot::udf::UDFTester<UPIDToPodNameUDF>(std::move(function_ctx));
auto upid = types::UInt128Value(528280978308, 5000);
udf_tester.ForInput(upid).Expect("pl/running_pod");
}

TEST_F(MetadataOpsTest, upid_to_parent_upid_test) {
metadata_state_->AddProcessParent(md::UPID(123, 900, 5000), md::UPID(123, 567, 89101));
auto function_ctx = std::make_unique<FunctionContext>(metadata_state_, nullptr);
UPIDToParentUPIDUDF udf;
auto child_upid = types::UInt128Value(528280978308, 5000);
EXPECT_EQ(udf.Exec(function_ctx.get(), child_upid).val, md::UPID(123, 567, 89101).value());
auto unknown_upid = types::UInt128Value(528280978308, 6000);
EXPECT_EQ(udf.Exec(function_ctx.get(), unknown_upid).val, md::UPID().value());
}

TEST_F(MetadataOpsTest, upid_to_container_name_test) {
auto function_ctx = std::make_unique<FunctionContext>(metadata_state_, nullptr);
auto udf_tester = px::carnot::udf::UDFTester<UPIDToContainerNameUDF>(std::move(function_ctx));
Expand Down
25 changes: 20 additions & 5 deletions src/common/system/proc_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ constexpr int kProcNetDevTxDropField = 12;
constexpr int kProcStatNumFields = 52;

constexpr int kProcStatPIDField = 0;
constexpr int kProcStatPPIDField = 3;

constexpr int kProcStatMinorFaultsField = 9;
constexpr int kProcStatMajorFaultsField = 11;
Expand Down Expand Up @@ -712,7 +713,9 @@ Status ProcParser::ReadNSPid(pid_t pid, std::vector<std::string>* ns_pids) const
return Status::OK();
}

StatusOr<int64_t> GetPIDStartTimeTicks(const std::filesystem::path& proc_pid_path) {
namespace {

StatusOr<int64_t> ReadProcPIDStatField(const std::filesystem::path& proc_pid_path, int field) {
const std::filesystem::path proc_pid_stat_path = proc_pid_path / "stat";
std::string line;

Expand Down Expand Up @@ -767,12 +770,24 @@ StatusOr<int64_t> GetPIDStartTimeTicks(const std::filesystem::path& proc_pid_pat
proc_pid_stat_path.string());
}

int64_t start_time_ticks;
if (!absl::SimpleAtoi(split[kProcStatStartTimeField + command_offset], &start_time_ticks)) {
return error::Internal("Time value does not parse in file: $0.", proc_pid_stat_path.string());
int64_t value;
if (!absl::SimpleAtoi(split[field + command_offset], &value)) {
return error::Internal("Field $0 does not parse in file: $1.", field,
proc_pid_stat_path.string());
}

return start_time_ticks;
return value;
}

} // namespace

StatusOr<int64_t> GetPIDStartTimeTicks(const std::filesystem::path& proc_pid_path) {
return ReadProcPIDStatField(proc_pid_path, kProcStatStartTimeField);
}

StatusOr<int32_t> ProcParser::GetPPID(int32_t pid) const {
PX_ASSIGN_OR_RETURN(int64_t ppid, ReadProcPIDStatField(ProcPidPath(pid), kProcStatPPIDField));
return static_cast<int32_t>(ppid);
}

namespace {
Expand Down
2 changes: 2 additions & 0 deletions src/common/system/proc_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ class ProcParser {
*/
StatusOr<int64_t> GetPIDStartTimeTicks(int32_t pid) const;

StatusOr<int32_t> GetPPID(int32_t pid) const;

/**
* Gets the command line for a given pid.
* @param pid is the pid for which we want the command line.
Expand Down
5 changes: 5 additions & 0 deletions src/common/system/proc_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ TEST_F(ProcParserTest, read_pid_start_time) {
ASSERT_OK_AND_EQ(parser_->GetPIDStartTimeTicks(123), 14329);
}

TEST_F(ProcParserTest, read_ppid) {
PX_SET_FOR_SCOPE(FLAGS_proc_path, GetPathToTestDataFile("testdata/proc"));
ASSERT_OK_AND_EQ(parser_->GetPPID(123), 3260);
}

TEST_F(ProcParserTest, read_pid_cmdline) {
PX_SET_FOR_SCOPE(FLAGS_proc_path, GetPathToTestDataFile("testdata/proc"));
EXPECT_THAT("/usr/lib/slack/slack --force-device-scale-factor=1.5 --high-dpi-support=1",
Expand Down
Loading
Loading