|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. | 
|  | 3 | + * All rights reserved. | 
|  | 4 | + * | 
|  | 5 | + * This source code is licensed under the BSD-style license found in the | 
|  | 6 | + * LICENSE file in the root directory of this source tree. | 
|  | 7 | + */ | 
|  | 8 | + | 
|  | 9 | +#pragma once | 
|  | 10 | + | 
|  | 11 | +#include <re2/re2.h> | 
|  | 12 | +#include <memory> | 
|  | 13 | + | 
|  | 14 | +#include <executorch/runtime/core/event_tracer.h> | 
|  | 15 | +#include <executorch/runtime/core/result.h> | 
|  | 16 | +#include <executorch/runtime/platform/platform.h> | 
|  | 17 | + | 
|  | 18 | +namespace executorch::etdump { | 
|  | 19 | + | 
|  | 20 | +using ::executorch::aten::string_view; | 
|  | 21 | +using ::executorch::runtime::Result; | 
|  | 22 | + | 
|  | 23 | +/** | 
|  | 24 | + * ETDumpFilter is a class that filters intermediate output based on output's | 
|  | 25 | + * name by full regex filtering, or delegate debug indices by range-based | 
|  | 26 | + * filtering. | 
|  | 27 | + * | 
|  | 28 | + * Note that this filter supports up to MAX_REGEX_PATTERNS regex patterns with a | 
|  | 29 | + * maximum length of MAX_PATTERN_LENGTH characters each. | 
|  | 30 | + */ | 
|  | 31 | + | 
|  | 32 | +class ETDumpFilter : public ::executorch::runtime::EventTracerFilterBase { | 
|  | 33 | + public: | 
|  | 34 | +  ETDumpFilter(); | 
|  | 35 | +  ~ETDumpFilter() override = default; | 
|  | 36 | +  /** | 
|  | 37 | +   * Adds a regex pattern to the filter. | 
|  | 38 | +   * | 
|  | 39 | +   * @param[in] pattern A c string representing the regex pattern to be added. | 
|  | 40 | +   * | 
|  | 41 | +   * @return A Result<bool> indicating the success or failure of adding the | 
|  | 42 | +   * regex pattern. | 
|  | 43 | +   *         - True if the pattern is successfully added. | 
|  | 44 | +   *         - False if the pattern could not be added or if the maximum number | 
|  | 45 | +   * of patterns is exceeded. | 
|  | 46 | +   *         - An error code if number of pattern has reached to cap, or any | 
|  | 47 | +   * error occurs during regex compilation. | 
|  | 48 | +   */ | 
|  | 49 | +  Result<bool> add_regex(string_view pattern); | 
|  | 50 | +  /** | 
|  | 51 | +   * Sets the range for the delegate debug index filtering as [start, end). | 
|  | 52 | +   * Note that this function will flush the existing range. | 
|  | 53 | +   * | 
|  | 54 | +   * @param[in] start The start of the range for filtering. | 
|  | 55 | +   * @param[in] end The end of the range for filtering. | 
|  | 56 | +   * | 
|  | 57 | +   * @return A Result<bool> indicating the success or failure of setting the | 
|  | 58 | +   * range. | 
|  | 59 | +   *         - True if the range is successfully set. | 
|  | 60 | +   *         - An error code if an error occurs. | 
|  | 61 | +   */ | 
|  | 62 | +  Result<bool> set_debug_handle_range(size_t start, size_t end); | 
|  | 63 | + | 
|  | 64 | +  /** | 
|  | 65 | +   * Filters events based on the given name or delegate debug index. | 
|  | 66 | +   * | 
|  | 67 | +   * Note that everytime only one of either the name or delegate_debug_index | 
|  | 68 | +   * should be passed in. | 
|  | 69 | +   * | 
|  | 70 | +   * @param[in] name A pointer to a string representing the `name` of the | 
|  | 71 | +   * event. If `delegate_debug_index` is not set to kUnsetDebugHandle, `name` | 
|  | 72 | +   * should be set to nullptr. | 
|  | 73 | +   * | 
|  | 74 | +   * @param[in] delegate_debug_index A DebugHandle representing the debug index | 
|  | 75 | +   * of the delegate. If `name` is not nullptr, this should be set to | 
|  | 76 | +   * kUnsetDebugHandle. | 
|  | 77 | +   * | 
|  | 78 | +   * @return A Result<bool> indicating whether the event matches the filter | 
|  | 79 | +   * criteria. | 
|  | 80 | +   *         - True if the event matches the filter, or filter is unset. | 
|  | 81 | +   *         - False if the event does not match or is unknown. | 
|  | 82 | +   *         - An error code if an error occurs during filtering. | 
|  | 83 | +   */ | 
|  | 84 | +  Result<bool> filter( | 
|  | 85 | +      const char* name, | 
|  | 86 | +      ::executorch::runtime::DelegateDebugIntId delegate_debug_index) override; | 
|  | 87 | + | 
|  | 88 | +  /** | 
|  | 89 | +   * Returns the number of regex patterns in the filter. | 
|  | 90 | +   */ | 
|  | 91 | +  size_t get_n_regex() const; | 
|  | 92 | + | 
|  | 93 | + private: | 
|  | 94 | +  std::vector<std::unique_ptr<re2::RE2>> regex_patterns_; | 
|  | 95 | +  size_t range_start_ = 0; | 
|  | 96 | +  size_t range_end_ = 0; | 
|  | 97 | +  Result<bool> filter_name_(const char* name); | 
|  | 98 | +  Result<bool> filter_delegate_debug_index_( | 
|  | 99 | +      ::executorch::runtime::DelegateDebugIntId delegate_debug_index); | 
|  | 100 | +}; | 
|  | 101 | + | 
|  | 102 | +} // namespace executorch::etdump | 
0 commit comments