Tick-Tock; The Clock You Mistook

Original article was published by Mohamed El-Geish on Artificial Intelligence on Medium


Tick-Tock; The Clock You Mistook

An AI-Rewrite of My LinkedIn Article With the Same Title & Introduction

Photo Credit: Buenosia Carol from Pexels

Disclaimer: This article was written mostly by GPT-3 given the introduction of my original article as a prompt; a few edits were made.

The concept of time in computing is as important as the role it plays in other aspects of life. For any non-trivial program, understanding the order of events (e.g. reading and writing data) is key to verifying its correctness (e.g. ACID for DBMSs); such order is enforced using a logical clock that keeps track of logical time. The more prominent uses of time in computing have to do with capturing and representing physical time as we — humans — use it (e.g. capturing the time when a payment was made); physical clocks provide methods to obtain and maintain physical time in computer systems. A typical computer has a real-time clock (RTC), which is an integrated circuit that keeps track of physical time even when the computer is powered-off (in which case, a battery installed on the motherboard keeps it running). When the operating system loads up, it initializes a system clock using the value of the RTC, and then proceeds to maintain physical time using a system timer.

Logical Clocks

Physical time is not the same as logical time, which is the concept at the heart of understanding computer clocks. Physical time moves forward in a linear fashion, whereas logical time moves forward in a more non-deterministic manner. Logical time is sometimes referred to as virtual time; the design of logical clocks is inspired by physical clocks, but there are a few important differences. In a logical clock, the current time is obtained by looking at the current state of two logical hands (also called logical clocks), one for the past and one for the future. Logical time is the basis for understanding the concept of CPU clock. We’ll start by explaining how logical time works, and then we’ll move to the concept of CPU clocks.

Logical Clocks: Past vs. Future

A logical clock is a data structure (typically a pair of integers) that keeps track of the current time: It can be used to distinguish between events that happened before or after the current time. The logical past and the logical future of a given logical clock are represented with the past and future logical clocks, respectively. The past and future logical clocks are represented as a pair of integers. The logical past and the logical future are not necessarily the same size: the past logical clock is typically smaller than the future logical clock, and is usually a few hundred bits long. This is because the logical past is typically used to represent a small time interval: for example, in databases, the logical past is used to store the transaction time in a transaction log. The future logical clock, on the other hand, is typically long enough to cover all the possible time in the future (the far-future logical clock).

Logical Clocks: Time Windows

In addition to keeping track of the logical past and the logical future, a logical clock is also used to define a time window, which is a period of time when the time value of the logical clock is guaranteed to stay the same. The time window for a logical clock is typically used to keep track of events that happened during a given time frame, and to order these events according to their logical time.

Logical Clocks: Updates

When a logical clock is updated, the size of the time window changes. A logical clock can be updated from the past logical clock to the future logical clock. As a result, the time window for this logical clock is updated from its previous size (between the past logical clock and the future logical clock) to its updated size (between the future logical clock and the far-future logical clock). In addition, when a logical clock is updated, a new past logical clock is created, and the future logical clock is updated.

Logical Clocks: Order

In a distributed system, logical clocks are distributed and kept in sync using the logical clock protocol (LCP). The LCP guarantees that the logical clocks of all the nodes in the distributed system are updated in the same order. Logical clocks are typically updated when an event happens. We’ll see later in this article how CPUs use logical clocks to order events in a given time frame.

CPU Clock

A CPU clock is a logical clock that is updated by the CPU itself. The CPU clock is used to keep track of physical time, and is typically represented by the real-time clock integrated circuit (RTC), which is a physical clock that keeps track of physical time when the computer is powered-off. There are two types of CPU clocks: system CPU clocks and thread CPU clocks. A system CPU clock is a CPU clock that is used by the operating system to keep track of physical time. Some operating systems use a single system CPU clock to represent the physical time for the entire computer, while other operating systems use a separate system CPU clock for each CPU. A thread CPU clock is a CPU clock that is used by a thread to keep track of the time that the thread has spent executing on the CPU.