AbstractConfig.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(const 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
  68. // in case it was never specified by the client
  69. virtual void validate(
  70. const std::chrono::time_point<std::chrono::system_clock>&
  71. fallbackProfileStartTime) = 0;
  72. // TODO: Separate out each profiler type into features?
  73. virtual void printActivityProfilerConfig(std::ostream& s) const;
  74. virtual void setActivityDependentConfig();
  75. // Helpers for use in handleOption
  76. // Split a string by delimiter and remove external white space
  77. std::vector<std::string> splitAndTrim(const std::string& s, char delim) const;
  78. // Lowercase for case-insensitive comparisons
  79. std::string toLower(std::string& s) const;
  80. // Does string end with suffix
  81. bool endsWith(const std::string& s, const std::string& suffix) const;
  82. // Conversions
  83. int64_t toIntRange(const std::string& val, int64_t min, int64_t max) const;
  84. int32_t toInt32(const std::string& val) const;
  85. int64_t toInt64(const std::string& val) const;
  86. bool toBool(std::string& val) const;
  87. void cloneFeaturesInto(AbstractConfig& cfg) const {
  88. for (const auto& feature : featureConfigs_) {
  89. cfg.featureConfigs_[feature.first] = feature.second->cloneDerived(cfg);
  90. }
  91. }
  92. private:
  93. // Time config was created / updated
  94. std::chrono::time_point<std::chrono::system_clock> timestamp_{};
  95. // Original configuration string, used for comparison
  96. std::string source_;
  97. // Configuration objects for optional features
  98. std::map<std::string, AbstractConfig*> featureConfigs_{};
  99. };
  100. } // namespace libkineto