0

trace_event: base::StringPiece -> std::string_view

Bug: 691162
Change-Id: I1477e89331f5773468a7744342120705549c4424
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5407651
Reviewed-by: Primiano Tucci <primiano@chromium.org>
Reviewed-by: Giovanni Ortuno Urquidi <ortuno@chromium.org>
Commit-Queue: Giovanni Ortuno Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1282920}
This commit is contained in:
Helmut Januschka
2024-04-05 02:33:17 +00:00
committed by Chromium LUCI CQ
parent bf1d5e5eb2
commit 278ff6faef
9 changed files with 56 additions and 47 deletions

@ -11,6 +11,7 @@
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "base/base_export.h"
@ -54,7 +55,7 @@ class BASE_EXPORT MemoryAllocatorDump {
// By design name, units and value_string are always coming from
// indefinitely lived const char* strings, the only reason we copy
// them into a std::string is to handle Mojo (de)serialization.
// TODO(hjd): Investigate optimization (e.g. using StringPiece).
// TODO(hjd): Investigate optimization (e.g. using std::string_view).
Entry(); // Only for deserialization.
Entry(std::string name, std::string units, uint64_t value);
Entry(std::string name, std::string units, std::string value);

@ -8,6 +8,7 @@
#include <algorithm>
#include <optional>
#include <string_view>
#include <utility>
#include "base/containers/contains.h"
@ -254,7 +255,7 @@ bool TraceConfig::EventFilterConfig::GetArgAsSet(
}
bool TraceConfig::EventFilterConfig::IsCategoryGroupEnabled(
const StringPiece& category_group_name) const {
std::string_view category_group_name) const {
return category_filter_.IsCategoryGroupEnabled(category_group_name);
}
@ -277,12 +278,12 @@ TraceConfig::TraceConfig() {
InitializeDefault();
}
TraceConfig::TraceConfig(StringPiece category_filter_string,
StringPiece trace_options_string) {
TraceConfig::TraceConfig(std::string_view category_filter_string,
std::string_view trace_options_string) {
InitializeFromStrings(category_filter_string, trace_options_string);
}
TraceConfig::TraceConfig(StringPiece category_filter_string,
TraceConfig::TraceConfig(std::string_view category_filter_string,
TraceRecordMode record_mode) {
InitializeFromStrings(category_filter_string,
TraceConfig::TraceRecordModeToStr(record_mode));
@ -292,7 +293,7 @@ TraceConfig::TraceConfig(const Value::Dict& config) {
InitializeFromConfigDict(config);
}
TraceConfig::TraceConfig(StringPiece config_string) {
TraceConfig::TraceConfig(std::string_view config_string) {
if (!config_string.empty())
InitializeFromConfigString(config_string);
else
@ -371,7 +372,7 @@ std::string TraceConfig::ToCategoryFilterString() const {
}
bool TraceConfig::IsCategoryGroupEnabled(
const StringPiece& category_group_name) const {
std::string_view category_group_name) const {
// TraceLog should call this method only as part of enabling/disabling
// categories.
return category_filter_.IsCategoryGroupEnabled(category_group_name);
@ -480,7 +481,7 @@ void TraceConfig::InitializeFromConfigDict(const Value::Dict& dict) {
}
}
void TraceConfig::InitializeFromConfigString(StringPiece config_string) {
void TraceConfig::InitializeFromConfigString(std::string_view config_string) {
std::optional<Value> dict = JSONReader::Read(config_string);
if (dict && dict->is_dict())
InitializeFromConfigDict(dict->GetDict());
@ -488,8 +489,8 @@ void TraceConfig::InitializeFromConfigString(StringPiece config_string) {
InitializeDefault();
}
void TraceConfig::InitializeFromStrings(StringPiece category_filter_string,
StringPiece trace_options_string) {
void TraceConfig::InitializeFromStrings(std::string_view category_filter_string,
std::string_view trace_options_string) {
if (!category_filter_string.empty())
category_filter_.InitializeFromString(category_filter_string);

@ -10,12 +10,12 @@
#include <memory>
#include <set>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>
#include "base/base_export.h"
#include "base/gtest_prod_util.h"
#include "base/strings/string_piece.h"
#include "base/trace_event/memory_dump_request_args.h"
#include "base/trace_event/trace_config_category_filter.h"
#include "base/values.h"
@ -142,7 +142,7 @@ class BASE_EXPORT TraceConfig {
bool GetArgAsSet(const char* key, std::unordered_set<std::string>*) const;
bool IsCategoryGroupEnabled(const StringPiece& category_group_name) const;
bool IsCategoryGroupEnabled(std::string_view category_group_name) const;
const std::string& predicate_name() const { return predicate_name_; }
const Value::Dict& filter_args() const { return args_; }
@ -193,10 +193,11 @@ class BASE_EXPORT TraceConfig {
// would disable everything but webkit; and use default options.
// Example: TraceConfig("-webkit", "");
// would enable everything but webkit; and use default options.
TraceConfig(StringPiece category_filter_string,
StringPiece trace_options_string);
TraceConfig(std::string_view category_filter_string,
std::string_view trace_options_string);
TraceConfig(StringPiece category_filter_string, TraceRecordMode record_mode);
TraceConfig(std::string_view category_filter_string,
TraceRecordMode record_mode);
// Create TraceConfig object from the trace config string.
//
@ -224,7 +225,7 @@ class BASE_EXPORT TraceConfig {
//
// Note: memory_dump_config can be specified only if
// disabled-by-default-memory-infra category is enabled.
explicit TraceConfig(StringPiece config_string);
explicit TraceConfig(std::string_view config_string);
// Functionally identical to the above, but takes a parsed dictionary as input
// instead of its JSON serialization.
@ -281,7 +282,7 @@ class BASE_EXPORT TraceConfig {
// Returns true if at least one category in the list is enabled by this
// trace config. This is used to determine if the category filters are
// enabled in the TRACE_* macros.
bool IsCategoryGroupEnabled(const StringPiece& category_group_name) const;
bool IsCategoryGroupEnabled(std::string_view category_group_name) const;
// Merges config with the current TraceConfig
void Merge(const TraceConfig& config);
@ -342,11 +343,11 @@ class BASE_EXPORT TraceConfig {
void InitializeFromConfigDict(const Value::Dict& dict);
// Initialize from a config string.
void InitializeFromConfigString(StringPiece config_string);
void InitializeFromConfigString(std::string_view config_string);
// Initialize from category filter and trace options strings
void InitializeFromStrings(StringPiece category_filter_string,
StringPiece trace_options_string);
void InitializeFromStrings(std::string_view category_filter_string,
std::string_view trace_options_string);
void SetMemoryDumpConfigFromConfigDict(const Value::Dict& memory_dump_config);
void SetDefaultMemoryDumpConfig();

@ -4,9 +4,10 @@
#include "base/trace_event/trace_config_category_filter.h"
#include <string_view>
#include "base/memory/ptr_util.h"
#include "base/strings/pattern.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
@ -49,10 +50,10 @@ bool TraceConfigCategoryFilter::IsEquivalentTo(
}
void TraceConfigCategoryFilter::InitializeFromString(
const StringPiece& category_filter_string) {
std::vector<StringPiece> split = SplitStringPiece(
std::string_view category_filter_string) {
std::vector<std::string_view> split = SplitStringPiece(
category_filter_string, ",", TRIM_WHITESPACE, SPLIT_WANT_ALL);
for (const StringPiece& category : split) {
for (std::string_view category : split) {
// Ignore empty categories.
if (category.empty())
continue;
@ -81,12 +82,12 @@ void TraceConfigCategoryFilter::InitializeFromConfigDict(
}
bool TraceConfigCategoryFilter::IsCategoryGroupEnabled(
const StringPiece& category_group_name) const {
std::string_view category_group_name) const {
bool had_enabled_by_default = false;
DCHECK(!category_group_name.empty());
StringViewTokenizer category_group_tokens(category_group_name, ",");
while (category_group_tokens.GetNext()) {
StringPiece category_group_token = category_group_tokens.token_piece();
std::string_view category_group_token = category_group_tokens.token_piece();
// Don't allow empty tokens, nor tokens with leading or trailing space.
DCHECK(IsCategoryNameAllowed(category_group_token))
<< "Disallowed category string";
@ -101,7 +102,7 @@ bool TraceConfigCategoryFilter::IsCategoryGroupEnabled(
category_group_tokens.Reset();
bool category_group_disabled = false;
while (category_group_tokens.GetNext()) {
StringPiece category_group_token = category_group_tokens.token_piece();
std::string_view category_group_token = category_group_tokens.token_piece();
for (const std::string& category : excluded_categories_) {
if (MatchPattern(category_group_token, category)) {
// Current token of category_group_name is present in excluded_list.
@ -132,7 +133,7 @@ bool TraceConfigCategoryFilter::IsCategoryGroupEnabled(
}
bool TraceConfigCategoryFilter::IsCategoryEnabled(
const StringPiece& category_name) const {
std::string_view category_name) const {
// Check the disabled- filters and the disabled-* wildcard first so that a
// "*" filter does not include the disabled.
for (const std::string& category : disabled_categories_) {
@ -246,7 +247,7 @@ void TraceConfigCategoryFilter::WriteCategoryFilterString(
}
// static
bool TraceConfigCategoryFilter::IsCategoryNameAllowed(StringPiece str) {
bool TraceConfigCategoryFilter::IsCategoryNameAllowed(std::string_view str) {
return !str.empty() && str.front() != ' ' && str.back() != ' ';
}

@ -6,10 +6,10 @@
#define BASE_TRACE_EVENT_TRACE_CONFIG_CATEGORY_FILTER_H_
#include <string>
#include <string_view>
#include <vector>
#include "base/base_export.h"
#include "base/strings/string_piece.h"
#include "base/values.h"
namespace base::trace_event {
@ -29,7 +29,7 @@ class BASE_EXPORT TraceConfigCategoryFilter {
// Initializes from category filter string. See TraceConfig constructor for
// description of how to write category filter string.
void InitializeFromString(const StringPiece& category_filter_string);
void InitializeFromString(std::string_view category_filter_string);
// Initializes TraceConfigCategoryFilter object from the config dictionary.
void InitializeFromConfigDict(const Value::Dict& dict);
@ -41,20 +41,20 @@ class BASE_EXPORT TraceConfigCategoryFilter {
// Returns true if at least one category in the list is enabled by this
// trace config. This is used to determine if the category filters are
// enabled in the TRACE_* macros.
bool IsCategoryGroupEnabled(const StringPiece& category_group_name) const;
bool IsCategoryGroupEnabled(std::string_view category_group_name) const;
// Returns true if the category is enabled according to this trace config.
// This tells whether a category is enabled from the TraceConfig's
// perspective. Please refer to IsCategoryGroupEnabled() to determine if a
// category is enabled from the tracing runtime's perspective.
bool IsCategoryEnabled(const StringPiece& category_name) const;
bool IsCategoryEnabled(std::string_view category_name) const;
void ToDict(Value::Dict& dict) const;
std::string ToFilterString() const;
// Returns true if category name is a valid string.
static bool IsCategoryNameAllowed(StringPiece str);
static bool IsCategoryNameAllowed(std::string_view str);
const StringList& included_categories() const { return included_categories_; }
const StringList& excluded_categories() const { return excluded_categories_; }

@ -4,11 +4,14 @@
#include "base/trace_event/trace_event_etw_export_win.h"
#include <windows.h>
#include <evntrace.h>
#include <guiddef.h>
#include <stddef.h>
#include <stdlib.h>
#include <windows.h>
#include <string_view>
#include <utility>
#include "base/at_exit.h"
@ -350,7 +353,7 @@ void TraceEventETWExport::AddCompleteEndEvent(
// static
bool TraceEventETWExport::IsCategoryGroupEnabled(
StringPiece category_group_name) {
std::string_view category_group_name) {
DCHECK(!category_group_name.empty());
auto* instance = GetInstanceIfExists();
@ -363,7 +366,7 @@ bool TraceEventETWExport::IsCategoryGroupEnabled(
StringViewTokenizer category_group_tokens(category_group_name.begin(),
category_group_name.end(), ",");
while (category_group_tokens.GetNext()) {
StringPiece category_group_token = category_group_tokens.token_piece();
std::string_view category_group_token = category_group_tokens.token_piece();
if (instance->IsCategoryEnabled(category_group_token)) {
return true;
}
@ -397,7 +400,8 @@ bool TraceEventETWExport::UpdateEnabledCategories() {
return true;
}
bool TraceEventETWExport::IsCategoryEnabled(StringPiece category_name) const {
bool TraceEventETWExport::IsCategoryEnabled(
std::string_view category_name) const {
// Try to find the category and return its status if found
auto it = categories_status_.find(category_name);
if (it != categories_status_.end())
@ -478,7 +482,7 @@ uint64_t CategoryGroupToETWKeyword(std::string_view category_group_name) {
StringViewTokenizer category_group_tokens(category_group_name.begin(),
category_group_name.end(), ",");
while (category_group_tokens.GetNext()) {
StringPiece category_group_token = category_group_tokens.token_piece();
std::string_view category_group_token = category_group_tokens.token_piece();
// Lookup the keyword for this part of the category_group_name
// and or in the keyword.

@ -6,14 +6,15 @@
#ifndef BASE_TRACE_EVENT_TRACE_EVENT_ETW_EXPORT_WIN_H_
#define BASE_TRACE_EVENT_TRACE_EVENT_ETW_EXPORT_WIN_H_
#include <stdint.h>
#include <windows.h>
#include <stdint.h>
#include <map>
#include <memory>
#include <string_view>
#include "base/base_export.h"
#include "base/strings/string_piece.h"
#include "base/trace_event/trace_event_impl.h"
#include "base/trace_event/trace_logging_minimal_win.h"
@ -67,7 +68,7 @@ class BASE_EXPORT TraceEventETWExport {
const char* name);
// Returns true if any category in the group is enabled.
static bool IsCategoryGroupEnabled(StringPiece category_group_name);
static bool IsCategoryGroupEnabled(std::string_view category_group_name);
private:
// Ensure only the provider can construct us.
@ -83,7 +84,7 @@ class BASE_EXPORT TraceEventETWExport {
bool UpdateEnabledCategories();
// Returns true if the category is enabled.
bool IsCategoryEnabled(StringPiece category_name) const;
bool IsCategoryEnabled(std::string_view category_name) const;
uint64_t CategoryStateToETWKeyword(const uint8_t* category_state);
@ -97,7 +98,7 @@ class BASE_EXPORT TraceEventETWExport {
std::unique_ptr<TlmProvider> etw_provider_;
// Maps category names to their status (enabled/disabled).
std::map<StringPiece, bool> categories_status_;
std::map<std::string_view, bool> categories_status_;
};
BASE_EXPORT uint64_t

@ -1600,7 +1600,7 @@ bool TraceLog::ShouldAddAfterUpdatingState(
} else {
// This is a thread id that we've seen before, but potentially with a
// new name.
std::vector<StringPiece> existing_names = base::SplitStringPiece(
std::vector<std::string_view> existing_names = base::SplitStringPiece(
existing_name->second, ",", base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
if (!Contains(existing_names, new_name)) {

@ -235,8 +235,8 @@ class BASE_EXPORT TracedValue : public ConvertableToTraceFormat {
ValueHolder(double value); // NOLINT(google-explicit-constructor)
ValueHolder(bool value); // NOLINT(google-explicit-constructor)
ValueHolder(void* value); // NOLINT(google-explicit-constructor)
// StringPiece's backing storage / const char* pointer needs to remain valid
// until TracedValue::Build is called.
// std::string_view's backing storage / const char* pointer needs to remain
// valid until TracedValue::Build is called.
// NOLINTNEXTLINE(google-explicit-constructor)
ValueHolder(std::string_view value);
// Create a copy to avoid holding a reference to a non-existing string: