Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add grpc_rs for exporting internal APIs #50

Open
wants to merge 2 commits into
base: rs-release
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,25 @@ GRPCXX_PUBLIC_HDRS = [
"include/grpcpp/impl/codegen/sync.h",
]

grpc_cc_library(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why need a new library?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that code can be placed outside of src/core and let grpc and grpc_unsecure depends on it using its build system.
Anyway, what's your suggestion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not put the source in the source list just like CMakeLists.txt does?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMakeLists.txt is generated from BUILD, upstream may overwrite CMakeLists.txt if they add or remove a file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BusyJay PTAL, thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused. Now is CMakeLists.txt auto-generated? Note we always need to modify CMakeLists.txt as the generated one from upstream doesn't always compile.

name = "grpc_rs",
srcs = [
"//:src/rs/stats.cc",
],
language = "c++",
public_hdrs = [
"include/grpcrs/stats.h",
],
tags = [
"nofixdeps",
],
visibility = ["@grpc:public"],
deps = [
"stats",
"//src/core:slice",
],
)

grpc_cc_library(
name = "grpc_unsecure",
srcs = [
Expand All @@ -560,6 +579,7 @@ grpc_cc_library(
"grpc_client_channel",
"grpc_common",
"grpc_http_filters",
"grpc_rs",
"grpc_security_base",
"grpc_trace",
"http_connect_handshaker",
Expand Down Expand Up @@ -636,6 +656,7 @@ grpc_cc_library(
"grpc_http_filters",
"grpc_jwt_credentials",
"grpc_public_hdrs",
"grpc_rs",
"grpc_security_base",
"grpc_trace",
"http_connect_handshaker",
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions build_autogenerated.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions include/grpcrs/stats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2023 TiKV Project Authors. Licensed under Apache-2.0.

#ifndef GRPC_RS_STATS_H
#define GRPC_RS_STATS_H

#include <grpc/support/port_platform.h>
#include <grpc/slice.h>

#ifndef GRPCRSAPI
#define GRPCRSAPI GPRAPI
#endif

#ifdef __cplusplus
extern "C" {
#endif

/** gRPC stats.
See: grpc/src/core/lib/debug/stats_data.yaml */
typedef struct grpcrs_stats grpcrs_stats;

enum grpcrs_stats_counter {
ClientCallsCreated,
ServerCallsCreated,
ClientChannelsCreated,
ClientSubchannelsCreated,
ServerChannelsCreated,
InsecureConnectionsCreated,
SyscallWrite,
SyscallRead,
TcpReadAlloc8k,
TcpReadAlloc64k,
Http2SettingsWrites,
Http2PingsSent,
Http2WritesBegun,
Http2TransportStalls,
Http2StreamStalls,
CqPluckCreates,
CqNextCreates,
CqCallbackCreates,
COUNTER_COUNT
};

enum grpcrs_stats_histogram {
CallInitialSize,
TcpWriteSize,
TcpWriteIovSize,
TcpReadSize,
TcpReadOffer,
TcpReadOfferIovSize,
Http2SendMessageSize,
Http2MetadataSize,
HISTOGRAM_COUNT
};

GRPCRSAPI grpcrs_stats* grpcrs_stats_collect();

GRPCRSAPI void grpcrs_stats_free(grpcrs_stats* stats);

GRPCRSAPI uint64_t grpcrs_stats_get_counter(
const grpcrs_stats* stats, grpcrs_stats_counter which);

GRPCRSAPI grpc_slice grpcrs_stats_counter_name(grpcrs_stats_counter which);

GRPCRSAPI grpc_slice grpcrs_stats_counter_doc(grpcrs_stats_counter which);

GRPCRSAPI double grpcrs_stats_get_histogram_percentile(
const grpcrs_stats* stats, grpcrs_stats_histogram which,
double percentile);

GRPCRSAPI double grpcrs_stats_get_histogram_count(
const grpcrs_stats* stats, grpcrs_stats_histogram which);

GRPCRSAPI grpc_slice grpcrs_stats_histogram_name(grpcrs_stats_histogram which);

GRPCRSAPI grpc_slice grpcrs_stats_histogram_doc(grpcrs_stats_histogram which);

#ifdef __cplusplus
}
#endif

#endif /* GRPC_RS_STATS_H */
74 changes: 74 additions & 0 deletions src/rs/stats.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2023 TiKV Project Authors. Licensed under Apache-2.0.

#include <include/grpcrs/stats.h>

#include "src/core/lib/debug/stats.h"
#include "src/core/lib/debug/stats_data.h"

// Just make sure they have the same number of counters.
static_assert(static_cast<int>(grpcrs_stats_counter::COUNTER_COUNT) ==
static_cast<int>(grpc_core::GlobalStats::Counter::COUNT),
"Counter count must be the same");

// Just make sure they have the same number of histograms.
static_assert(static_cast<int>(grpcrs_stats_histogram::HISTOGRAM_COUNT) ==
static_cast<int>(grpc_core::GlobalStats::Histogram::COUNT),
"Histogram count must be the same");

grpcrs_stats* grpcrs_stats_collect() {
return (grpcrs_stats*)grpc_core::global_stats().Collect().release();
}

void grpcrs_stats_free(grpcrs_stats* stats) {
auto s = (grpc_core::GlobalStats*)stats;
delete s;
}

uint64_t grpcrs_stats_get_counter(
const grpcrs_stats* stats, grpcrs_stats_counter which) {
auto s = (const grpc_core::GlobalStats*)stats;
return s->counters[which];
}

grpc_slice
grpcrs_stats_counter_name(grpcrs_stats_counter which) {
auto name = grpc_core::GlobalStats::counter_name[which];
auto slice = grpc_slice_from_static_buffer(name.data(), name.size());
return slice;
}

grpc_slice
grpcrs_stats_counter_doc(grpcrs_stats_counter which) {
auto doc = grpc_core::GlobalStats::counter_doc[which];
auto slice = grpc_slice_from_static_buffer(doc.data(), doc.size());
return slice;
}

double grpcrs_stats_get_histogram_percentile(
const grpcrs_stats* stats, grpcrs_stats_histogram which,
double percentile) {
auto s = (const grpc_core::GlobalStats*)stats;
return s->histogram(static_cast<grpc_core::GlobalStats::Histogram>(which))
.Percentile(percentile);
}

double grpcrs_stats_get_histogram_count(
const grpcrs_stats* stats, grpcrs_stats_histogram which) {
auto s = (const grpc_core::GlobalStats*)stats;
return s->histogram(static_cast<grpc_core::GlobalStats::Histogram>(which))
.Count();
}

grpc_slice
grpcrs_stats_histogram_name(grpcrs_stats_histogram which) {
auto name = grpc_core::GlobalStats::histogram_name[which];
auto slice = grpc_slice_from_static_buffer(name.data(), name.size());
return slice;
}

grpc_slice
grpcrs_stats_histogram_doc(grpcrs_stats_histogram which) {
auto doc = grpc_core::GlobalStats::histogram_doc[which];
auto slice = grpc_slice_from_static_buffer(doc.data(), doc.size());
return slice;
}