• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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_MINDDATA_DATASET_ENGINE_CACHE_HW_H_
17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_CACHE_HW_H_
18 
19 #ifdef NUMA_ENABLED
20 #include <numa.h>
21 #endif  // NUMA_ENABLED
22 #include <sched.h>
23 #include <stdlib.h>
24 #include <map>
25 #include <memory>
26 #include <set>
27 #include <string>
28 #include <vector>
29 #include "minddata/dataset/engine/cache/cache_common.h"
30 #include "minddata/dataset/util/memory_pool.h"
31 #include "minddata/dataset/util/path.h"
32 #include "minddata/dataset/util/status.h"
33 #include "minddata/dataset/util/task.h"
34 
35 #if defined(__APPLE__)
36 #define SYSCTL_CORE_COUNT "machdep.cpu.core_count"
37 #include <sys/sysctl.h>
38 #include <mach/thread_policy.h>
39 
40 typedef struct cpu_set {
41   uint32_t count;
42 } cpu_set_t;
43 
CPU_ZERO(cpu_set_t * cs)44 static inline void CPU_ZERO(cpu_set_t *cs) { cs->count = 0; }
45 
CPU_SET(int num,cpu_set_t * cs)46 static inline void CPU_SET(int num, cpu_set_t *cs) { cs->count |= (1 << num); }
47 
CPU_ISSET(int num,cpu_set_t * cs)48 static inline int CPU_ISSET(int num, cpu_set_t *cs) { return (cs->count & (1 << num)); }
49 
50 #endif  // __APPLE__
51 
52 namespace mindspore {
53 namespace dataset {
54 class CacheServerHW {
55  public:
56   CacheServerHW();
57   ~CacheServerHW() = default;
58 
59   /// \brief Get Numa node info without using numa library
60   /// \return Status object
61   Status GetNumaNodeInfo();
62 
63   /// \brief Set thread affinity
64   Status SetAffinity(const Task &tk, numa_id_t numa_node);
65 
66   /// \brief Get total number of cpu(s)
GetCpuCount()67   int32_t GetCpuCount() const { return num_cpus_; }
68 
69   /// \brief Get total number of numa nodes
GetNumaNodeCount()70   int32_t GetNumaNodeCount() const { return numa_cpuset_.empty() ? 1 : numa_cpuset_.size(); }
71 
72   /// \brief Get a list of cpu for a given numa node.
73   std::vector<cpu_id_t> GetCpuList(numa_id_t numa_id);
74 
75   static bool numa_enabled();
76 
77   /// \brief Return the numa the current thread is running on.
78   numa_id_t GetMyNode() const;
79 
80   /// \brief Interleave a given memory block. Used by shared memory only.
81   static void InterleaveMemory(void *ptr, size_t sz);
82 
83   /// \brief Assign a given memory block to a numa node. Used by shared memory only.
84   void AssignToNode(numa_id_t numa_id, void *ptr, size_t sz) const;
85 
86   /// \brief Set default memory policy.
87   static Status SetDefaultMemoryPolicy(CachePoolPolicy);
88 
89   /// \brief This returns the size (in bytes) of the physical RAM on the machine.
90   /// \return the size (in bytes) of the physical RAM on the machine.
91   static int64_t GetTotalSystemMemory();
92 
93   /// \brief Get the size (in bytes) of available memory on the machine by reading from file /proc/meminfo.
94   static uint64_t GetAvailableMemory();
95 
96  private:
97   constexpr static char kSysNodePath[] = "/sys/devices/system/node";
98   constexpr static char kMemInfoFileName[] = "/proc/meminfo";
99   int32_t num_cpus_;
100   std::map<numa_id_t, cpu_set_t> numa_cpuset_;
101   std::map<numa_id_t, int32_t> numa_cpu_cnt_;
102 };
103 }  // namespace dataset
104 }  // namespace mindspore
105 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_CACHE_HW_H_
106