티끌모아 태산

Chapter 4 : Timing Measurements 본문

CS 지식/시스템프로그래밍

Chapter 4 : Timing Measurements

goldpig 2023. 12. 25. 16:15
728x90

  Many computerized activites are time-driven; Operating system must have a notion of time. Main time services in the Linux kernel:

  • Maintain system uptime
  • Maintain wall clock time (the actual time of day, 'what time is it?')
  • Provide a mechanism to trigger activites at a certain time. 타이머는 특정 시간 간격이 지났음을 커널 또는 사용자 프로그램에 알립니다(알람 시계와 유사).

Computer Time

 

  • Computer operates on two time scales:
    • Processor hardware operates on microscopic time scale: Events(CPU instructions) have duration of only a few nanosecond(ns)
    • Operating system operates on macroscopic time scale: 이벤트(키 입력, 화면 새로고침, 디스크 접근 시간)의 지속 시간은 몇 밀리초(ms)입니다

Kernel (OS) Notion of Time

  • 시스템 가동 시간(System uptime): 시스템 시작 이후 경과한 시간(System uptime: time elapsed since system startup). Computer flow of time is not continuous, but discrete
  • Hardware provides system timer (aka kernel timer, PIT)
  • Period between two timer interrupts is called a 'tick'

Timer interrupt Frequency

Trade-off: A higher timer interrupt frequency provides finer granularity of time, but it increases the overhead for timer interrupt handling(타이머 인터럽트가 자주 발생하면 미세한 시간 분할이 가능하지만 너무 많이 발생하면 오버헤드가 증가한다. 빈번한 모드 스위칭으로 인한 오버헤드)

  • 단점: 타이머 인터럽트가 더 자주 실행됨 → 타이머 인터럽트 핸들러에서 보내는 시간이 더 많고, "유용한" 작업을 수행할 수 있는 시간이 덜 함.
  • jiffies: number of timer interrupts (ticks) since system boot. 시스템 부팅 이후 발생한 타이머 인터럽트의 횟수를 나타냅니다. 
    • During boot, jiffies is set to zero
    • jiffies incremented by one during each timer interrupt.
    • jiffies is a 32bit available.

Timing Measurement

  • 리눅스 커널은 다음과 같은 작업을 수행해야 합니다:
    • 현재 시간과 날짜(wall clock time) 유지; Keeping the current time and date
    • 현재 프로세스의 실행 시간 결정
    • 자원 통계 업데이트; Update resource staistics
    • 특정 시간 간격이 지났음을 커널 또는 사용자 프로그램에 알리기 위한 타이머 유지 관리
  • 구성 요소들:
    1. 하드웨어 시계 장치
      • 실시간 시계: RTC (Real Time Clock)
      • CPU 사이클 카운터: TSC (Time Stamp Counter) 레지스터
      • 커널 시계: PIT (Programmable Interval Timer)
    2. 시간 측정을 위한 커널 데이터 구조와 함수
    3. 시간 관련 시스템 호출

Hardware Clock Devices

  • Real Time Clock(RTC)
    • Keep track of time even when the PC is switched off (RTC is powered by battery in the motherboard)
    • Linux uses the RTC only during boot to derive the time and date
      1. During boot, kernel reads the RTC to initialize the wall clock time.
      2. wall clock time kept in the xtime variable
      3. Programming RCT by /dev/rtc/ device file
      4. Setting up clock by /sbin/clock program
  • Time Stamp Counter(TSC register)
    • For highly accurate time measurement but, it has Issues on the use of TSC ...
  • Programmable Interval Timer(PIT)
    • For the kernel to keep track of time.
    • PIT issues a special interrupt called "timer interrupt", which notifies the kernel that one more time interval has elapsed; PIT는 "타이머 인터럽트"라고 불리는 특별한 인터럽트를 발생시키며, 이는 커널에게 또 다른 시간 간격이 지났음을 알립니다.

⭐️The Linux Timekeepping Architecture

  • During system boot
    • The kernel reads the RTC to initialize the wall clock time
    • Wall clock time kept in the xtime variable
  • Kernel timer interrupt triggered by PIT on IRQ0
  • In timer interrupt handler, the kernel does the following things:
    1. increment jiffies_64 variables by 1
    2. Update wall clock time in the xtime variable
    3. Update resoruce usage statistics
    4. Raise softirq to run expired dynamic timers
    5. Execute scheduler_tick()

Timer Interrupt Handling

  • kernel/timer.c: do_timer()
    • Increment the jiffies_64 count by one
    • Update the system date and time and to compute the current system load
  • kernel/timer.c: update_process_times()
    • Update statistics about local CPU workload
    • Run any dynamic timer and have expired
    • Invoke scheduler_tick() to decrease the time slice counter of the current process and check if its quantum is exhausted

Supporting Software Timers

  • 소프트웨어 타이머
    • 주어진 시간 간격이 지난 후 함수를 호출할 수 있는 소프트웨어 기능입니다(타임아웃).
  • 소프트웨어 타이머의 유형
    • 다이나믹 타이머(일명, 커널 타이머 또는 커널 이벤트 타이머)
      • 커널에 의해 사용되며, 대부분은 장치 드라이버가 비정상적인 조건을 감지하기 위해 사용합니다.
      • Function calls scheduled at a specified/predefined time
      • Dynamically created and destroyed
      • No limit for the number of currently active dynamic timers
    • 인터벌 타이머
      • 프로그래머(사용자 프로세스)가 시스템 호출을 통해 미래의 특정 시점에 특정 함수의 실행을 강제하기 위해 사용합니다.
      • Send UNIX signals periodically to the process Or, send just one signal after a sepcified delay(single-shot)
      • Activated by means of the POSIX settimer() and alarm() system calls
  • 리눅스 소프트웨어 타이머의 한계
    • 타이머 함수에 대한 검사가 바텀 하프에 의해 이루어지므로, 커널은 정확한 만료 시간에 그들의 실행을 보장할 수 없습니다.
    • 실시간 애플리케이션에는 적합하지 않습니다.
더보기

⭐️Kernel에서 시간관련 정보를 관리하는 방식을 boot 시점, timer interrupt handling 시점에서 서술하시오.

시스템 부팅 시점에서의 시간 관리

  1. RTC(Real Time Clock)사용: 시스템 부팅 시, 리눅스 커널은 RTC 를 읽어 현재 시간과 날짜(wall clock time)를 초기화합니다. RTC는 PC가 꺼져 있을 때에도 시간을 유지하며, 마더보드에 있는 배터리에 의해 구동됩니다. (Keeps track of time even when the PC is switched off (RTC is powered by battery in the motherboard) and Linux uses the RTC only during boot to derive the time and date)
  2. Wall Clock Time Initilaization: 부팅 과정에서 커널은 RTC를 읽어 xtime 변수에 벽시계 시간(wall clock time)을 초기화합니다.(During boot, kernel reads the RTC to initialize the wall clock time) 벽시계 시간(wall clock time)은 주로 사용자 공간 프로그램에 의해 사용되며, 커널 자체는 주로 파일 시스템 활동에 이 시간을 사용합니다

타이머 인터럽트 핸들링 시점에서의 시간 관리

  1. jiffies 카운트 증가: 타이머 인터럽트가 발생할 때마다, 커널은 jiffies 변수를 1씩 증가시킵니다. jiffies는 시스템 부팅 이후 발생한 타이머 인터럽트의 횟수를 나타냅니다.
  2. 벽시계 시간 업데이트: 타이머 인터럽트 핸들러는 xtime 변수에 저장된 벽시계 시간을 업데이트합니다.
  3. 자원 사용 통계 업데이트: 타이머 인터럽트 핸들러는 또한 시스템 자원 사용 통계를 업데이트합니다. 이것은 프로세스가 사용자 모드와 커널 모드에서 소비한 시간을 계산하는 데 사용됩니다.
  4. Raise softirq to run expired dynamic timers. checking and executing dynamic timers; Raising softirq at every timer interrupt
  5. Execute scheduler_tick()

 

728x90

'CS 지식 > 시스템프로그래밍' 카테고리의 다른 글

Chapter 5 : System Calls  (0) 2023.12.25
Chapter 3: Interrupt and Exception  (0) 2023.12.23
Chapter 1: Linux Processes  (0) 2023.12.22