Improve buffer allocation to avoid stream too long errors + remove redundant code
Some checks failed
PikaOS Package Build & Release (amd64-v3) / build (push) Failing after 18s

This commit is contained in:
ferreo 2025-02-10 10:06:37 +00:00
parent cae8fbb27c
commit 829b587002
7 changed files with 48 additions and 108 deletions

View File

@ -1 +1 @@
1
2

View File

@ -1,3 +1,9 @@
pikafetch (0.5.1-101pika1) pika; urgency=medium
* Improve buffer allocation to avoid stream too long errors + remove redundant code
-- ferreo <harderthanfire@gmail.com> Wed, 18 Jan 2023 21:48:14 +0000
pikafetch (0.5.0-101pika6) pika; urgency=medium
* Swap GPU info source location

View File

@ -69,12 +69,16 @@ pub fn getShellInfo(allocator: std.mem.Allocator) !ShellInfo {
}
const stat_path = try std.fmt.allocPrint(allocator, "/proc/{d}/stat", .{current_pid});
defer allocator.free(stat_path);
const stat_file = std.fs.openFileAbsolute(stat_path, .{}) catch break;
defer stat_file.close();
var stat_buf: [1024]u8 = undefined;
const stat_bytes = try stat_file.readAll(&stat_buf);
const stat = try stat_file.stat();
const stat_buf = try allocator.alloc(u8, @intCast(stat.size));
defer allocator.free(stat_buf);
const stat_bytes = try stat_file.readAll(stat_buf);
const stat_content = stat_buf[0..stat_bytes];
var stat_iter = std.mem.tokenizeScalar(u8, stat_content, ' ');

View File

@ -41,9 +41,16 @@ pub fn getDisksInfo(allocator: mem.Allocator) ![]DiskInfo {
var buf_reader = std.io.bufferedReader(file.reader());
var reader = buf_reader.reader();
var buf: [1024]u8 = undefined;
var line_buf = ArrayList(u8).init(allocator);
defer line_buf.deinit();
while (true) {
reader.readUntilDelimiterArrayList(&line_buf, '\n', 16384) catch |err| switch (err) {
error.EndOfStream => break,
else => return err,
};
const line = line_buf.items;
while (try reader.readUntilDelimiterOrEof(&buf, '\n')) |line| {
var iter = mem.splitScalar(u8, line, ' ');
const device = iter.next() orelse continue;
const mount_point = iter.next() orelse continue;
@ -62,6 +69,8 @@ pub fn getDisksInfo(allocator: mem.Allocator) ![]DiskInfo {
.used = info.used,
.fs_type = try allocator.dupe(u8, fs_type),
});
try line_buf.resize(0);
}
return try result.toOwnedSlice();

View File

@ -88,19 +88,25 @@ pub fn getGPUInfo(allocator: mem.Allocator) ![][]const u8 {
fn detectGPU(allocator: mem.Allocator, card_name: []const u8) !?[]const u8 {
const vendor_path = try allocPrint(allocator, "/sys/class/drm/{s}/device/vendor", .{card_name});
defer allocator.free(vendor_path);
const vendor_file = fs.openFileAbsolute(vendor_path, .{}) catch return null;
defer vendor_file.close();
var vendor_buf: [32]u8 = undefined;
const vendor_len = vendor_file.readAll(&vendor_buf) catch return null;
const vendor_stat = try vendor_file.stat();
const vendor_buf = try allocator.alloc(u8, @intCast(vendor_stat.size));
defer allocator.free(vendor_buf);
const vendor_len = try vendor_file.readAll(vendor_buf);
const vendor_str = mem.trim(u8, vendor_buf[0..vendor_len], "\n\r \t0x");
const device_path = try allocPrint(allocator, "/sys/class/drm/{s}/device/device", .{card_name});
defer allocator.free(device_path);
const device_file = fs.openFileAbsolute(device_path, .{}) catch return null;
defer device_file.close();
var device_buf: [32]u8 = undefined;
const device_len = device_file.readAll(&device_buf) catch return null;
const device_stat = try device_file.stat();
const device_buf = try allocator.alloc(u8, @intCast(device_stat.size));
defer allocator.free(device_buf);
const device_len = try device_file.readAll(device_buf);
const device_str = mem.trim(u8, device_buf[0..device_len], "\n\r \t0x");
const pci_info = lookupPCIInfo(allocator, vendor_str, device_str) catch null;
@ -233,98 +239,6 @@ fn lookupPCIInfo(allocator: mem.Allocator, vendor_id: []const u8, device_id: []c
return null;
}
fn queryHwdb(allocator: mem.Allocator, card_name: []const u8) !?[]const u8 {
const modalias_path = try allocPrint(allocator, "/sys/class/drm/{s}/device/modalias", .{card_name});
const modalias_file = fs.openFileAbsolute(modalias_path, .{}) catch return null;
defer modalias_file.close();
var modalias_buf: [256]u8 = undefined;
const modalias = modalias_file.readAll(&modalias_buf) catch return null;
const modalias_str = mem.trimRight(u8, modalias_buf[0..modalias], "\n");
if (!mem.startsWith(u8, modalias_str, "pci:")) return null;
var vendor_id: ?[]const u8 = null;
var device_id: ?[]const u8 = null;
var modalias_idx: usize = 4;
while (modalias_idx < modalias_str.len) : (modalias_idx += 1) {
if (modalias_str[modalias_idx] == 'v' and modalias_idx + 9 <= modalias_str.len) {
vendor_id = modalias_str[modalias_idx + 1 .. modalias_idx + 9];
var j = modalias_idx + 9;
while (j < modalias_str.len) : (j += 1) {
if (modalias_str[j] == 'd' and j + 9 <= modalias_str.len) {
device_id = modalias_str[j + 1 .. j + 9];
break;
}
}
break;
}
}
if (vendor_id == null or device_id == null) return null;
const hwdb_file = fs.openFileAbsolute("/usr/lib/udev/hwdb.d/20-pci-vendor-model.hwdb", .{}) catch return null;
defer hwdb_file.close();
const stat = try hwdb_file.stat();
const mapped = try std.posix.mmap(
null,
@as(usize, @intCast(stat.size)),
std.posix.PROT.READ,
.{ .TYPE = std.os.linux.MAP_TYPE.PRIVATE },
hwdb_file.handle,
0,
);
defer std.posix.munmap(mapped);
var vendor_name: ?[]const u8 = null;
var model_name: ?[]const u8 = null;
const vendor_pattern = try allocPrint(allocator, "pci:v{s}*", .{vendor_id.?});
const device_pattern = try allocPrint(allocator, "pci:v{s}d{s}*", .{ vendor_id.?, device_id.? });
var it = mem.splitScalar(u8, mapped, '\n');
while (it.next()) |line| {
const trimmed = mem.trim(u8, line, " \t\r\n");
if (trimmed.len == 0 or simdStartsWith(trimmed, "#")) continue;
if (vendor_name == null and simdStartsWith(trimmed, vendor_pattern)) {
if (it.next()) |next_line| {
const next_trimmed = mem.trim(u8, next_line, " \t\r\n");
if (simdStartsWith(next_trimmed, "ID_VENDOR_FROM_DATABASE=")) {
const raw_vendor = next_trimmed["ID_VENDOR_FROM_DATABASE=".len..];
vendor_name = try allocator.dupe(u8, raw_vendor);
}
}
} else if (model_name == null and simdStartsWith(trimmed, device_pattern)) {
if (it.next()) |next_line| {
const next_trimmed = mem.trim(u8, next_line, " \t\r\n");
if (simdStartsWith(next_trimmed, "ID_MODEL_FROM_DATABASE=")) {
const raw_model = next_trimmed["ID_MODEL_FROM_DATABASE=".len..];
model_name = try allocator.dupe(u8, raw_model);
}
}
}
if (vendor_name != null and model_name != null) break;
}
if (vendor_name != null and model_name != null) {
const gpu_info = GPUInfo{
.vendor = try allocator.dupe(u8, getShortVendorName(vendor_name.?)),
.model = try allocator.dupe(u8, cleanModelName(model_name.?)),
.driver = null,
.memory = null,
};
const formatted = try gpu_info.format(allocator);
return formatted;
}
return null;
}
fn getBootTime() !i64 {
const file = try fs.openFileAbsolute("/proc/stat", .{});
defer file.close();

View File

@ -4,16 +4,23 @@ pub fn getMotherboard(allocator: std.mem.Allocator) ![]const u8 {
const board_name = try std.fs.openFileAbsolute("/sys/class/dmi/id/board_name", .{});
defer board_name.close();
var buffer: [256]u8 = undefined;
const size = try board_name.readAll(&buffer);
const stat = try board_name.stat();
const buffer = try allocator.alloc(u8, @intCast(stat.size));
defer allocator.free(buffer);
const size = try board_name.readAll(buffer);
const name = std.mem.trim(u8, buffer[0..size], "\n\r ");
if (name.len == 0) {
const product_name = try std.fs.openFileAbsolute("/sys/class/dmi/id/product_name", .{});
defer product_name.close();
const product_size = try product_name.readAll(&buffer);
return allocator.dupe(u8, std.mem.trim(u8, buffer[0..product_size], "\n\r "));
const product_stat = try product_name.stat();
const product_buffer = try allocator.alloc(u8, @intCast(product_stat.size));
defer allocator.free(product_buffer);
const product_size = try product_name.readAll(product_buffer);
return allocator.dupe(u8, std.mem.trim(u8, product_buffer[0..product_size], "\n\r "));
}
return allocator.dupe(u8, name);

View File

@ -1,9 +1,9 @@
const std = @import("std");
pub fn getHostname(allocator: std.mem.Allocator) ![]const u8 {
var hostname_buffer: [64]u8 = undefined;
const hostname = try std.posix.gethostname(&hostname_buffer);
return allocator.dupe(u8, hostname);
var hostname_buf: [std.posix.HOST_NAME_MAX]u8 = undefined;
const hostname = try std.posix.gethostname(&hostname_buf);
return allocator.dupe(u8, std.mem.trim(u8, hostname, "\x00"));
}
pub fn getUsername(allocator: std.mem.Allocator) ![]const u8 {