Files
miclib-zig/examples/enumerate.zig
T
2026-07-01 16:28:57 +02:00

117 lines
3.9 KiB
Zig

const std = @import("std");
const mic = @import("miclib");
pub fn main() !void {
var devices = mic.DeviceList.init() catch |err| {
std.debug.print("mic_get_devices failed: {s}\n", .{@errorName(err)});
printLastMicError();
return err;
};
defer devices.deinit();
const count = try devices.count();
std.debug.print("found {} MIC device(s)\n", .{count});
for (0..count) |index| {
const device_num = try devices.deviceAtIndex(index);
var device = mic.Device.open(device_num) catch |err| {
std.debug.print("mic{}: open failed: {s}\n", .{ device_num, @errorName(err) });
printLastMicError();
continue;
};
defer device.deinit();
try printDevice(device_num, device);
}
}
fn printDevice(device_num: u32, device: mic.Device) !void {
var buffer: [mic.default_string_buffer_len]u8 = undefined;
const device_type = device.deviceType() catch 0;
std.debug.print("\nmic{}: {s}\n", .{ device_num, device.name() });
std.debug.print(" type: {}{s}\n", .{ device_type, if (device_type == mic.knc_id) " (KNC)" else "" });
if (device.serialNumber(buffer[0..])) |serial| {
std.debug.print(" serial: {s}\n", .{serial});
} else |err| printSkipped("serial", err);
if (device.siliconSku(buffer[0..])) |sku| {
std.debug.print(" sku: {s}\n", .{sku});
} else |err| printSkipped("sku", err);
if (device.pciConfig()) |pci_value| {
var pci = pci_value;
defer pci.deinit();
std.debug.print(" pci: {x:0>4}:{x:0>2}:{x:0>2}, vendor={x:0>4}, device={x:0>4}\n", .{
pci.domainId() catch 0,
pci.busNumber() catch 0,
pci.deviceNumber() catch 0,
pci.vendorId() catch 0,
pci.deviceId() catch 0,
});
if (pci.linkSpeed(buffer[0..])) |speed| {
std.debug.print(" pci-link: {s} x{}\n", .{ speed, pci.linkWidth() catch 0 });
} else |err| printSkipped("pci-link", err);
} else |err| printSkipped("pci", err);
if (device.coresInfo()) |cores_value| {
var cores = cores_value;
defer cores.deinit();
std.debug.print(" cores: {}, freq={} MHz, voltage={} mV\n", .{
cores.count() catch 0,
cores.frequency() catch 0,
cores.voltage() catch 0,
});
} else |err| printSkipped("cores", err);
if (device.memoryInfo()) |memory_value| {
var memory = memory_value;
defer memory.deinit();
const memory_type = memory.memoryType(buffer[0..]) catch "unknown";
std.debug.print(" memory: {} MiB, type={s}, freq={} MHz, ecc={}\n", .{
memory.size() catch 0,
memory_type,
memory.frequency() catch 0,
memory.eccMode() catch 0,
});
} else |err| printSkipped("memory", err);
if (device.thermalInfo()) |thermal_value| {
var thermal = thermal_value;
defer thermal.deinit();
std.debug.print(" thermals: die={} C, fan={} RPM, pwm={}\n", .{
thermal.dieTemp() catch 0,
thermal.fanRpm() catch 0,
thermal.fanPwm() catch 0,
});
} else |err| printSkipped("thermals", err);
if (device.versionInfo()) |version_value| {
var version = version_value;
defer version.deinit();
if (version.uosVersion(buffer[0..])) |uos| {
std.debug.print(" uos: {s}\n", .{uos});
} else |err| printSkipped("uos", err);
if (version.flashVersion(buffer[0..])) |flash| {
std.debug.print(" flash: {s}\n", .{flash});
} else |err| printSkipped("flash", err);
} else |err| printSkipped("version", err);
}
fn printSkipped(name: []const u8, err: anyerror) void {
std.debug.print(" {s}: unavailable ({s})\n", .{ name, @errorName(err) });
}
fn printLastMicError() void {
const last = mic.lastErrorString();
if (last.len != 0) std.debug.print("miclib: {s}\n", .{last});
}