Make TraceMeEncode compile with C++14
PiperOrigin-RevId: 315535324 Change-Id: I2d6bebd93a283d36b09f9b29fd84503402b4a5ac
This commit is contained in:
parent
e8bf41c3c6
commit
099b5eeead
tensorflow/core/profiler/lib
@ -1,5 +1,5 @@
|
||||
load("//tensorflow/core/platform:build_config_root.bzl", "if_static")
|
||||
load("//tensorflow:tensorflow.bzl", "if_not_android", "tf_cuda_library")
|
||||
load("//tensorflow:tensorflow.bzl", "if_not_android", "tf_cc_test", "tf_cuda_library")
|
||||
load("//tensorflow:tensorflow.bzl", "tf_pybind_cc_library_wrapper")
|
||||
|
||||
package(
|
||||
@ -113,6 +113,18 @@ cc_library(
|
||||
],
|
||||
)
|
||||
|
||||
tf_cc_test(
|
||||
name = "traceme_encode_test",
|
||||
srcs = ["traceme_encode_test.cc"],
|
||||
deps = [
|
||||
":traceme_encode",
|
||||
"//tensorflow/core:test",
|
||||
"//tensorflow/core:test_main",
|
||||
"//tensorflow/core/platform",
|
||||
"@com_google_absl//absl/strings",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "annotated_traceme",
|
||||
hdrs = ["annotated_traceme.h"],
|
||||
|
@ -19,7 +19,6 @@ limitations under the License.
|
||||
|
||||
#include <initializer_list>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
@ -28,6 +27,19 @@ limitations under the License.
|
||||
|
||||
namespace tensorflow {
|
||||
namespace profiler {
|
||||
|
||||
// An argument passed to TraceMeEncode.
|
||||
struct TraceMeArg {
|
||||
// This constructor is required because absl::AlphaNum is non-copyable.
|
||||
template <typename Value>
|
||||
TraceMeArg(absl::string_view k, Value v) : key(k), value(v) {}
|
||||
|
||||
TF_DISALLOW_COPY_AND_ASSIGN(TraceMeArg);
|
||||
|
||||
absl::string_view key;
|
||||
absl::AlphaNum value;
|
||||
};
|
||||
|
||||
namespace traceme_internal {
|
||||
|
||||
// Copies the contents of str to the address pointed by out.
|
||||
@ -45,23 +57,21 @@ TF_ATTRIBUTE_ALWAYS_INLINE inline char* Append(char* out,
|
||||
|
||||
// Appends args encoded as TraceMe metadata to name.
|
||||
TF_ATTRIBUTE_ALWAYS_INLINE inline std::string AppendArgs(
|
||||
std::string name,
|
||||
const std::initializer_list<std::pair<absl::string_view, absl::AlphaNum>>&
|
||||
args) {
|
||||
std::string name, std::initializer_list<TraceMeArg> args) {
|
||||
if (TF_PREDICT_TRUE(args.size() > 0)) {
|
||||
const auto old_size = name.size();
|
||||
auto new_size = old_size + args.size() * 2 + 1;
|
||||
for (const auto& arg : args) {
|
||||
new_size += arg.first.size() + arg.second.size();
|
||||
new_size += arg.key.size() + arg.value.size();
|
||||
}
|
||||
name.resize(new_size);
|
||||
char* const begin = &name[0];
|
||||
char* out = begin + old_size;
|
||||
*out++ = '#';
|
||||
for (const auto& arg : args) {
|
||||
out = Append(out, arg.first);
|
||||
out = Append(out, arg.key);
|
||||
*out++ = '=';
|
||||
out = Append(out, arg.second.Piece());
|
||||
out = Append(out, arg.value.Piece());
|
||||
*out++ = ',';
|
||||
}
|
||||
*(out - 1) = '#';
|
||||
@ -92,19 +102,16 @@ TF_ATTRIBUTE_ALWAYS_INLINE inline void AppendMetadata(
|
||||
// TraceMe trace_me([value1]() {
|
||||
// return TraceMeEncode("my_trace", {{"key1", value1}, {"key2", 42}});
|
||||
// });
|
||||
inline std::string TraceMeEncode(
|
||||
std::string name,
|
||||
std::initializer_list<std::pair<absl::string_view, absl::AlphaNum>> args) {
|
||||
TF_ATTRIBUTE_ALWAYS_INLINE inline std::string TraceMeEncode(
|
||||
std::string name, std::initializer_list<TraceMeArg> args) {
|
||||
return traceme_internal::AppendArgs(std::move(name), args);
|
||||
}
|
||||
inline std::string TraceMeEncode(
|
||||
absl::string_view name,
|
||||
std::initializer_list<std::pair<absl::string_view, absl::AlphaNum>> args) {
|
||||
TF_ATTRIBUTE_ALWAYS_INLINE inline std::string TraceMeEncode(
|
||||
absl::string_view name, std::initializer_list<TraceMeArg> args) {
|
||||
return traceme_internal::AppendArgs(std::string(name), args);
|
||||
}
|
||||
inline std::string TraceMeEncode(
|
||||
const char* name,
|
||||
std::initializer_list<std::pair<absl::string_view, absl::AlphaNum>> args) {
|
||||
TF_ATTRIBUTE_ALWAYS_INLINE inline std::string TraceMeEncode(
|
||||
const char* name, std::initializer_list<TraceMeArg> args) {
|
||||
return traceme_internal::AppendArgs(std::string(name), args);
|
||||
}
|
||||
|
||||
@ -116,8 +123,8 @@ inline std::string TraceMeEncode(
|
||||
// trace_me.AppendMetadata([value1]() {
|
||||
// return TraceMeEncode({{"key1", value1}, {"key2", 42}});
|
||||
// });
|
||||
inline std::string TraceMeEncode(
|
||||
std::initializer_list<std::pair<absl::string_view, absl::AlphaNum>> args) {
|
||||
TF_ATTRIBUTE_ALWAYS_INLINE inline std::string TraceMeEncode(
|
||||
std::initializer_list<TraceMeArg> args) {
|
||||
return traceme_internal::AppendArgs(std::string(), args);
|
||||
}
|
||||
|
||||
|
63
tensorflow/core/profiler/lib/traceme_encode_test.cc
Normal file
63
tensorflow/core/profiler/lib/traceme_encode_test.cc
Normal file
@ -0,0 +1,63 @@
|
||||
/* Copyright 2020 The TensorFlow Authors All Rights Reserved.
|
||||
|
||||
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.
|
||||
==============================================================================*/
|
||||
#include "tensorflow/core/profiler/lib/traceme_encode.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "tensorflow/core/platform/platform.h"
|
||||
#include "tensorflow/core/platform/test.h"
|
||||
|
||||
namespace tensorflow {
|
||||
namespace profiler {
|
||||
namespace {
|
||||
|
||||
TEST(TraceMeEncodeTest, NoArgTest) {
|
||||
EXPECT_EQ(TraceMeEncode("Hello!", {}), "Hello!");
|
||||
}
|
||||
|
||||
TEST(TraceMeEncodeTest, OneArgTest) {
|
||||
EXPECT_EQ(TraceMeEncode("Hello", {{"context", "World"}}),
|
||||
"Hello#context=World#");
|
||||
}
|
||||
|
||||
TEST(TraceMeEncodeTest, TwoArgsTest) {
|
||||
EXPECT_EQ(TraceMeEncode("Hello", {{"context", "World"}, {"request_id", 42}}),
|
||||
"Hello#context=World,request_id=42#");
|
||||
}
|
||||
|
||||
TEST(TraceMeEncodeTest, ThreeArgsTest) {
|
||||
EXPECT_EQ(TraceMeEncode("Hello", {{"context", "World"},
|
||||
{"request_id", 42},
|
||||
{"addr", absl::Hex(0xdeadbeef)}}),
|
||||
"Hello#context=World,request_id=42,addr=deadbeef#");
|
||||
}
|
||||
|
||||
#if !defined(PLATFORM_WINDOWS)
|
||||
TEST(TraceMeEncodeTest, TemporaryStringTest) {
|
||||
EXPECT_EQ(TraceMeEncode("Hello", {{std::string("context"),
|
||||
absl::StrCat("World:", 2020)}}),
|
||||
"Hello#context=World:2020#");
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(TraceMeEncodeTest, NoNameTest) {
|
||||
EXPECT_EQ(TraceMeEncode({{"context", "World"}, {"request_id", 42}}),
|
||||
"#context=World,request_id=42#");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace profiler
|
||||
} // namespace tensorflow
|
Loading…
Reference in New Issue
Block a user