Skip to content

Commit

Permalink
[gpr] Synchronize env
Browse files Browse the repository at this point in the history
  • Loading branch information
yashykt committed Jan 23, 2025
1 parent ba9b36f commit 736d071
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 375 deletions.
79 changes: 50 additions & 29 deletions BUILD

Large diffs are not rendered by default.

610 changes: 313 additions & 297 deletions src/core/BUILD

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/core/util/gpr_time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@

// Generic implementation of time calls.

#include <grpc/support/log.h>
#include <grpc/support/port_platform.h>
#include <grpc/support/time.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>

#include "absl/log/check.h"
#include "src/core/util/crash.h"

int gpr_time_cmp(gpr_timespec a, gpr_timespec b) {
int cmp = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
Expand Down
7 changes: 7 additions & 0 deletions src/core/util/linux/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@
#include <stdlib.h>

#include "src/core/util/env.h"
#include "src/core/util/no_destruct.h"
#include "src/core/util/sync.h"

namespace grpc_core {

NoDestruct<Mutex> g_mu;

std::optional<std::string> GetEnv(const char* name) {
char* result = nullptr;
MutexLock lock(g_mu.get());
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17)
result = secure_getenv(name);
#else
Expand All @@ -47,11 +52,13 @@ std::optional<std::string> GetEnv(const char* name) {
}

void SetEnv(const char* name, const char* value) {
MutexLock lock(g_mu.get());
int res = setenv(name, value, 1);
if (res != 0) abort();
}

void UnsetEnv(const char* name) {
MutexLock lock(g_mu.get());
int res = unsetenv(name);
if (res != 0) abort();
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/util/posix/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,28 @@
#include <stdlib.h>

#include "src/core/util/env.h"
#include "src/core/util/no_destruct.h"
#include "src/core/util/sync.h"

namespace grpc_core {

NoDestruct<Mutex> g_mu;

std::optional<std::string> GetEnv(const char* name) {
MutexLock lock(g_mu.get());
char* result = getenv(name);
if (result == nullptr) return std::nullopt;
return result;
}

void SetEnv(const char* name, const char* value) {
MutexLock lock(g_mu.get());
int res = setenv(name, value, 1);
if (res != 0) abort();
}

void UnsetEnv(const char* name) {
MutexLock lock(g_mu.get());
int res = unsetenv(name);
if (res != 0) abort();
}
Expand Down
1 change: 0 additions & 1 deletion src/core/util/posix/time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <grpc/support/time.h>

#include "absl/log/check.h"
#include "src/core/util/crash.h"

static struct timespec timespec_from_gpr(gpr_timespec gts) {
struct timespec rv;
Expand Down
1 change: 0 additions & 1 deletion src/core/util/sync_abseil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "src/core/util/crash.h"
#include "src/core/util/time_util.h"

void gpr_mu_init(gpr_mu* mu) {
Expand Down
1 change: 0 additions & 1 deletion src/core/util/time_precise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <algorithm>

#include "absl/log/log.h"
#include "src/core/util/crash.h"
#include "src/core/util/time_precise.h"

#ifndef GPR_CYCLE_COUNTER_CUSTOM
Expand Down
7 changes: 7 additions & 0 deletions src/core/util/windows/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@
#include <memory>

#include "src/core/util/env.h"
#include "src/core/util/no_destruct.h"
#include "src/core/util/sync.h"
#include "src/core/util/tchar.h"

namespace grpc_core {

NoDestruct<Mutex> g_mu;

std::optional<std::string> GetEnv(const char* name) {
auto tname = CharToTchar(name);
MutexLock lock(g_mu.get());
DWORD ret = GetEnvironmentVariable(tname.c_str(), NULL, 0);
if (ret == 0) return std::nullopt;
std::unique_ptr<TCHAR[]> tresult(new TCHAR[ret]);
Expand All @@ -41,12 +46,14 @@ std::optional<std::string> GetEnv(const char* name) {
}

void SetEnv(const char* name, const char* value) {
MutexLock lock(g_mu.get());
BOOL res = SetEnvironmentVariable(CharToTchar(name).c_str(),
CharToTchar(value).c_str());
if (!res) abort();
}

void UnsetEnv(const char* name) {
MutexLock lock(g_mu.get());
BOOL res = SetEnvironmentVariable(CharToTchar(name).c_str(), NULL);
if (!res) abort();
}
Expand Down
8 changes: 4 additions & 4 deletions src/cpp/ext/gcp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ grpc_cc_library(
],
deps = [
"//:gpr",
"//:grpc_base",
"//:grpc_public_hdrs",
"//:iomgr",
"//src/core:env",
"//src/core:error",
"//src/core:error_utils",
Expand All @@ -111,7 +109,7 @@ grpc_cc_library(
],
external_deps = [
"absl/base:core_headers",
"absl/log:log",
"absl/log",
"absl/numeric:int128",
"absl/strings",
"absl/strings:str_format",
Expand All @@ -134,6 +132,7 @@ grpc_cc_library(
"//src/core:env",
"//src/core:json",
"//src/core:logging_sink",
"//src/core:sync",
"//src/core:time",
"//src/core:uuid_v4",
],
Expand All @@ -151,8 +150,8 @@ grpc_cc_library(
"absl/base:core_headers",
"absl/container:flat_hash_map",
"absl/functional:any_invocable",
"absl/log",
"absl/log:check",
"absl/log:log",
"absl/status",
"absl/status:statusor",
],
Expand All @@ -178,6 +177,7 @@ grpc_cc_library(
"//src/core:load_file",
"//src/core:slice",
"//src/core:status_helper",
"//src/core:sync",
"//src/core:time",
],
)
1 change: 1 addition & 0 deletions src/cpp/ext/otel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ grpc_cc_library(
"//src/core:metrics",
"//src/core:slice",
"//src/core:slice_buffer",
"//src/core:sync",
],
)
61 changes: 21 additions & 40 deletions tools/distrib/fix_build_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"absl/base/config.h": "absl/base:config",
# TODO(ctiller) remove this
"absl/base/internal/endian.h": "absl/base:endian",
"absl/base/no_destructor.h": "absl/base:no_destructor",
"absl/base/thread_annotations.h": "absl/base:core_headers",
"absl/container/flat_hash_map.h": "absl/container:flat_hash_map",
"absl/container/flat_hash_set.h": "absl/container:flat_hash_set",
Expand All @@ -65,9 +66,11 @@
"absl/functional/function_ref.h": "absl/functional:function_ref",
"absl/hash/hash.h": "absl/hash",
"absl/log/check.h": "absl/log:check",
"absl/log/globals.h": "absl/log:globals",
"absl/log/log.h": "absl/log",
"absl/memory/memory.h": "absl/memory",
"absl/meta/type_traits.h": "absl/meta:type_traits",
"absl/numeric/bits.h": "absl/numeric:bits",
"absl/numeric/int128.h": "absl/numeric:int128",
"absl/random/random.h": "absl/random",
"absl/random/bit_gen_ref.h": "absl/random:bit_gen_ref",
Expand Down Expand Up @@ -116,9 +119,7 @@
"opentelemetry/sdk/resource/semantic_conventions.h": "otel/sdk:headers",
"ares.h": "cares",
"fuzztest/fuzztest.h": ["fuzztest", "fuzztest_main"],
"google/api/monitored_resource.pb.h": (
"google/api:monitored_resource_cc_proto"
),
"google/api/monitored_resource.pb.h": ("google/api:monitored_resource_cc_proto"),
"google/devtools/cloudtrace/v2/tracing.grpc.pb.h": (
"googleapis_trace_grpc_service"
),
Expand All @@ -137,9 +138,7 @@
"opencensus-trace-stackdriver_exporter"
),
"opencensus/trace/context_util.h": "opencensus-trace-context_util",
"opencensus/trace/propagation/grpc_trace_bin.h": (
"opencensus-trace-propagation"
),
"opencensus/trace/propagation/grpc_trace_bin.h": ("opencensus-trace-propagation"),
"opencensus/tags/context_util.h": "opencensus-tags-context_util",
"opencensus/trace/span_context.h": "opencensus-trace-span_context",
"openssl/base.h": "libssl",
Expand All @@ -162,13 +161,13 @@
"openssl/x509.h": "libcrypto",
"openssl/x509v3.h": "libcrypto",
"re2/re2.h": "re2",
"upb/base/status.hpp": "upb_base_lib",
"upb/base/string_view.h": "upb_base_lib",
"upb/message/map.h": "upb_message_lib",
"upb/base/status.hpp": "@com_google_protobuf//upb:base",
"upb/base/string_view.h": "@com_google_protobuf//upb:base",
"upb/message/map.h": "@com_google_protobuf//upb:message",
"upb/reflection/def.h": "upb_reflection",
"upb/json/encode.h": "upb_json_lib",
"upb/mem/arena.h": "upb_mem_lib",
"upb/mem/arena.hpp": "upb_mem_lib",
"upb/mem/arena.h": "@com_google_protobuf//upb:mem",
"upb/mem/arena.hpp": "@com_google_protobuf//upb:mem",
"upb/text/encode.h": "upb_textformat_lib",
"upb/reflection/def.hpp": "upb_reflection",
"xxhash.h": "xxhash",
Expand Down Expand Up @@ -229,11 +228,7 @@ def config_setting_group(self, **kwargs):
# Convert the source or header target to a relative path.
def _get_filename(name, parsing_path):
filename = "%s%s" % (
(
parsing_path + "/"
if (parsing_path and not name.startswith("//"))
else ""
),
(parsing_path + "/" if (parsing_path and not name.startswith("//")) else ""),
name,
)
filename = filename.replace("//:", "")
Expand Down Expand Up @@ -319,9 +314,7 @@ def buildozer_set_list(name, values, target, via=""):
buildozer("remove %s" % name, target)
return
adjust = via if via else name
buildozer(
"set %s %s" % (adjust, " ".join('"%s"' % s for s in values)), target
)
buildozer("set %s %s" % (adjust, " ".join('"%s"' % s for s in values)), target)
if via:
buildozer("remove %s" % name, target)
buildozer("rename %s %s" % (via, name), target)
Expand Down Expand Up @@ -413,9 +406,9 @@ def score_best(proposed, existing):
"test/core/transport/chaotic_good",
"test/core/transport/test_suite",
"test/core/transport",
"fuzztest",
"fuzztest/core/channel",
"fuzztest/core/transport/chttp2",
# "fuzztest",
# "fuzztest/core/channel",
# "fuzztest/core/transport/chttp2",
]:
parsing_path = dirname
exec(
Expand Down Expand Up @@ -444,7 +437,7 @@ def score_best(proposed, existing):
"grpc_proto_fuzzer": grpc_cc_library,
"grpc_proto_library": grpc_proto_library,
"grpc_internal_proto_library": grpc_proto_library,
"grpc_cc_proto_library": grpc_cc_library,
"grpc_cc_proto_library": lambda **kwargs: None,
"select": lambda d: d["//conditions:default"],
"glob": lambda files, **kwargs: None,
"grpc_end2end_tests": lambda: None,
Expand Down Expand Up @@ -502,19 +495,11 @@ def __init__(self, library, substitutions):
def add_one_of(self, choices, trigger):
if not choices:
return
choices = sum(
[self.apply_substitutions(choice) for choice in choices], []
)
choices = sum([self.apply_substitutions(choice) for choice in choices], [])
if args.explain and (args.why is None or args.why in choices):
print(
"{}: Adding one of {} for {}".format(
self.library, choices, trigger
)
)
print("{}: Adding one of {} for {}".format(self.library, choices, trigger))
self.to_add.append(
tuple(
make_relative_path(choice, self.library) for choice in choices
)
tuple(make_relative_path(choice, self.library) for choice in choices)
)

def add(self, choice, trigger):
Expand Down Expand Up @@ -661,16 +646,12 @@ def make_library(library):
# assume a system include
continue

print(
"# ERROR: can't categorize header: %s used by %s" % (hdr, library)
)
print("# ERROR: can't categorize header: %s used by %s" % (hdr, library))
error = True

deps.remove(library)

deps = sorted(
deps.best(lambda x: SCORERS[args.score](x, original_deps[library]))
)
deps = sorted(deps.best(lambda x: SCORERS[args.score](x, original_deps[library])))
external_deps = sorted(
external_deps.best(
lambda x: SCORERS[args.score](x, original_external_deps[library])
Expand Down

0 comments on commit 736d071

Please sign in to comment.