Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Term9// File : Packages/SPTermKit/Sources/PTYProcess/ProcessInfoProbe.swift10// Module : PTYProcess11// Repository : https://git.spboucher.ai/sp-term12// Created : 2026-11-1713// Last Modified : 2026-11-1714//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : None specified (no license file in this repository)80// Maintainer : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Darwin88import Foundation8990/// Snapshot of a process as reported by the kernel (libproc). Read-only, no privileges needed91/// for processes of the same user.92public struct ProcessSnapshot: Sendable, Equatable {93 public var pid: pid_t94 public var name: String95 public var residentBytes: UInt6496 /// Cumulated user + system CPU time in seconds.97 public var cpuSeconds: Double98 public var startDate: Date?99100 public init(pid: pid_t, name: String, residentBytes: UInt64, cpuSeconds: Double, startDate: Date?) {101 self.pid = pid; self.name = name; self.residentBytes = residentBytes102 self.cpuSeconds = cpuSeconds; self.startDate = startDate103 }104}105106public enum ProcessProbe {107 /// Short name of a process (`proc_name`).108 public static func name(of pid: pid_t) -> String? {109 var buffer = [CChar](repeating: 0, count: 256)110 let n = proc_name(pid, &buffer, UInt32(buffer.count))111 guard n > 0 else { return nil }112 return String(decoding: buffer.prefix(Int(n)).map { UInt8(bitPattern: $0) }, as: UTF8.self)113 }114115 /// Current working directory of a process (`PROC_PIDVNODEPATHINFO`).116 public static func workingDirectory(of pid: pid_t) -> String? {117 var info = proc_vnodepathinfo()118 let size = Int32(MemoryLayout<proc_vnodepathinfo>.size)119 guard proc_pidinfo(pid, PROC_PIDVNODEPATHINFO, 0, &info, size) == size else { return nil }120 let path = withUnsafePointer(to: &info.pvi_cdir.vip_path) {121 $0.withMemoryRebound(to: CChar.self, capacity: Int(MAXPATHLEN)) { String(cString: $0) }122 }123 return path.isEmpty ? nil : path124 }125126 /// Memory, CPU time and start date of a process.127 public static func snapshot(of pid: pid_t) -> ProcessSnapshot? {128 var task = proc_taskallinfo()129 let size = Int32(MemoryLayout<proc_taskallinfo>.size)130 guard proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &task, size) == size else { return nil }131 var timebase = mach_timebase_info_data_t()132 mach_timebase_info(&timebase)133 let ticks = task.ptinfo.pti_total_user + task.ptinfo.pti_total_system134 let nanos = Double(ticks) * Double(timebase.numer) / Double(timebase.denom)135 let start = Date(timeIntervalSince1970: TimeInterval(task.pbsd.pbi_start_tvsec)136 + TimeInterval(task.pbsd.pbi_start_tvusec) / 1_000_000)137 return ProcessSnapshot(pid: pid, name: name(of: pid) ?? "?",138 residentBytes: task.ptinfo.pti_resident_size,139 cpuSeconds: nanos / 1_000_000_000, startDate: start)140 }141142 /// Process ids belonging to a process group.143 public static func members(ofProcessGroup group: pid_t) -> [pid_t] {144 var pids = [pid_t](repeating: 0, count: 512)145 let bytes = proc_listpids(UInt32(PROC_PGRP_ONLY), UInt32(group), &pids,146 Int32(pids.count * MemoryLayout<pid_t>.size))147 guard bytes > 0 else { return [] }148 return Array(pids.prefix(Int(bytes) / MemoryLayout<pid_t>.size)).filter { $0 > 0 }149 }150}151