system
Given the following post, respond as if you're commenting on Reddit:
human
ELI5: How do our bodies know when we've had enough sleep and wake us up?

I'm about to go to bed and then I started to wonder, how the body measures how much sleep it has and how it just wakes itself up.

Edit: Thanks wow that makes sense considering i sleep with the window open.
gpt
You have an internal clock of sorts, the circadian rhythm, so your body knows more or less what time it is. This is partially controlled by sunlight and will go all out of whack if you live in a cave. 

The pineal gland, towards the end of the day as determined by the circadian rhythm, will produce a chemical that makes you sleepy. Early in the morning, you're asleep but the circadian rhythm knows it's time to get up and the pineal gland stops producing the chemical. After a while it dissipates and you wake up.

If you've ever had to get up very early you may have noticed you're exceptionally groggy for a while, this is because you're still full of sleep hormones as your internal clock can't predict a change to your routine. After a few days of getting up at 3am you do adapt, but it's always a little harder to go against the natural sunlight hours.

As for how the circadian rhythms are driven by biological clocks, that's a bit more complicated and afaik, invloves timing how long it takes for DNA instructions to be read inside the brain.