1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use libc::{c_int, c_uint, c_ulonglong, c_char, pid_t, pthread_t};
use num::{ToPrimitive, FromPrimitive};
use topology_object::TopologyObject;
use bitmap::IntHwlocBitmap;
use std::cmp::{PartialOrd, Ordering};
use support::TopologySupport;

pub enum HwlocTopology {}

/// Represents the type of a topology object.
///
/// Note that (partial) ordering for object types is implemented as a call
/// into the `hwloc` library which defines ordering as follows:
///
/// - A == B if `ObjectType::A` and `ObjectType::B` are the same.
/// - A < B if `ObjectType::A` includes objects of type `ObjectType::B`.
/// - A > B if objects of `ObjectType::A` are included in type `ObjectType::B`.
///
/// It can also help to think of it as comparing the relative depths of each type, so
/// a `ObjectType::System` will be smaller than a `ObjectType::PU` since the system
/// contains processing units.
#[repr(u32)]
#[derive(Debug,Clone)]
pub enum ObjectType {
    /// The whole system that is accessible to hwloc. That may comprise several
    /// machines in SSI systems like Kerrighed.
    System,
    /// The typical root object type. A set of processors and memory with cache
    /// coherency.
    Machine,
    /// A set of processors around memory which the processors can directly
    /// access.
    NUMANode,
    /// Physical package, what goes into a socket. In the physical meaning,
    /// i.e. that you can add or remove physically.
    Package,
    /// The Cache. Can be L1i, L1d, L2, L3,...
    Cache,
    /// A computation unit (may be shared by several logical processors).
    Core,
    /// Processing Unit, or (Logical) Processor.
    ///
    /// An execution unit (may share a core with some other logical
    /// processors, e.g. in the case of an SMT core). Objects of this kind
    /// are always reported and can thus be used as fallback when others are
    /// not.
    PU,
    /// Group objects.
    ///
    /// Objects which do not fit in the above but are detected by hwloc and
    /// are useful to take into account for affinity. For instance, some
    /// operating systems expose their arbitrary processors aggregation this
    /// way. And hwloc may insert such objects to group NUMA nodes according
    /// to their distances.
    ///
    /// These objects are ignored when they do not bring any structure.
    Group,
    /// Miscellaneous objects.
    ///
    /// Objects without particular meaning, that can e.g. be
    /// added by the application for its own use, or by hwloc
    /// for miscellaneous objects such as MemoryModule (DIMMs).
    Misc,
    /// Any bridge that connects the host or an I/O bus, to another I/O bus.
    ///
    /// Bridge objects have neither CPU sets nor node sets.
    /// They are not added to the topology unless I/O discovery
    /// is enabled through the custom flags.
    Bridge,
    /// PCI device.
    ///
    /// These objects have neither CPU sets nor node sets.
    /// They are not added to the topology unless I/O discovery
    /// is enabled through the custom flags.
    PCIDevice,
    /// Operating system device.
    ///
    /// These objects have neither CPU sets nor node sets. They are not
    /// added to the topology unless I/O discovery is enabled
    /// through the custom flags.
    OSDevice,
    /// An internal sentinel value.
    TypeMax,
}

impl PartialOrd for ObjectType {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        let compared = unsafe { hwloc_compare_types(self.clone(), other.clone()) };
        match compared {
            c if c < 0 => Some(Ordering::Less),
            c if c == 0 => Some(Ordering::Equal),
            c if c > 0 => Some(Ordering::Greater),
            _ => None,
        }
    }
}

impl PartialEq for ObjectType {
    fn eq(&self, other: &Self) -> bool {
        match self.partial_cmp(other) {
            Some(Ordering::Equal) => true,
            _ => false,
        }
    }
}

#[derive(Debug,PartialEq)]
pub enum TypeDepthError {
    /// No object of given type exists in the topology.
    TypeDepthUnknown = -1,
    /// Objects of given type exist at different depth in the topology.
    TypeDepthMultiple = -2,
    /// Virtual depth for bridge object level.
    TypeDepthBridge = -3,
    /// Virtual depth for PCI device object level.
    TypeDepthPCIDevice = -4,
    /// Virtual depth for software device object level.
    TypeDepthOSDevice = -5,
    /// HWLOC returned a depth error which is not known to the rust binding.
    UnkownTypeDepthError = -99,
}

const TOPOLOGY_FLAG_WHOLE_SYSTEM: i64 = 1;
const TOPOLOGY_FLAG_IS_THIS_SYSTEM: i64 = 2;
const TOPOLOGY_FLAG_IO_DEVICES: i64 = 4;
const TOPOLOGY_FLAG_IO_BRIDGES: i64 = 8;
const TOPOLOGY_FLAG_WHOLE_IO: i64 = 16;
const TOPOLOGY_FLAG_I_CACHES: i64 = 32;

#[derive(Debug,PartialEq)]
pub enum TopologyFlag {
    WholeSystem = TOPOLOGY_FLAG_WHOLE_SYSTEM as isize,
    IsThisSystem = TOPOLOGY_FLAG_IS_THIS_SYSTEM as isize,
    IoDevices = TOPOLOGY_FLAG_IO_DEVICES as isize,
    IoBridges = TOPOLOGY_FLAG_IO_BRIDGES as isize,
    WholeIo = TOPOLOGY_FLAG_WHOLE_IO as isize,
    ICaches = TOPOLOGY_FLAG_I_CACHES as isize,
}

impl ToPrimitive for TopologyFlag {
    fn to_i64(&self) -> Option<i64> {
        match *self {
            TopologyFlag::WholeSystem => Some(TopologyFlag::WholeSystem as i64),
            TopologyFlag::IsThisSystem => Some(TopologyFlag::IsThisSystem as i64),
            TopologyFlag::IoDevices => Some(TopologyFlag::IoDevices as i64),
            TopologyFlag::IoBridges => Some(TopologyFlag::IoBridges as i64),
            TopologyFlag::WholeIo => Some(TopologyFlag::WholeIo as i64),
            TopologyFlag::ICaches => Some(TopologyFlag::ICaches as i64),
        }
    }

    fn to_u64(&self) -> Option<u64> {
        self.to_i64().and_then(|x| x.to_u64())
    }
}

impl FromPrimitive for TopologyFlag {
    fn from_i64(n: i64) -> Option<Self> {
        match n {
            TOPOLOGY_FLAG_WHOLE_SYSTEM => Some(TopologyFlag::WholeSystem),
            TOPOLOGY_FLAG_IS_THIS_SYSTEM => Some(TopologyFlag::IsThisSystem),
            TOPOLOGY_FLAG_IO_DEVICES => Some(TopologyFlag::IoDevices),
            TOPOLOGY_FLAG_IO_BRIDGES => Some(TopologyFlag::IoBridges),
            TOPOLOGY_FLAG_WHOLE_IO => Some(TopologyFlag::WholeIo),
            TOPOLOGY_FLAG_I_CACHES => Some(TopologyFlag::ICaches),
            _ => None,
        }
    }

    fn from_u64(n: u64) -> Option<Self> {
        FromPrimitive::from_i64(n as i64)
    }
}


#[link(name = "hwloc")]
extern "C" {

    // === Topology Creation and Destruction ===

    pub fn hwloc_topology_init(topology: *mut *mut HwlocTopology) -> c_int;
    pub fn hwloc_topology_load(topology: *mut HwlocTopology) -> c_int;
    pub fn hwloc_topology_destroy(topology: *mut HwlocTopology);

    // === Topology Detection Configuration and Query ===

    pub fn hwloc_topology_set_flags(topology: *mut HwlocTopology, flags: c_ulonglong) -> c_int;
    pub fn hwloc_topology_get_flags(topology: *mut HwlocTopology) -> c_ulonglong;
    pub fn hwloc_topology_get_support(topology: *mut HwlocTopology) -> *const TopologySupport;

    // === Object levels, depths and types ===

    pub fn hwloc_topology_get_depth(topology: *mut HwlocTopology) -> c_uint;
    pub fn hwloc_get_type_depth(topology: *mut HwlocTopology, object_type: ObjectType) -> c_int;
    pub fn hwloc_get_depth_type(topology: *mut HwlocTopology, depth: c_uint) -> ObjectType;
    pub fn hwloc_get_nbobjs_by_depth(topology: *mut HwlocTopology, depth: c_uint) -> c_uint;


    pub fn hwloc_get_obj_by_depth(topology: *mut HwlocTopology,
                                  depth: c_uint,
                                  idx: c_uint)
                                  -> *mut TopologyObject;

    // === CPU Binding ===
    pub fn hwloc_set_cpubind(topology: *mut HwlocTopology,
                             set: *const IntHwlocBitmap,
                             flags: c_int)
                             -> c_int;
    pub fn hwloc_get_cpubind(topology: *mut HwlocTopology,
                             set: *mut IntHwlocBitmap,
                             flags: c_int)
                             -> c_int;
    pub fn hwloc_get_last_cpu_location(topology: *mut HwlocTopology,
                                       set: *mut IntHwlocBitmap,
                                       flags: c_int)
                                       -> c_int;
    pub fn hwloc_set_proc_cpubind(topology: *mut HwlocTopology,
                                  pid: pid_t,
                                  set: *const IntHwlocBitmap,
                                  flags: c_int)
                                  -> c_int;
    pub fn hwloc_get_proc_cpubind(topology: *mut HwlocTopology,
                                  pid: pid_t,
                                  set: *const IntHwlocBitmap,
                                  flags: c_int)
                                  -> c_int;
    pub fn hwloc_set_thread_cpubind(topology: *mut HwlocTopology,
                                thread: pthread_t,
                                set: *const IntHwlocBitmap,
                                flags: c_int)
                                -> c_int;
    pub fn hwloc_get_thread_cpubind(topology: *mut HwlocTopology,
                                  pid: pthread_t,
                                  set: *const IntHwlocBitmap,
                                  flags: c_int)
                                  -> c_int;
      //int hwloc_get_proc_last_cpu_location(hwloc_topology_t topology, hwloc_pid_t pid, hwloc_cpuset_t set, int flags);

    // === Bitmap Methods ===
    pub fn hwloc_bitmap_alloc() -> *mut IntHwlocBitmap;
    pub fn hwloc_bitmap_free(bitmap: *mut IntHwlocBitmap);
    pub fn hwloc_bitmap_list_asprintf(strp: *mut *mut c_char,
                                      bitmap: *mut IntHwlocBitmap)
                                      -> c_int;
    pub fn hwloc_bitmap_set(bitmap: *mut IntHwlocBitmap, id: c_uint);
    pub fn hwloc_bitmap_set_range(bitmap: *mut IntHwlocBitmap, begin: c_uint, end: c_int);
    pub fn hwloc_bitmap_clr(bitmap: *mut IntHwlocBitmap, id: c_uint);
    pub fn hwloc_bitmap_clr_range(bitmap: *mut IntHwlocBitmap, begin: c_uint, end: c_int);
    pub fn hwloc_bitmap_weight(bitmap: *mut IntHwlocBitmap) -> c_int;
    pub fn hwloc_bitmap_zero(bitmap: *mut IntHwlocBitmap);
    pub fn hwloc_bitmap_iszero(bitmap: *mut IntHwlocBitmap) -> c_int;
    pub fn hwloc_bitmap_isset(bitmap: *mut IntHwlocBitmap, id: c_uint) -> c_int;
    pub fn hwloc_bitmap_singlify(bitmap: *mut IntHwlocBitmap);
    pub fn hwloc_bitmap_not(result: *mut IntHwlocBitmap, bitmap: *mut IntHwlocBitmap);
    pub fn hwloc_bitmap_first(bitmap: *mut IntHwlocBitmap) -> c_int;
    pub fn hwloc_bitmap_last(bitmap: *mut IntHwlocBitmap) -> c_int;
    pub fn hwloc_bitmap_dup(src: *const IntHwlocBitmap) -> *mut IntHwlocBitmap;
    pub fn hwloc_bitmap_compare(left: *const IntHwlocBitmap, right: *const IntHwlocBitmap) -> c_int;

    pub fn hwloc_obj_type_snprintf(into: *mut c_char,
                                   size: c_int,
                                   object: *const TopologyObject,
                                   verbose: bool)
                                   -> c_int;
    pub fn hwloc_obj_attr_snprintf(into: *mut c_char,
                                   size: c_int,
                                   object: *const TopologyObject,
                                   separator: *const c_char,
                                   verbose: bool)
                                   -> c_int;

    pub fn hwloc_compare_types(type1: ObjectType, type2: ObjectType) -> c_int;
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn should_convert_flag_to_primitive() {
        assert_eq!(1, TopologyFlag::WholeSystem as u64);
        assert_eq!(16, TopologyFlag::WholeIo as u64);
    }

    #[test]
    fn should_compare_object_types() {
        assert!(ObjectType::Machine == ObjectType::Machine);
        assert!(ObjectType::PU == ObjectType::PU);

        assert!(ObjectType::Machine < ObjectType::PU);
        assert!(ObjectType::PU > ObjectType::Cache);
    }

}