AbstractConfig.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #pragma once
  9. #include <chrono>
  10. #include <map>
  11. #include <memory>
  12. #include <string>
  13. #include <vector>
  14. namespace libkineto {
  15. class AbstractConfig {
  16. public:
  17. AbstractConfig& operator=(const AbstractConfig&) = delete;
  18. AbstractConfig(AbstractConfig&&) = delete;
  19. AbstractConfig& operator=(AbstractConfig&&) = delete;
  20. virtual ~AbstractConfig() {
  21. for (const auto& p : featureConfigs_) {
  22. delete p.second;
  23. }
  24. }
  25. // Return a copy of the full derived class
  26. virtual AbstractConfig* cloneDerived(AbstractConfig& parent) const = 0;
  27. // Returns true if successfully parsed the config string
  28. bool parse(const std::string& conf);
  29. // Default setup for signal-triggered profiling
  30. virtual void setSignalDefaults() {
  31. for (auto& p : featureConfigs_) {
  32. p.second->setSignalDefaults();
  33. }
  34. }
  35. // Default setup for client-triggered profiling
  36. virtual void setClientDefaults() {
  37. for (auto& p : featureConfigs_) {
  38. p.second->setClientDefaults();
  39. }
  40. }
  41. // Time config was created / updated
  42. std::chrono::time_point<std::chrono::system_clock> timestamp() const {
  43. return timestamp_;
  44. }
  45. // Source config string that this was parsed from
  46. const std::string& source() const {
  47. return source_;
  48. }
  49. AbstractConfig& feature(std::string name) const {
  50. const auto& pos = featureConfigs_.find(name);
  51. return *pos->second;
  52. }
  53. // Transfers ownership of cfg arg
  54. void addFeature(const std::string& name, AbstractConfig* cfg) {
  55. featureConfigs_[name] = cfg;
  56. }
  57. protected:
  58. AbstractConfig() {}
  59. AbstractConfig(const AbstractConfig& other) = default;
  60. // Return true if the option was recognized and successfully parsed.
  61. // Throw std::invalid_argument if val is invalid.
  62. virtual bool handleOption(const std::string& name, std::string& val);
  63. // Perform post-validation checks, typically conditons involving
  64. // multiple options.
  65. // Throw std::invalid_argument if automatic correction can not be made.
  66. //
  67. // @param fallbackProfileStartTime Specify a fallback profile start timestamp in case it was never specified by the client
  68. virtual void validate(const std::chrono::time_point<std::chrono::system_clock>& fallbackProfileStartTime) = 0;
  69. // TODO: Separate out each profiler type into features?
  70. virtual void printActivityProfilerConfig(std::ostream& s) const;
  71. // Helpers for use in handleOption
  72. // Split a string by delimiter and remove external white space
  73. std::vector<std::string> splitAndTrim(const std::string& s, char delim) const;
  74. // Lowercase for case-insensitive comparisons
  75. std::string toLower(std::string& s) const;
  76. // Does string end with suffix
  77. bool endsWith(const std::string& s, const std::string& suffix) const;
  78. // Conversions
  79. int64_t toIntRange(const std::string& val, int64_t min, int64_t max) const;
  80. int32_t toInt32(const std::string& val) const;
  81. int64_t toInt64(const std::string& val) const;
  82. bool toBool(std::string& val) const;
  83. void cloneFeaturesInto(AbstractConfig& cfg) const {
  84. for (const auto& feature : featureConfigs_) {
  85. cfg.featureConfigs_[feature.first] = feature.second->cloneDerived(cfg);
  86. }
  87. }
  88. private:
  89. // Time config was created / updated
  90. std::chrono::time_point<std::chrono::system_clock> timestamp_{};
  91. // Original configuration string, used for comparison
  92. std::string source_{""};
  93. // Configuration objects for optional features
  94. std::map<std::string, AbstractConfig*> featureConfigs_{};
  95. };
  96. } // namespace libkineto