AbstractConfig.h 3.9 KB

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