$ show lilydjwg/swapview-rosetta
This repository contains implementations of a 'swapview' program that displays Linux process swap usage in many different programming languages. Each implementation reads from the /proc filesystem to gather swap information for all processes and outputs a formatted table sorted by swap usage. The project includes a top-level Makefile to build all implementations, benchmarking tools (bench.py and a Rust-based benchmarker) to compare performance across languages, and result files showing historical performance data. The code serves as both a practical utility for monitoring swap usage and a comparative study of language performance and idioms.
The LLM identified 4 files worth highlighting for reuse:
A well-designed Python benchmarking script that runs multiple implementations, collects timing data with resource.getrusage for accuracy, calculates statistics (mean, min, max, stdev), handles outliers using valid_percent, sorts results by performance, and shows diff output for mismatched implementations. Includes proper error handling, keyboard interrupt support, and clean separation of concerns. This framework could be reused to benchmark any set of command-line implementations with minimal configuration changes.
Demonstrates excellent use of the Rayon crate for parallel processing of /proc directory entries. Uses rayon::in_place_scope for thread safety, mpsc::channel for communication between workers, properly clones senders for each thread, collects results while filtering nulls, and employs par_sort_unstable_by_key for efficient parallel sorting. This pattern of spawning workers to process independent units of work (process IDs) and collecting results via channels is a solid parallel approach reusable for similar embarrassingly parallel problems.
Shows a clean implementation of Go goroutines and channels for concurrent processing of /proc entries. Uses a worker pool pattern where goroutines process individual PIDs and send results through a channel, with a WaitGroup to synchronize completion. The implementation properly handles errors, resource cleanup, and demonstrates idiomatic Go concurrency patterns. This worker pool approach is a classic Go pattern for parallelizing work and could be reused for similar problems requiring concurrent processing of independent tasks.
Exhibits solid Linux system programming practices with proper error handling via an assure macro, efficient memory allocation/reallocation strategies, correct resource cleanup (fclose/free), and careful parsing of /proc filesystem entries. The implementation handles edge cases like inaccessible processes, null-terminated command lines, and efficiently reads smaps files. While specific to Linux /proc, the patterns of directory enumeration, file reading, error checking, and resource management demonstrate valuable skills applicable to broader system programming contexts.