CUT is a multi-threaded C++ application designed to track and analyze CPU usage on a system. It reads data from the "/proc/stat" file, processes the information, and displays the CPU usage for each CPU core.
Before running the application, ensure that you have the following dependencies installed:
- C++ compiler supporting C++20
- CMake version 3.25 or higher
- ncurses library
To use it, follow these steps:
- Create a build directory:
- Generate the build files using CMake:
- Build the application:
- Run the compiled executable:
mkdir build
cd build
cmake ..
make
./CUT
The application consists of several classes:
- Reader: Reads data from the /proc/stat file and pushes it into a buffer for analysis.
- Logger: Receives messages from all threads and writes them into a log file.
- Printer: Prints the CPU usage information to the console.
- Analyzer: Analyzes CPU data received from the reader and calculates the CPU usage.
The main()
function creates instances of these classes, starts the corresponding threads, and manages the termination process. It also registers a signal handler to handle the SIGINT signal (generated when the user interrupts the program).
Each class has its own implementation file (Reader.cpp, Logger.cpp, Printer.cpp, and Analyzer.cpp) that defines the member functions and implements the necessary logic.
The communication between threads is achieved using lock-free SPSC (Single Producer Single Consumer) queues. These queues act as buffers for passing data between the reader, logger, analyzer, and printer threads.
- The Reader thread reads data from the /proc/stat file and pushes it into the logger and analyzer buffers.
- The Logger thread receives messages from all threads and writes them into a log file.
- The Analyzer thread analyzes the CPU data received from the reader and calculates the CPU usage, pushing it into the printer buffer.
- The Printer thread prints the CPU usage information to the console.
The termination of the application is controlled by a termination flag (terminate
). When the SIGINT signal is received (e.g., by pressing CTRL+C), the signal handler sets the termination flag to true, and the main thread stops all the worker threads. This ensures a graceful shutdown of the application.
Contributions to the project are welcome. If you find any issues or have suggestions for improvements, please create a GitHub issue or submit a pull request.
The code is open source and available under the MIT License. Feel free to modify and distribute it as needed.