Skip to content

Commit

Permalink
df
Browse files Browse the repository at this point in the history
  • Loading branch information
sshterm committed Sep 7, 2024
1 parent f791088 commit 2cd35d2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Machine/LinuxStats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,32 @@ public struct NetworkIoInfoAll: Identifiable {
}

public struct Threads: Identifiable {
// 线程的唯一标识符,使用pid作为id
public var id: String {
pid
}

// 线程的进程ID
public let pid: String
// 线程的ID
public let tid: String
// 线程占用的CPU百分比
public let cpu: Double
// 线程占用的内存百分比
public let mem: Double
// 线程的用户
public let user: String
// 线程的命令名称
public let comm: String
// 线程的命令行参数
public let args: String
}

public struct DiskInfo: Identifiable {
// 磁盘信息的唯一标识符,使用UUID生成
public let id = UUID()
// 磁盘已使用的空间大小
public var used: Int64 = 0
// 磁盘可用的空间大小
public var avail: Int64 = 0
}
22 changes: 22 additions & 0 deletions src/Machine/Machine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ public extension SSH {
return await getTemp()?.max()
}

/// 获取当前系统中的线程信息
/// - Returns: 返回一个包含线程信息的数组,如果获取失败则返回nil
func getThreads() async -> [Threads]? {
let (text, _) = await exec(command: "ps -eo pid,tid,%cpu,%mem,user,comm,args --sort=-%cpu | awk 'NR>1 {print $1\",\"$2\",\"$3\",\"$4\",\"$5\",\"$6\",\"$7}'")
guard let text = text?.trim(),!text.isEmpty else {
Expand All @@ -478,4 +480,24 @@ public extension SSH {
}
return threads
}

/// 获取磁盘使用信息
/// - Returns: 返回一个DiskInfo对象,包含总的已用空间和可用空间,如果获取失败则返回nil
func getDiskInfo() async -> DiskInfo? {
let (text, _) = await exec(command: "df | awk 'NR>1 {print $3\",\"$4}'")
guard let text = text?.trim(),!text.isEmpty else {
return nil
}
var info = DiskInfo()
let lines = text.components(separatedBy: .newlines)
for line in lines {
let df = line.trim().components(separatedBy: ",").map { Int64($0) ?? 0 }
guard df.count == 2 else {
continue
}
info.used += df[0]
info.avail += df[1]
}
return info
}
}

0 comments on commit 2cd35d2

Please sign in to comment.