Skip to content

Commit

Permalink
[CLEANUP] Move CPP code inside common.h to common.hpp
Browse files Browse the repository at this point in the history
Summary:
Bundled here is a minor change in conn_id_t's ToString(), where upid is now printed as one unit, from:
pid=%d start_time_ticks=%d
to
upid=%d:%d

This reduces the length of CONN_TRACE() logs.

Test Plan: Since this just moves code between files, the existing tests already cover them

Reviewers: #stirling, oazizi

Reviewed By: #stirling, oazizi

Signed-off-by: yzhao1012 <yzhao@pixielabs.ai>

Differential Revision: https://phab.corp.pixielabs.ai/D10743

GitOrigin-RevId: a3d1e11
  • Loading branch information
yzhao1012 authored and copybaranaut committed Feb 16, 2022
1 parent 271b0be commit 1d00d0b
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 73 deletions.
35 changes: 0 additions & 35 deletions src/stirling/source_connectors/socket_tracer/bcc_bpf_intf/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,7 @@

#pragma once

#ifdef __cplusplus
#include <algorithm>
#include <map>
#include <string>

#include <absl/strings/substitute.h>
#include <magic_enum.hpp>

#include "src/common/base/enum_utils.h"
#include "src/stirling/bpf_tools/bcc_bpf_intf/upid.h"
#endif

// This file contains definitions that are shared between various kprobes and uprobes.

Expand Down Expand Up @@ -68,13 +58,6 @@ enum traffic_protocol_t {
#endif
};

#ifdef __cplusplus
constexpr int kNumProtocols = magic_enum::enum_count<traffic_protocol_t>();

static const std::map<int64_t, std::string_view> kTrafficProtocolDecoder =
px::EnumDefToMap<traffic_protocol_t>();
#endif

struct protocol_message_t {
enum traffic_protocol_t protocol;
enum message_type_t type;
Expand All @@ -89,11 +72,6 @@ enum endpoint_role_t {
kRoleUnknown = 1 << 2,
};

#ifdef __cplusplus
static const std::map<int64_t, std::string_view> kEndpointRoleDecoder =
px::EnumDefToMap<endpoint_role_t>();
#endif

struct conn_id_t {
// The unique identifier of the pid/tgid.
struct upid_t upid;
Expand All @@ -103,19 +81,6 @@ struct conn_id_t {
uint64_t tsid;
};

#ifdef __cplusplus
inline std::string ToString(const conn_id_t& conn_id) {
return absl::Substitute("[pid=$0 start_time_ticks=$1 fd=$2 gen=$3]", conn_id.upid.pid,
conn_id.upid.start_time_ticks, conn_id.fd, conn_id.tsid);
}

inline bool operator==(const struct conn_id_t& a, const struct conn_id_t& b) {
return a.upid == b.upid && a.fd == b.fd && a.tsid == b.tsid;
}

inline bool operator!=(const struct conn_id_t& a, const struct conn_id_t& b) { return !(a == b); }
#endif

// Specifies the corresponding indexes of the entries of a per-cpu array.
enum control_value_index_t {
// This specify one pid to monitor. This is used during test to eliminate noise.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2018- The Pixie Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "src/stirling/source_connectors/socket_tracer/bcc_bpf_intf/common.hpp"

#include <algorithm>
#include <map>
#include <string>

#include <absl/strings/substitute.h>
#include <magic_enum.hpp>

#include "src/common/base/enum_utils.h"
#include "src/stirling/source_connectors/socket_tracer/bcc_bpf_intf/common.h"

constexpr int kNumProtocols = magic_enum::enum_count<traffic_protocol_t>();

static const std::map<int64_t, std::string_view> kTrafficProtocolDecoder =
px::EnumDefToMap<traffic_protocol_t>();

static const std::map<int64_t, std::string_view> kEndpointRoleDecoder =
px::EnumDefToMap<endpoint_role_t>();

inline std::string ToString(const conn_id_t& conn_id) {
return absl::Substitute("[upid=$0:$1 fd=$2 gen=$3]", conn_id.upid.pid,
conn_id.upid.start_time_ticks, conn_id.fd, conn_id.tsid);
}

inline bool operator==(const struct conn_id_t& a, const struct conn_id_t& b) {
return a.upid == b.upid && a.fd == b.fd && a.tsid == b.tsid;
}

inline bool operator!=(const struct conn_id_t& a, const struct conn_id_t& b) { return !(a == b); }
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <magic_enum.hpp>

#include "src/common/base/base.h"
#include "src/stirling/source_connectors/socket_tracer/bcc_bpf_intf/common.hpp"
#include "src/stirling/source_connectors/socket_tracer/bcc_bpf_intf/go_grpc_types.h"

// perf_submit() uses PERF_RECORD_SAMPLE with PERF_SAMPLE_RAW, which has the following structure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,38 +245,6 @@ struct socket_control_event_t {
};
};

#ifdef __cplusplus

#include <string>

#include "src/common/base/base.h"

inline std::string ToString(const socket_data_event_t::attr_t& attr) {
return absl::Substitute(
"[ts=$0 conn_id=$1 protocol=$2 role=$3 dir=$4 ssl=$5 source_fn=$6 pos=$7 size=$8 "
"buf_size=$9]",
attr.timestamp_ns, ToString(attr.conn_id), magic_enum::enum_name(attr.protocol),
magic_enum::enum_name(attr.role), magic_enum::enum_name(attr.direction), attr.ssl,
magic_enum::enum_name(attr.source_fn), attr.pos, attr.msg_size, attr.msg_buf_size);
}

inline std::string ToString(const close_event_t& event) {
return absl::Substitute("[wr_bytes=$0 rd_bytes=$1]", event.wr_bytes, event.rd_bytes);
}

inline std::string ToString(const conn_event_t& event) {
return absl::Substitute("[addr=$0]",
::px::ToString(reinterpret_cast<const struct sockaddr*>(&event.addr)));
}

inline std::string ToString(const socket_control_event_t& event) {
return absl::Substitute("[type=$0 ts=$1 conn_id=$2 $3]", magic_enum::enum_name(event.type),
event.timestamp_ns, ToString(event.conn_id),
event.type == kConnOpen ? ToString(event.open) : ToString(event.close));
}

#endif

struct connect_args_t {
const struct sockaddr* addr;
int32_t fd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,35 @@

#include "src/common/base/base.h"
#include "src/stirling/source_connectors/socket_tracer/bcc_bpf_intf/socket_trace.h"
#include "src/stirling/source_connectors/socket_tracer/bcc_bpf_intf/common.hpp"

// This header defines the C++ counterparts of the BPF data structures.
// The file name is kept identical to its BPF counterpart as well.

inline std::string ToString(const socket_data_event_t::attr_t& attr) {
return absl::Substitute(
"[ts=$0 conn_id=$1 protocol=$2 role=$3 dir=$4 ssl=$5 source_fn=$6 pos=$7 size=$8 "
"buf_size=$9]",
attr.timestamp_ns, ToString(attr.conn_id), magic_enum::enum_name(attr.protocol),
magic_enum::enum_name(attr.role), magic_enum::enum_name(attr.direction), attr.ssl,
magic_enum::enum_name(attr.source_fn), attr.pos, attr.msg_size, attr.msg_buf_size);
}

inline std::string ToString(const close_event_t& event) {
return absl::Substitute("[wr_bytes=$0 rd_bytes=$1]", event.wr_bytes, event.rd_bytes);
}

inline std::string ToString(const conn_event_t& event) {
return absl::Substitute("[addr=$0]",
::px::ToString(reinterpret_cast<const struct sockaddr*>(&event.addr)));
}

inline std::string ToString(const socket_control_event_t& event) {
return absl::Substitute("[type=$0 ts=$1 conn_id=$2 $3]", magic_enum::enum_name(event.type),
event.timestamp_ns, ToString(event.conn_id),
event.type == kConnOpen ? ToString(event.open) : ToString(event.close));
}

namespace px {
namespace stirling {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <string_view>

#include "src/stirling/core/canonical_types.h"
#include "src/stirling/source_connectors/socket_tracer/bcc_bpf_intf/common.h"
#include "src/stirling/source_connectors/socket_tracer/bcc_bpf_intf/common.hpp"

namespace px {
namespace stirling {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ TEST_F(ConnTrackersManagerTest, DebugInfo) {
std::string debug_info = trackers_mgr_.DebugInfo();
EXPECT_THAT(debug_info, HasSubstr("ConnTracker count statistics: kTotal=1 kReadyForDestruction=0 "
"kCreated=1 kDestroyed=0 kDestroyedGens=0"));
EXPECT_THAT(
debug_info,
HasSubstr("conn_tracker=conn_id=[pid=1 start_time_ticks=1 fd=1 gen=1] state=kCollecting "
"remote_addr=-:-1 role=kRoleUnknown protocol=kProtocolUnknown zombie=false "
"ready_for_destruction=false\n"));
EXPECT_THAT(debug_info,
HasSubstr("conn_tracker=conn_id=[upid=1:1 fd=1 gen=1] state=kCollecting "
"remote_addr=-:-1 role=kRoleUnknown protocol=kProtocolUnknown zombie=false "
"ready_for_destruction=false\n"));
}

class ConnTrackerGenerationsTest : public ::testing::Test {
Expand Down

0 comments on commit 1d00d0b

Please sign in to comment.