Workspace IndexAlgorithms › Day 47

Observing Production with eBPF TODO

Algorithms · Day 47 / 100 · C. Concurrency & Performance Engineering (Day 36-51)

Concept

eBPF is a sandboxed VM that runs safely inside the kernel. When a user-written program is loaded into the kernel, a verifier statically checks termination and memory-access safety; only programs that pass are JIT-compiled and attached to kprobes, uprobes, tracepoints, perf events, network hooks, and the like. The program and userspace exchange data through shared structures called maps (hash maps, arrays, ring buffers, etc.). The key benefit is being able to observe a running system's internal events without writing a new kernel module or restarting/recompiling the application. Higher-level tools like bpftrace and BCC wrap this whole process down to a one-line script.

It's nearly the only way to trace a latency spike or a specific syscall bottleneck in production that won't reproduce elsewhere, without a code change or redeploy, and it reaches layers that application logs never touch.

Code & Formula

# eBPF로 프로덕션 관측 — 커널 프로브가 이벤트를 map에 쌓고, 유저스페이스가 읽어 히스토그램을 낸다.
# 실제 커널 훅 대신 "probe가 이벤트를 map에 기록한다"는 구조만 순수 파이썬으로 흉내낸다.

import random
from collections import defaultdict

random.seed(1)

class EbpfMap:
    """kprobe/tracepoint가 기록하는 공유 map(dict)을 흉내"""
    def __init__(self):
        self.hist = defaultdict(int)  # latency bucket(us, 로그 스케일) -> count

    def record(self, latency_us):
        bucket = 1
        while bucket * 2 <= latency_us:
            bucket *= 2
        self.hist[bucket] += 1

def bpftrace_like_probe(events, ebpf_map):
    """실제로는 커널이 syscall 진입/종료 시각차를 계산해 넣어주는 부분을 시뮬레이션"""
    for latency_us in events:
        ebpf_map.record(latency_us)

# 유휴 상태: 대부분 짧은 지연
idle_events = [max(1, int(random.gauss(50, 15))) for _ in range(200)]
# 부하 상태: 디스크 I/O 경합으로 꼬리가 길어짐
loaded_events = [max(1, int(random.gauss(50, 15))) for _ in range(200)]
loaded_events += [int(random.gauss(4000, 800)) for _ in range(20)]  # I/O 경합 스파이크

idle_map = EbpfMap()
loaded_map = EbpfMap()
bpftrace_like_probe(idle_events, idle_map)
bpftrace_like_probe(loaded_events, loaded_map)

def print_hist(name, m):
    print(f"--- {name} (syscall latency, us, log2 bucket) ---")
    for bucket in sorted(m.hist):
        print(f"  <= {bucket:5d}us : {'#' * m.hist[bucket]} ({m.hist[bucket]})")

print_hist("idle", idle_map)
print_hist("loaded", loaded_map)
print("\n부하 상태에서 4000us대 버킷이 새로 등장 -> 재배포 없이 I/O 경합 구간을 특정.")

Exercise

Use a one-line bpftrace script to pull a histogram of disk I/O latency or syscall call counts for a specific process, and compare it under load versus idle.

Practical Connection

When sync stalls on a blockchain node, or tail latency in Verex's backend API, come from the filesystem or network stack rather than application code, this lets you narrow down the cause with evidence instead of guessing.

Where it lands in Jayverse

Key expressions

Words and phrases from this page worth keeping, with the Korean meaning and the sentence they come from.

Expression뜻 · 쓰이는 자리
sandboxed샌드박스 처리된, 격리된 · 커널 안에서 안전하게 격리되어 실행되는 환경을 가리킬 때. "a sandboxed VM that runs safely inside the kernel"
statically check정적으로 검사하다 · 실행 전에 코드를 분석해 안전성을 검증할 때. "a verifier statically checks termination and memory-access safety"
attach to~에 연결되다, 붙다 · 프로그램이 특정 커널 지점(훅)에 걸릴 때. "attached to kprobes, uprobes, tracepoints, perf events"
wrap ... down to~로 간단히 축약해주다 · 복잡한 과정을 한 줄 스크립트 수준으로 압축할 때. "wrap this whole process down to a one-line script"
narrow down범위를 좁혀가다 · 원인을 후보군에서 하나씩 줄여나갈 때. "this lets you narrow down the cause with evidence"
reaches layers that ... never touch~가 닿지 못하는 층까지 도달하다 · 애플리케이션 로그로는 볼 수 없는 곳까지 관찰할 때. "it reaches layers that application logs never touch"
tail latency테일 레이턴시(상위 퍼센타일의 느린 응답) · 대부분은 빠르지만 일부 요청이 느릴 때 쓰는 성능 용어. "tail latency in Verex's backend API"
eBPF확장 버클리 패킷 필터(extended Berkeley Packet Filter) · 커널 안에서 안전하게 실행되는 관찰·트레이싱 기술, 이 카드의 주제. "eBPF is a sandboxed VM that runs safely inside the kernel"
bpftraceeBPF를 한 줄 스크립트로 쓸 수 있게 해주는 고수준 트레이싱 도구 · 프로덕션에서 지연시간을 즉석에서 관찰할 때 쓰는 실제 도구. "Use a one-line bpftrace script to pull a histogram of disk I/O latency"
BCCBPF 컴파일러 컬렉션(BPF Compiler Collection) · eBPF 프로그램 작성을 감싸주는 고수준 툴킷. "Higher-level tools like bpftrace and BCC wrap this whole process"
JIT즉시 컴파일(Just-In-Time compilation) · 검증을 통과한 eBPF 프로그램이 커널 훅에 붙기 전 거치는 컴파일 단계. "only programs that pass are JIT-compiled and attached to kprobes"

If you study this on a given day, add a note link and a ✅ to this line in the source curriculum (docs/knowledge/dev-100-curriculum.md) and this spot will lead straight to the note body. You can also write directly on this page — but regenerating overwrites it, so it's safer to keep anything you want to save as markdown under docs/algorithms/.


한국어

eBPF로 프로덕션 관측 TODO

Algorithms · Day 47 / 100 · C. 동시성·성능 엔지니어링 (Day 36–51)

개념

eBPF는 커널 안에서 안전하게 실행되는 샌드박스 VM이다. 사용자가 작성한 작은 프로그램을 커널에 로드하면 verifier가 종료성과 메모리 접근 안전성을 정적으로 검증하고, 통과한 프로그램만 JIT 컴파일되어 kprobe, uprobe, tracepoint, perf 이벤트, 네트워크 훅 등에 붙어 실행된다. 프로그램과 유저스페이스는 map이라는 공유 자료구조(해시맵, 배열, 링버퍼 등)로 데이터를 주고받는다. 커널 모듈을 새로 짜거나 애플리케이션을 재시작·재컴파일하지 않고도 실행 중인 시스템의 내부 이벤트를 관측할 수 있다는 것이 핵심 이점이다. bpftrace나 BCC 같은 상위 도구가 이 과정을 스크립트 한 줄 수준으로 감싸 준다.

프로덕션에서 재현되지 않는 지연 스파이크나 특정 syscall 병목을 코드 수정·재배포 없이, 그리고 애플리케이션 로그에 없는 계층까지 내려가 볼 수 있는 거의 유일한 수단이다.

코드 · 수식

# eBPF로 프로덕션 관측 — 커널 프로브가 이벤트를 map에 쌓고, 유저스페이스가 읽어 히스토그램을 낸다.
# 실제 커널 훅 대신 "probe가 이벤트를 map에 기록한다"는 구조만 순수 파이썬으로 흉내낸다.

import random
from collections import defaultdict

random.seed(1)

class EbpfMap:
    """kprobe/tracepoint가 기록하는 공유 map(dict)을 흉내"""
    def __init__(self):
        self.hist = defaultdict(int)  # latency bucket(us, 로그 스케일) -> count

    def record(self, latency_us):
        bucket = 1
        while bucket * 2 <= latency_us:
            bucket *= 2
        self.hist[bucket] += 1

def bpftrace_like_probe(events, ebpf_map):
    """실제로는 커널이 syscall 진입/종료 시각차를 계산해 넣어주는 부분을 시뮬레이션"""
    for latency_us in events:
        ebpf_map.record(latency_us)

# 유휴 상태: 대부분 짧은 지연
idle_events = [max(1, int(random.gauss(50, 15))) for _ in range(200)]
# 부하 상태: 디스크 I/O 경합으로 꼬리가 길어짐
loaded_events = [max(1, int(random.gauss(50, 15))) for _ in range(200)]
loaded_events += [int(random.gauss(4000, 800)) for _ in range(20)]  # I/O 경합 스파이크

idle_map = EbpfMap()
loaded_map = EbpfMap()
bpftrace_like_probe(idle_events, idle_map)
bpftrace_like_probe(loaded_events, loaded_map)

def print_hist(name, m):
    print(f"--- {name} (syscall latency, us, log2 bucket) ---")
    for bucket in sorted(m.hist):
        print(f"  <= {bucket:5d}us : {'#' * m.hist[bucket]} ({m.hist[bucket]})")

print_hist("idle", idle_map)
print_hist("loaded", loaded_map)
print("\n부하 상태에서 4000us대 버킷이 새로 등장 -> 재배포 없이 I/O 경합 구간을 특정.")

연습

bpftrace 한 줄짜리로 특정 프로세스의 디스크 I/O 지연이나 특정 syscall 호출 횟수를 히스토그램으로 뽑고, 부하를 준 상태와 유휴 상태를 비교해 보라.

실무 · Verex 연결

블록체인 노드의 동기화 정체나 Verex 백엔드 API의 꼬리 지연이 애플리케이션 코드가 아니라 파일시스템·네트워크 스택에서 나올 때, 추측 대신 증거로 원인을 좁힐 수 있다.

Jayverse에서의 위치

핵심 표현

이 페이지의 영어 본문에서 배울 만한 단어와 표현, 뜻과 나온 자리.

Expression뜻 · 쓰이는 자리
sandboxed샌드박스 처리된, 격리된 · 커널 안에서 안전하게 격리되어 실행되는 환경을 가리킬 때. "a sandboxed VM that runs safely inside the kernel"
statically check정적으로 검사하다 · 실행 전에 코드를 분석해 안전성을 검증할 때. "a verifier statically checks termination and memory-access safety"
attach to~에 연결되다, 붙다 · 프로그램이 특정 커널 지점(훅)에 걸릴 때. "attached to kprobes, uprobes, tracepoints, perf events"
wrap ... down to~로 간단히 축약해주다 · 복잡한 과정을 한 줄 스크립트 수준으로 압축할 때. "wrap this whole process down to a one-line script"
narrow down범위를 좁혀가다 · 원인을 후보군에서 하나씩 줄여나갈 때. "this lets you narrow down the cause with evidence"
reaches layers that ... never touch~가 닿지 못하는 층까지 도달하다 · 애플리케이션 로그로는 볼 수 없는 곳까지 관찰할 때. "it reaches layers that application logs never touch"
tail latency테일 레이턴시(상위 퍼센타일의 느린 응답) · 대부분은 빠르지만 일부 요청이 느릴 때 쓰는 성능 용어. "tail latency in Verex's backend API"
eBPF확장 버클리 패킷 필터(extended Berkeley Packet Filter) · 커널 안에서 안전하게 실행되는 관찰·트레이싱 기술, 이 카드의 주제. "eBPF is a sandboxed VM that runs safely inside the kernel"
bpftraceeBPF를 한 줄 스크립트로 쓸 수 있게 해주는 고수준 트레이싱 도구 · 프로덕션에서 지연시간을 즉석에서 관찰할 때 쓰는 실제 도구. "Use a one-line bpftrace script to pull a histogram of disk I/O latency"
BCCBPF 컴파일러 컬렉션(BPF Compiler Collection) · eBPF 프로그램 작성을 감싸주는 고수준 툴킷. "Higher-level tools like bpftrace and BCC wrap this whole process"
JIT즉시 컴파일(Just-In-Time compilation) · 검증을 통과한 eBPF 프로그램이 커널 훅에 붙기 전 거치는 컴파일 단계. "only programs that pass are JIT-compiled and attached to kprobes"

공부한 날 원본 커리큘럼(docs/knowledge/dev-100-curriculum.md)의 이 줄에 노트 링크와 ✅ 를 붙이면, 이 자리는 노트 본문으로 바로 이어집니다. 노트 없이 이 페이지에 바로 적어도 됩니다 — 다만 다시 생성하면 덮어쓰이므로, 남길 글은 docs/algorithms/ 의 마크다운으로 쓰는 편이 안전합니다.

← 1099. 벤치마크 방법론1101. 분산 트레이싱과 샘플링 전략 →