| ... | ... | @@ -1169,3 +1169,38 @@ pub fn execve( |
| 1169 | 1169 | |
| 1170 | 1170 | return os.execvpeZ_expandArg0(.no_expand, argv_buf.ptr[0].?, argv_buf.ptr, envp); |
| 1171 | 1171 | } |
| 1172 | |
| 1173 | pub const TotalSystemMemoryError = error{ |
| 1174 | UnknownTotalSystemMemory, |
| 1175 | }; |
| 1176 | |
| 1177 | /// Returns the total system memory, in bytes. |
| 1178 | pub fn totalSystemMemory() TotalSystemMemoryError!usize { |
| 1179 | switch (builtin.os.tag) { |
| 1180 | .linux => { |
| 1181 | return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory; |
| 1182 | }, |
| 1183 | .windows => { |
| 1184 | var kilobytes: std.os.windows.ULONGLONG = undefined; |
| 1185 | assert(std.os.windows.kernel32.GetPhysicallyInstalledSystemMemory(&kilobytes) == std.os.windows.TRUE); |
| 1186 | return kilobytes * 1024; |
| 1187 | }, |
| 1188 | else => return error.UnknownTotalSystemMemory, |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | fn totalSystemMemoryLinux() !usize { |
| 1193 | var file = try std.fs.openFileAbsoluteZ("/proc/meminfo", .{}); |
| 1194 | defer file.close(); |
| 1195 | var buf: [50]u8 = undefined; |
| 1196 | const amt = try file.read(&buf); |
| 1197 | if (amt != 50) return error.Unexpected; |
| 1198 | var it = std.mem.tokenize(u8, buf[0..amt], " \n"); |
| 1199 | const label = it.next().?; |
| 1200 | if (!std.mem.eql(u8, label, "MemTotal:")) return error.Unexpected; |
| 1201 | const int_text = it.next() orelse return error.Unexpected; |
| 1202 | const units = it.next() orelse return error.Unexpected; |
| 1203 | if (!std.mem.eql(u8, units, "kB")) return error.Unexpected; |
| 1204 | const kilobytes = try std.fmt.parseInt(usize, int_text, 10); |
| 1205 | return kilobytes * 1024; |
| 1206 | } |