• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MINDSPORE_CCSRC_INCLUDE_COMMON_DEBUG_RDR_RECORDER_MANAGER_H_
17 #define MINDSPORE_CCSRC_INCLUDE_COMMON_DEBUG_RDR_RECORDER_MANAGER_H_
18 #include <vector>
19 #include <string>
20 #include <sstream>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <utility>
25 #include "utils/hash_map.h"
26 #include "include/common/debug/rdr/base_recorder.h"
27 #include "include/common/debug/env_config_parser.h"
28 #include "include/common/visible.h"
29 
30 namespace mindspore {
31 // The number is the reciprocal of the golden ratio.
32 const unsigned int MAGIC_CONSTANT = 0x9e3779b9;
33 const unsigned int HASH_SHIFT_LEFT = 6;
34 const unsigned int HASH_SHIFT_RIGHT = 2;
35 
36 template <typename T>
hash_combine(std::size_t * seed,const T & val)37 inline void hash_combine(std::size_t *seed, const T &val) {
38   *seed ^= std::hash<T>()(val) + MAGIC_CONSTANT + (*seed << HASH_SHIFT_LEFT) + (*seed >> HASH_SHIFT_RIGHT);
39 }
40 
41 template <typename T1, typename T2>
hash_seed(const T1 & val1,const T2 & val2)42 inline std::size_t hash_seed(const T1 &val1, const T2 &val2) {
43   std::size_t seed = 0;
44   hash_combine(&seed, val1);
45   hash_combine(&seed, val2);
46   return seed;
47 }
48 
49 struct pair_hash {
50   template <class T1, class T2>
operatorpair_hash51   std::size_t operator()(const std::pair<T1, T2> &p) const {
52     return hash_seed(p.first, p.second);
53   }
54 };
55 
56 class COMMON_EXPORT RecorderManager {
57  public:
58   static RecorderManager &Instance();
59 
60   void UpdateRdrEnable();
61   bool RdrEnable() const;
62   bool CheckRdrMemIsRecord() const;
63   void SetRdrMemIsRecord(bool is_enable = true);
64 
65   bool RecordObject(const BaseRecorderPtr &recorder);
66   BaseRecorderPtr GetRecorder(std::string module, std::string name);
67   void TriggerAll();
68   void Snapshot();
69   void ClearAll();
70 
71  private:
RecorderManager()72   RecorderManager() {}
~RecorderManager()73   ~RecorderManager() {}
74 
75   bool rdr_enable_{false};
76   int rdr_mode_{Exceptional};
77   bool rdr_has_record_mem_{false};
78 
79   mutable std::mutex mtx_;
80   // <module, name>, BaserRecorderPtr
81   mindspore::HashMap<std::pair<std::string, std::string>, BaseRecorderPtr, pair_hash> recorder_container_;
82 };
83 
84 namespace RDR {
85 COMMON_EXPORT void TriggerAll();
86 COMMON_EXPORT void Snapshot();
87 COMMON_EXPORT void ResetRecorder();
88 }  // namespace RDR
89 }  // namespace mindspore
90 #endif  // MINDSPORE_CCSRC_DEBUG_RDR_RECORDER_MANAGER_H_
91