So, what does it mean when you see 20% CPU usage? If I ask you to allocate resources to a specific microservice in Docker, how much CPU would you assign? Is it as simple as saying 20% means it uses 20% of the CPU, leaving the rest for others? Or is there more to it?
Let’s start with memory first since it’s fairly simple. Let’s examine what processes use how much RAM in our PC. Linux users can use the command
TOP
%MEM indicates the percentage of memory usage. Given that my PC has 32 GB of memory, Chrome with PID 4902 uses 3.1% of this, which is approximately 0.992 GB or about 1015 MB. Therefore, if I want to allocate cloud resources to a process with a memory usage of about 1 GB, capable of handling around 10 concurrent users, we should allocate 10 GB of RAM.
Unraveling CPU Allocation: Is It Just Like Memory Management?
CPU doesn’t work half or full at a single point of time. Understanding how the CPU works itself is a very big topic of study. Let’s try to abstract things. Let’s first understand what is a unit of work for CPU contrasting with RAM storage i.e in BYTES .
A CPU core processes instructions through the instruction cycle, which consists of five stages, repeating for every instruction:
1. Fetch (Instruction Fetch Cycle)
Retrieves an instruction from RAM using the Program Counter (PC).
Stores it in the Instruction Register (IR).
2. Decode (Instruction Decode Cycle)
The Control Unit (CU) interprets the instruction.
Identifies the operation (e.g., ADD, LOAD) and operands (registers or memory).
3. Execute (Execution Cycle)
The Arithmetic Logic Unit (ALU) or Floating-Point Unit (FPU) performs the operation.
If needed, fetches data from registers or memory.
4. Memory Access (if needed)
- Reads or writes data from/to RAM if the instruction involves memory.
5. Write Back (Result Write-Back Cycle)
Stores the result in a register or memory.
Updates the PC to fetch the next instruction.
This is one unit of work done by the CPU in one cycle, and the GHz speed of a CPU (gigahertz) represents how many such cycles it completes per second.
For example, a 3 GHz CPU executes 3 billion cycles per second, meaning it can process up to 3 billion instructions per second (depending on instruction complexity and pipeline efficiency). 🚀
Understanding the CPU's unit of work, it's clear that the CPU operates in cycles. The key point is that the CPU processes tasks one cycle at a time, and there's no direct indicator showing, for example, that Process A uses 30% of the CPU while Process B uses 40%. Instead, the CPU fully dedicates itself to Process A for several cycles before moving on to Process B. Alternatively, it might handle Process A for a few cycles, then switch to Process B. The takeaway is that the CPU works on one task at a time, without any divisions. It's just that the CPU is so fast, it seems like everything is happening in parallel.
So, if CPU works in cycles and one at a time, then how do we say process A uses 20% and process B uses 50% CPU?
🚀 The Magic of Time-Sharing
Your operating system (Windows, macOS, Linux) uses time-sharing to create an illusion of multitasking. Here’s what happens behind the scenes:
CPU Slices Time Between Tasks
The CPU rapidly switches between different processes, giving each one a small slice of time (e.g., a few milliseconds).
This switching is so fast that you don’t notice the gaps, making it seem like everything is running at once.
Measuring CPU Usage Over Time
Let’s say we observe the CPU for 1 second.
If Process A gets to run for 200 milliseconds (ms), it means it used 20% of that second.
If Process B runs for 500 ms, its CPU usage is 50%.
So this is the mystery of CPU usage and how it is calculated. Even though your CPU technically runs one instruction at a time per core, smart scheduling, fast switching, and multicore processing give us the illusion of true multitasking.
Next time your laptop fan spins up and your CPU usage spikes, you’ll know exactly what’s happening under the hood. 🎉 🚀 😃 🙌