Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

🌡️ Temperature Measurer Project

📌 Overview

This project is about understanding how your computer measures temperature. We'll learn how the system checks how hot your CPU (the main brain of your computer) is getting!


🔍 What Is Temperature Measuring?

Your computer has a special tool called a sensor that watches the temperature of your CPU. Think of it like a thermometer, but for your computer's brain!

On Linux systems using XFCE desktop, the temperature display is usually shown in the taskbar (the bar at the bottom or top of your screen).


📱 How to Set Up Temperature Display in Kali Linux

If you want to see your CPU temperature on your taskbar, follow these simple steps:

Step 1: Open Panel Preferences

Run this command in your terminal:

xfce4-panel --preferences

Step 2: Add the Temperature Sensor

  • Right-click on the taskbar
  • Click "Add new items" (you'll see this option when you hover over the panel)
  • A window will pop up - select "Sensor Plugin"

Step 3: Verify the Tool

Once you follow these steps, your computer is using this program to measure temperature:

xfce4-sensors-plugin

This is just a tool that reads and displays the temperature - it's not actually measuring it directly. It just shows you the number!


🔧 Where Does the Temperature Measuring Actually Happen?

The real temperature measurement happens deep inside your computer. Let's trace where:

The Real Program

The program doing the actual work is called xfce4-sensors-plugin, which is part of the XFCE project:

📎 Source Code: https://gitlab.xfce.org/panel-plugins/xfce4-sensors-plugin

How to Learn from the Source Code

You can use a helpful trick to read the code more easily:

  1. Go to the GitHub link
  2. Replace github.com with github.dev in the URL
  3. This opens the code in a special viewer inside your web browser - much easier to explore!

You can also use this helpful project: GitNexus (https://github.com/abhigyanpatwari/GitNexus) - it helps you understand code better.


💡 Important Discovery: How Temperature Really Gets Measured

After digging deep into the code, we found something important:

The XFCE Temperature Plugin Does NOT Directly Touch Your Hardware! 🎯

The plugin is just a reader - it reads information that's already been measured by something else. It gets the temperature data from:

  • /sys/class/hwmon/ - a special folder in Linux
  • libsensors - a library that reads sensors
  • lm-sensors - Linux monitoring tools

All of these ultimately come from something called the kernel driver named coretemp.

So the Real Chain Is:

Your CPU HardwareKernel Driver (coretemp)Linux System FilesXFCE PluginYour Screen


🔎 Finding Which Temperature Sensor You Have

To check which sensor driver your computer uses, run this command:

for hw in /sys/class/hwmon/hwmon*; do
    echo "=== $hw ==="
    cat $hw/name
done

You'll See One of These Two:

  • coretemp - If you have an Intel processor
  • k10temp - If you have an AMD processor

🛠️ Understanding the Kernel Driver

What Is a Kernel Driver?

A kernel driver is a special program that talks directly to your hardware. It's the real sensor that's actually measuring temperature!

Finding Where the Kernel Driver Lives

After you know which sensor you have (like coretemp), you can find where its code is stored.

Step 1: Find the Device Path

readlink -f /sys/class/hwmon/hwmon0/device

This command finds the actual physical location of your sensor. It might show something like:

/sys/devices/platform/coretemp.0

What does this mean?

  • coretemp = your temperature sensor driver
  • hwmon = short for "Hardware Monitor"

Step 2: Find the Kernel Driver File

Use this command to get information about the driver:

modinfo coretemp

This will show you where the kernel driver file is stored. On some systems, it might be at:

/lib/modules/6.18.12+kali-amd64/kernel/drivers/hwmon/coretemp.ko.xz

📊 How Temperature Is Actually Calculated

The Simple Explanation

Your CPU has special registers (tiny memory areas) that store information. Your computer:

  1. Reads the current temperature data from these registers
  2. Does some math to turn the raw data into degrees Celsius
  3. Saves the result so programs like XFCE can read it
  4. Displays it on your screen

The Technical Steps

Here's the journey of temperature measurement:

Hardware (MSR 0x19C Register)
    ↓  Raw temperature data is read (DTS bits)
    ↓  Maximum safe temperature is read (TjMax from MSR 0x1A2)
    ↓  Math formula is used: Temp = TjMax − DTS
    ↓  coretemp kernel driver saves this to /sys files
    ↓  xfce4-sensors-plugin reads /sys files
    ↓  Temperature appears on your taskbar!

📐 The Exact Formula Used

Here's how your computer calculates the actual temperature:

Step 1: Read Raw Temperature Data

The CPU's temperature sensor (DTS - Digital Thermal Sensor) reads the raw data from a special location called MSR 0x19C.

Step 2: Get the Maximum Safe Temperature

The system reads the maximum allowed temperature (TjMax) from another location called MSR 0x1A2. This is different for each CPU type!

Step 3: Do the Math

Temperature = TjMax − DTS_value

For example, if TjMax is 95°C and DTS is 20, then:

Temperature = 95 − 20 = 75°C

Step 4: Save and Display

The kernel driver saves this number to the Linux file system, and XFCE reads it and shows it on your screen!


⚙️ The Important Registers (For Advanced Users)

These are the special memory areas your CPU uses:

Register Purpose
0x1A2 Stores the maximum safe temperature (TjMax)
0xEE Backup location if 0x1A2 doesn't work
0x17 Third backup location
0x19C Stores the actual temperature reading (DTS)

⚠️ Limitations of This Temperature Measurement

While the temperature sensor does a good job, it has some limitations:

🔄 No Time Smoothing

Each temperature reading is independent. If your CPU gets a quick spike of heat, you might see a big jump in temperature (10°C in one second!). It doesn't smooth out quick changes.

📌 Fixed Maximum Temperature

The maximum safe temperature (TjMax) is read once when your computer starts up. If your BIOS (computer settings) tells it the wrong number, all readings will be wrong forever.

1️⃣ Only Whole Degree Precision

The temperature is stored as whole numbers only. So if the real temperature is 63.7°C, you'll only see 63 or 64°C on your screen.

🌡️ No Thermal Physics Model

Real silicon (the material of your CPU) has something called "thermal mass" - it takes time to heat up and cool down. The sensor doesn't account for this physics.

🌪️ No Fan Feedback

The temperature sensor doesn't talk to your cooling fan. The sensor and fan system are completely separate - they don't share information.


🚀 How to Compare and Verify (Advanced)

If you want to check if your temperature is being measured correctly, you can run this script:

#!/usr/bin/env bash
sudo modprobe msr >/dev/null 2>&1

# Read the maximum safe temperature for each core
RAW_TJ0=$(sudo rdmsr -p 0 0x1A2 2>/dev/null)
RAW_TJ1=$(sudo rdmsr -p 1 0x1A2 2>/dev/null)
TJ0=$(( (0x$RAW_TJ0 >> 16) & 0xFF ))
TJ1=$(( (0x$RAW_TJ1 >> 16) & 0xFF ))
[ "$TJ0" -eq 0 ] || [ "$TJ0" -gt 125 ] && TJ0=95
[ "$TJ1" -eq 0 ] || [ "$TJ1" -gt 125 ] && TJ1=95

echo "Per-core Maximum Temperature → Core 0: ${TJ0}°C  Core 1: ${TJ1}°C"
echo ""

while true; do
    # Get the timestamp
    T_READ=$(date '+%H:%M:%S.%N')

    # Read the normal system files
    SYS2=$(cat /sys/class/hwmon/hwmon0/temp2_input 2>/dev/null || echo 0)
    SYS3=$(cat /sys/class/hwmon/hwmon0/temp3_input 2>/dev/null || echo 0)

    # Read the raw hardware data
    RAW0=$(sudo rdmsr -p 0 0x19C 2>/dev/null)
    RAW1=$(sudo rdmsr -p 1 0x19C 2>/dev/null)
    T_MSR=$(date '+%H:%M:%S.%N')

    # Do the math to calculate temperature
    F0=$(( 0x$RAW0 )); F1=$(( 0x$RAW1 ))
    VALID0=$(( (F0 >> 31) & 1 ))
    VALID1=$(( (F1 >> 31) & 1 ))
    DTS0=$(( (F0 >> 16) & 0x7F ))
    DTS1=$(( (F1 >> 16) & 0x7F ))

    MSR0=$(( TJ0 - DTS0 ))
    MSR1=$(( TJ1 - DTS1 ))

    S0=$(( SYS2 / 1000 ))
    S1=$(( SYS3 / 1000 ))

    DIFF0=$(( MSR0 - S0 ))
    DIFF1=$(( MSR1 - S1 ))

    # Check if CPU is being throttled (slowed down due to heat)
    THR0=$(( F0 & 1 ))
    THR1=$(( F1 & 1 ))

    # Find the highest temperature of both cores
    PKG_MSR=$(( MSR0 > MSR1 ? MSR0 : MSR1 ))
    PKG_SYS=$(( S0 > S1 ? S0 : S1 ))

    # Display the results nicely
    clear
    printf "%-22s %10s %10s\n"  ""           "Core 0"     "Core 1"
    printf "%-22s %9s°C %9s°C\n" "Max Temp"    "$TJ0"       "$TJ1"
    printf "%-22s %10s %10s\n"  "Raw Data"    "$DTS0"      "$DTS1"
    printf "%-22s %9s°C %9s°C\n" "Calculated"  "$MSR0"      "$MSR1"
    printf "%-22s %9s°C %9s°C\n" "System Read" "$S0"        "$S1"
    printf "%-22s %9s°C %9s°C\n" "Difference"  "$DIFF0"     "$DIFF1"
    printf "%-22s %10s %10s\n"  "Valid Data"  "$VALID0"    "$VALID1"
    printf "%-22s %10s %10s\n"  "Throttling"  \
        "$([ $THR0 -eq 1 ] && echo YES || echo No)" \
        "$([ $THR1 -eq 1 ] && echo YES || echo No)"
    echo ""
    echo "Package (Highest of Both Cores)"
    printf "  Calculated: %d°C   System: %d°C   Difference: %d°C\n" \
        "$PKG_MSR" "$PKG_SYS" "$(( PKG_MSR - PKG_SYS ))"
    echo ""
    echo "Read time  : $T_READ"
    echo "Data time  : $T_MSR"

    sleep 1
done

What This Script Does:

  • Compares the temperature calculated from raw hardware data
  • With the temperature shown by the normal system
  • Shows if they match (they should be very close!)

If the difference is only 1°C or less, your temperature measurement is working correctly! ✅


📈 Our Research Results

Here's the complete flow from your CPU to your screen:

The Full Temperature Chain:

Your CPU Hardware
    ↓ (reads MSR 0x19C register)
Kernel Driver (coretemp.ko)
    ↓ (applies math formula)
System Files (/sys/class/hwmon/)
    ↓ (caches every ~250 milliseconds)
Linux Software
    ↓ (reads the files)
XFCE Sensors Plugin
    ↓ (shows the number)
Your Taskbar Display

Why Each Step Matters:

  1. Hardware - Actually measures the temperature using the Digital Thermal Sensor
  2. Kernel Driver - Does the math: Temperature = TjMax - DTS
  3. System Files - Makes temperature available for any program to read
  4. XFCE Plugin - Reads the temperature and displays it
  5. Your Screen - Shows you the number!

✅ Conclusion

Your computer's temperature measurement system works by:

  1. Reading raw sensor data from special CPU registers
  2. Doing math to convert that data to degrees
  3. Saving it to files that programs can read
  4. Displaying it on your screen

The amazing part? It all happens automatically, hundreds of times per second! 🚀

Now you understand how your computer checks its own temperature! 🎉

About

here is the efficient temp measurer that will measure temp better then the original temp is measuring the temp and it will help you to decide what to do with the pc at and that instant with the correct exact temp known to you at that time. That why you need this repo very much especially in summer.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors